Skip to content

Commit 5c317a6

Browse files
authored
Switch to slog (#841)
1 parent 46f7fbf commit 5c317a6

11 files changed

+43
-64
lines changed

.github/workflows/ci.yml

-6
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ jobs:
6262
with:
6363
go-version: stable
6464

65-
- name: Determine GOPATH
66-
id: go
67-
run: |
68-
echo "go_path=$(go env GOPATH)" >> $GITHUB_OUTPUT
69-
7065
- name: Setup QEMU
7166
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
7267
with:
@@ -183,7 +178,6 @@ jobs:
183178
args: ${{ github.ref_type == 'tag' && 'release' || 'build --snapshot' }} ${{ github.event_name == 'pull_request' && '--single-target' || '' }} --clean
184179
env:
185180
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
186-
GOPATH: ${{ steps.go.outputs.go_path }}
187181
NGINX_GITHUB_TOKEN: ${{ secrets.NGINX_PAT }}
188182
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_COMMUNITY }}
189183
SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_LOGIN }}

.golangci.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ linters-settings:
2727
- name: unused-parameter
2828
- name: var-declaration
2929
- name: var-naming
30-
errcheck:
31-
exclude-functions:
32-
- (github.com/go-kit/log.Logger).Log
3330
govet:
3431
enable-all: true
32+
sloglint:
33+
static-msg: true
34+
key-naming-case: snake
3535
linters:
3636
enable:
3737
- asasalint
3838
- asciicheck
3939
- bidichk
4040
- contextcheck
41+
- copyloopvar
4142
- dupword
4243
- durationcheck
4344
- errcheck
4445
- errchkjson
4546
- errname
4647
- errorlint
47-
- exportloopref
4848
- fatcontext
4949
- forcetypeassert
5050
- gocheckcompilerdirectives
@@ -74,6 +74,7 @@ linters:
7474
- promlinter
7575
- reassign
7676
- revive
77+
- sloglint
7778
- staticcheck
7879
- stylecheck
7980
- tagalign

.goreleaser.yml

-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ builds:
3030
goarch: arm
3131
flags:
3232
- -trimpath
33-
gcflags:
34-
- all=-trimpath={{.Env.GOPATH}}
35-
asmflags:
36-
- all=-trimpath={{.Env.GOPATH}}
3733
ldflags:
3834
- "-s -w -X github.com/prometheus/common/version.Version={{.Version}} -X github.com/prometheus/common/version.BuildDate={{.Date}} -X github.com/prometheus/common/version.Branch={{.Branch}} -X github.com/prometheus/common/version.BuildUser=goreleaser"
3935

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ nginx-prometheus-exporter: ## Build nginx-prometheus-exporter binary
1717
.PHONY: build-goreleaser
1818
build-goreleaser: ## Build all binaries using GoReleaser
1919
@goreleaser -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with GoReleaser. Follow the docs to install it https://goreleaser.com/install\n"; exit $$code)
20-
GOPATH=$(shell go env GOPATH) goreleaser build --clean --snapshot
20+
goreleaser build --clean --snapshot
2121

2222
.PHONY: lint
2323
lint: ## Run linter

