Skip to content

Commit 897e552

Browse files
committed
Switch from go-kit/log to log/slog for logging.
Bump github.com/prometheus/exporter-toolkit to v0.14.0. Bump github.com/prometheus/common to v0.63.0. Bump github.com/prometheus/client_golang to v1.20.4. Drops deps for go-kit/log and go-logfmt/logfmt. Update other deps. The logging system has been replaced with log/slog from the stdlib. This change is being made across the prometheus ecosystem. The logging output has changed, but the messages and levels remain the same. The ts label for the timestamp has between replaced with time. The caller field has been replaced by the source field. The level field now exposes the log level in capital letters. See prometheus/exporter-toolkit#240 and prometheus/common#677.
1 parent 1606984 commit 897e552

File tree

337 files changed

+36929
-4504
lines changed

Some content is hidden

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

337 files changed

+36929
-4504
lines changed

backrest/backrest_backup_metrics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package backrest
22

33
import (
4+
"log/slog"
45
"strconv"
56
"sync"
67
"time"
78

8-
"github.com/go-kit/log"
99
"github.com/prometheus/client_golang/prometheus"
1010
"github.com/prometheus/client_golang/prometheus/promauto"
1111
)
@@ -210,7 +210,7 @@ var (
210210
// - pgbackrest_backup_annotations
211211
//
212212
// And returns info about last backups.
213-
func getBackupMetrics(stanzaName string, backupRefCount bool, backupData []backup, dbData []db, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) lastBackupsStruct {
213+
func getBackupMetrics(stanzaName string, backupRefCount bool, backupData []backup, dbData []db, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) lastBackupsStruct {
214214
lastBackups := initLastBackupStruct()
215215
// Each backup for current stanza.
216216
for _, backup := range backupData {
@@ -407,7 +407,7 @@ func getBackupMetrics(stanzaName string, backupRefCount bool, backupData []backu
407407

408408
// Set backup metrics:
409409
// - pgbackrest_backup_databases
410-
func getBackupDBCountMetrics(maxParallelProcesses int, config, configIncludePath, stanzaName string, backupData []backup, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
410+
func getBackupDBCountMetrics(maxParallelProcesses int, config, configIncludePath, stanzaName string, backupData []backup, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
411411
// Create a buffered channel to enforce maximum parallelism.
412412
ch := make(chan struct{}, maxParallelProcesses)
413413
var wg sync.WaitGroup

backrest/backrest_backup_metrics_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package backrest
33
import (
44
"bytes"
55
"fmt"
6+
"log/slog"
67
"strings"
78
"testing"
89

9-
"github.com/go-kit/log"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"github.com/prometheus/common/expfmt"
1212
)
@@ -793,10 +793,10 @@ func TestGetBackupMetricsErrorsAndDebugs(t *testing.T) {
793793
t.Run(tt.name, func(t *testing.T) {
794794
resetBackupMetrics()
795795
out := &bytes.Buffer{}
796-
lc := log.NewLogfmtLogger(out)
796+
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
797797
getBackupMetrics(tt.args.stanzaName, tt.args.referenceCountFlag, tt.args.backupData, tt.args.dbData, tt.args.setUpMetricValueFun, lc)
798-
errorsOutputCount := strings.Count(out.String(), "level=error")
799-
debugsOutputCount := strings.Count(out.String(), "level=debug")
798+
errorsOutputCount := strings.Count(out.String(), "level=ERROR")
799+
debugsOutputCount := strings.Count(out.String(), "level=DEBUG")
800800
if tt.args.errorsCount != errorsOutputCount || tt.args.debugsCount != debugsOutputCount {
801801
t.Errorf("\nVariables do not match:\nerrors=%d, debugs=%d\nwant:\nerrors=%d, debugs=%d",
802802
tt.args.errorsCount, tt.args.debugsCount,

backrest/backrest_exporter.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package backrest
22

33
import (
4+
"log/slog"
45
"net/http"
56
"os"
67
"time"
78

8-
"github.com/go-kit/log"
9-
"github.com/go-kit/log/level"
109
"github.com/prometheus/client_golang/prometheus/promhttp"
1110
"github.com/prometheus/exporter-toolkit/web"
1211
)
@@ -31,8 +30,8 @@ func SetPromPortAndPath(flagsConfig web.FlagConfig, endpoint string) {
3130
}
3231

3332
// StartPromEndpoint run HTTP endpoint
34-
func StartPromEndpoint(logger log.Logger) {
35-
go func(logger log.Logger) {
33+
func StartPromEndpoint(logger *slog.Logger) {
34+
go func(logger *slog.Logger) {
3635
http.Handle(webEndpoint, promhttp.Handler())
3736
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
3837
w.Write([]byte(`<html>
@@ -47,14 +46,14 @@ func StartPromEndpoint(logger log.Logger) {
4746
ReadHeaderTimeout: 5 * time.Second,
4847
}
4948
if err := web.ListenAndServe(server, &webFlagsConfig, logger); err != nil {
50-
level.Error(logger).Log("msg", "Run web endpoint failed", "err", err)
49+
logger.Error("Run web endpoint failed", "err", err)
5150
os.Exit(1)
5251
}
5352
}(logger)
5453
}
5554

5655
// GetPgBackRestInfo get and parse pgBackRest info and set metrics
57-
func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, stanzasExclude []string, backupReferenceCount, backupDBCount, backupDBCountLatest, verboseWAL bool, backupDBCountParallelProcesses int, logger log.Logger) {
56+
func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, stanzasExclude []string, backupReferenceCount, backupDBCount, backupDBCountLatest, verboseWAL bool, backupDBCountParallelProcesses int, logger *slog.Logger) {
5857
// To calculate the time elapsed since the last completed full, differential or incremental backup.
5958
// For all stanzas values are calculated relative to one value.
6059
currentUnixTime := time.Now().Unix()
@@ -78,15 +77,15 @@ func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, st
7877
stanzaData, err := getAllInfoData(config, configIncludePath, stanza, backupType, logger)
7978
if err != nil {
8079
getDataSuccessStatus = false
81-
level.Error(logger).Log("msg", "Get data from pgBackRest failed", "err", err)
80+
logger.Error("Get data from pgBackRest failed", "err", err)
8281
}
8382
parseStanzaData, err := parseResult(stanzaData)
8483
if err != nil {
8584
getDataSuccessStatus = false
86-
level.Error(logger).Log("msg", "Parse JSON failed", "err", err)
85+
logger.Error("Parse JSON failed", "err", err)
8786
}
8887
if len(parseStanzaData) == 0 {
89-
level.Warn(logger).Log("msg", "No backup data returned")
88+
logger.Warn("No backup data returned")
9089
}
9190
// When no specific stanzas set for collecting we can reset the metrics as late as possible.
9291
if MetricResetFlag {
@@ -127,12 +126,12 @@ func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, st
127126
// It is necessary to set zero metric value for this stanza.
128127
getDataSuccessStatus = false
129128
getExporterStatusMetrics(stanza, getDataSuccessStatus, setUpMetricValue, logger)
130-
level.Warn(logger).Log("msg", "Stanza is specified in include and exclude lists", "stanza", stanza)
129+
logger.Warn("Stanza is specified in include and exclude lists", "stanza", stanza)
131130
}
132131
}
133132
}
134133

135134
// GetExporterInfo set exporter info metric
136-
func GetExporterInfo(exporterVersion string, logger log.Logger) {
135+
func GetExporterInfo(exporterVersion string, logger *slog.Logger) {
137136
getExporterMetrics(exporterVersion, setUpMetricValue, logger)
138137
}

backrest/backrest_exporter_metrics.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package backrest
22

33
import (
4-
"github.com/go-kit/log"
4+
"log/slog"
5+
56
"github.com/prometheus/client_golang/prometheus"
67
"github.com/prometheus/client_golang/prometheus/promauto"
78
)
@@ -21,7 +22,7 @@ var (
2122

2223
// Set exporter info metrics:
2324
// - pgbackrest_exporter_info
24-
func getExporterMetrics(exporterVer string, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
25+
func getExporterMetrics(exporterVer string, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
2526
setUpMetric(
2627
pgbrExporterInfoMetric,
2728
"pgbackrest_exporter_info",
@@ -34,7 +35,7 @@ func getExporterMetrics(exporterVer string, setUpMetricValueFun setUpMetricValue
3435

3536
// Set exporter metrics:
3637
// - pgbackrest_exporter_status
37-
func getExporterStatusMetrics(stanzaName string, getDataStatus bool, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
38+
func getExporterStatusMetrics(stanzaName string, getDataStatus bool, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
3839
// If the information is collected for all available stanzas,
3940
// the value of the label 'stanza' will be 'all-stanzas',
4041
// otherwise the stanza name will be set.

backrest/backrest_exporter_metrics_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package backrest
33
import (
44
"bytes"
55
"fmt"
6+
"log/slog"
67
"strings"
78
"testing"
89

9-
"github.com/go-kit/log"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"github.com/prometheus/common/expfmt"
1212
)
@@ -78,10 +78,10 @@ func TestGetExporterInfoErrorsAndDebugs(t *testing.T) {
7878
for _, tt := range tests {
7979
t.Run(tt.name, func(t *testing.T) {
8080
out := &bytes.Buffer{}
81-
lc := log.NewLogfmtLogger(out)
81+
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
8282
getExporterMetrics(tt.args.exporterVer, tt.args.setUpMetricValueFun, lc)
83-
errorsOutputCount := strings.Count(out.String(), "level=error")
84-
debugsOutputCount := strings.Count(out.String(), "level=debug")
83+
errorsOutputCount := strings.Count(out.String(), "level=ERROR")
84+
debugsOutputCount := strings.Count(out.String(), "level=DEBUG")
8585
if tt.args.errorsCount != errorsOutputCount || tt.args.debugsCount != debugsOutputCount {
8686
t.Errorf("\nVariables do not match:\nerrors=%d, debugs=%d\nwant:\nerrors=%d, debugs=%d",
8787
tt.args.errorsCount, tt.args.debugsCount,
@@ -173,10 +173,10 @@ func TestGetExporterStatusErrorsAndDebugs(t *testing.T) {
173173
for _, tt := range tests {
174174
t.Run(tt.name, func(t *testing.T) {
175175
out := &bytes.Buffer{}
176-
lc := log.NewLogfmtLogger(out)
176+
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
177177
getExporterStatusMetrics(tt.args.stanzaName, tt.args.getDataStatus, tt.args.setUpMetricValueFun, lc)
178-
errorsOutputCount := strings.Count(out.String(), "level=error")
179-
debugsOutputCount := strings.Count(out.String(), "level=debug")
178+
errorsOutputCount := strings.Count(out.String(), "level=ERROR")
179+
debugsOutputCount := strings.Count(out.String(), "level=DEBUG")
180180
if tt.args.errorsCount != errorsOutputCount || tt.args.debugsCount != debugsOutputCount {
181181
t.Errorf("\nVariables do not match:\nerrors=%d, debugs=%d\nwant:\nerrors=%d, debugs=%d",
182182
tt.args.errorsCount, tt.args.debugsCount,

backrest/backrest_exporter_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package backrest
33
import (
44
"bytes"
55
"fmt"
6+
"log/slog"
67
"os"
78
"os/exec"
89
"strconv"
910
"strings"
1011
"testing"
1112

12-
"github.com/go-kit/log"
13-
"github.com/prometheus/common/promlog"
1413
"github.com/prometheus/exporter-toolkit/web"
1514
)
1615

@@ -148,7 +147,7 @@ func TestGetPgBackRestInfo(t *testing.T) {
148147
execCommand = fakeExecCommand
149148
defer func() { execCommand = exec.Command }()
150149
out := &bytes.Buffer{}
151-
lc := log.NewLogfmtLogger(out)
150+
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
152151
GetPgBackRestInfo(
153152
tt.args.config,
154153
tt.args.configIncludePath,
@@ -195,16 +194,10 @@ func TestExecCommandHelper(t *testing.T) {
195194
// If it's necessary to capture the logs output in the test,
196195
// a separate logger is used inside the test.
197196
// The info logging level is used.
198-
func getLogger() log.Logger {
199-
var err error
200-
logLevel := &promlog.AllowedLevel{}
201-
err = logLevel.Set("info")
202-
if err != nil {
203-
panic(err)
204-
}
205-
promlogConfig := &promlog.Config{}
206-
promlogConfig.Level = logLevel
207-
return promlog.New(promlogConfig)
197+
func getLogger() *slog.Logger {
198+
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
199+
Level: slog.LevelInfo,
200+
}))
208201
}
209202

210203
// Helper for displaying web.FlagConfig values test messages.

backrest/backrest_last_backup_metrics.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package backrest
22

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

7-
"github.com/go-kit/log"
8-
"github.com/go-kit/log/level"
98
"github.com/prometheus/client_golang/prometheus"
109
"github.com/prometheus/client_golang/prometheus/promauto"
1110
)
@@ -134,7 +133,7 @@ var (
134133
// - pgbackrest_backup_last_repo_delta_map_bytes
135134
// - pgbackrest_backup_last_error_status
136135
// - pgbackrest_backup_last_annotations
137-
func getBackupLastMetrics(stanzaName string, lastBackups lastBackupsStruct, currentUnixTime int64, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
136+
func getBackupLastMetrics(stanzaName string, lastBackups lastBackupsStruct, currentUnixTime int64, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
138137
for _, backup := range []backupStruct{lastBackups.full, lastBackups.diff, lastBackups.incr} {
139138
// Repo backup map size for last backups.
140139
setUpMetric(
@@ -265,7 +264,7 @@ func getBackupLastMetrics(stanzaName string, lastBackups lastBackupsStruct, curr
265264

266265
// Set backup metrics:
267266
// - pgbackrest_backup_last_databases
268-
func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, lastBackups lastBackupsStruct, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
267+
func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, lastBackups lastBackupsStruct, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
269268
// For diff and incr run in parallel.
270269
var wg sync.WaitGroup
271270
// If name for diff backup is equal to full, there is no point in re-receiving data.
@@ -310,8 +309,8 @@ func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, l
310309
// Try to get info for full backup.
311310
parseStanzaDataSpecific, err := getParsedSpecificBackupInfoData(config, configIncludePath, stanzaName, lastBackups.full.backupLabel, logger)
312311
if err != nil {
313-
level.Error(logger).Log(
314-
"msg", "Get data from pgBackRest failed",
312+
logger.Error(
313+
"Get data from pgBackRest failed",
315314
"stanza", stanzaName,
316315
"backup", lastBackups.full.backupLabel,
317316
"err", err,
@@ -321,7 +320,8 @@ func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, l
321320
parseStanzaDataSpecific[0].Backup[0].DatabaseRef != nil {
322321
metricValue = convertDatabaseRefPointerToFloat(parseStanzaDataSpecific[0].Backup[0].DatabaseRef)
323322
} else {
324-
level.Warn(logger).Log("msg", "No backup data returned",
323+
logger.Warn(
324+
"No backup data returned",
325325
"stanza", stanzaName,
326326
"backup", lastBackups.full.backupLabel,
327327
)

backrest/backrest_last_backup_metrics_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package backrest
33
import (
44
"bytes"
55
"fmt"
6+
"log/slog"
7+
"os"
68
"os/exec"
79
"testing"
810

9-
"github.com/go-kit/log"
1011
"github.com/prometheus/client_golang/prometheus"
1112
"github.com/prometheus/common/expfmt"
1213
)
@@ -284,7 +285,7 @@ pgbackrest_backup_last_databases{backup_type="incr",block_incr="n",stanza="demo"
284285
mockDataBackupLast = tt.mockTestDataBackupLast
285286
execCommand = fakeExecCommandSpecificDatabase
286287
defer func() { execCommand = exec.Command }()
287-
lc := log.NewNopLogger()
288+
lc := slog.New(slog.NewTextHandler(os.Stdout, nil))
288289
getBackupLastDBCountMetrics(tt.args.config, tt.args.configIncludePath, tt.args.stanzaName, tt.args.lastBackups, tt.args.setUpMetricValueFun, lc)
289290
reg := prometheus.NewRegistry()
290291
reg.MustRegister(

0 commit comments

Comments
 (0)