Skip to content
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

minor(deps): update module github.com/prometheus/exporter-toolkit to v0.13.0 #332

Merged
merged 2 commits into from
Sep 14, 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endif

GOBUILD ?= CGO_ENABLED=0 go build
PACKAGES ?= $(shell go list ./...)
SOURCES ?= $(shell find . -name "*.go" -type f)
SOURCES ?= $(shell find . -name "*.go" -type f -not -path ./.devenv/\* -not -path ./.direnv/\*)
GENERATE ?= $(PACKAGES)

TAGS ?= netgo
Expand Down
7 changes: 7 additions & 0 deletions changelog/unreleased/logging-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Change: Switch to official logging library

Since there have been a structured logger part of the Go standard library we
thought it's time to replace the library with that. Be aware that log messages
should change a little bit.

https://github.com/promhippie/prometheus-hcloud-sd/issues/334
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/oklog/run v1.1.0
github.com/prometheus/client_golang v1.20.3
github.com/prometheus/common v0.59.1
github.com/prometheus/exporter-toolkit v0.12.0
github.com/prometheus/exporter-toolkit v0.13.0
github.com/prometheus/prometheus v1.8.2-0.20210220213500-8c8de46003d1
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ github.com/prometheus/common v0.17.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
github.com/prometheus/exporter-toolkit v0.5.1/go.mod h1:OCkM4805mmisBhLmVFw858QYi3v0wKdY6/UxrT0pZVg=
github.com/prometheus/exporter-toolkit v0.12.0 h1:DkE5RcEZR3lQA2QD5JLVQIf41dFKNsVMXFhgqcif7fo=
github.com/prometheus/exporter-toolkit v0.12.0/go.mod h1:fQH0KtTn0yrrS0S82kqppRjDDiwMfIQUwT+RBRRhwUc=
github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c=
github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
Expand Down
20 changes: 7 additions & 13 deletions pkg/action/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package action
import (
"context"
"fmt"
"log/slog"
"strconv"
"strings"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"
Expand Down Expand Up @@ -53,7 +52,7 @@ var (
// Discoverer implements the Prometheus discoverer interface.
type Discoverer struct {
clients map[string]*hcloud.Client
logger log.Logger
logger *slog.Logger
refresh int
separator string
lasts map[string]struct{}
Expand Down Expand Up @@ -90,8 +89,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
networks, err := client.Network.All(ctx)

if err != nil {
level.Warn(d.logger).Log(
"msg", "Failed to fetch networks",
d.logger.Warn("Failed to fetch networks",
"project", project,
"err", err,
)
Expand All @@ -103,8 +101,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
servers, err := client.Server.All(ctx)

if err != nil {
level.Warn(d.logger).Log(
"msg", "Failed to fetch servers",
d.logger.Warn("Failed to fetch servers",
"project", project,
"err", err,
)
Expand All @@ -115,8 +112,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro

requestDuration.WithLabelValues(project).Observe(time.Since(now).Seconds())

level.Debug(d.logger).Log(
"msg", "Requested servers",
d.logger.Debug("Requested servers",
"project", project,
"count", len(servers),
)
Expand Down Expand Up @@ -180,8 +176,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
}
}

level.Debug(d.logger).Log(
"msg", "Server added",
d.logger.Debug("Server added",
"project", project,
"source", target.Source,
)
Expand All @@ -194,8 +189,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro

for k := range d.lasts {
if _, ok := current[k]; !ok {
level.Debug(d.logger).Log(
"msg", "Server deleted",
d.logger.Debug("Server deleted",
"source", k,
)

Expand Down
9 changes: 3 additions & 6 deletions pkg/action/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package action

import (
"fmt"
"log/slog"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/promhippie/prometheus-hcloud-sd/pkg/version"
Expand Down Expand Up @@ -49,11 +48,9 @@ func init() {
}

type promLogger struct {
logger log.Logger
logger *slog.Logger
}

func (pl promLogger) Println(v ...interface{}) {
level.Error(pl.logger).Log(
"msg", fmt.Sprintln(v...),
)
pl.logger.Error(fmt.Sprintln(v...))
}
27 changes: 10 additions & 17 deletions pkg/action/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -23,9 +22,8 @@ import (
)

// Server handles the server sub-command.
func Server(cfg *config.Config, logger log.Logger) error {
level.Info(logger).Log(
"msg", "Launching Prometheus HetznerCloud SD",
func Server(cfg *config.Config, logger *slog.Logger) error {
logger.Info("Launching Prometheus HetznerCloud SD",
"version", version.String,
"revision", version.Revision,
"date", version.Date,
Expand All @@ -43,8 +41,7 @@ func Server(cfg *config.Config, logger log.Logger) error {
token, err := config.Value(credential.Token)

if err != nil {
level.Error(logger).Log(
"msg", "Failed to read token secret",
logger.Error("Failed to read token secret",
"project", credential.Project,
"err", err,
)
Expand Down Expand Up @@ -80,9 +77,8 @@ func Server(cfg *config.Config, logger log.Logger) error {
}

gr.Add(func() error {
level.Info(logger).Log(
"msg", "Starting metrics server",
"addr", cfg.Server.Addr,
logger.Info("Starting metrics server",
"address", cfg.Server.Addr,
)

return web.ListenAndServe(
Expand All @@ -99,16 +95,14 @@ func Server(cfg *config.Config, logger log.Logger) error {
defer cancel()

if err := server.Shutdown(ctx); err != nil {
level.Error(logger).Log(
"msg", "Failed to shutdown metrics gracefully",
logger.Error("Failed to shutdown metrics gracefully",
"err", err,
)

return
}

level.Info(logger).Log(
"msg", "Metrics shutdown gracefully",
logger.Info("Metrics shutdown gracefully",
"reason", reason,
)
})
Expand All @@ -131,7 +125,7 @@ func Server(cfg *config.Config, logger log.Logger) error {
return gr.Run()
}

func handler(cfg *config.Config, logger log.Logger) *chi.Mux {
func handler(cfg *config.Config, logger *slog.Logger) *chi.Mux {
mux := chi.NewRouter()
mux.Use(middleware.Recoverer(logger))
mux.Use(middleware.RealIP)
Expand Down Expand Up @@ -171,8 +165,7 @@ func handler(cfg *config.Config, logger log.Logger) *chi.Mux {
content, err := os.ReadFile(cfg.Target.File)

if err != nil {
level.Info(logger).Log(
"msg", "Failed to read service discovery data",
logger.Error("Failed to read service discovery data",
"err", err,
)

Expand Down
11 changes: 5 additions & 6 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"reflect"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/targetgroup"
)
Expand All @@ -43,7 +42,7 @@ type Adapter struct {
manager *discovery.Manager
output string
name string
logger log.Logger
logger *slog.Logger
}

func mapToArray(m map[string]*customSD) []customSD {
Expand Down Expand Up @@ -84,7 +83,7 @@ func (a *Adapter) generateTargetGroups(allTargetGroups map[string][]*targetgroup
a.groups = tempGroups
err := a.writeOutput()
if err != nil {
level.Error(log.With(a.logger, "component", "sd-adapter")).Log("err", err)
a.logger.With("component", "sd-adapter").Error("", "err", err)
}
}

Expand Down Expand Up @@ -138,12 +137,12 @@ func (a *Adapter) Run() {
}

// NewAdapter creates a new instance of Adapter.
func NewAdapter(ctx context.Context, file string, name string, d discovery.Discoverer, logger log.Logger) *Adapter {
func NewAdapter(ctx context.Context, file string, name string, d discovery.Discoverer, logger *slog.Logger) *Adapter {
return &Adapter{
ctx: ctx,
disc: d,
groups: make(map[string]*customSD),
manager: discovery.NewManager(ctx, logger),
manager: discovery.NewManager(ctx, nil),
output: file,
name: name,
logger: logger,
Expand Down
14 changes: 7 additions & 7 deletions pkg/command/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/http"

"github.com/go-kit/log/level"
"github.com/promhippie/prometheus-hcloud-sd/pkg/config"
"github.com/urfave/cli/v2"
)
Expand All @@ -20,8 +19,7 @@ func Health(cfg *config.Config) *cli.Command {

if c.IsSet("hcloud.config") {
if err := readConfig(c.String("hcloud.config"), cfg); err != nil {
level.Error(logger).Log(
"msg", "Failed to read config",
logger.Error("Failed to read config",
"err", err,
)

Expand All @@ -37,8 +35,7 @@ func Health(cfg *config.Config) *cli.Command {
)

if err != nil {
level.Error(logger).Log(
"msg", "Failed to request health check",
logger.Error("Failed to request health check",
"err", err,
)

Expand All @@ -48,15 +45,18 @@ func Health(cfg *config.Config) *cli.Command {
defer resp.Body.Close()

if resp.StatusCode != 200 {
level.Error(logger).Log(
"msg", "Health check seems to be in bad state",
logger.Error("Health check seems to be in bad state",
"err", err,
"code", resp.StatusCode,
)

return err
}

logger.Debug("Health check seems to be fine",
"code", resp.StatusCode,
)

return nil
},
}
Expand Down
19 changes: 4 additions & 15 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package command
import (
"errors"

"github.com/go-kit/log/level"
"github.com/promhippie/prometheus-hcloud-sd/pkg/action"
"github.com/promhippie/prometheus-hcloud-sd/pkg/config"
"github.com/urfave/cli/v2"
Expand All @@ -20,8 +19,7 @@ func Server(cfg *config.Config) *cli.Command {

if c.IsSet("hcloud.config") {
if err := readConfig(c.String("hcloud.config"), cfg); err != nil {
level.Error(logger).Log(
"msg", "Failed to read config",
logger.Error("Failed to read config",
"err", err,
)

Expand All @@ -30,10 +28,7 @@ func Server(cfg *config.Config) *cli.Command {
}

if cfg.Target.File == "" {
level.Error(logger).Log(
"msg", "Missing path for output.file",
)

logger.Error("Missing path for output.file")
return errors.New("missing path for output.file")
}

Expand All @@ -49,19 +44,13 @@ func Server(cfg *config.Config) *cli.Command {
)

if credentials.Token == "" {
level.Error(logger).Log(
"msg", "Missing required hcloud.token",
)

logger.Error("Missing required hcloud.token")
return errors.New("missing required hcloud.token")
}
}

if len(cfg.Target.Credentials) == 0 {
level.Error(logger).Log(
"msg", "Missing any credentials",
)

logger.Error("Missing any credentials")
return errors.New("missing any credentials")
}

Expand Down
Loading
Loading