Skip to content

Commit

Permalink
Update logging (prometheus#3097)
Browse files Browse the repository at this point in the history
Switch from promlog/go-kit to promslog/slog for logging.
* Update Go build to 1.23.

Signed-off-by: Ben Kochie <superq@gmail.com>
Signed-off-by: Vitaly Zhuravlev <v-zhuravlev@users.noreply.github.com>
  • Loading branch information
SuperQ authored and v-zhuravlev committed Nov 1, 2024
1 parent fbb01de commit 881d882
Show file tree
Hide file tree
Showing 133 changed files with 601 additions and 671 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ linters-settings:
exclude-functions:
# Used in HTTP handlers, any error is handled by the server itself.
- (net/http.ResponseWriter).Write
# Never check for logger errors.
- (github.com/go-kit/log.Logger).Log
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
Expand Down
6 changes: 3 additions & 3 deletions collector/arp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package collector
import (
"errors"
"fmt"
"log/slog"
"net"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
Expand All @@ -39,15 +39,15 @@ type arpCollector struct {
fs procfs.FS
deviceFilter deviceFilter
entries *prometheus.Desc
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector("arp", defaultEnabled, NewARPCollector)
}

// NewARPCollector returns a new Collector exposing ARP stats.
func NewARPCollector(logger log.Logger) (Collector, error) {
func NewARPCollector(logger *slog.Logger) (Collector, error) {
fs, err := procfs.NewFS(*procPath)
if err != nil {
return nil, fmt.Errorf("failed to open procfs: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions collector/bcache_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package collector

import (
"fmt"
"log/slog"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs/bcache"
)
Expand All @@ -36,12 +36,12 @@ func init() {
// A bcacheCollector is a Collector which gathers metrics from Linux bcache.
type bcacheCollector struct {
fs bcache.FS
logger log.Logger
logger *slog.Logger
}

// NewBcacheCollector returns a newly allocated bcacheCollector.
// It exposes a number of Linux bcache statistics.
func NewBcacheCollector(logger log.Logger) (Collector, error) {
func NewBcacheCollector(logger *slog.Logger) (Collector, error) {
fs, err := bcache.NewFS(*sysPath)
if err != nil {
return nil, fmt.Errorf("failed to open sysfs: %w", err)
Expand Down
9 changes: 4 additions & 5 deletions collector/bonding_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package collector
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"

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

type bondingCollector struct {
slaves, active typedDesc
logger log.Logger
logger *slog.Logger
}

func init() {
Expand All @@ -39,7 +38,7 @@ func init() {

// NewBondingCollector returns a newly allocated bondingCollector.
// It exposes the number of configured and active slave of linux bonding interfaces.
func NewBondingCollector(logger log.Logger) (Collector, error) {
func NewBondingCollector(logger *slog.Logger) (Collector, error) {
return &bondingCollector{
slaves: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, "bonding", "slaves"),
Expand All @@ -61,7 +60,7 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {
bondingStats, err := readBondingStats(statusfile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "Not collecting bonding, file does not exist", "file", statusfile)
c.logger.Debug("Not collecting bonding, file does not exist", "file", statusfile)
return ErrNoData
}
return err
Expand Down
6 changes: 3 additions & 3 deletions collector/boot_time_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
package collector

import (
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
"log/slog"
)

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

func init() {
registerCollector("boottime", defaultEnabled, newBootTimeCollector)
}

// newBootTimeCollector returns a new Collector exposing system boot time on BSD systems.
func newBootTimeCollector(logger log.Logger) (Collector, error) {
func newBootTimeCollector(logger *slog.Logger) (Collector, error) {
return &bootTimeCollector{
logger: logger,
}, nil
Expand Down
6 changes: 3 additions & 3 deletions collector/boot_time_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
package collector

import (
"github.com/go-kit/log"
"github.com/illumos/go-kstat"
"github.com/prometheus/client_golang/prometheus"
"log/slog"
)

type bootTimeCollector struct {
boottime typedDesc
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector("boottime", defaultEnabled, newBootTimeCollector)
}

func newBootTimeCollector(logger log.Logger) (Collector, error) {
func newBootTimeCollector(logger *slog.Logger) (Collector, error) {
return &bootTimeCollector{
boottime: typedDesc{
prometheus.NewDesc(
Expand Down
23 changes: 11 additions & 12 deletions collector/btrfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@ package collector

import (
"fmt"
"log/slog"
"path"
"strings"
"syscall"

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

// A btrfsCollector is a Collector which gathers metrics from Btrfs filesystems.
type btrfsCollector struct {
fs btrfs.FS
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector("btrfs", defaultEnabled, NewBtrfsCollector)
}

// NewBtrfsCollector returns a new Collector exposing Btrfs statistics.
func NewBtrfsCollector(logger log.Logger) (Collector, error) {
func NewBtrfsCollector(logger *slog.Logger) (Collector, error) {
fs, err := btrfs.NewFS(*sysPath)
if err != nil {
return nil, fmt.Errorf("failed to open sysfs: %w", err)
Expand All @@ -62,8 +61,8 @@ func (c *btrfsCollector) Update(ch chan<- prometheus.Metric) error {

ioctlStatsMap, err := c.getIoctlStats()
if err != nil {
level.Debug(c.logger).Log(
"msg", "Error querying btrfs device stats with ioctl",
c.logger.Debug(
"Error querying btrfs device stats with ioctl",
"err", err)
ioctlStatsMap = make(map[string]*btrfsIoctlFsStats)
}
Expand Down Expand Up @@ -129,8 +128,8 @@ func (c *btrfsCollector) getIoctlStats() (map[string]*btrfsIoctlFsStats, error)
if err != nil {
// Failed to open this mount point, maybe we didn't have permission
// maybe we'll find another mount point for this FS later.
level.Debug(c.logger).Log(
"msg", "Error inspecting btrfs mountpoint",
c.logger.Debug(
"Error inspecting btrfs mountpoint",
"mountPoint", mountPath,
"err", err)
continue
Expand All @@ -141,8 +140,8 @@ func (c *btrfsCollector) getIoctlStats() (map[string]*btrfsIoctlFsStats, error)
if err != nil {
// Failed to get the FS info for some reason,
// perhaps it'll work with a different mount point
level.Debug(c.logger).Log(
"msg", "Error querying btrfs filesystem",
c.logger.Debug(
"Error querying btrfs filesystem",
"mountPoint", mountPath,
"err", err)
continue
Expand All @@ -156,8 +155,8 @@ func (c *btrfsCollector) getIoctlStats() (map[string]*btrfsIoctlFsStats, error)

deviceStats, err := c.getIoctlDeviceStats(fs, &fsInfo)
if err != nil {
level.Debug(c.logger).Log(
"msg", "Error querying btrfs device stats",
c.logger.Debug(
"Error querying btrfs device stats",
"mountPoint", mountPath,
"err", err)
continue
Expand Down
9 changes: 4 additions & 5 deletions collector/buddyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package collector

import (
"fmt"
"log/slog"
"strconv"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
)
Expand All @@ -33,15 +32,15 @@ const (
type buddyinfoCollector struct {
fs procfs.FS
desc *prometheus.Desc
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector("buddyinfo", defaultDisabled, NewBuddyinfoCollector)
}

// NewBuddyinfoCollector returns a new Collector exposing buddyinfo stats.
func NewBuddyinfoCollector(logger log.Logger) (Collector, error) {
func NewBuddyinfoCollector(logger *slog.Logger) (Collector, error) {
desc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, buddyInfoSubsystem, "blocks"),
"Count of free blocks according to size.",
Expand All @@ -62,7 +61,7 @@ func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) error {
return fmt.Errorf("couldn't get buddyinfo: %w", err)
}

level.Debug(c.logger).Log("msg", "Set node_buddy", "buddyInfo", buddyInfo)
c.logger.Debug("Set node_buddy", "buddyInfo", buddyInfo)
for _, entry := range buddyInfo {
for size, value := range entry.Sizes {
ch <- prometheus.MustNewConstMetric(
Expand Down
6 changes: 3 additions & 3 deletions collector/cgroups_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package collector

import (
"fmt"
"log/slog"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
)
Expand All @@ -30,15 +30,15 @@ type cgroupSummaryCollector struct {
fs procfs.FS
cgroups *prometheus.Desc
enabled *prometheus.Desc
logger log.Logger
logger *slog.Logger
}

func init() {
registerCollector(cgroupsCollectorSubsystem, defaultDisabled, NewCgroupSummaryCollector)
}

// NewCgroupSummaryCollector returns a new Collector exposing a summary of cgroups.
func NewCgroupSummaryCollector(logger log.Logger) (Collector, error) {
func NewCgroupSummaryCollector(logger *slog.Logger) (Collector, error) {
fs, err := procfs.NewFS(*procPath)
if err != nil {
return nil, fmt.Errorf("failed to open procfs: %w", err)
Expand Down
21 changes: 10 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ package collector
import (
"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"
)

Expand Down Expand Up @@ -50,14 +49,14 @@ 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)
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
)

func registerCollector(collector string, isDefaultEnabled bool, factory func(logger log.Logger) (Collector, error)) {
func registerCollector(collector string, isDefaultEnabled bool, factory func(logger *slog.Logger) (Collector, error)) {
var helpDefaultState string
if isDefaultEnabled {
helpDefaultState = "enabled"
Expand All @@ -78,7 +77,7 @@ func registerCollector(collector string, isDefaultEnabled bool, factory func(log
// NodeCollector implements the prometheus.Collector interface.
type NodeCollector struct {
Collectors map[string]Collector
logger log.Logger
logger *slog.Logger
}

// DisableDefaultCollectors sets the collector state to false for all collectors which
Expand All @@ -104,7 +103,7 @@ func collectorFlagAction(collector string) func(ctx *kingpin.ParseContext) error
}

// NewNodeCollector creates a new NodeCollector.
func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, error) {
func NewNodeCollector(logger *slog.Logger, filters ...string) (*NodeCollector, error) {
f := make(map[string]bool)
for _, filter := range filters {
enabled, exist := collectorState[filter]
Expand All @@ -126,7 +125,7 @@ func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, err
if collector, ok := initiatedCollectors[key]; ok {
collectors[key] = collector
} else {
collector, err := factories[key](log.With(logger, "collector", key))
collector, err := factories[key](logger.With("collector", key))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,21 +155,21 @@ func (n NodeCollector) 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) {
begin := time.Now()
err := c.Update(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
Loading

0 comments on commit 881d882

Please sign in to comment.