Skip to content

Commit

Permalink
start slog rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
fbegyn committed Aug 29, 2023
1 parent 2ac541a commit 8e2f21c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 58 deletions.
24 changes: 11 additions & 13 deletions cmd/tc_exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"log/slog"
"net/http"
"os"

"net/http/pprof"

tcexporter "github.com/fbegyn/tc_exporter/collector"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -31,14 +31,12 @@ func main() {
kingpin.Parse()

// Start up the logger
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "version", Version, "caller", log.DefaultCaller)
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// Read the data from the config file
// currently the following options can be used in the configuration folder
// interfaces: array - array holding the dvice names
logger.Log("msg", "reading config file ...")
logger.Info("reading config file")
// Set config locations
viper.SetConfigName("config")
viper.SetConfigType("toml")
Expand All @@ -50,19 +48,19 @@ func main() {
// Read config file
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
logger.Log("level", "ERROR", "msg", "could not find the config file")
logger.Error("could not find the config file")
} else {
logger.Log("level", "ERROR", "msg", "something went wrong while reading the config", "err", err)
logger.Error("something went wrong while reading the config", "err", err)
}
}

var cf Config
cf.ListenAddres = viper.GetString("listen-address")
err := viper.Unmarshal(&cf)
if err != nil {
logger.Log("level", "ERROR", "msg", "failed to read config file", "error", err)
logger.Error("failed to read config file", "error", err)
}
logger.Log("msg", "successfully read config file")
logger.Info("successfully read config file")

// registering application information
prometheus.MustRegister(NewVersionCollector("tc_exporter"))
Expand All @@ -71,14 +69,14 @@ func main() {
for ns, sp := range cf.NetNS {
interfaces, err := getInterfaceInNS(sp.Interfaces, ns)
if err != nil {
logger.Log("msg", "failed to get interfaces from ns", "err", err, "netns", ns)
logger.Error("failed to get interfaces from ns", "err", err, "netns", ns)
}
netns[ns] = interfaces
}

collector, err := tcexporter.NewTcCollector(netns, logger)
if err != nil {
logger.Log("msg", "failed to create TC collector", "err", err)
logger.Error("msg", "failed to create TC collector", "err", err)
}
prometheus.MustRegister(collector)

Expand All @@ -88,9 +86,9 @@ func main() {
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)

// Start listening for HTTP connections.
logger.Log("msg", "starting TC exporter", "listen-address", cf.ListenAddres)
logger.Info("starting TC exporter", "listen-address", cf.ListenAddres)
if err := http.ListenAndServe(cf.ListenAddres, mux); err != nil {
logger.Log("msg", "cannot start TC exporter", "err", err)
logger.Error("msg", "cannot start TC exporter", "err", err)
}
}

Expand Down
30 changes: 15 additions & 15 deletions collector/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package tccollector

import (
"fmt"
"log/slog"

Check failure on line 5 in collector/class.go

View workflow job for this annotation

GitHub Actions / test-collector

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.19.12/x64/src/log/slog)
"os"

