Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ executors:
# This must match .promu.yml.
golang:
docker:
- image: cimg/go:1.22.6
- image: cimg/go:1.23
jobs:
test:
executor: golang
Expand Down
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
linters:
enable:
- revive
- sloglint

issues:
exclude-rules:
Expand All @@ -11,7 +12,7 @@ issues:

linters-settings:
errcheck:
exclude: scripts/errcheck_excludes.txt
exclude-functions: scripts/errcheck_excludes.txt
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
Expand Down
2 changes: 1 addition & 1 deletion .promu.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
go:
# This must match .circle/config.yml.
version: 1.22
version: 1.23
repository:
path: github.com/prometheus-community/elasticsearch_exporter
build:
Expand Down
15 changes: 7 additions & 8 deletions collector/cluster_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"path"

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

Expand All @@ -46,7 +45,7 @@ type clusterHealthStatusMetric struct {

// ClusterHealth type defines the collector struct
type ClusterHealth struct {
logger log.Logger
logger *slog.Logger
client *http.Client
url *url.URL

Expand All @@ -55,7 +54,7 @@ type ClusterHealth struct {
}

// NewClusterHealth returns a new Collector exposing ClusterHealth stats.
func NewClusterHealth(logger log.Logger, client *http.Client, url *url.URL) *ClusterHealth {
func NewClusterHealth(logger *slog.Logger, client *http.Client, url *url.URL) *ClusterHealth {
subsystem := "cluster_health"

return &ClusterHealth{
Expand Down Expand Up @@ -225,8 +224,8 @@ func (c *ClusterHealth) fetchAndDecodeClusterHealth() (clusterHealthResponse, er
defer func() {
err = res.Body.Close()
if err != nil {
level.Warn(c.logger).Log(
"msg", "failed to close http.Client",
c.logger.Warn(
"failed to close http.Client",
"err", err,
)
}
Expand All @@ -252,8 +251,8 @@ func (c *ClusterHealth) fetchAndDecodeClusterHealth() (clusterHealthResponse, er
func (c *ClusterHealth) Collect(ch chan<- prometheus.Metric) {
clusterHealthResp, err := c.fetchAndDecodeClusterHealth()
if err != nil {
level.Warn(c.logger).Log(
"msg", "failed to fetch and decode cluster health",
c.logger.Warn(
"failed to fetch and decode cluster health",
"err", err,
)
return
Expand Down
4 changes: 2 additions & 2 deletions collector/cluster_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/common/promslog"
)

func TestClusterHealth(t *testing.T) {
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestClusterHealth(t *testing.T) {
t.Fatal(err)
}

c := NewClusterHealth(log.NewNopLogger(), http.DefaultClient, u)
c := NewClusterHealth(promslog.NewNopLogger(), http.DefaultClient, u)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions collector/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/url"

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

Expand All @@ -30,12 +30,12 @@ func init() {
}

type ClusterInfoCollector struct {
logger log.Logger
logger *slog.Logger
u *url.URL
hc *http.Client
}

func NewClusterInfo(logger log.Logger, u *url.URL, hc *http.Client) (Collector, error) {
func NewClusterInfo(logger *slog.Logger, u *url.URL, hc *http.Client) (Collector, error) {
return &ClusterInfoCollector{
logger: logger,
u: u,
Expand Down
4 changes: 2 additions & 2 deletions collector/cluster_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/common/promslog"
)

func TestClusterInfo(t *testing.T) {
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestClusterInfo(t *testing.T) {
t.Fatal(err)
}

c, err := NewClusterInfo(log.NewNopLogger(), u, http.DefaultClient)
c, err := NewClusterInfo(promslog.NewNopLogger(), u, http.DefaultClient)
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 9 additions & 10 deletions collector/cluster_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/imdario/mergo"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -34,12 +33,12 @@ func init() {
}

type ClusterSettingsCollector struct {
logger log.Logger
logger *slog.Logger
u *url.URL
hc *http.Client
}

func NewClusterSettings(logger log.Logger, u *url.URL, hc *http.Client) (Collector, error) {
func NewClusterSettings(logger *slog.Logger, u *url.URL, hc *http.Client) (Collector, error) {
return &ClusterSettingsCollector{
logger: logger,
u: u,
Expand Down Expand Up @@ -226,7 +225,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh
if strings.HasSuffix(merged.Cluster.Routing.Allocation.Disk.Watermark.High, "b") {
flooodStageBytes, err := getValueInBytes(merged.Cluster.Routing.Allocation.Disk.Watermark.FloodStage)
if err != nil {
level.Error(c.logger).Log("msg", "failed to parse flood_stage bytes", "err", err)
c.logger.Error("failed to parse flood_stage bytes", "err", err)
} else {
ch <- prometheus.MustNewConstMetric(
clusterSettingsDesc["floodStageBytes"],
Expand All @@ -237,7 +236,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh

highBytes, err := getValueInBytes(merged.Cluster.Routing.Allocation.Disk.Watermark.High)
if err != nil {
level.Error(c.logger).Log("msg", "failed to parse high bytes", "err", err)
c.logger.Error("failed to parse high bytes", "err", err)
} else {
ch <- prometheus.MustNewConstMetric(
clusterSettingsDesc["highBytes"],
Expand All @@ -248,7 +247,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh

lowBytes, err := getValueInBytes(merged.Cluster.Routing.Allocation.Disk.Watermark.Low)
if err != nil {
level.Error(c.logger).Log("msg", "failed to parse low bytes", "err", err)
c.logger.Error("failed to parse low bytes", "err", err)
} else {
ch <- prometheus.MustNewConstMetric(
clusterSettingsDesc["lowBytes"],
Expand All @@ -263,7 +262,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh
// Watermark ratio metrics
floodRatio, err := getValueAsRatio(merged.Cluster.Routing.Allocation.Disk.Watermark.FloodStage)
if err != nil {
level.Error(c.logger).Log("msg", "failed to parse flood_stage ratio", "err", err)
c.logger.Error("failed to parse flood_stage ratio", "err", err)
} else {
ch <- prometheus.MustNewConstMetric(
clusterSettingsDesc["floodStageRatio"],
Expand All @@ -274,7 +273,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh

highRatio, err := getValueAsRatio(merged.Cluster.Routing.Allocation.Disk.Watermark.High)
if err != nil {
level.Error(c.logger).Log("msg", "failed to parse high ratio", "err", err)
c.logger.Error("failed to parse high ratio", "err", err)
} else {
ch <- prometheus.MustNewConstMetric(
clusterSettingsDesc["highRatio"],
Expand All @@ -285,7 +284,7 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh

lowRatio, err := getValueAsRatio(merged.Cluster.Routing.Allocation.Disk.Watermark.Low)
if err != nil {
level.Error(c.logger).Log("msg", "failed to parse low ratio", "err", err)
c.logger.Error("failed to parse low ratio", "err", err)
} else {
ch <- prometheus.MustNewConstMetric(
clusterSettingsDesc["lowRatio"],
Expand Down
4 changes: 2 additions & 2 deletions collector/cluster_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/common/promslog"
)

func TestClusterSettingsStats(t *testing.T) {
Expand Down Expand Up @@ -136,7 +136,7 @@ elasticsearch_clustersettings_allocation_watermark_low_bytes 5.24288e+07
t.Fatal(err)
}

c, err := NewClusterSettings(log.NewNopLogger(), u, http.DefaultClient)
c, err := NewClusterSettings(promslog.NewNopLogger(), u, http.DefaultClient)
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 9 additions & 10 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"sync"
"time"

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

Expand All @@ -37,7 +36,7 @@ const (
defaultDisabled = false
)

type factoryFunc func(logger log.Logger, u *url.URL, hc *http.Client) (Collector, error)
type factoryFunc func(logger *slog.Logger, u *url.URL, hc *http.Client) (Collector, error)

var (
factories = make(map[string]factoryFunc)
Expand Down Expand Up @@ -90,15 +89,15 @@ func registerCollector(name string, isDefaultEnabled bool, createFunc factoryFun

type ElasticsearchCollector struct {
Collectors map[string]Collector
logger log.Logger
logger *slog.Logger
esURL *url.URL
httpClient *http.Client
}

type Option func(*ElasticsearchCollector) error

// NewElasticsearchCollector creates a new ElasticsearchCollector
func NewElasticsearchCollector(logger log.Logger, filters []string, options ...Option) (*ElasticsearchCollector, error) {
func NewElasticsearchCollector(logger *slog.Logger, filters []string, options ...Option) (*ElasticsearchCollector, error) {
e := &ElasticsearchCollector{logger: logger}
// Apply options to customize the collector
for _, o := range options {
Expand Down Expand Up @@ -128,7 +127,7 @@ func NewElasticsearchCollector(logger log.Logger, filters []string, options ...O
if collector, ok := initiatedCollectors[key]; ok {
collectors[key] = collector
} else {
collector, err := factories[key](log.With(logger, "collector", key), e.esURL, e.httpClient)
collector, err := factories[key](logger.With("collector", key), e.esURL, e.httpClient)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,21 +175,21 @@ func (e ElasticsearchCollector) Collect(ch chan<- prometheus.Metric) {
wg.Wait()
}

func execute(ctx context.Context, name string, c Collector, ch chan<- prometheus.Metric, logger log.Logger) {
func execute(ctx context.Context, name string, c Collector, ch chan<- prometheus.Metric, logger *slog.Logger) {
begin := time.Now()
err := c.Update(ctx, ch)
duration := time.Since(begin)
var success float64

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)
Expand Down
15 changes: 7 additions & 8 deletions collector/data_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"path"

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

Expand All @@ -42,15 +41,15 @@ var (

// DataStream Information Struct
type DataStream struct {
logger log.Logger
logger *slog.Logger
client *http.Client
url *url.URL

dataStreamMetrics []*dataStreamMetric
}

// NewDataStream defines DataStream Prometheus metrics
func NewDataStream(logger log.Logger, client *http.Client, url *url.URL) *DataStream {
func NewDataStream(logger *slog.Logger, client *http.Client, url *url.URL) *DataStream {
return &DataStream{
logger: logger,
client: client,
Expand Down Expand Up @@ -106,8 +105,8 @@ func (ds *DataStream) fetchAndDecodeDataStreamStats() (DataStreamStatsResponse,
defer func() {
err = res.Body.Close()
if err != nil {
level.Warn(ds.logger).Log(
"msg", "failed to close http.Client",
ds.logger.Warn(
"failed to close http.Client",
"err", err,
)
}
Expand All @@ -134,8 +133,8 @@ func (ds *DataStream) Collect(ch chan<- prometheus.Metric) {

dataStreamStatsResp, err := ds.fetchAndDecodeDataStreamStats()
if err != nil {
level.Warn(ds.logger).Log(
"msg", "failed to fetch and decode data stream stats",
ds.logger.Warn(
"failed to fetch and decode data stream stats",
"err", err,
)
return
Expand Down
Loading