Skip to content

Commit

Permalink
*: cleanup collector API 1 (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored Aug 5, 2024
1 parent dffc53e commit d1e3a63
Show file tree
Hide file tree
Showing 86 changed files with 2,101 additions and 1,869 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.go text eol=lf
*.sh text eol=lf
Makefile text eol=lf
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.58
args: "--timeout=5m --out-format github-actions,colored-line-number"
version: v1.59
args: "--timeout=5m"
10 changes: 6 additions & 4 deletions .github/workflows/pr-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ jobs:
- name: check
run: |
PR_TITLE_PREFIX=$(echo "$PR_TITLE" | cut -d':' -f1)
if [[ ! -d "pkg/collector/$PR_TITLE_PREFIX" ]] || [[ "$PR_TITLE_PREFIX" == "chore(deps)" ]] || [[ "$PR_TITLE_PREFIX" == "chore" ]] || [[ "$PR_TITLE_PREFIX" == "*" ]]; then
echo "PR title must start with an name of an collector package"
echo "Example: 'logical_disk: description'"
exit 1
if [[ -d "pkg/collector/$PR_TITLE_PREFIX" ]] || [[ "$PR_TITLE_PREFIX" == "chore" ]] || [[ "$PR_TITLE_PREFIX" == "chore(deps)" ]] || [[ "$PR_TITLE_PREFIX" == "*" ]]; then
exit 0
fi
echo "PR title must start with an name of an collector package"
echo "Example: 'logical_disk: description'"
exit 1
env:
PR_TITLE: ${{ github.event.pull_request.title }}
18 changes: 0 additions & 18 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
linters:
enable-all: true
disable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- copyloopvar
- cyclop
- decorder
- depguard
Expand All @@ -24,10 +18,7 @@ linters:
- exhaustruct
- exportloopref
- fatcontext
- forbidigo
- forcetypeassert
- funlen
- gci
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
Expand All @@ -40,12 +31,9 @@ linters:
- godot
- godox
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- grouper
Expand All @@ -56,7 +44,6 @@ linters:
- ireturn
- lll
- maintidx
- makezero
- mirror
- misspell
- mnd
Expand All @@ -65,7 +52,6 @@ linters:
- nestif
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- paralleltest
Expand All @@ -86,13 +72,9 @@ linters:
- testpackage
- thelper
- tparallel
- usestdlibvars
- varnamelen
- wastedassign
- whitespace
- wrapcheck
- wsl
- zerologlint
- execinquery
- gomnd

Expand Down
50 changes: 34 additions & 16 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
package main

import (
// Its important that we do these first so that we can register with the Windows service control ASAP to avoid timeouts
"github.com/prometheus-community/windows_exporter/pkg/initiate"

"context"
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"os/user"
"runtime"
"sort"
"strings"

winlog "github.com/prometheus-community/windows_exporter/pkg/log"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/utils"
"github.com/prometheus-community/windows_exporter/pkg/wmi"
"time"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log/level"
"github.com/prometheus-community/windows_exporter/pkg/collector"
"github.com/prometheus-community/windows_exporter/pkg/config"
// Its important that we do these first so that we can register with the Windows service control ASAP to avoid timeouts
"github.com/prometheus-community/windows_exporter/pkg/initiate"
winlog "github.com/prometheus-community/windows_exporter/pkg/log"
"github.com/prometheus-community/windows_exporter/pkg/log/flag"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/utils"
"github.com/prometheus-community/windows_exporter/pkg/wmi"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
Expand Down Expand Up @@ -171,9 +172,9 @@ func main() {
collectorNames := collector.Available()
sort.Strings(collectorNames)

fmt.Printf("Available collectors:\n")
fmt.Printf("Available collectors:\n") //nolint:forbidigo
for _, n := range collectorNames {
fmt.Printf(" - %s\n", n)
fmt.Printf(" - %s\n", n) //nolint:forbidigo
}

return
Expand Down Expand Up @@ -259,20 +260,37 @@ func main() {
_ = level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
_ = level.Debug(logger).Log("msg", "Go MAXPROCS", "procs", runtime.GOMAXPROCS(0))

server := &http.Server{
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: 60 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Minute,
Handler: mux,
}

go func() {
server := &http.Server{Handler: mux}
if err := web.ListenAndServe(server, webConfig, logger); err != nil {
_ = level.Error(logger).Log("msg", "cannot start windows_exporter", "err", err)
os.Exit(1)
}
}()

for {
if <-initiate.StopCh {
_ = level.Info(logger).Log("msg", "Shutting down windows_exporter")
break
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()

select {
case <-ctx.Done():
_ = level.Info(logger).Log("msg", "Shutting down windows_exporter via kill signal")
case <-initiate.StopCh:
_ = level.Info(logger).Log("msg", "Shutting down windows_exporter via service control")
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_ = server.Shutdown(ctx)

_ = level.Info(logger).Log("msg", "windows_exporter has shut down")
}

func withConcurrencyLimit(n int, next http.HandlerFunc) http.HandlerFunc {
Expand Down
Loading

0 comments on commit d1e3a63

Please sign in to comment.