"github.com/florianl/go-tc"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
Expand All @@ -19,7 +19,7 @@ var (
// ClassCollector is the object that will collect Class data for the interface
// It is a basic reperesentation of the Stats and Stats2 struct of iproute
type ClassCollector struct {
logger log.Logger
logger slog.Logger
netns map[string][]rtnetlink.LinkMessage
bytes *prometheus.Desc
packets *prometheus.Desc
Expand All @@ -33,13 +33,13 @@ type ClassCollector struct {
}

// NewClassCollector create a new ClassCollector given a network interface
func NewClassCollector(netns map[string][]rtnetlink.LinkMessage, clog log.Logger) (prometheus.Collector, error) {
func NewClassCollector(netns map[string][]rtnetlink.LinkMessage, clog *slog.Logger) (prometheus.Collector, error) {
// Setup logger for the class collector
clog = log.With(clog, "collector", "class")
clog.Log("msg", "making class collector")
clog = clog.With("collector", "class")
clog.Info("making class collector")

return &ClassCollector{
logger: clog,
logger: *clog,
netns: netns,
bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "class", "bytes_total"),
Expand Down Expand Up @@ -113,7 +113,7 @@ func (cc *ClassCollector) Collect(ch chan<- prometheus.Metric) {
// First we go and get the hostname of the system, so it can later be used in the labels
host, err := os.Hostname()
if err != nil {
cc.logger.Log("msg", "failed to get hostname", "err", err)
cc.logger.Info("failed to get hostname", "err", err)
}

// start iterating over the defined namespaces and devices
Expand All @@ -124,7 +124,7 @@ func (cc *ClassCollector) Collect(ch chan<- prometheus.Metric) {
// Get all TC classes for the specified device
classes, err := getClasses(uint32(interf.Index), ns)
if err != nil {
cc.logger.Log("msg", "failed to get classes", "interface", interf.Attributes.Name, "err", err)
cc.logger.Error("failed to get classes", "interface", interf.Attributes.Name, "err", err)
}

// Range over each class and report the statisctics of the class to the channel for Prometheus
Expand Down Expand Up @@ -252,7 +252,7 @@ func (cc *ClassCollector) Collect(ch chan<- prometheus.Metric) {
// ServiceCurveCollector is the object that will collect Service Curve data for the interface. It is
// mainly used to determine the current limits imposed by the service curve
type ServiceCurveCollector struct {
logger log.Logger
logger slog.Logger
netns map[string][]rtnetlink.LinkMessage
curves map[string]*tc.ServiceCurve
Burst *prometheus.Desc
Expand All @@ -261,16 +261,16 @@ type ServiceCurveCollector struct {
}

// NewServiceCurveCollector create a new ServiceCurveCollector given a network interface
func NewServiceCurveCollector(netns map[string][]rtnetlink.LinkMessage, sclog log.Logger) (prometheus.Collector, error) {
func NewServiceCurveCollector(netns map[string][]rtnetlink.LinkMessage, sclog *slog.Logger) (prometheus.Collector, error) {
// Set up the logger for the service curve collector
sclog = log.With(sclog, "collector", "hfsc")
sclog.Log("msg", "making SC collector")
sclog = sclog.With("collector", "hfsc")
sclog.Info("making SC collector")

// We need an object to persust the different types of curves in for each HFSC class
curves := make(map[string]*tc.ServiceCurve)

return &ServiceCurveCollector{
logger: sclog,
logger: *sclog,
curves: curves,
netns: netns,
Burst: prometheus.NewDesc(
Expand Down Expand Up @@ -309,7 +309,7 @@ func (c *ServiceCurveCollector) Collect(ch chan<- prometheus.Metric) {
// First we go and get the hostname of the system, so it can later be used in the labels
host, err := os.Hostname()
if err != nil {
c.logger.Log("msg", "failed to get hostname", "err", err)
c.logger.Info("failed to get hostname", "err", err)
}

// start iterating over the defined namespaces and devices
Expand All @@ -320,7 +320,7 @@ func (c *ServiceCurveCollector) Collect(ch chan<- prometheus.Metric) {
// Get all the classes for the interface
classes, err := getClasses(uint32(interf.Index), ns)
if err != nil {
c.logger.Log("msg", "failed to get classes", "interface", interf.Attributes.Name, "err", err)
c.logger.Info("failed to get classes", "interface", interf.Attributes.Name, "err", err)
}

// Iterate over each class
Expand Down
16 changes: 7 additions & 9 deletions collector/class_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package tccollector_test

import (
"log/slog"
"os"
"testing"

tcexporter "github.com/fbegyn/tc_exporter/collector"
"github.com/florianl/go-tc"
"github.com/florianl/go-tc/core"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/mdlayher/promtest"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -70,9 +70,8 @@ func TestClassCollector(t *testing.T) {
links, _ = con.Link.List()
test[tt.ns] = links

var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "test", "collector")
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger = logger.With("test", "class")

// Create a ClassCollector with the test "config"
qc, err := tcexporter.NewClassCollector(test, logger)
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestServiceCurveCollector(t *testing.T) {
if err != nil {
t.Logf("removing interface %s from %s\n", tt.name, tt.ns)
rtnl.Link.Delete(tt.linkid)
t.Fatalf("failed to add HFSC qdisc: %v", err)
t.Fatalf("failed to create HFSC qdisc: %v", err)
}

// After the HFSC qdisc has been created, we can add the HFSC class
Expand Down Expand Up @@ -235,9 +234,8 @@ func TestServiceCurveCollector(t *testing.T) {
}

// Setup a logger for the test collector
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "test", "collector")
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger = logger.With("test", "class")

// Fetch classes and select a HFSC class
classes, err := sock.Class().Get(&tc.Msg{
Expand All @@ -257,7 +255,7 @@ func TestServiceCurveCollector(t *testing.T) {
if c.Kind == "hfsc" {
found = true
cl = c
logger.Log("msg", "found HFSC class", "class", cl.Kind)
logger.Info("found HFSC class", "class", cl.Kind)
break
}
}
Expand Down
10 changes: 5 additions & 5 deletions collector/collector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tccollector

import (
"log/slog"
"sync"

"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -12,13 +12,13 @@ const namespace = "tc"

// TcCollector is the object that will collect TC data for the interface
type TcCollector struct {
logger log.Logger
logger slog.Logger
netns map[string][]rtnetlink.LinkMessage
Collectors []prometheus.Collector
}

// NewTcCollector create a new TcCollector given a network interface
func NewTcCollector(netns map[string][]rtnetlink.LinkMessage, logger log.Logger) (prometheus.Collector, error) {
func NewTcCollector(netns map[string][]rtnetlink.LinkMessage, logger *slog.Logger) (prometheus.Collector, error) {
collectors := []prometheus.Collector{}
// Setup Qdisc collector for interface
qColl, err := NewQdiscCollector(netns, logger)
Expand All @@ -40,7 +40,7 @@ func NewTcCollector(netns map[string][]rtnetlink.LinkMessage, logger log.Logger)
collectors = append(collectors, scColl)

return &TcCollector{
logger: logger,
logger: *logger,
netns: netns,
Collectors: collectors,
}, nil
Expand All @@ -57,7 +57,7 @@ func (t TcCollector) Describe(ch chan<- *prometheus.Desc) {
func (t TcCollector) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(t.Collectors))
t.logger.Log("msg", "processing scrape")
t.logger.Info("processing scrape")
for _, coll := range t.Collectors {
go func(c prometheus.Collector) {
c.Collect(ch)
Expand Down
7 changes: 3 additions & 4 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tccollector_test

import (
"log/slog"
"os"
"testing"

tcexporter "github.com/fbegyn/tc_exporter/collector"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/mdlayher/promtest"
)
Expand Down Expand Up @@ -40,9 +40,8 @@ func TestTcCollector(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "test", "collector")
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger = logger.With("test", "collector")

test := make(map[string][]rtnetlink.LinkMessage)
con, _ := tcexporter.GetNetlinkConn("default")
Expand Down
16 changes: 8 additions & 8 deletions collector/qdisc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package tccollector

import (
"fmt"
"log/slog"
"os"

"github.com/florianl/go-tc"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -16,7 +16,7 @@ var (

// QdiscCollector is the object that will collect Qdisc data for the interface
type QdiscCollector struct {
logger log.Logger
logger slog.Logger
netns map[string][]rtnetlink.LinkMessage
bytes *prometheus.Desc
packets *prometheus.Desc
Expand All @@ -29,13 +29,13 @@ type QdiscCollector struct {
}

// NewQdiscCollector create a new QdiscCollector given a network interface
func NewQdiscCollector(netns map[string][]rtnetlink.LinkMessage, qlog log.Logger) (prometheus.Collector, error) {
func NewQdiscCollector(netns map[string][]rtnetlink.LinkMessage, qlog *slog.Logger) (prometheus.Collector, error) {
// Setup logger for qdisc collector
qlog = log.With(qlog, "collector", "qdisc")
qlog.Log("msg", "making qdisc collector")
qlog = qlog.With("collector", "qdisc")
qlog.Info("making qdisc collector")

return &QdiscCollector{
logger: qlog,
logger: *qlog,
netns: netns,
bytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "qdisc", "bytes_total"),
Expand Down Expand Up @@ -103,7 +103,7 @@ func (qc *QdiscCollector) Collect(ch chan<- prometheus.Metric) {
// fetch the host for useage later on
host, err := os.Hostname()
if err != nil {
qc.logger.Log("msg", "failed to fetch hostname", "err", err)
qc.logger.Error("failed to fetch hostname", "err", err)
}

// iterate through the netns and devices
Expand All @@ -112,7 +112,7 @@ func (qc *QdiscCollector) Collect(ch chan<- prometheus.Metric) {
// fetch all the the qdisc for this interface
qdiscs, err := getQdiscs(uint32(interf.Index), ns)
if err != nil {
qc.logger.Log("msg", "failed to get qdiscs", "interface", interf.Attributes.Name, "err", err)
qc.logger.Error("failed to get qdiscs", "interface", interf.Attributes.Name, "err", err)
}

// iterate through all the qdiscs and sent the data to the prometheus metric channel
Expand Down
7 changes: 3 additions & 4 deletions collector/qdisc_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tccollector_test

import (
"log/slog"
"os"
"testing"

tcexporter "github.com/fbegyn/tc_exporter/collector"
"github.com/go-kit/log"
"github.com/jsimonetti/rtnetlink"
"github.com/mdlayher/promtest"
)
Expand Down Expand Up @@ -57,9 +57,8 @@ func TestQdiscCollector(t *testing.T) {
links, _ = con.Link.List()
test[tt.ns] = links

var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "test", "collector")
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger = logger.With("test", "qdisc")

qc, err := tcexporter.NewQdiscCollector(test, logger)
if err != nil {
Expand Down

0 comments on commit 8e2f21c

Please sign in to comment.