chore: pass context aware logger to collectors (#1582)

This commit is contained in:
Jan-Otto Kröpke
2024-08-24 19:14:38 +02:00
committed by GitHub
parent fbead0fb79
commit 89c832feb0
65 changed files with 876 additions and 1164 deletions

View File

@@ -20,7 +20,6 @@ var ConfigDefaults = Config{}
// A Collector is a Prometheus Collector for WMI Win32_PerfRawData_NETFramework_NETCLRExceptions metrics.
type Collector struct {
config Config
logger log.Logger
numberOfExceptionsThrown *prometheus.Desc
numberOfFilters *prometheus.Desc
@@ -28,7 +27,7 @@ type Collector struct {
throwToCatchDepth *prometheus.Desc
}
func New(logger log.Logger, config *Config) *Collector {
func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}
@@ -37,8 +36,6 @@ func New(logger log.Logger, config *Config) *Collector {
config: *config,
}
c.SetLogger(logger)
return c
}
@@ -50,11 +47,7 @@ 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
}
@@ -62,7 +55,7 @@ func (c *Collector) Close() error {
return nil
}
func (c *Collector) Build() error {
func (c *Collector) Build(_ log.Logger) error {
c.numberOfExceptionsThrown = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "exceptions_thrown_total"),
"Displays the total number of exceptions thrown since the application started. This includes both .NET exceptions and unmanaged exceptions that are converted into .NET exceptions.",
@@ -92,9 +85,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 win32_perfrawdata_netframework_netclrexceptions 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 win32_perfrawdata_netframework_netclrexceptions metrics", "err", err)
return err
}
return nil
@@ -110,9 +104,9 @@ type Win32_PerfRawData_NETFramework_NETCLRExceptions struct {
ThrowToCatchDepthPersec 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_NETFramework_NETCLRExceptions
q := wmi.QueryAll(&dst, c.logger)
q := wmi.QueryAll(&dst, logger)
if err := wmi.Query(q, &dst); err != nil {
return err
}