Skip to content

Commit

Permalink
chore!: adopt log/slog, drop go-kit/log
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
tjhop committed Oct 18, 2024
1 parent 311131a commit 4ab6e60
Show file tree
Hide file tree
Showing 30 changed files with 145 additions and 166 deletions.
11 changes: 5 additions & 6 deletions cmd/postgres_exporter/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"regexp"
"strings"

"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -39,19 +38,19 @@ func (e *Exporter) discoverDatabaseDSNs() []string {
var err error
dsnURI, err = url.Parse(dsn)
if err != nil {
level.Error(logger).Log("msg", "Unable to parse DSN as URI", "dsn", loggableDSN(dsn), "err", err)
logger.Error("Unable to parse DSN as URI", "dsn", loggableDSN(dsn), "err", err)
continue
}
} else if connstringRe.MatchString(dsn) {
dsnConnstring = dsn
} else {
level.Error(logger).Log("msg", "Unable to parse DSN as either URI or connstring", "dsn", loggableDSN(dsn))
logger.Error("Unable to parse DSN as either URI or connstring", "dsn", loggableDSN(dsn))
continue
}

server, err := e.servers.GetServer(dsn)
if err != nil {
level.Error(logger).Log("msg", "Error opening connection to database", "dsn", loggableDSN(dsn), "err", err)
logger.Error("Error opening connection to database", "dsn", loggableDSN(dsn), "err", err)
continue
}
dsns[dsn] = struct{}{}
Expand All @@ -61,7 +60,7 @@ func (e *Exporter) discoverDatabaseDSNs() []string {

databaseNames, err := queryDatabases(server)
if err != nil {
level.Error(logger).Log("msg", "Error querying databases", "dsn", loggableDSN(dsn), "err", err)
logger.Error("Error querying databases", "dsn", loggableDSN(dsn), "err", err)
continue
}
for _, databaseName := range databaseNames {
Expand Down Expand Up @@ -109,7 +108,7 @@ func (e *Exporter) scrapeDSN(ch chan<- prometheus.Metric, dsn string) error {

// Check if map versions need to be updated
if err := e.checkMapVersions(ch, server); err != nil {
level.Warn(logger).Log("msg", "Proceeding with outdated query maps, as the Postgres version could not be determined", "err", err)
logger.Warn("Proceeding with outdated query maps, as the Postgres version could not be determined", "err", err)
}

return server.Scrape(ch, e.disableSettingsMetrics)
Expand Down
35 changes: 17 additions & 18 deletions cmd/postgres_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import (
"strings"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/collector"
"github.com/prometheus-community/postgres_exporter/config"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
Expand All @@ -50,7 +49,7 @@ var (
excludeDatabases = kingpin.Flag("exclude-databases", "A list of databases to remove when autoDiscoverDatabases is enabled (DEPRECATED)").Default("").Envar("PG_EXPORTER_EXCLUDE_DATABASES").String()
includeDatabases = kingpin.Flag("include-databases", "A list of databases to include when autoDiscoverDatabases is enabled (DEPRECATED)").Default("").Envar("PG_EXPORTER_INCLUDE_DATABASES").String()
metricPrefix = kingpin.Flag("metric-prefix", "A metric prefix can be used to have non-default (not \"pg\") prefixes for each of the metrics").Default("pg").Envar("PG_EXPORTER_METRIC_PREFIX").String()
logger = log.NewNopLogger()
logger = promslog.NewNopLogger()
)

// Metric name parts.
Expand All @@ -70,11 +69,11 @@ const (

func main() {
kingpin.Version(version.Print(exporterName))
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
promslogConfig := &promslog.Config{}
flag.AddFlags(kingpin.CommandLine, promslogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger = promlog.New(promlogConfig)
logger = promslog.New(promslogConfig)

if *onlyDumpMaps {
dumpMaps()
Expand All @@ -83,28 +82,28 @@ func main() {

if err := c.ReloadConfig(*configFile, logger); err != nil {
// This is not fatal, but it means that auth must be provided for every dsn.
level.Warn(logger).Log("msg", "Error loading config", "err", err)
logger.Warn("Error loading config", "err", err)
}

dsns, err := getDataSources()
if err != nil {
level.Error(logger).Log("msg", "Failed reading data sources", "err", err.Error())
logger.Error("Failed reading data sources", "err", err.Error())
os.Exit(1)
}

excludedDatabases := strings.Split(*excludeDatabases, ",")
level.Info(logger).Log("msg", "Excluded databases", "databases", fmt.Sprintf("%v", excludedDatabases))
logger.Info("Excluded databases", "databases", fmt.Sprintf("%v", excludedDatabases))

if *queriesPath != "" {
level.Warn(logger).Log("msg", "The extended queries.yaml config is DEPRECATED", "file", *queriesPath)
logger.Warn("The extended queries.yaml config is DEPRECATED", "file", *queriesPath)
}

if *autoDiscoverDatabases || *excludeDatabases != "" || *includeDatabases != "" {
level.Warn(logger).Log("msg", "Scraping additional databases via auto discovery is DEPRECATED")
logger.Warn("Scraping additional databases via auto discovery is DEPRECATED")
}

if *constantLabelsList != "" {
level.Warn(logger).Log("msg", "Constant labels on all metrics is DEPRECATED")
logger.Warn("Constant labels on all metrics is DEPRECATED")
}

opts := []ExporterOpt{
Expand All @@ -122,7 +121,7 @@ func main() {
exporter.servers.Close()
}()

prometheus.MustRegister(version.NewCollector(exporterName))
prometheus.MustRegister(versioncollector.NewCollector(exporterName))

prometheus.MustRegister(exporter)

Expand All @@ -139,7 +138,7 @@ func main() {
[]string{},
)
if err != nil {
level.Warn(logger).Log("msg", "Failed to create PostgresCollector", "err", err.Error())
logger.Warn("Failed to create PostgresCollector", "err", err.Error())
} else {
prometheus.MustRegister(pe)
}
Expand All @@ -160,7 +159,7 @@ func main() {
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
level.Error(logger).Log("err", err)
logger.Error("error creating landing page", "err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
Expand All @@ -170,7 +169,7 @@ func main() {

srv := &http.Server{}
if err := web.ListenAndServe(srv, webConfig, logger); err != nil {
level.Error(logger).Log("msg", "Error running HTTP server", "err", err)
logger.Error("Error running HTTP server", "err", err)
os.Exit(1)
}
}
11 changes: 5 additions & 6 deletions cmd/postgres_exporter/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"time"

"github.com/blang/semver/v4"
"github.com/go-kit/log/level"
"github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -190,10 +189,10 @@ func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[str
scrapeStart := time.Now()

for namespace, mapping := range server.metricMap {
level.Debug(logger).Log("msg", "Querying namespace", "namespace", namespace)
logger.Debug("Querying namespace", "namespace", namespace)

if mapping.master && !server.master {
level.Debug(logger).Log("msg", "Query skipped...")
logger.Debug("Query skipped...")
continue
}

Expand All @@ -202,7 +201,7 @@ func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[str
serVersion, _ := semver.Parse(server.lastMapVersion.String())
runServerRange, _ := semver.ParseRange(server.runonserver)
if !runServerRange(serVersion) {
level.Debug(logger).Log("msg", "Query skipped for this database version", "version", server.lastMapVersion.String(), "target_version", server.runonserver)
logger.Debug("Query skipped for this database version", "version", server.lastMapVersion.String(), "target_version", server.runonserver)
continue
}
}
Expand Down Expand Up @@ -233,12 +232,12 @@ func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[str
// Serious error - a namespace disappeared
if err != nil {
namespaceErrors[namespace] = err
level.Info(logger).Log("err", err)
logger.Info("error finding namespace", "err", err)
}
// Non-serious errors - likely version or parsing problems.
if len(nonFatalErrors) > 0 {
for _, err := range nonFatalErrors {
level.Info(logger).Log("err", err)
logger.Info("error querying namespace", "err", err)
}
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/postgres_exporter/pg_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strconv"
"strings"

"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -32,7 +31,7 @@ var (

// Query the pg_settings view containing runtime variables
func querySettings(ch chan<- prometheus.Metric, server *Server) error {
level.Debug(logger).Log("msg", "Querying pg_setting view", "server", server)
logger.Debug("Querying pg_setting view", "server", server)

// pg_settings docs: https://www.postgresql.org/docs/current/static/view-pg-settings.html
//
Expand Down
21 changes: 10 additions & 11 deletions cmd/postgres_exporter/postgres_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/blang/semver/v4"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -284,7 +283,7 @@ func makeDescMap(pgVersion semver.Version, serverLabels prometheus.Labels, metri
if !columnMapping.supportedVersions(pgVersion) {
// It's very useful to be able to see what columns are being
// rejected.
level.Debug(logger).Log("msg", "Column is being forced to discard due to version incompatibility", "column", columnName)
logger.Debug("Column is being forced to discard due to version incompatibility", "column", columnName)
thisMap[columnName] = MetricMap{
discard: true,
conversion: func(_ interface{}) (float64, bool) {
Expand Down Expand Up @@ -371,7 +370,7 @@ func makeDescMap(pgVersion semver.Version, serverLabels prometheus.Labels, metri
case string:
durationString = t
default:
level.Error(logger).Log("msg", "Duration conversion metric was not a string")
logger.Error("Duration conversion metric was not a string")
return math.NaN(), false
}

Expand All @@ -381,7 +380,7 @@ func makeDescMap(pgVersion semver.Version, serverLabels prometheus.Labels, metri

d, err := time.ParseDuration(durationString)
if err != nil {
level.Error(logger).Log("msg", "Failed converting result to metric", "column", columnName, "in", in, "err", err)
logger.Error("Failed converting result to metric", "column", columnName, "in", in, "err", err)
return math.NaN(), false
}
return float64(d / time.Millisecond), true
Expand Down Expand Up @@ -491,7 +490,7 @@ func parseConstLabels(s string) prometheus.Labels {
for _, p := range parts {
keyValue := strings.Split(strings.TrimSpace(p), "=")
if len(keyValue) != 2 {
level.Error(logger).Log(`Wrong constant labels format, should be "key=value"`, "input", p)
logger.Error(`Wrong constant labels format, should be "key=value"`, "input", p)
continue
}
key := strings.TrimSpace(keyValue[0])
Expand Down Expand Up @@ -582,7 +581,7 @@ func newDesc(subsystem, name, help string, labels prometheus.Labels) *prometheus
}

func checkPostgresVersion(db *sql.DB, server string) (semver.Version, string, error) {
level.Debug(logger).Log("msg", "Querying PostgreSQL version", "server", server)
logger.Debug("Querying PostgreSQL version", "server", server)
versionRow := db.QueryRow("SELECT version();")
var versionString string
err := versionRow.Scan(&versionString)
Expand All @@ -605,12 +604,12 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server)
}

if !e.disableDefaultMetrics && semanticVersion.LT(lowestSupportedVersion) {
level.Warn(logger).Log("msg", "PostgreSQL version is lower than our lowest supported version", "server", server, "version", semanticVersion, "lowest_supported_version", lowestSupportedVersion)
logger.Warn("PostgreSQL version is lower than our lowest supported version", "server", server, "version", semanticVersion, "lowest_supported_version", lowestSupportedVersion)
}

// Check if semantic version changed and recalculate maps if needed.
if semanticVersion.NE(server.lastMapVersion) || server.metricMap == nil {
level.Info(logger).Log("msg", "Semantic version changed", "server", server, "from", server.lastMapVersion, "to", semanticVersion)
logger.Info("Semantic version changed", "server", server, "from", server.lastMapVersion, "to", semanticVersion)
server.mappingMtx.Lock()

// Get Default Metrics only for master database
Expand All @@ -631,13 +630,13 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server)
// Calculate the hashsum of the useQueries
userQueriesData, err := os.ReadFile(e.userQueriesPath)
if err != nil {
level.Error(logger).Log("msg", "Failed to reload user queries", "path", e.userQueriesPath, "err", err)
logger.Error("Failed to reload user queries", "path", e.userQueriesPath, "err", err)
e.userQueriesError.WithLabelValues(e.userQueriesPath, "").Set(1)
} else {
hashsumStr := fmt.Sprintf("%x", sha256.Sum256(userQueriesData))

if err := addQueries(userQueriesData, semanticVersion, server); err != nil {
level.Error(logger).Log("msg", "Failed to reload user queries", "path", e.userQueriesPath, "err", err)
logger.Error("Failed to reload user queries", "path", e.userQueriesPath, "err", err)
e.userQueriesError.WithLabelValues(e.userQueriesPath, hashsumStr).Set(1)
} else {
// Mark user queries as successfully loaded
Expand Down Expand Up @@ -679,7 +678,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
if err := e.scrapeDSN(ch, dsn); err != nil {
errorsCount++

level.Error(logger).Log("err", err)
logger.Error("error scraping dsn", "err", err, "dsn", dsn)

if _, ok := err.(*ErrorConnectToServer); ok {
connectionErrorsCount++
Expand Down
13 changes: 6 additions & 7 deletions cmd/postgres_exporter/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ package main

import (
"fmt"
"log/slog"
"net/http"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/collector"
"github.com/prometheus-community/postgres_exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func handleProbe(logger log.Logger, excludeDatabases []string) http.HandlerFunc {
func handleProbe(logger *slog.Logger, excludeDatabases []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
conf := c.GetConfig()
Expand All @@ -38,7 +37,7 @@ func handleProbe(logger log.Logger, excludeDatabases []string) http.HandlerFunc
var authModule config.AuthModule
authModuleName := params.Get("auth_module")
if authModuleName == "" {
level.Info(logger).Log("msg", "no auth_module specified, using default")
logger.Info("no auth_module specified, using default")
} else {
var ok bool
authModule, ok = conf.AuthModules[authModuleName]
Expand All @@ -54,14 +53,14 @@ func handleProbe(logger log.Logger, excludeDatabases []string) http.HandlerFunc

dsn, err := authModule.ConfigureTarget(target)
if err != nil {
level.Error(logger).Log("msg", "failed to configure target", "err", err)
logger.Error("failed to configure target", "err", err)
http.Error(w, fmt.Sprintf("could not configure dsn for target: %v", err), http.StatusBadRequest)
return
}

// TODO(@sysadmind): Timeout

tl := log.With(logger, "target", target)
tl := logger.With("target", target)

registry := prometheus.NewRegistry()

Expand All @@ -85,7 +84,7 @@ func handleProbe(logger log.Logger, excludeDatabases []string) http.HandlerFunc
// Run the probe
pc, err := collector.NewProbeCollector(tl, excludeDatabases, registry, dsn)
if err != nil {
level.Error(logger).Log("msg", "Error creating probe collector", "err", err)
logger.Error("Error creating probe collector", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down
Loading

0 comments on commit 4ab6e60

Please sign in to comment.