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

Refactor logger #183

Merged
merged 1 commit into from
Aug 23, 2023
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
42 changes: 22 additions & 20 deletions bind_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const (
)

var (
logger = log.NewNopLogger()

up = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Was the Bind instance query successful?",
Expand Down Expand Up @@ -224,15 +222,16 @@ var (
)
)

type collectorConstructor func(*bind.Statistics) prometheus.Collector
type collectorConstructor func(log.Logger, *bind.Statistics) prometheus.Collector

type serverCollector struct {
stats *bind.Statistics
logger log.Logger
stats *bind.Statistics
}

// newServerCollector implements collectorConstructor.
func newServerCollector(s *bind.Statistics) prometheus.Collector {
return &serverCollector{stats: s}
func newServerCollector(logger log.Logger, s *bind.Statistics) prometheus.Collector {
return &serverCollector{logger: logger, stats: s}
}

// Describe implements prometheus.Collector.
Expand Down Expand Up @@ -297,12 +296,13 @@ func (c *serverCollector) Collect(ch chan<- prometheus.Metric) {
}

type viewCollector struct {
stats *bind.Statistics
logger log.Logger
stats *bind.Statistics
}

// newViewCollector implements collectorConstructor.
func newViewCollector(s *bind.Statistics) prometheus.Collector {
return &viewCollector{stats: s}
func newViewCollector(logger log.Logger, s *bind.Statistics) prometheus.Collector {
return &viewCollector{logger: logger, stats: s}
}

// Describe implements prometheus.Collector.
Expand Down Expand Up @@ -347,7 +347,7 @@ func (c *viewCollector) Collect(ch chan<- prometheus.Metric) {
resolverQueryDuration, count, math.NaN(), buckets, v.Name,
)
} else {
level.Warn(logger).Log("msg", "Error parsing RTT", "err", err)
level.Warn(c.logger).Log("msg", "Error parsing RTT", "err", err)
}
}

Expand All @@ -363,12 +363,13 @@ func (c *viewCollector) Collect(ch chan<- prometheus.Metric) {
}

type taskCollector struct {
stats *bind.Statistics
logger log.Logger
stats *bind.Statistics
}

// newTaskCollector implements collectorConstructor.
func newTaskCollector(s *bind.Statistics) prometheus.Collector {
return &taskCollector{stats: s}
func newTaskCollector(logger log.Logger, s *bind.Statistics) prometheus.Collector {
return &taskCollector{logger: logger, stats: s}
}

// Describe implements prometheus.Collector.
Expand All @@ -394,10 +395,11 @@ type Exporter struct {
client bind.Client
collectors []collectorConstructor
groups []bind.StatisticGroup
logger log.Logger
}

// NewExporter returns an initialized Exporter.
func NewExporter(version, url string, timeout time.Duration, g []bind.StatisticGroup) *Exporter {
func NewExporter(logger log.Logger, version, url string, timeout time.Duration, g []bind.StatisticGroup) *Exporter {
var c bind.Client
switch version {
case "json":
Expand All @@ -422,15 +424,15 @@ func NewExporter(version, url string, timeout time.Duration, g []bind.StatisticG
}
}

return &Exporter{client: c, collectors: cs, groups: g}
return &Exporter{logger: logger, client: c, collectors: cs, groups: g}
}

// Describe describes all the metrics ever exported by the bind exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- up
for _, c := range e.collectors {
c(&bind.Statistics{}).Describe(ch)
c(e.logger, &bind.Statistics{}).Describe(ch)
}
}

Expand All @@ -440,11 +442,11 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
status := 0.
if stats, err := e.client.Stats(e.groups...); err == nil {
for _, c := range e.collectors {
c(&stats).Collect(ch)
c(e.logger, &stats).Collect(ch)
}
status = 1
} else {
level.Error(logger).Log("msg", "Couldn't retrieve BIND stats", "err", err)
level.Error(e.logger).Log("msg", "Couldn't retrieve BIND stats", "err", err)
}
ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, status)
}
Expand Down Expand Up @@ -558,15 +560,15 @@ func main() {
kingpin.Version(version.Print(exporter))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger = promlog.New(promlogConfig)
logger := promlog.New(promlogConfig)

level.Info(logger).Log("msg", "Starting bind_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
level.Info(logger).Log("msg", "Collectors enabled", "collectors", groups.String())

prometheus.MustRegister(
version.NewCollector(exporter),
NewExporter(*bindVersion, *bindURI, *bindTimeout, groups),
NewExporter(logger, *bindVersion, *bindURI, *bindTimeout, groups),
)
if *bindPidFile != "" {
procExporter := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{
Expand Down
3 changes: 2 additions & 1 deletion bind_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/go-kit/log"
"github.com/prometheus-community/bind_exporter/bind"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
Expand Down Expand Up @@ -152,7 +153,7 @@ type bindExporterTest struct {
func (b bindExporterTest) run(t *testing.T) {
defer b.server.Close()

o, err := collect(NewExporter(b.version, b.server.URL, time.Second, b.groups))
o, err := collect(NewExporter(log.NewNopLogger(), b.version, b.server.URL, time.Second, b.groups))
if err != nil {
t.Fatal(err)
}
Expand Down