Skip to content

Commit

Permalink
Merge pull request #298 from navidys/exporter_toolkit
Browse files Browse the repository at this point in the history
Bump github.com/prometheus/exporter-toolkit to v0.13.0
  • Loading branch information
navidys authored Oct 26, 2024
2 parents 28cef03 + 0c117ea commit e114e20
Show file tree
Hide file tree
Showing 75 changed files with 3,954 additions and 2,556 deletions.
21 changes: 10 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package collector

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

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

Expand Down Expand Up @@ -37,7 +36,7 @@ const (
)

var (
factories = make(map[string]func(logger log.Logger) (Collector, error))
factories = make(map[string]func(logger *slog.Logger) (Collector, error))
initiatedCollectorsMtx = sync.Mutex{}
initiatedCollectors = make(map[string]Collector)
collectorState = make(map[string]bool)
Expand All @@ -57,11 +56,11 @@ type typedDesc struct {
// PodmanCollector implements the prometheus.Collector interface.
type PodmanCollector struct {
Collectors map[string]Collector
logger log.Logger
logger *slog.Logger
}

// NewPodmanCollector creates a new PodmanCollector.
func NewPodmanCollector(logger log.Logger) (*PodmanCollector, error) {
func NewPodmanCollector(logger *slog.Logger) (*PodmanCollector, error) {
collectors := make(map[string]Collector)

initiatedCollectorsMtx.Lock()
Expand All @@ -75,7 +74,7 @@ func NewPodmanCollector(logger log.Logger) (*PodmanCollector, error) {
if collector, ok := initiatedCollectors[key]; ok {
collectors[key] = collector
} else {
collector, err := factories[key](log.With(logger, "collector", key))
collector, err := factories[key](logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,7 +108,7 @@ func (p PodmanCollector) Collect(ch chan<- prometheus.Metric) {
wg.Wait()
}

func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.Logger) {
func execute(name string, c Collector, ch chan<- prometheus.Metric, logger *slog.Logger) {
var success float64

begin := time.Now()
Expand All @@ -118,13 +117,13 @@ func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.L

if err != nil {
if IsNoDataError(err) {
level.Debug(logger).Log("msg", "collector returned no data", "name",
logger.Debug("collector returned no data", "name",
name,
"duration_seconds",
duration.Seconds(),
"err", err)
} else {
level.Error(logger).Log("msg", "collector failed", "name",
logger.Error("collector failed", "name",
name,
"duration_seconds",
duration.Seconds(),
Expand All @@ -133,7 +132,7 @@ func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.L

success = 0
} else {
level.Debug(logger).Log("msg", "collector succeeded", "name",
logger.Debug("collector succeeded", "name",
name,
"duration_seconds",
duration.Seconds())
Expand All @@ -154,7 +153,7 @@ func (d *typedDesc) mustNewConstMetric(value float64, labels ...string) promethe
return prometheus.MustNewConstMetric(d.desc, d.valueType, value, labels...)
}

func registerCollector(collector string, enabledByDefault bool, factory func(logger log.Logger) (Collector, error)) {
func registerCollector(collector string, enabledByDefault bool, factory func(logger *slog.Logger) (Collector, error)) {
collectorState[collector] = enabledByDefault
factories[collector] = factory
}
Expand Down
7 changes: 4 additions & 3 deletions collector/container.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package collector

import (
"log/slog"

"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -31,7 +32,7 @@ type containerCollector struct {
blockOutput typedDesc
rwSize typedDesc
rootFsSize typedDesc
logger log.Logger
logger *slog.Logger
}

type containerDescLabels struct {
Expand All @@ -44,7 +45,7 @@ func init() {
}

// NewContainerStatsCollector returns a Collector exposing containers stats information.
func NewContainerStatsCollector(logger log.Logger) (Collector, error) {
func NewContainerStatsCollector(logger *slog.Logger) (Collector, error) {
return &containerCollector{
info: typedDesc{
nil, prometheus.GaugeValue,
Expand Down
7 changes: 4 additions & 3 deletions collector/image.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package collector

import (
"log/slog"

"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

type imageCollector struct {
info typedDesc
size typedDesc
created typedDesc
logger log.Logger
logger *slog.Logger
}

type imageDescLabels struct {
Expand All @@ -23,7 +24,7 @@ func init() {
}

// NewImageStatsCollector returns a Collector exposing image stats information.
func NewImageStatsCollector(logger log.Logger) (Collector, error) {
func NewImageStatsCollector(logger *slog.Logger) (Collector, error) {
return &imageCollector{
info: typedDesc{
nil, prometheus.GaugeValue,
Expand Down
7 changes: 4 additions & 3 deletions collector/network.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package collector

import (
"log/slog"

"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

type networkCollector struct {
info typedDesc
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector("network", defaultDisabled, NewNetworkStatsCollector)
}

// NewNetworkStatsCollector returns a Collector exposing network stats information.
func NewNetworkStatsCollector(logger log.Logger) (Collector, error) {
func NewNetworkStatsCollector(logger *slog.Logger) (Collector, error) {
return &networkCollector{
info: typedDesc{
prometheus.NewDesc(
Expand Down
7 changes: 4 additions & 3 deletions collector/pod.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package collector

import (
"log/slog"

"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -11,7 +12,7 @@ type podCollector struct {
state typedDesc
numOfContainers typedDesc
created typedDesc
logger log.Logger
logger *slog.Logger
}

type podDescLabels struct {
Expand All @@ -24,7 +25,7 @@ func init() {
}

// NewPodStatsCollector returns a Collector exposing pod stats information.
func NewPodStatsCollector(logger log.Logger) (Collector, error) {
func NewPodStatsCollector(logger *slog.Logger) (Collector, error) {
return &podCollector{
info: typedDesc{
nil, prometheus.GaugeValue,
Expand Down
7 changes: 4 additions & 3 deletions collector/system.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package collector

import (
"log/slog"

"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -11,15 +12,15 @@ type systemCollector struct {
runtimeVer typedDesc
conmonVer typedDesc
buildahVer typedDesc
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector("system", defaultDisabled, NewSystemCollector)
}

// NewSystemCollector returns a Collector exposing podman system information.
func NewSystemCollector(logger log.Logger) (Collector, error) {
func NewSystemCollector(logger *slog.Logger) (Collector, error) {
return &systemCollector{
podmanVer: typedDesc{
prometheus.NewDesc(
Expand Down
7 changes: 4 additions & 3 deletions collector/volume.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package collector

import (
"log/slog"

"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

type volumeCollector struct {
info typedDesc
created typedDesc
logger log.Logger
logger *slog.Logger
}

var volumeDefaultLAbels = []string{"name", "driver", "mount_point"}
Expand All @@ -19,7 +20,7 @@ func init() {
}

// NewVolumeStatsCollector returns a Collector exposing volume stats information.
func NewVolumeStatsCollector(logger log.Logger) (Collector, error) {
func NewVolumeStatsCollector(logger *slog.Logger) (Collector, error) {
createdLabels := []string{"name"}

if enhanceAllMetrics {
Expand Down
15 changes: 7 additions & 8 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (

"github.com/containers/prometheus-podman-exporter/collector"
"github.com/containers/prometheus-podman-exporter/pdcs"
"github.com/go-kit/log/level"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,7 +41,7 @@ type exporterOptions struct {
func Start(cmd *cobra.Command, _ []string) error {
// setup exporter
logLevel := "info"
promlogConfig := &promlog.Config{Level: &promlog.AllowedLevel{}}
promlogConfig := &promslog.Config{Level: &promslog.AllowedLevel{}}

cmdOptions, err := parseOptions(cmd)
if err != nil {
Expand All @@ -57,16 +56,16 @@ func Start(cmd *cobra.Command, _ []string) error {
return err
}

logger := promlog.New(promlogConfig)
logger := promslog.New(promlogConfig)

if err := setEnabledCollectors(cmdOptions); err != nil {
level.Error(logger).Log("msg", "cannot set enabled collectors", "err", err)
logger.Error("cannot set enabled collectors", "err", err)

return err
}

level.Info(logger).Log("msg", "Starting podman-prometheus-exporter", "version", version.Info())
level.Info(logger).Log("msg", "metrics", "enhanced", cmdOptions.enhanceMetrics)
logger.Info("starting podman-prometheus-exporter", "version", version.Info())
logger.Info("metrics", "enhanced", cmdOptions.enhanceMetrics)

http.Handle(
cmdOptions.webTelemetryPath,
Expand All @@ -93,7 +92,7 @@ func Start(cmd *cobra.Command, _ []string) error {
pdcs.StartEventStreamer(logger, updateImages)
pdcs.StartCacheSizeTicker(logger, cmdOptions.cacheDuration)

level.Info(logger).Log("msg", "Listening on", "address", cmdOptions.webListen)
logger.Info("Listening on", "address", cmdOptions.webListen)

server := &http.Server{
ReadHeaderTimeout: 3 * time.Second, //nolint:mnd
Expand Down
18 changes: 8 additions & 10 deletions exporter/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package exporter

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

"github.com/containers/prometheus-podman-exporter/collector"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
promcollectors "github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/collectors/version"
Expand All @@ -25,13 +23,13 @@ type handler struct {
disableExporterMetrics bool
exporterMetricsRegistry *prometheus.Registry
maxRequests int
logger log.Logger
logger *slog.Logger
}

// ServeHTTP implements http.Handler.
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
filters := r.URL.Query()["collect[]"]
level.Debug(h.logger).Log("msg", "collect query:", "filters", fmt.Sprintf("%v", filters))
h.logger.Debug("collect query:", "filters", fmt.Sprintf("%v", filters))

if len(filters) == 0 {
// No filters, use the prepared unfiltered handler.
Expand All @@ -42,7 +40,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// To serve filtered metrics, we create a filtering handler on the fly.
filteredHandler, err := h.innerHandler(filters...)
if err != nil {
level.Warn(h.logger).Log("msg", "couldn't create filtered metrics handler:", "err", err)
h.logger.Warn("couldn't create filtered metrics handler:", "err", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("couldn't create filtered metrics handler: %s", err)))

Expand All @@ -52,7 +50,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
filteredHandler.ServeHTTP(w, r)
}

func newHandler(disableExporterMetrics bool, maxRequests int, logger log.Logger) *handler {
func newHandler(disableExporterMetrics bool, maxRequests int, logger *slog.Logger) *handler {
h := &handler{
exporterMetricsRegistry: prometheus.NewRegistry(),
maxRequests: maxRequests,
Expand Down Expand Up @@ -91,7 +89,7 @@ func (h *handler) innerHandler(filters ...string) (http.Handler, error) {
// Only log the creation of an unfiltered handler, which should happen
// only once upon startup.
if len(filters) == 0 {
level.Info(h.logger).Log("msg", "enabled collectors")
h.logger.Info("enabled collectors")

collectors := []string{}

Expand All @@ -102,7 +100,7 @@ func (h *handler) innerHandler(filters ...string) (http.Handler, error) {
sort.Strings(collectors)

for _, c := range collectors {
level.Info(h.logger).Log("collector", c)
h.logger.Info("collector", "name", c)
}
}

Expand All @@ -116,7 +114,7 @@ func (h *handler) innerHandler(filters ...string) (http.Handler, error) {
handler := promhttp.HandlerFor(
prometheus.Gatherers{h.exporterMetricsRegistry, r},
promhttp.HandlerOpts{
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0),
ErrorLog: slog.NewLogLogger(h.logger.Handler(), slog.LevelError),
ErrorHandling: promhttp.ContinueOnError,
MaxRequestsInFlight: h.maxRequests,
Registry: h.exporterMetricsRegistry,
Expand Down
Loading

0 comments on commit e114e20

Please sign in to comment.