1
0
mirror of https://github.com/prometheus-community/postgres_exporter.git synced 2025-08-08 04:42:07 +03:00

chore!: adopt log/slog, drop go-kit/log (#1073)

* ci: update go to version 1.23

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>

* build(deps): bump prometheus/{client_golang,common,exporter-toolkit}

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>

* chore!: adopt log/slog, drop go-kit/log

The bulk of this change set was automated by the following script which
is being used to aid in converting the various exporters/projects to use
slog:

https://gist.github.com/tjhop/49f96fb7ebbe55b12deee0b0312d8434

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>

---------

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>
Co-authored-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
TJ Hoplock
2024-10-26 15:44:17 -04:00
committed by GitHub
parent 3743987494
commit e8540767e4
32 changed files with 206 additions and 228 deletions

View File

@@ -17,12 +17,11 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"sync"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -62,7 +61,7 @@ type Collector interface {
}
type collectorConfig struct {
logger log.Logger
logger *slog.Logger
excludeDatabases []string
}
@@ -89,7 +88,7 @@ func registerCollector(name string, isDefaultEnabled bool, createFunc func(colle
// PostgresCollector implements the prometheus.Collector interface.
type PostgresCollector struct {
Collectors map[string]Collector
logger log.Logger
logger *slog.Logger
instance *instance
}
@@ -97,7 +96,7 @@ type PostgresCollector struct {
type Option func(*PostgresCollector) error
// NewPostgresCollector creates a new PostgresCollector.
func NewPostgresCollector(logger log.Logger, excludeDatabases []string, dsn string, filters []string, options ...Option) (*PostgresCollector, error) {
func NewPostgresCollector(logger *slog.Logger, excludeDatabases []string, dsn string, filters []string, options ...Option) (*PostgresCollector, error) {
p := &PostgresCollector{
logger: logger,
}
@@ -131,7 +130,7 @@ func NewPostgresCollector(logger log.Logger, excludeDatabases []string, dsn stri
collectors[key] = collector
} else {
collector, err := factories[key](collectorConfig{
logger: log.With(logger, "collector", key),
logger: logger.With("collector", key),
excludeDatabases: excludeDatabases,
})
if err != nil {
@@ -173,7 +172,7 @@ func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
// Set up the database connection for the collector.
err := inst.setup()
if err != nil {
level.Error(p.logger).Log("msg", "Error opening connection to database", "err", err)
p.logger.Error("Error opening connection to database", "err", err)
return
}
defer inst.Close()
@@ -189,7 +188,7 @@ func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
wg.Wait()
}
func execute(ctx context.Context, name string, c Collector, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) {
func execute(ctx context.Context, name string, c Collector, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) {
begin := time.Now()
err := c.Update(ctx, instance, ch)
duration := time.Since(begin)
@@ -197,13 +196,13 @@ func execute(ctx context.Context, name string, c Collector, instance *instance,
if err != nil {
if IsNoDataError(err) {
level.Debug(logger).Log("msg", "collector returned no data", "name", name, "duration_seconds", duration.Seconds(), "err", err)
logger.Debug("collector returned no data", "name", name, "duration_seconds", duration.Seconds(), "err", err)
} else {
level.Error(logger).Log("msg", "collector failed", "name", name, "duration_seconds", duration.Seconds(), "err", err)
logger.Error("collector failed", "name", name, "duration_seconds", duration.Seconds(), "err", err)
}
success = 0
} else {
level.Debug(logger).Log("msg", "collector succeeded", "name", name, "duration_seconds", duration.Seconds())
logger.Debug("collector succeeded", "name", name, "duration_seconds", duration.Seconds())
success = 1
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGDatabaseCollector struct {
log log.Logger
log *slog.Logger
excludedDatabases []string
}

View File

@@ -16,9 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -29,7 +28,7 @@ func init() {
}
type PGDatabaseWraparoundCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGDatabaseWraparoundCollector(config collectorConfig) (Collector, error) {
@@ -81,15 +80,15 @@ func (c *PGDatabaseWraparoundCollector) Update(ctx context.Context, instance *in
}
if !datname.Valid {
level.Debug(c.log).Log("msg", "Skipping database with NULL name")
c.log.Debug("Skipping database with NULL name")
continue
}
if !ageDatfrozenxid.Valid {
level.Debug(c.log).Log("msg", "Skipping stat emission with NULL age_datfrozenxid")
c.log.Debug("Skipping stat emission with NULL age_datfrozenxid")
continue
}
if !ageDatminmxid.Valid {
level.Debug(c.log).Log("msg", "Skipping stat emission with NULL age_datminmxid")
c.log.Debug("Skipping stat emission with NULL age_datminmxid")
continue
}

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGLocksCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGLocksCollector(config collectorConfig) (Collector, error) {

View File

@@ -15,8 +15,8 @@ package collector
import (
"context"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -27,7 +27,7 @@ func init() {
}
type PGLongRunningTransactionsCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGLongRunningTransactionsCollector(config collectorConfig) (Collector, error) {

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGProcessIdleCollector struct {
log log.Logger
log *slog.Logger
}
const processIdleSubsystem = "process_idle"

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGReplicationSlotCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGReplicationSlotCollector(config collectorConfig) (Collector, error) {

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGRolesCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGRolesCollector(config collectorConfig) (Collector, error) {

View File

@@ -15,8 +15,8 @@ package collector
import (
"context"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -27,7 +27,7 @@ func init() {
}
type PGStatActivityAutovacuumCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGStatActivityAutovacuumCollector(config collectorConfig) (Collector, error) {

View File

@@ -17,11 +17,10 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"strings"
"github.com/blang/semver/v4"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -32,7 +31,7 @@ func init() {
}
type PGStatDatabaseCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGStatDatabaseCollector(config collectorConfig) (Collector, error) {
@@ -299,85 +298,85 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
}
if !datid.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no datid")
c.log.Debug("Skipping collecting metric because it has no datid")
continue
}
if !datname.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no datname")
c.log.Debug("Skipping collecting metric because it has no datname")
continue
}
if !numBackends.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no numbackends")
c.log.Debug("Skipping collecting metric because it has no numbackends")
continue
}
if !xactCommit.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no xact_commit")
c.log.Debug("Skipping collecting metric because it has no xact_commit")
continue
}
if !xactRollback.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no xact_rollback")
c.log.Debug("Skipping collecting metric because it has no xact_rollback")
continue
}
if !blksRead.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no blks_read")
c.log.Debug("Skipping collecting metric because it has no blks_read")
continue
}
if !blksHit.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no blks_hit")
c.log.Debug("Skipping collecting metric because it has no blks_hit")
continue
}
if !tupReturned.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no tup_returned")
c.log.Debug("Skipping collecting metric because it has no tup_returned")
continue
}
if !tupFetched.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no tup_fetched")
c.log.Debug("Skipping collecting metric because it has no tup_fetched")
continue
}
if !tupInserted.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no tup_inserted")
c.log.Debug("Skipping collecting metric because it has no tup_inserted")
continue
}
if !tupUpdated.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no tup_updated")
c.log.Debug("Skipping collecting metric because it has no tup_updated")
continue
}
if !tupDeleted.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no tup_deleted")
c.log.Debug("Skipping collecting metric because it has no tup_deleted")
continue
}
if !conflicts.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no conflicts")
c.log.Debug("Skipping collecting metric because it has no conflicts")
continue
}
if !tempFiles.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no temp_files")
c.log.Debug("Skipping collecting metric because it has no temp_files")
continue
}
if !tempBytes.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no temp_bytes")
c.log.Debug("Skipping collecting metric because it has no temp_bytes")
continue
}
if !deadlocks.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no deadlocks")
c.log.Debug("Skipping collecting metric because it has no deadlocks")
continue
}
if !blkReadTime.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no blk_read_time")
c.log.Debug("Skipping collecting metric because it has no blk_read_time")
continue
}
if !blkWriteTime.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no blk_write_time")
c.log.Debug("Skipping collecting metric because it has no blk_write_time")
continue
}
if activeTimeAvail && !activeTime.Valid {
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no active_time")
c.log.Debug("Skipping collecting metric because it has no active_time")
continue
}
statsResetMetric := 0.0
if !statsReset.Valid {
level.Debug(c.log).Log("msg", "No metric for stats_reset, will collect 0 instead")
c.log.Debug("No metric for stats_reset, will collect 0 instead")
}
if statsReset.Valid {
statsResetMetric = float64(statsReset.Time.Unix())

View File

@@ -19,9 +19,9 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/blang/semver/v4"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)
@@ -92,7 +92,7 @@ func TestPGStatDatabaseCollector(t *testing.T) {
go func() {
defer close(ch)
c := PGStatDatabaseCollector{
log: log.With(log.NewNopLogger(), "collector", "pg_stat_database"),
log: promslog.NewNopLogger().With("collector", "pg_stat_database"),
}
if err := c.Update(context.Background(), inst, ch); err != nil {
@@ -219,7 +219,7 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
go func() {
defer close(ch)
c := PGStatDatabaseCollector{
log: log.With(log.NewNopLogger(), "collector", "pg_stat_database"),
log: promslog.NewNopLogger().With("collector", "pg_stat_database"),
}
if err := c.Update(context.Background(), inst, ch); err != nil {
@@ -368,7 +368,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
go func() {
defer close(ch)
c := PGStatDatabaseCollector{
log: log.With(log.NewNopLogger(), "collector", "pg_stat_database"),
log: promslog.NewNopLogger().With("collector", "pg_stat_database"),
}
if err := c.Update(context.Background(), inst, ch); err != nil {
@@ -489,7 +489,7 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
go func() {
defer close(ch)
c := PGStatDatabaseCollector{
log: log.With(log.NewNopLogger(), "collector", "pg_stat_database"),
log: promslog.NewNopLogger().With("collector", "pg_stat_database"),
}
if err := c.Update(context.Background(), inst, ch); err != nil {

View File

@@ -16,9 +16,9 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/blang/semver/v4"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -32,7 +32,7 @@ func init() {
}
type PGStatStatementsCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGStatStatementsCollector(config collectorConfig) (Collector, error) {

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGStatUserTablesCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGStatUserTablesCollector(config collectorConfig) (Collector, error) {

View File

@@ -16,9 +16,8 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -27,7 +26,7 @@ func init() {
}
type PGStatWalReceiverCollector struct {
log log.Logger
log *slog.Logger
}
const statWalReceiverSubsystem = "stat_wal_receiver"
@@ -157,55 +156,55 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
}
}
if !upstreamHost.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because upstream host is null")
c.log.Debug("Skipping wal receiver stats because upstream host is null")
continue
}
if !slotName.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because slotname host is null")
c.log.Debug("Skipping wal receiver stats because slotname host is null")
continue
}
if !status.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because status is null")
c.log.Debug("Skipping wal receiver stats because status is null")
continue
}
labels := []string{upstreamHost.String, slotName.String, status.String}
if !receiveStartLsn.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because receive_start_lsn is null")
c.log.Debug("Skipping wal receiver stats because receive_start_lsn is null")
continue
}
if !receiveStartTli.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because receive_start_tli is null")
c.log.Debug("Skipping wal receiver stats because receive_start_tli is null")
continue
}
if hasFlushedLSN && !flushedLsn.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because flushed_lsn is null")
c.log.Debug("Skipping wal receiver stats because flushed_lsn is null")
continue
}
if !receivedTli.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because received_tli is null")
c.log.Debug("Skipping wal receiver stats because received_tli is null")
continue
}
if !lastMsgSendTime.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because last_msg_send_time is null")
c.log.Debug("Skipping wal receiver stats because last_msg_send_time is null")
continue
}
if !lastMsgReceiptTime.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because last_msg_receipt_time is null")
c.log.Debug("Skipping wal receiver stats because last_msg_receipt_time is null")
continue
}
if !latestEndLsn.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because latest_end_lsn is null")
c.log.Debug("Skipping wal receiver stats because latest_end_lsn is null")
continue
}
if !latestEndTime.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because latest_end_time is null")
c.log.Debug("Skipping wal receiver stats because latest_end_time is null")
continue
}
if !upstreamNode.Valid {
level.Debug(c.log).Log("msg", "Skipping wal receiver stats because upstream_node is null")
c.log.Debug("Skipping wal receiver stats because upstream_node is null")
continue
}
ch <- prometheus.MustNewConstMetric(

View File

@@ -15,8 +15,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -25,7 +25,7 @@ func init() {
}
type PGStatioUserIndexesCollector struct {
log log.Logger
log *slog.Logger
}
const statioUserIndexesSubsystem = "statio_user_indexes"

View File

@@ -16,8 +16,8 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
@@ -28,7 +28,7 @@ func init() {
}
type PGStatIOUserTablesCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGStatIOUserTablesCollector(config collectorConfig) (Collector, error) {

View File

@@ -15,10 +15,9 @@ package collector
import (
"context"
"log/slog"
"github.com/blang/semver/v4"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -29,7 +28,7 @@ func init() {
}
type PGXlogLocationCollector struct {
log log.Logger
log *slog.Logger
}
func NewPGXlogLocationCollector(config collectorConfig) (Collector, error) {
@@ -59,7 +58,7 @@ func (c PGXlogLocationCollector) Update(ctx context.Context, instance *instance,
// https://wiki.postgresql.org/wiki/New_in_postgres_10#Renaming_of_.22xlog.22_to_.22wal.22_Globally_.28and_location.2Flsn.29
after10 := instance.version.Compare(semver.MustParse("10.0.0"))
if after10 >= 0 {
level.Warn(c.log).Log("msg", "xlog_location collector is not available on PostgreSQL >= 10.0.0, skipping")
c.log.Warn("xlog_location collector is not available on PostgreSQL >= 10.0.0, skipping")
return nil
}

View File

@@ -15,10 +15,9 @@ package collector
import (
"context"
"log/slog"
"sync"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/config"
"github.com/prometheus/client_golang/prometheus"
)
@@ -26,11 +25,11 @@ import (
type ProbeCollector struct {
registry *prometheus.Registry
collectors map[string]Collector
logger log.Logger
logger *slog.Logger
instance *instance
}
func NewProbeCollector(logger log.Logger, excludeDatabases []string, registry *prometheus.Registry, dsn config.DSN) (*ProbeCollector, error) {
func NewProbeCollector(logger *slog.Logger, excludeDatabases []string, registry *prometheus.Registry, dsn config.DSN) (*ProbeCollector, error) {
collectors := make(map[string]Collector)
initiatedCollectorsMtx.Lock()
defer initiatedCollectorsMtx.Unlock()
@@ -47,7 +46,7 @@ func NewProbeCollector(logger log.Logger, excludeDatabases []string, registry *p
} else {
collector, err := factories[key](
collectorConfig{
logger: log.With(logger, "collector", key),
logger: logger.With("collector", key),
excludeDatabases: excludeDatabases,
})
if err != nil {
@@ -78,7 +77,7 @@ func (pc *ProbeCollector) Collect(ch chan<- prometheus.Metric) {
// Set up the database connection for the collector.
err := pc.instance.setup()
if err != nil {
level.Error(pc.logger).Log("msg", "Error opening connection to database", "err", err)
pc.logger.Error("Error opening connection to database", "err", err)
return
}
defer pc.instance.Close()