Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions backrest/backrest_backup_metrics.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package backrest

import (
"log/slog"
"strconv"
"sync"
"time"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand Down Expand Up @@ -210,7 +210,7 @@ var (
// - pgbackrest_backup_annotations
//
// And returns info about last backups.
func getBackupMetrics(stanzaName string, backupRefCount bool, backupData []backup, dbData []db, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) lastBackupsStruct {
func getBackupMetrics(stanzaName string, backupRefCount bool, backupData []backup, dbData []db, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) lastBackupsStruct {
lastBackups := initLastBackupStruct()
// Each backup for current stanza.
for _, backup := range backupData {
Expand Down Expand Up @@ -407,7 +407,7 @@ func getBackupMetrics(stanzaName string, backupRefCount bool, backupData []backu

// Set backup metrics:
// - pgbackrest_backup_databases
func getBackupDBCountMetrics(maxParallelProcesses int, config, configIncludePath, stanzaName string, backupData []backup, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
func getBackupDBCountMetrics(maxParallelProcesses int, config, configIncludePath, stanzaName string, backupData []backup, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
// Create a buffered channel to enforce maximum parallelism.
ch := make(chan struct{}, maxParallelProcesses)
var wg sync.WaitGroup
Expand Down
8 changes: 4 additions & 4 deletions backrest/backrest_backup_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package backrest
import (
"bytes"
"fmt"
"log/slog"
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
Expand Down Expand Up @@ -793,10 +793,10 @@ func TestGetBackupMetricsErrorsAndDebugs(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
resetBackupMetrics()
out := &bytes.Buffer{}
lc := log.NewLogfmtLogger(out)
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
getBackupMetrics(tt.args.stanzaName, tt.args.referenceCountFlag, tt.args.backupData, tt.args.dbData, tt.args.setUpMetricValueFun, lc)
errorsOutputCount := strings.Count(out.String(), "level=error")
debugsOutputCount := strings.Count(out.String(), "level=debug")
errorsOutputCount := strings.Count(out.String(), "level=ERROR")
debugsOutputCount := strings.Count(out.String(), "level=DEBUG")
if tt.args.errorsCount != errorsOutputCount || tt.args.debugsCount != debugsOutputCount {
t.Errorf("\nVariables do not match:\nerrors=%d, debugs=%d\nwant:\nerrors=%d, debugs=%d",
tt.args.errorsCount, tt.args.debugsCount,
Expand Down
21 changes: 10 additions & 11 deletions backrest/backrest_exporter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package backrest

import (
"log/slog"
"net/http"
"os"
"time"

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

// StartPromEndpoint run HTTP endpoint
func StartPromEndpoint(logger log.Logger) {
go func(logger log.Logger) {
func StartPromEndpoint(logger *slog.Logger) {
go func(logger *slog.Logger) {
http.Handle(webEndpoint, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
Expand All @@ -47,14 +46,14 @@ func StartPromEndpoint(logger log.Logger) {
ReadHeaderTimeout: 5 * time.Second,
}
if err := web.ListenAndServe(server, &webFlagsConfig, logger); err != nil {
level.Error(logger).Log("msg", "Run web endpoint failed", "err", err)
logger.Error("Run web endpoint failed", "err", err)
os.Exit(1)
}
}(logger)
}

// GetPgBackRestInfo get and parse pgBackRest info and set metrics
func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, stanzasExclude []string, backupReferenceCount, backupDBCount, backupDBCountLatest, verboseWAL bool, backupDBCountParallelProcesses int, logger log.Logger) {
func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, stanzasExclude []string, backupReferenceCount, backupDBCount, backupDBCountLatest, verboseWAL bool, backupDBCountParallelProcesses int, logger *slog.Logger) {
// To calculate the time elapsed since the last completed full, differential or incremental backup.
// For all stanzas values are calculated relative to one value.
currentUnixTime := time.Now().Unix()
Expand All @@ -78,15 +77,15 @@ func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, st
stanzaData, err := getAllInfoData(config, configIncludePath, stanza, backupType, logger)
if err != nil {
getDataSuccessStatus = false
level.Error(logger).Log("msg", "Get data from pgBackRest failed", "err", err)
logger.Error("Get data from pgBackRest failed", "err", err)
}
parseStanzaData, err := parseResult(stanzaData)
if err != nil {
getDataSuccessStatus = false
level.Error(logger).Log("msg", "Parse JSON failed", "err", err)
logger.Error("Parse JSON failed", "err", err)
}
if len(parseStanzaData) == 0 {
level.Warn(logger).Log("msg", "No backup data returned")
logger.Warn("No backup data returned")
}
// When no specific stanzas set for collecting we can reset the metrics as late as possible.
if MetricResetFlag {
Expand Down Expand Up @@ -127,12 +126,12 @@ func GetPgBackRestInfo(config, configIncludePath, backupType string, stanzas, st
// It is necessary to set zero metric value for this stanza.
getDataSuccessStatus = false
getExporterStatusMetrics(stanza, getDataSuccessStatus, setUpMetricValue, logger)
level.Warn(logger).Log("msg", "Stanza is specified in include and exclude lists", "stanza", stanza)
logger.Warn("Stanza is specified in include and exclude lists", "stanza", stanza)
}
}
}

// GetExporterInfo set exporter info metric
func GetExporterInfo(exporterVersion string, logger log.Logger) {
func GetExporterInfo(exporterVersion string, logger *slog.Logger) {
getExporterMetrics(exporterVersion, setUpMetricValue, logger)
}
7 changes: 4 additions & 3 deletions backrest/backrest_exporter_metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package backrest

import (
"github.com/go-kit/log"
"log/slog"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand All @@ -21,7 +22,7 @@ var (

// Set exporter info metrics:
// - pgbackrest_exporter_info
func getExporterMetrics(exporterVer string, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
func getExporterMetrics(exporterVer string, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
setUpMetric(
pgbrExporterInfoMetric,
"pgbackrest_exporter_info",
Expand All @@ -34,7 +35,7 @@ func getExporterMetrics(exporterVer string, setUpMetricValueFun setUpMetricValue

// Set exporter metrics:
// - pgbackrest_exporter_status
func getExporterStatusMetrics(stanzaName string, getDataStatus bool, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
func getExporterStatusMetrics(stanzaName string, getDataStatus bool, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
// If the information is collected for all available stanzas,
// the value of the label 'stanza' will be 'all-stanzas',
// otherwise the stanza name will be set.
Expand Down
14 changes: 7 additions & 7 deletions backrest/backrest_exporter_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package backrest
import (
"bytes"
"fmt"
"log/slog"
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
Expand Down Expand Up @@ -78,10 +78,10 @@ func TestGetExporterInfoErrorsAndDebugs(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
lc := log.NewLogfmtLogger(out)
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
getExporterMetrics(tt.args.exporterVer, tt.args.setUpMetricValueFun, lc)
errorsOutputCount := strings.Count(out.String(), "level=error")
debugsOutputCount := strings.Count(out.String(), "level=debug")
errorsOutputCount := strings.Count(out.String(), "level=ERROR")
debugsOutputCount := strings.Count(out.String(), "level=DEBUG")
if tt.args.errorsCount != errorsOutputCount || tt.args.debugsCount != debugsOutputCount {
t.Errorf("\nVariables do not match:\nerrors=%d, debugs=%d\nwant:\nerrors=%d, debugs=%d",
tt.args.errorsCount, tt.args.debugsCount,
Expand Down Expand Up @@ -173,10 +173,10 @@ func TestGetExporterStatusErrorsAndDebugs(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
lc := log.NewLogfmtLogger(out)
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
getExporterStatusMetrics(tt.args.stanzaName, tt.args.getDataStatus, tt.args.setUpMetricValueFun, lc)
errorsOutputCount := strings.Count(out.String(), "level=error")
debugsOutputCount := strings.Count(out.String(), "level=debug")
errorsOutputCount := strings.Count(out.String(), "level=ERROR")
debugsOutputCount := strings.Count(out.String(), "level=DEBUG")
if tt.args.errorsCount != errorsOutputCount || tt.args.debugsCount != debugsOutputCount {
t.Errorf("\nVariables do not match:\nerrors=%d, debugs=%d\nwant:\nerrors=%d, debugs=%d",
tt.args.errorsCount, tt.args.debugsCount,
Expand Down
19 changes: 6 additions & 13 deletions backrest/backrest_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package backrest
import (
"bytes"
"fmt"
"log/slog"
"os"
"os/exec"
"strconv"
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/common/promlog"
"github.com/prometheus/exporter-toolkit/web"
)

Expand Down Expand Up @@ -148,7 +147,7 @@ func TestGetPgBackRestInfo(t *testing.T) {
execCommand = fakeExecCommand
defer func() { execCommand = exec.Command }()
out := &bytes.Buffer{}
lc := log.NewLogfmtLogger(out)
lc := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug}))
GetPgBackRestInfo(
tt.args.config,
tt.args.configIncludePath,
Expand Down Expand Up @@ -195,16 +194,10 @@ func TestExecCommandHelper(t *testing.T) {
// If it's necessary to capture the logs output in the test,
// a separate logger is used inside the test.
// The info logging level is used.
func getLogger() log.Logger {
var err error
logLevel := &promlog.AllowedLevel{}
err = logLevel.Set("info")
if err != nil {
panic(err)
}
promlogConfig := &promlog.Config{}
promlogConfig.Level = logLevel
return promlog.New(promlogConfig)
func getLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
}

// Helper for displaying web.FlagConfig values test messages.
Expand Down
14 changes: 7 additions & 7 deletions backrest/backrest_last_backup_metrics.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package backrest

import (
"log/slog"
"sync"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand Down Expand Up @@ -134,7 +133,7 @@ var (
// - pgbackrest_backup_last_repo_delta_map_bytes
// - pgbackrest_backup_last_error_status
// - pgbackrest_backup_last_annotations
func getBackupLastMetrics(stanzaName string, lastBackups lastBackupsStruct, currentUnixTime int64, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
func getBackupLastMetrics(stanzaName string, lastBackups lastBackupsStruct, currentUnixTime int64, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
for _, backup := range []backupStruct{lastBackups.full, lastBackups.diff, lastBackups.incr} {
// Repo backup map size for last backups.
setUpMetric(
Expand Down Expand Up @@ -265,7 +264,7 @@ func getBackupLastMetrics(stanzaName string, lastBackups lastBackupsStruct, curr

// Set backup metrics:
// - pgbackrest_backup_last_databases
func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, lastBackups lastBackupsStruct, setUpMetricValueFun setUpMetricValueFunType, logger log.Logger) {
func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, lastBackups lastBackupsStruct, setUpMetricValueFun setUpMetricValueFunType, logger *slog.Logger) {
// For diff and incr run in parallel.
var wg sync.WaitGroup
// If name for diff backup is equal to full, there is no point in re-receiving data.
Expand Down Expand Up @@ -310,8 +309,8 @@ func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, l
// Try to get info for full backup.
parseStanzaDataSpecific, err := getParsedSpecificBackupInfoData(config, configIncludePath, stanzaName, lastBackups.full.backupLabel, logger)
if err != nil {
level.Error(logger).Log(
"msg", "Get data from pgBackRest failed",
logger.Error(
"Get data from pgBackRest failed",
"stanza", stanzaName,
"backup", lastBackups.full.backupLabel,
"err", err,
Expand All @@ -321,7 +320,8 @@ func getBackupLastDBCountMetrics(config, configIncludePath, stanzaName string, l
parseStanzaDataSpecific[0].Backup[0].DatabaseRef != nil {
metricValue = convertDatabaseRefPointerToFloat(parseStanzaDataSpecific[0].Backup[0].DatabaseRef)
} else {
level.Warn(logger).Log("msg", "No backup data returned",
logger.Warn(
"No backup data returned",
"stanza", stanzaName,
"backup", lastBackups.full.backupLabel,
)
Expand Down
5 changes: 3 additions & 2 deletions backrest/backrest_last_backup_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package backrest
import (
"bytes"
"fmt"
"log/slog"
"os"
"os/exec"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
Expand Down Expand Up @@ -284,7 +285,7 @@ pgbackrest_backup_last_databases{backup_type="incr",block_incr="n",stanza="demo"
mockDataBackupLast = tt.mockTestDataBackupLast
execCommand = fakeExecCommandSpecificDatabase
defer func() { execCommand = exec.Command }()
lc := log.NewNopLogger()
lc := slog.New(slog.NewTextHandler(os.Stdout, nil))
getBackupLastDBCountMetrics(tt.args.config, tt.args.configIncludePath, tt.args.stanzaName, tt.args.lastBackups, tt.args.setUpMetricValueFun, lc)
reg := prometheus.NewRegistry()
reg.MustRegister(
Expand Down
Loading
Loading