Skip to content

Commit 951e1f0

Browse files
authored
Merge pull request #1586 from tjhop/ref/adopt-slog
ref!: adopt log/slog + other prom configs, drop custom logging pkg
2 parents eacd05f + 0c41386 commit 951e1f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+385
-495
lines changed

.golangci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ linters:
3131
- typecheck
3232
- unconvert
3333
- unused
34+
- sloglint
3435

3536
linters-settings:
36-
errcheck:
37-
exclude-functions:
38-
- (github.com/go-kit/log.Logger).Log
3937
goimports:
4038
local-prefixes: "github.com/prometheus-community/yet-another-cloudwatch-exporter"

cmd/yace/main.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ package main
1515
import (
1616
"context"
1717
"fmt"
18+
"log/slog"
1819
"net/http"
1920
"net/http/pprof"
2021
"os"
22+
"slices"
2123
"strings"
2224

25+
"github.com/prometheus/common/promslog"
26+
promslogflag "github.com/prometheus/common/promslog/flag"
2327
"github.com/urfave/cli/v2"
2428
"golang.org/x/sync/semaphore"
2529

@@ -28,7 +32,6 @@ import (
2832
v1 "github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/clients/v1"
2933
v2 "github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/clients/v2"
3034
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/config"
31-
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/logging"
3235
)
3336

3437
const (
@@ -56,7 +59,7 @@ const (
5659
var (
5760
addr string
5861
configFile string
59-
debug bool
62+
logLevel string
6063
logFormat string
6164
fips bool
6265
cloudwatchConcurrency cloudwatch.ConcurrencyConfig
@@ -66,17 +69,19 @@ var (
6669
labelsSnakeCase bool
6770
profilingEnabled bool
6871

69-
logger logging.Logger
72+
logger *slog.Logger
7073
)
7174

7275
func main() {
7376
app := NewYACEApp()
7477
if err := app.Run(os.Args); err != nil {
7578
// if we exit very early we'll not have set up the logger yet
7679
if logger == nil {
77-
logger = logging.NewLogger(defaultLogFormat, debug, "version", version)
80+
jsonFmt := &promslog.AllowedFormat{}
81+
_ = jsonFmt.Set("json")
82+
logger = promslog.New(&promslog.Config{Format: jsonFmt})
7883
}
79-
logger.Error(err, "Error running yace")
84+
logger.Error("Error running yace", "err", err)
8085
os.Exit(1)
8186
}
8287
}
@@ -107,23 +112,25 @@ func NewYACEApp() *cli.App {
107112
Destination: &configFile,
108113
EnvVars: []string{"config.file"},
109114
},
110-
&cli.BoolFlag{
111-
Name: "debug",
112-
Value: false,
113-
Usage: "Verbose logging",
114-
Destination: &debug,
115-
EnvVars: []string{"debug"},
115+
&cli.StringFlag{
116+
Name: "log.level",
117+
Value: "",
118+
Usage: promslogflag.LevelFlagHelp,
119+
Destination: &logLevel,
120+
Action: func(_ *cli.Context, s string) error {
121+
if !slices.Contains(promslog.LevelFlagOptions, s) {
122+
return fmt.Errorf("unrecognized log format %q", s)
123+
}
124+
return nil
125+
},
116126
},
117127
&cli.StringFlag{
118128
Name: "log.format",
119129
Value: defaultLogFormat,
120-
Usage: "Output format of log messages. One of: [logfmt, json]. Default: [json].",
130+
Usage: promslogflag.FormatFlagHelp,
121131
Destination: &logFormat,
122132
Action: func(_ *cli.Context, s string) error {
123-
switch s {
124-
case "logfmt", "json":
125-
break
126-
default:
133+
if !slices.Contains(promslog.FormatFlagOptions, s) {
127134
return fmt.Errorf("unrecognized log format %q", s)
128135
}
129136
return nil
@@ -212,11 +219,11 @@ func NewYACEApp() *cli.App {
212219
&cli.StringFlag{Name: "config.file", Value: "config.yml", Usage: "Path to configuration file.", Destination: &configFile},
213220
},
214221
Action: func(_ *cli.Context) error {
215-
logger = logging.NewLogger(logFormat, debug, "version", version)
222+
logger = newLogger(logFormat, logLevel).With("version", version)
216223
logger.Info("Parsing config")
217224
cfg := config.ScrapeConf{}
218225
if _, err := cfg.Load(configFile, logger); err != nil {
219-
logger.Error(err, "Couldn't read config file", "path", configFile)
226+
logger.Error("Couldn't read config file", "err", err, "path", configFile)
220227
os.Exit(1)
221228
}
222229
logger.Info("Config file is valid", "path", configFile)
@@ -242,7 +249,7 @@ func NewYACEApp() *cli.App {
242249
}
243250

244251
func startScraper(c *cli.Context) error {
245-
logger = logging.NewLogger(logFormat, debug, "version", version)
252+
logger = newLogger(logFormat, logLevel).With("version", version)
246253

247254
// log warning if the two concurrency limiting methods are configured via CLI
248255
if c.IsSet("cloudwatch-concurrency") && c.IsSet("cloudwatch-concurrency.per-api-limit-enabled") {
@@ -310,7 +317,7 @@ func startScraper(c *cli.Context) error {
310317
newCfg := config.ScrapeConf{}
311318
newJobsCfg, err := newCfg.Load(configFile, logger)
312319
if err != nil {
313-
logger.Error(err, "Couldn't read config file", "path", configFile)
320+
logger.Error("Couldn't read config file", "err", err, "path", configFile)
314321
return
315322
}
316323

@@ -323,7 +330,7 @@ func startScraper(c *cli.Context) error {
323330
// Can't override cache while also creating err
324331
cache, err = v2.NewFactory(logger, newJobsCfg, fips)
325332
if err != nil {
326-
logger.Error(err, "Failed to construct aws sdk v2 client cache", "path", configFile)
333+
logger.Error("Failed to construct aws sdk v2 client cache", "err", err, "path", configFile)
327334
return
328335
}
329336
}
@@ -339,3 +346,16 @@ func startScraper(c *cli.Context) error {
339346
srv := &http.Server{Addr: addr, Handler: mux}
340347
return srv.ListenAndServe()
341348
}
349+
350+
func newLogger(format, level string) *slog.Logger {
351+
// If flag parsing was successful, then we know that format and level
352+
// are both valid options; no need to error check their returns, just
353+
// set their values.
354+
f := &promslog.AllowedFormat{}
355+
_ = f.Set(format)
356+
357+
lvl := &promslog.AllowedLevel{}
358+
_ = lvl.Set(level)
359+
360+
return promslog.New(&promslog.Config{Format: f, Level: lvl})
361+
}

cmd/yace/scraper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package main
1414

1515
import (
1616
"context"
17+
"log/slog"
1718
"net/http"
1819
"sync/atomic"
1920
"time"
@@ -23,7 +24,6 @@ import (
2324

2425
exporter "github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg"
2526
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/clients"
26-
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/logging"
2727
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/model"
2828
)
2929

@@ -56,7 +56,7 @@ func (s *scraper) makeHandler() func(http.ResponseWriter, *http.Request) {
5656
}
5757
}
5858

59-
func (s *scraper) decoupled(ctx context.Context, logger logging.Logger, jobsCfg model.JobsConfig, cache cachingFactory) {
59+
func (s *scraper) decoupled(ctx context.Context, logger *slog.Logger, jobsCfg model.JobsConfig, cache cachingFactory) {
6060
logger.Debug("Starting scraping async")
6161
s.scrape(ctx, logger, jobsCfg, cache)
6262

@@ -75,7 +75,7 @@ func (s *scraper) decoupled(ctx context.Context, logger logging.Logger, jobsCfg
7575
}
7676
}
7777

78-
func (s *scraper) scrape(ctx context.Context, logger logging.Logger, jobsCfg model.JobsConfig, cache cachingFactory) {
78+
func (s *scraper) scrape(ctx context.Context, logger *slog.Logger, jobsCfg model.JobsConfig, cache cachingFactory) {
7979
if !sem.TryAcquire(1) {
8080
// This shouldn't happen under normal use, users should adjust their configuration when this occurs.
8181
// Let them know by logging a warning.
@@ -120,7 +120,7 @@ func (s *scraper) scrape(ctx context.Context, logger logging.Logger, jobsCfg mod
120120
options...,
121121
)
122122
if err != nil {
123-
logger.Error(err, "error updating metrics")
123+
logger.Error("error updating metrics", "err", err)
124124
}
125125

126126
s.registry.Store(newRegistry)

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ require (
2020
github.com/aws/aws-sdk-go-v2/service/storagegateway v1.34.6
2121
github.com/aws/aws-sdk-go-v2/service/sts v1.33.1
2222
github.com/aws/smithy-go v1.22.1
23-
github.com/go-kit/log v0.2.1
2423
github.com/grafana/regexp v0.0.0-20240607082908-2cb410fa05da
2524
github.com/prometheus/client_golang v1.20.5
2625
github.com/prometheus/client_model v0.6.1
@@ -34,6 +33,8 @@ require (
3433
)
3534

3635
require (
36+
github.com/alecthomas/kingpin/v2 v2.4.0 // indirect
37+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
3738
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20 // indirect
3839
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.24 // indirect
3940
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.24 // indirect
@@ -46,7 +47,6 @@ require (
4647
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4748
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
4849
github.com/davecgh/go-spew v1.1.1 // indirect
49-
github.com/go-logfmt/logfmt v0.5.1 // indirect
5050
github.com/jmespath/go-jmespath v0.4.0 // indirect
5151
github.com/klauspost/compress v1.17.9 // indirect
5252
github.com/kr/text v0.2.0 // indirect
@@ -56,6 +56,7 @@ require (
5656
github.com/russross/blackfriday/v2 v2.1.0 // indirect
5757
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
5858
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
59+
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
5960
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
6061
golang.org/x/sys v0.25.0 // indirect
6162
google.golang.org/protobuf v1.34.2 // indirect

go.sum

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY=
2+
github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
3+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
4+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
15
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
26
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
37
github.com/aws/aws-sdk-go-v2 v1.32.5 h1:U8vdWJuY7ruAkzaOdD7guwJjD06YSKmnKCJs7s3IkIo=
@@ -58,10 +62,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
5862
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5963
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6064
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
61-
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
62-
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
63-
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
64-
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
6565
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6666
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6767
github.com/grafana/regexp v0.0.0-20240607082908-2cb410fa05da h1:BML5sNe+bw2uO8t8cQSwe5QhvoP04eHPF7bnaQma0Kw=
@@ -97,6 +97,7 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
9797
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
9898
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9999
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
100+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
100101
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
101102
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
102103
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
@@ -106,6 +107,8 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9
106107
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
107108
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
108109
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
110+
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
111+
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
109112
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
110113
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
111114
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
@@ -119,6 +122,7 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
119122
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
120123
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
121124
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
125+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
122126
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
123127
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
124128
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

pkg/clients/account/v1/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@ package v1
1515
import (
1616
"context"
1717
"errors"
18+
"log/slog"
1819

1920
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/clients/account"
2021

2122
"github.com/aws/aws-sdk-go/service/iam"
2223
"github.com/aws/aws-sdk-go/service/iam/iamiface"
2324
"github.com/aws/aws-sdk-go/service/sts"
2425
"github.com/aws/aws-sdk-go/service/sts/stsiface"
25-
26-
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/logging"
2726
)
2827

2928
type client struct {
30-
logger logging.Logger
29+
logger *slog.Logger
3130
stsClient stsiface.STSAPI
3231
iamClient iamiface.IAMAPI
3332
}
3433

35-
func NewClient(logger logging.Logger, stsClient stsiface.STSAPI, iamClient iamiface.IAMAPI) account.Client {
34+
func NewClient(logger *slog.Logger, stsClient stsiface.STSAPI, iamClient iamiface.IAMAPI) account.Client {
3635
return &client{
3736
logger: logger,
3837
stsClient: stsClient,

pkg/clients/account/v2/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ package v2
1515
import (
1616
"context"
1717
"errors"
18+
"log/slog"
1819

1920
"github.com/aws/aws-sdk-go-v2/service/iam"
2021
"github.com/aws/aws-sdk-go-v2/service/sts"
2122

2223
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/clients/account"
23-
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/logging"
2424
)
2525

2626
type client struct {
27-
logger logging.Logger
27+
logger *slog.Logger
2828
stsClient *sts.Client
2929
iamClient *iam.Client
3030
}
3131

32-
func NewClient(logger logging.Logger, stsClient *sts.Client, iamClient *iam.Client) account.Client {
32+
func NewClient(logger *slog.Logger, stsClient *sts.Client, iamClient *iam.Client) account.Client {
3333
return &client{
3434
logger: logger,
3535
stsClient: stsClient,

pkg/clients/cloudwatch/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ package cloudwatch
1414

1515
import (
1616
"context"
17+
"log/slog"
1718
"time"
1819

19-
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/logging"
2020
"github.com/prometheus-community/yet-another-cloudwatch-exporter/pkg/model"
2121
)
2222

@@ -37,7 +37,7 @@ type Client interface {
3737
GetMetricData(ctx context.Context, getMetricData []*model.CloudwatchData, namespace string, startTime time.Time, endTime time.Time) []MetricDataResult
3838

3939
// GetMetricStatistics returns the output of the GetMetricStatistics CloudWatch API.
40-
GetMetricStatistics(ctx context.Context, logger logging.Logger, dimensions []model.Dimension, namespace string, metric *model.MetricConfig) []*model.Datapoint
40+
GetMetricStatistics(ctx context.Context, logger *slog.Logger, dimensions []model.Dimension, namespace string, metric *model.MetricConfig) []*model.Datapoint
4141
}
4242

4343
// ConcurrencyLimiter limits the concurrency when calling AWS CloudWatch APIs. The functions implemented
@@ -73,7 +73,7 @@ func NewLimitedConcurrencyClient(client Client, limiter ConcurrencyLimiter) Clie
7373
}
7474
}
7575

76-
func (c limitedConcurrencyClient) GetMetricStatistics(ctx context.Context, logger logging.Logger, dimensions []model.Dimension, namespace string, metric *model.MetricConfig) []*model.Datapoint {
76+
func (c limitedConcurrencyClient) GetMetricStatistics(ctx context.Context, logger *slog.Logger, dimensions []model.Dimension, namespace string, metric *model.MetricConfig) []*model.Datapoint {
7777
c.limiter.Acquire(getMetricStatisticsCall)
7878
res := c.client.GetMetricStatistics(ctx, logger, dimensions, namespace, metric)
7979
c.limiter.Release(getMetricStatisticsCall)

0 commit comments

Comments
 (0)