Skip to content

Commit

Permalink
Refactor logger
Browse files Browse the repository at this point in the history
Remove global logger var in favor of passing logger from main() to the
Exporter struct.

Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ committed Aug 23, 2023
1 parent 5195bb3 commit 05f6211
Showing 1 changed file with 22 additions and 20 deletions.
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

0 comments on commit 05f6211

Please sign in to comment.