Skip to content

Commit

Permalink
switch to go-kit/log (#1575)
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <[email protected]>
  • Loading branch information
yeya24 authored and SuperQ committed Dec 31, 2019
1 parent a80b7d0 commit 2477c5c
Show file tree
Hide file tree
Showing 158 changed files with 3,434 additions and 4,636 deletions.
5 changes: 4 additions & 1 deletion collector/arp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@ import (
"os"
"strings"

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

type arpCollector struct {
entries *prometheus.Desc
logger log.Logger
}

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

// NewARPCollector returns a new Collector exposing ARP stats.
func NewARPCollector() (Collector, error) {
func NewARPCollector(logger log.Logger) (Collector, error) {
return &arpCollector{
entries: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "arp", "entries"),
"ARP entries by device",
[]string{"device"}, nil,
),
logger: logger,
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions collector/bcache_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package collector
import (
"fmt"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs/bcache"
)
Expand All @@ -28,19 +29,21 @@ func init() {

// A bcacheCollector is a Collector which gathers metrics from Linux bcache.
type bcacheCollector struct {
fs bcache.FS
fs bcache.FS
logger log.Logger
}

// NewBcacheCollector returns a newly allocated bcacheCollector.
// It exposes a number of Linux bcache statistics.
func NewBcacheCollector() (Collector, error) {
func NewBcacheCollector(logger log.Logger) (Collector, error) {
fs, err := bcache.NewFS(*sysPath)
if err != nil {
return nil, fmt.Errorf("failed to open sysfs: %w", err)
}

return &bcacheCollector{
fs: fs,
fs: fs,
logger: logger,
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions collector/bonding_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"path/filepath"
"strings"

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

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

func init() {
Expand All @@ -36,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() (Collector, error) {
func NewBondingCollector(logger log.Logger) (Collector, error) {
return &bondingCollector{
slaves: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, "bonding", "slaves"),
Expand All @@ -48,6 +50,7 @@ func NewBondingCollector() (Collector, error) {
"Number of active slaves per bonding interface.",
[]string{"master"}, nil,
), prometheus.GaugeValue},
logger: logger,
}, nil
}

Expand All @@ -57,7 +60,7 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {
bondingStats, err := readBondingStats(statusfile)
if err != nil {
if os.IsNotExist(err) {
log.Debugf("Not collecting bonding, file does not exist: %s", statusfile)
level.Debug(c.logger).Log("msg", "Not collecting bonding, file does not exist", "file", statusfile)
return nil
}
return err
Expand Down
9 changes: 7 additions & 2 deletions collector/boot_time_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@
package collector

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

type bootTimeCollector struct{ boottime bsdSysctl }
type bootTimeCollector struct {
boottime bsdSysctl
logger log.Logger
}

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

// newBootTimeCollector returns a new Collector exposing system boot time on BSD systems.
func newBootTimeCollector() (Collector, error) {
func newBootTimeCollector(logger log.Logger) (Collector, error) {
return &bootTimeCollector{
boottime: bsdSysctl{
name: "boot_time_seconds",
description: "Unix time of last boot, including microseconds.",
mib: "kern.boottime",
dataType: bsdSysctlTypeStructTimeval,
},
logger: logger,
}, nil
}

Expand Down
5 changes: 4 additions & 1 deletion collector/boot_time_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@
package collector

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

type bootTimeCollector struct {
boottime typedDesc
logger log.Logger
}

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

func newBootTimeCollector() (Collector, error) {
func newBootTimeCollector(logger log.Logger) (Collector, error) {
return &bootTimeCollector{
boottime: typedDesc{
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "boot_time_seconds"),
"Unix time of last boot, including microseconds.",
nil, nil,
), prometheus.GaugeValue},
logger: logger,
}, nil
}

Expand Down
14 changes: 8 additions & 6 deletions collector/buddyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"fmt"
"strconv"

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

Expand All @@ -30,16 +31,17 @@ const (
)

type buddyinfoCollector struct {
fs procfs.FS
desc *prometheus.Desc
fs procfs.FS
desc *prometheus.Desc
logger log.Logger
}

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

// NewBuddyinfoCollector returns a new Collector exposing buddyinfo stats.
func NewBuddyinfoCollector() (Collector, error) {
func NewBuddyinfoCollector(logger log.Logger) (Collector, error) {
desc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, buddyInfoSubsystem, "blocks"),
"Count of free blocks according to size.",
Expand All @@ -49,7 +51,7 @@ func NewBuddyinfoCollector() (Collector, error) {
if err != nil {
return nil, fmt.Errorf("failed to open procfs: %w", err)
}
return &buddyinfoCollector{fs, desc}, nil
return &buddyinfoCollector{fs, desc, logger}, nil
}

// Update calls (*buddyinfoCollector).getBuddyInfo to get the platform specific
Expand All @@ -60,7 +62,7 @@ func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) error {
return fmt.Errorf("couldn't get buddyinfo: %s", err)
}

log.Debugf("Set node_buddy: %#v", buddyInfo)
level.Debug(c.logger).Log("msg", "Set node_buddy", "buddyInfo", buddyInfo)
for _, entry := range buddyInfo {
for size, value := range entry.Sizes {
ch <- prometheus.MustNewConstMetric(
Expand Down
22 changes: 12 additions & 10 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"sync"
"time"

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

Expand Down Expand Up @@ -48,11 +49,11 @@ const (
)

var (
factories = make(map[string]func() (Collector, error))
factories = make(map[string]func(logger log.Logger) (Collector, error))
collectorState = make(map[string]*bool)
)

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

// NewNodeCollector creates a new NodeCollector.
func NewNodeCollector(filters ...string) (*NodeCollector, error) {
func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, error) {
f := make(map[string]bool)
for _, filter := range filters {
enabled, exist := collectorState[filter]
Expand All @@ -91,7 +93,7 @@ func NewNodeCollector(filters ...string) (*NodeCollector, error) {
collectors := make(map[string]Collector)
for key, enabled := range collectorState {
if *enabled {
collector, err := factories[key]()
collector, err := factories[key](log.With(logger, "collector", key))
if err != nil {
return nil, err
}
Expand All @@ -100,7 +102,7 @@ func NewNodeCollector(filters ...string) (*NodeCollector, error) {
}
}
}
return &NodeCollector{Collectors: collectors}, nil
return &NodeCollector{Collectors: collectors, logger: logger}, nil
}

// Describe implements the prometheus.Collector interface.
Expand All @@ -115,24 +117,24 @@ func (n NodeCollector) Collect(ch chan<- prometheus.Metric) {
wg.Add(len(n.Collectors))
for name, c := range n.Collectors {
go func(name string, c Collector) {
execute(name, c, ch)
execute(name, c, ch, n.logger)
wg.Done()
}(name, c)
}
wg.Wait()
}

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

if err != nil {
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)
level.Error(logger).Log("msg", "collector failed", "name", name, "duration_seconds", duration.Seconds(), "err", err)
success = 0
} else {
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
level.Debug(logger).Log("msg", "collector succeeded", "name", name, "duration_seconds", duration.Seconds())
success = 1
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
Expand Down
5 changes: 4 additions & 1 deletion collector/conntrack_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
package collector

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

type conntrackCollector struct {
current *prometheus.Desc
limit *prometheus.Desc
logger log.Logger
}

func init() {
registerCollector("conntrack", defaultEnabled, NewConntrackCollector)
}

// NewConntrackCollector returns a new Collector exposing conntrack stats.
func NewConntrackCollector() (Collector, error) {
func NewConntrackCollector(logger log.Logger) (Collector, error) {
return &conntrackCollector{
current: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "nf_conntrack_entries"),
Expand All @@ -41,6 +43,7 @@ func NewConntrackCollector() (Collector, error) {
"Maximum size of connection tracking table.",
nil, nil,
),
logger: logger,
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions collector/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strconv"
"unsafe"

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

Expand All @@ -49,17 +50,19 @@ import "C"
const ClocksPerSec = float64(C.CLK_TCK)

type statCollector struct {
cpu *prometheus.Desc
cpu *prometheus.Desc
logger log.Logger
}

func init() {
registerCollector("cpu", defaultEnabled, NewCPUCollector)
}

// NewCPUCollector returns a new Collector exposing CPU stats.
func NewCPUCollector() (Collector, error) {
func NewCPUCollector(logger log.Logger) (Collector, error) {
return &statCollector{
cpu: nodeCPUSecondsDesc,
cpu: nodeCPUSecondsDesc,
logger: logger,
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions collector/cpu_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"unsafe"

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

Expand Down Expand Up @@ -75,17 +76,19 @@ import "C"
const maxCPUTimesLen = C.MAXCPU * C.CPUSTATES

type statCollector struct {
cpu *prometheus.Desc
cpu *prometheus.Desc
logger log.Logger
}

func init() {
registerCollector("cpu", defaultEnabled, NewStatCollector)
}

// NewStatCollector returns a new Collector exposing CPU stats.
func NewStatCollector() (Collector, error) {
func NewStatCollector(logger log.Logger) (Collector, error) {
return &statCollector{
cpu: nodeCPUSecondsDesc,
cpu: nodeCPUSecondsDesc,
logger: logger,
}, nil
}

Expand Down
Loading

0 comments on commit 2477c5c

Please sign in to comment.