Skip to content

Commit

Permalink
chore: pass context aware logger to collectors (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored Aug 24, 2024
1 parent fbead0f commit 89c832f
Show file tree
Hide file tree
Showing 65 changed files with 876 additions and 1,164 deletions.
10 changes: 5 additions & 5 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/pprof"
Expand Down Expand Up @@ -202,15 +203,14 @@ func main() {

enabledCollectorList := utils.ExpandEnabledCollectors(*enabledCollectors)
collectors.Enable(enabledCollectorList)
collectors.SetLogger(logger)

// Initialize collectors before loading
err = collectors.Build()
err = collectors.Build(logger)
if err != nil {
_ = level.Error(logger).Log("msg", "Couldn't load collectors", "err", err)
os.Exit(1)
}
err = collectors.SetPerfCounterQuery()
err = collectors.SetPerfCounterQuery(logger)
if err != nil {
_ = level.Error(logger).Log("msg", "Couldn't set performance counter query", "err", err)
os.Exit(1)
Expand All @@ -229,7 +229,7 @@ func main() {
_ = level.Info(logger).Log("msg", fmt.Sprintf("Enabled collectors: %v", strings.Join(enabledCollectorList, ", ")))

mux := http.NewServeMux()
mux.HandleFunc(*metricsPath, withConcurrencyLimit(*maxRequests, collectors.BuildServeHTTP(*disableExporterMetrics, *timeoutMargin)))
mux.HandleFunc(*metricsPath, withConcurrencyLimit(*maxRequests, collectors.BuildServeHTTP(logger, *disableExporterMetrics, *timeoutMargin)))
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := fmt.Fprintln(w, `{"status":"ok"}`)
Expand Down Expand Up @@ -274,7 +274,7 @@ func main() {
}

go func() {
if err := web.ListenAndServe(server, webConfig, logger); err != nil {
if err := web.ListenAndServe(server, webConfig, logger); err != nil && !errors.Is(err, http.ErrServerClosed) {
_ = level.Error(logger).Log("msg", "cannot start windows_exporter", "err", err)
os.Exit(1)
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require github.com/pkg/errors v0.9.1
require (
github.com/google/uuid v1.6.0
github.com/pkg/errors v0.9.1
)

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
Expand Down
24 changes: 9 additions & 15 deletions pkg/collector/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var ConfigDefaults = Config{}
// A Collector is a Prometheus Collector for WMI Win32_PerfRawData_DirectoryServices_DirectoryServices metrics.
type Collector struct {
config Config
logger log.Logger

addressBookClientSessions *prometheus.Desc
addressBookOperationsTotal *prometheus.Desc
Expand Down Expand Up @@ -88,7 +87,7 @@ type Collector struct {
tombstonesObjectsVisitedTotal *prometheus.Desc
}

func New(logger log.Logger, config *Config) *Collector {
func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}
Expand All @@ -97,8 +96,6 @@ func New(logger log.Logger, config *Config) *Collector {
config: *config,
}

c.SetLogger(logger)

return c
}

Expand All @@ -110,19 +107,15 @@ func (c *Collector) GetName() string {
return Name
}

func (c *Collector) SetLogger(logger log.Logger) {
c.logger = log.With(logger, "collector", Name)
}

func (c *Collector) GetPerfCounter() ([]string, error) {
func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) {
return []string{}, nil
}

func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build() error {
func (c *Collector) Build(_ log.Logger) error {
c.addressBookOperationsTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "address_book_operations_total"),
"",
Expand Down Expand Up @@ -502,9 +495,10 @@ func (c *Collector) Build() error {

// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *Collector) Collect(_ *types.ScrapeContext, ch chan<- prometheus.Metric) error {
if err := c.collect(ch); err != nil {
_ = level.Error(c.logger).Log("msg", "failed collecting ad metrics", "err", err)
func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
if err := c.collect(logger, ch); err != nil {
_ = level.Error(logger).Log("msg", "failed collecting ad metrics", "err", err)
return err
}
return nil
Expand Down Expand Up @@ -662,9 +656,9 @@ type Win32_PerfRawData_DirectoryServices_DirectoryServices struct {
TransitivesuboperationsPersec uint32
}

func (c *Collector) collect(ch chan<- prometheus.Metric) error {
func (c *Collector) collect(logger log.Logger, ch chan<- prometheus.Metric) error {
var dst []Win32_PerfRawData_DirectoryServices_DirectoryServices
q := wmi.QueryAll(&dst, c.logger)
q := wmi.QueryAll(&dst, logger)
if err := wmi.Query(q, &dst); err != nil {
return err
}
Expand Down
24 changes: 9 additions & 15 deletions pkg/collector/adcs/adcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var ConfigDefaults = Config{}

type Collector struct {
config Config
logger log.Logger

challengeResponseProcessingTime *prometheus.Desc
challengeResponsesPerSecond *prometheus.Desc
Expand All @@ -40,7 +39,7 @@ type Collector struct {
signedCertificateTimestampListsPerSecond *prometheus.Desc
}

func New(logger log.Logger, config *Config) *Collector {
func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}
Expand All @@ -49,8 +48,6 @@ func New(logger log.Logger, config *Config) *Collector {
config: *config,
}

c.SetLogger(logger)

return c
}

Expand All @@ -62,19 +59,15 @@ func (c *Collector) GetName() string {
return Name
}

func (c *Collector) SetLogger(logger log.Logger) {
c.logger = log.With(logger, "collector", Name)
}

func (c *Collector) GetPerfCounter() ([]string, error) {
func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) {
return []string{"Certification Authority"}, nil
}

func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build() error {
func (c *Collector) Build(_ log.Logger) error {
c.requestsPerSecond = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "requests_total"),
"Total certificate requests processed",
Expand Down Expand Up @@ -157,9 +150,10 @@ func (c *Collector) Build() error {
return nil
}

func (c *Collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
if err := c.collectADCSCounters(ctx, ch); err != nil {
_ = level.Error(c.logger).Log("msg", "failed collecting ADCS metrics", "err", err)
func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
if err := c.collectADCSCounters(ctx, logger, ch); err != nil {
_ = level.Error(logger).Log("msg", "failed collecting ADCS metrics", "err", err)
return err
}
return nil
Expand All @@ -182,12 +176,12 @@ type perflibADCS struct {
SignedCertificateTimestampListProcessingTime float64 `perflib:"Signed Certificate Timestamp List processing time (ms)"`
}

func (c *Collector) collectADCSCounters(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
func (c *Collector) collectADCSCounters(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
dst := make([]perflibADCS, 0)
if _, ok := ctx.PerfObjects["Certification Authority"]; !ok {
return errors.New("perflib did not contain an entry for Certification Authority")
}
err := perflib.UnmarshalObject(ctx.PerfObjects["Certification Authority"], &dst, c.logger)
err := perflib.UnmarshalObject(ctx.PerfObjects["Certification Authority"], &dst, logger)
if err != nil {
return err
}
Expand Down
18 changes: 6 additions & 12 deletions pkg/collector/adfs/adfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var ConfigDefaults = Config{}

type Collector struct {
config Config
logger log.Logger

adLoginConnectionFailures *prometheus.Desc
artifactDBFailures *prometheus.Desc
Expand Down Expand Up @@ -67,7 +66,7 @@ type Collector struct {
wstrustTokenRequests *prometheus.Desc
}

func New(logger log.Logger, config *Config) *Collector {
func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}
Expand All @@ -76,8 +75,6 @@ func New(logger log.Logger, config *Config) *Collector {
config: *config,
}

c.SetLogger(logger)

return c
}

Expand All @@ -89,19 +86,15 @@ func (c *Collector) GetName() string {
return Name
}

func (c *Collector) SetLogger(logger log.Logger) {
c.logger = log.With(logger, "collector", Name)
}

func (c *Collector) GetPerfCounter() ([]string, error) {
func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) {
return []string{"AD FS"}, nil
}

func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build() error {
func (c *Collector) Build(_ log.Logger) error {
c.adLoginConnectionFailures = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "ad_login_connection_failures_total"),
"Total number of connection failures to an Active Directory domain controller",
Expand Down Expand Up @@ -410,9 +403,10 @@ type perflibADFS struct {
FederationMetadataRequests float64 `perflib:"Federation Metadata Requests"`
}

func (c *Collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
var adfsData []perflibADFS
err := perflib.UnmarshalObject(ctx.PerfObjects["AD FS"], &adfsData, c.logger)
err := perflib.UnmarshalObject(ctx.PerfObjects["AD FS"], &adfsData, logger)
if err != nil {
return err
}
Expand Down
25 changes: 10 additions & 15 deletions pkg/collector/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var ConfigDefaults = Config{}
// A Collector is a Prometheus Collector for Perflib Cache metrics.
type Collector struct {
config Config
logger log.Logger

asyncCopyReadsTotal *prometheus.Desc
asyncDataMapsTotal *prometheus.Desc
Expand Down Expand Up @@ -54,7 +53,7 @@ type Collector struct {
syncPinReadsTotal *prometheus.Desc
}

func New(logger log.Logger, config *Config) *Collector {
func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}
Expand All @@ -63,8 +62,6 @@ func New(logger log.Logger, config *Config) *Collector {
config: *config,
}

c.SetLogger(logger)

return c
}

Expand All @@ -76,19 +73,15 @@ func (c *Collector) GetName() string {
return Name
}

func (c *Collector) SetLogger(logger log.Logger) {
c.logger = log.With(logger, "collector", Name)
}

func (c *Collector) GetPerfCounter() ([]string, error) {
func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) {
return []string{"Cache"}, nil
}

func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build() error {
func (c *Collector) Build(_ log.Logger) error {
c.asyncCopyReadsTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "async_copy_reads_total"),
"(AsyncCopyReadsTotal)",
Expand Down Expand Up @@ -267,9 +260,10 @@ func (c *Collector) Build() error {
}

// Collect implements the Collector interface.
func (c *Collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
if err := c.collect(ctx, ch); err != nil {
_ = level.Error(c.logger).Log("msg", "failed collecting cache metrics", "err", err)
func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
if err := c.collect(ctx, logger, ch); err != nil {
_ = level.Error(logger).Log("msg", "failed collecting cache metrics", "err", err)

return err
}
Expand Down Expand Up @@ -311,9 +305,10 @@ type perflibCache struct {
DataMapHitsPercent float64 `perflib:"Data Map Hits %"`
}

func (c *Collector) collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
var dst []perflibCache // Single-instance class, array is required but will have single entry.
if err := perflib.UnmarshalObject(ctx.PerfObjects["Cache"], &dst, c.logger); err != nil {
if err := perflib.UnmarshalObject(ctx.PerfObjects["Cache"], &dst, logger); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 89c832f

Please sign in to comment.