collector/helper_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func TestMergeLabels(t *testing.T) {
3434
},
3535
}
3636
for _, tt := range tests {
37-
tt := tt
3837
t.Run(tt.name, func(t *testing.T) {
3938
t.Parallel()
4039
if got := MergeLabels(tt.mapA, tt.mapB); !reflect.DeepEqual(got, tt.want) {

collector/nginx.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package collector
22

33
import (
4+
"log/slog"
45
"sync"
56

6-
"github.com/go-kit/log"
7-
"github.com/go-kit/log/level"
87
"github.com/nginxinc/nginx-prometheus-exporter/client"
98
"github.com/prometheus/client_golang/prometheus"
109
)
1110

1211
// NginxCollector collects NGINX metrics. It implements prometheus.Collector interface.
1312
type NginxCollector struct {
1413
upMetric prometheus.Gauge
15-
logger log.Logger
14+
logger *slog.Logger
1615
nginxClient *client.NginxClient
1716
metrics map[string]*prometheus.Desc
1817
mutex sync.Mutex
1918
}
2019

2120
// NewNginxCollector creates an NginxCollector.
22-
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string, logger log.Logger) *NginxCollector {
21+
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string, logger *slog.Logger) *NginxCollector {
2322
return &NginxCollector{
2423
nginxClient: nginxClient,
2524
logger: logger,
@@ -55,7 +54,7 @@ func (c *NginxCollector) Collect(ch chan<- prometheus.Metric) {
5554
if err != nil {
5655
c.upMetric.Set(nginxDown)
5756
ch <- c.upMetric
58-
level.Error(c.logger).Log("msg", "Error getting stats", "error", err.Error())
57+
c.logger.Error("error getting stats", "error", err.Error())
5958
return
6059
}
6160

collector/nginx_plus.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package collector
22

33
import (
44
"fmt"
5+
"log/slog"
56
"strconv"
67
"sync"
78

8-
"github.com/go-kit/log"
9-
"github.com/go-kit/log/level"
109
plusclient "github.com/nginxinc/nginx-plus-go-client/client"
1110
"github.com/prometheus/client_golang/prometheus"
1211
)
@@ -32,7 +31,7 @@ type LabelUpdater interface {
3231
// NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface.
3332
type NginxPlusCollector struct {
3433
upMetric prometheus.Gauge
35-
logger log.Logger
34+
logger *slog.Logger
3635
cacheZoneMetrics map[string]*prometheus.Desc
3736
workerMetrics map[string]*prometheus.Desc
3837
nginxClient *plusclient.NginxClient
@@ -256,7 +255,7 @@ func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZone
256255
}
257256

258257
// NewNginxPlusCollector creates an NginxPlusCollector.
259-
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string, variableLabelNames VariableLabelNames, constLabels map[string]string, logger log.Logger) *NginxPlusCollector {
258+
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string, variableLabelNames VariableLabelNames, constLabels map[string]string, logger *slog.Logger) *NginxPlusCollector {
260259
upstreamServerVariableLabelNames := variableLabelNames.UpstreamServerVariableLabelNames
261260
streamUpstreamServerVariableLabelNames := variableLabelNames.StreamUpstreamServerVariableLabelNames
262261

@@ -627,7 +626,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
627626
if err != nil {
628627
c.upMetric.Set(nginxDown)
629628
ch <- c.upMetric
630-
level.Warn(c.logger).Log("msg", "Error getting stats", "error", err.Error())
629+
c.logger.Warn("error getting stats", "error", err.Error())
631630
return
632631
}
633632

@@ -658,7 +657,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
658657
varLabelValues := c.getServerZoneLabelValues(name)
659658

660659
if c.variableLabelNames.ServerZoneVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.ServerZoneVariableLabelNames) {
661-
level.Warn(c.logger).Log("msg", "wrong number of labels for http zone, empty labels will be used instead", "zone", name, "expected", len(c.variableLabelNames.ServerZoneVariableLabelNames), "got", len(varLabelValues))
660+
c.logger.Warn("wrong number of labels for http zone, empty labels will be used instead", "zone", name, "expected", len(c.variableLabelNames.ServerZoneVariableLabelNames), "got", len(varLabelValues))
662661
for range c.variableLabelNames.ServerZoneVariableLabelNames {
663662
labelValues = append(labelValues, "")
664663
}
@@ -779,7 +778,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
779778
varLabelValues := c.getStreamServerZoneLabelValues(name)
780779

781780
if c.variableLabelNames.StreamServerZoneVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.StreamServerZoneVariableLabelNames) {
782-
level.Warn(c.logger).Log("msg", "wrong number of labels for stream server zone, empty labels will be used instead", "zone", name, "expected", len(c.variableLabelNames.StreamServerZoneVariableLabelNames), "got", len(varLabelValues))
781+
c.logger.Warn("wrong number of labels for stream server zone, empty labels will be used instead", "zone", name, "expected", len(c.variableLabelNames.StreamServerZoneVariableLabelNames), "got", len(varLabelValues))
783782
for range c.variableLabelNames.StreamServerZoneVariableLabelNames {
784783
labelValues = append(labelValues, "")
785784
}
@@ -816,7 +815,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
816815
varLabelValues := c.getUpstreamServerLabelValues(name)
817816

818817
if c.variableLabelNames.UpstreamServerVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.UpstreamServerVariableLabelNames) {
819-
level.Warn(c.logger).Log("msg", "wrong number of labels for upstream, empty labels will be used instead", "upstream", name, "expected", len(c.variableLabelNames.UpstreamServerVariableLabelNames), "got", len(varLabelValues))
818+
c.logger.Warn("wrong number of labels for upstream, empty labels will be used instead", "upstream", name, "expected", len(c.variableLabelNames.UpstreamServerVariableLabelNames), "got", len(varLabelValues))
820819
for range c.variableLabelNames.UpstreamServerVariableLabelNames {
821820
labelValues = append(labelValues, "")
822821
}
@@ -827,7 +826,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
827826
upstreamServer := fmt.Sprintf("%v/%v", name, peer.Server)
828827
varPeerLabelValues := c.getUpstreamServerPeerLabelValues(upstreamServer)
829828
if c.variableLabelNames.UpstreamServerPeerVariableLabelNames != nil && len(varPeerLabelValues) != len(c.variableLabelNames.UpstreamServerPeerVariableLabelNames) {
830-
level.Warn(c.logger).Log("msg", "wrong number of labels for upstream peer, empty labels will be used instead", "upstream", name, "peer", peer.Server, "expected", len(c.variableLabelNames.UpstreamServerPeerVariableLabelNames), "got", len(varPeerLabelValues))
829+
c.logger.Warn("wrong number of labels for upstream peer, empty labels will be used instead", "upstream", name, "peer", peer.Server, "expected", len(c.variableLabelNames.UpstreamServerPeerVariableLabelNames), "got", len(varPeerLabelValues))
831830
for range c.variableLabelNames.UpstreamServerPeerVariableLabelNames {
832831
labelValues = append(labelValues, "")
833832
}
@@ -973,7 +972,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
973972
varLabelValues := c.getStreamUpstreamServerLabelValues(name)
974973

975974
if c.variableLabelNames.StreamUpstreamServerVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.StreamUpstreamServerVariableLabelNames) {
976-
level.Warn(c.logger).Log("msg", "wrong number of labels for stream server, empty labels will be used instead", "server", name, "labels", c.variableLabelNames.StreamUpstreamServerVariableLabelNames, "values", varLabelValues)
975+
c.logger.Warn("wrong number of labels for stream server, empty labels will be used instead", "server", name, "labels", c.variableLabelNames.StreamUpstreamServerVariableLabelNames, "values", varLabelValues)
977976
for range c.variableLabelNames.StreamUpstreamServerVariableLabelNames {
978977
labelValues = append(labelValues, "")
979978
}
@@ -984,7 +983,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
984983
upstreamServer := fmt.Sprintf("%v/%v", name, peer.Server)
985984
varPeerLabelValues := c.getStreamUpstreamServerPeerLabelValues(upstreamServer)
986985
if c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames != nil && len(varPeerLabelValues) != len(c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames) {
987-
level.Warn(c.logger).Log("msg", "wrong number of labels for stream upstream peer, empty labels will be used instead", "server", upstreamServer, "labels", c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames, "values", varPeerLabelValues)
986+
c.logger.Warn("wrong number of labels for stream upstream peer, empty labels will be used instead", "server", upstreamServer, "labels", c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames, "values", varPeerLabelValues)
988987
for range c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames {
989988
labelValues = append(labelValues, "")
990989
}
@@ -1204,7 +1203,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
12041203
varLabelValues := c.getCacheZoneLabelValues(name)
12051204

12061205
if c.variableLabelNames.CacheZoneVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.CacheZoneVariableLabelNames) {
1207-
level.Warn(c.logger).Log("msg", "wrong number of labels for cache zone, empty labels will be used instead", "zone", name, "labels", c.variableLabelNames.CacheZoneVariableLabelNames, "values", varLabelValues)
1206+
c.logger.Warn("wrong number of labels for cache zone, empty labels will be used instead", "zone", name, "labels", c.variableLabelNames.CacheZoneVariableLabelNames, "values", varLabelValues)
12081207
for range c.variableLabelNames.CacheZoneVariableLabelNames {
12091208
labelValues = append(labelValues, "")
12101209
}

exporter.go

+19-20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/x509"
77
"errors"
88
"fmt"
9+
"log/slog"
910
"maps"
1011
"net"
1112
"net/http"
@@ -20,13 +21,11 @@ import (
2021
"github.com/nginxinc/nginx-prometheus-exporter/collector"
2122

2223
"github.com/alecthomas/kingpin/v2"
23-
"github.com/go-kit/log"
24-
"github.com/go-kit/log/level"
2524
"github.com/prometheus/client_golang/prometheus"
2625
"github.com/prometheus/client_golang/prometheus/collectors/version"
2726
"github.com/prometheus/client_golang/prometheus/promhttp"
28-
"github.com/prometheus/common/promlog"
29-
"github.com/prometheus/common/promlog/flag"
27+
"github.com/prometheus/common/promslog"
28+
"github.com/prometheus/common/promslog/flag"
3029
common_version "github.com/prometheus/common/version"
3130

3231
"github.com/prometheus/exporter-toolkit/web"
@@ -110,24 +109,24 @@ func main() {
110109
}
111110
}
112111

113-
promlogConfig := &promlog.Config{}
112+
config := &promslog.Config{}
114113

115-
flag.AddFlags(kingpin.CommandLine, promlogConfig)
114+
flag.AddFlags(kingpin.CommandLine, config)
116115
kingpin.Version(common_version.Print(exporterName))
117116
kingpin.HelpFlag.Short('h')
118117

119118
addMissingEnvironmentFlags(kingpin.CommandLine)
120119

121120
kingpin.Parse()
122-
logger := promlog.New(promlogConfig)
121+
logger := promslog.New(config)
123122

124-
level.Info(logger).Log("msg", "Starting nginx-prometheus-exporter", "version", common_version.Info())
125-
level.Info(logger).Log("msg", "Build context", "build_context", common_version.BuildContext())
123+
logger.Info("nginx-prometheus-exporter", "version", common_version.Info())
124+
logger.Info("build context", "build_context", common_version.BuildContext())
126125

127126
prometheus.MustRegister(version.NewCollector(exporterName))
128127

129128
if len(*scrapeURIs) == 0 {
130-
level.Error(logger).Log("msg", "No scrape addresses provided")
129+
logger.Error("no scrape addresses provided")
131130
os.Exit(1)
132131
}
133132

@@ -136,13 +135,13 @@ func main() {
136135
if *sslCaCert != "" {
137136
caCert, err := os.ReadFile(*sslCaCert)
138137
if err != nil {
139-
level.Error(logger).Log("msg", "Loading CA cert failed", "err", err.Error())
138+
logger.Error("loading CA cert failed", "err", err.Error())
140139
os.Exit(1)
141140
}
142141
sslCaCertPool := x509.NewCertPool()
143142
ok := sslCaCertPool.AppendCertsFromPEM(caCert)
144143
if !ok {
145-
level.Error(logger).Log("msg", "Parsing CA cert file failed.")
144+
logger.Error("parsing CA cert file failed.")
146145
os.Exit(1)
147146
}
148147
sslConfig.RootCAs = sslCaCertPool
@@ -151,7 +150,7 @@ func main() {
151150
if *sslClientCert != "" && *sslClientKey != "" {
152151
clientCert, err := tls.LoadX509KeyPair(*sslClientCert, *sslClientKey)
153152
if err != nil {
154-
level.Error(logger).Log("msg", "Loading client certificate failed", "error", err.Error())
153+
logger.Error("loading client certificate failed", "error", err.Error())
155154
os.Exit(1)
156155
}
157156
sslConfig.Certificates = []tls.Certificate{clientCert}
@@ -190,7 +189,7 @@ func main() {
190189
}
191190
landingPage, err := web.NewLandingPage(landingConfig)
192191
if err != nil {
193-
level.Error(logger).Log("err", err)
192+
logger.Error("failed to create landing page", "error", err.Error())
194193
os.Exit(1)
195194
}
196195
http.Handle("/", landingPage)
@@ -206,28 +205,28 @@ func main() {
206205
go func() {
207206
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {
208207
if errors.Is(err, http.ErrServerClosed) {
209-
level.Info(logger).Log("msg", "HTTP server closed")
208+
logger.Info("HTTP server closed", "error", err.Error())
210209
os.Exit(0)
211210
}
212-
level.Error(logger).Log("err", err)
211+
logger.Error("HTTP server failed", "error", err.Error())
213212
os.Exit(1)
214213
}
215214
}()
216215

217216
<-ctx.Done()
218-
level.Info(logger).Log("msg", "Shutting down")
217+
logger.Info("shutting down")
219218
srvCtx, srvCancel := context.WithTimeout(context.Background(), 5*time.Second)
220219
defer srvCancel()
221220
_ = srv.Shutdown(srvCtx)
222221
}
223222

224-
func registerCollector(logger log.Logger, transport *http.Transport,
223+
func registerCollector(logger *slog.Logger, transport *http.Transport,
225224
addr string, labels map[string]string,
226225
) {
227226
if strings.HasPrefix(addr, "unix:") {
228227
socketPath, requestPath, err := parseUnixSocketAddress(addr)
229228
if err != nil {
230-
level.Error(logger).Log("msg", "Parsing unix domain socket scrape address failed", "uri", addr, "error", err.Error())
229+
logger.Error("parsing unix domain socket scrape address failed", "uri", addr, "error", err.Error())
231230
os.Exit(1)
232231
}
233232

@@ -250,7 +249,7 @@ func registerCollector(logger log.Logger, transport *http.Transport,
250249
if *nginxPlus {
251250
plusClient, err := plusclient.NewNginxClient(addr, plusclient.WithHTTPClient(httpClient))
252251
if err != nil {
253-
level.Error(logger).Log("msg", "Could not create Nginx Plus Client", "error", err.Error())
252+
logger.Error("could not create Nginx Plus Client", "error", err.Error())
254253
os.Exit(1)
255254
}
256255
variableLabelNames := collector.NewVariableLabelNames(nil, nil, nil, nil, nil, nil, nil)

exporter_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func TestParsePositiveDuration(t *testing.T) {
3838
},
3939
}
4040
for _, tt := range tests {
41-
tt := tt
4241
t.Run(tt.name, func(t *testing.T) {
4342
t.Parallel()
4443
got, err := parsePositiveDuration(tt.testInput)
@@ -93,7 +92,6 @@ func TestParseUnixSocketAddress(t *testing.T) {
9392
},
9493
}
9594
for _, tt := range tests {
96-
tt := tt
9795
t.Run(tt.name, func(t *testing.T) {
9896
t.Parallel()
9997
socketPath, requestPath, err := parseUnixSocketAddress(tt.testInput)

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ go 1.22.6
44

55
require (
66
github.com/alecthomas/kingpin/v2 v2.4.0
7-
github.com/go-kit/log v0.2.1
87
github.com/nginxinc/nginx-plus-go-client v1.3.0
98
github.com/prometheus/client_golang v1.20.3
109
github.com/prometheus/common v0.59.1
11-
github.com/prometheus/exporter-toolkit v0.12.0
10+
github.com/prometheus/exporter-toolkit v0.13.0
1211
)
1312

1413
require (
1514
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
1615
github.com/beorn7/perks v1.0.1 // indirect
1716
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1817
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
19-
github.com/go-logfmt/logfmt v0.5.1 // indirect
2018
github.com/jpillora/backoff v1.0.0 // indirect
2119
github.com/klauspost/compress v1.17.9 // indirect
2220
github.com/mdlayher/socket v0.4.1 // indirect

0 commit comments

Comments
 (0)