From 86fe6d8f23b6115fe27f77831f116cbb56f30a36 Mon Sep 17 00:00:00 2001 From: Pavel Okhlopkov Date: Tue, 5 Nov 2024 10:34:49 +0300 Subject: [PATCH] refactoring Signed-off-by: Pavel Okhlopkov --- pkg/metric_storage/metric_storage.go | 32 ++++++++++------------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/pkg/metric_storage/metric_storage.go b/pkg/metric_storage/metric_storage.go index 90a6c35e..3c74fae2 100644 --- a/pkg/metric_storage/metric_storage.go +++ b/pkg/metric_storage/metric_storage.go @@ -60,7 +60,7 @@ func NewMetricStorage(ctx context.Context, prefix string, newRegistry bool, logg Gatherer: prometheus.DefaultGatherer, Registerer: prometheus.DefaultRegisterer, - logger: logger, + logger: logger.With("operator.component", "metricsStorage"), } m.groupedVault = vault.NewGroupedVault(m.resolveMetricName) m.groupedVault.SetRegisterer(m.Registerer) @@ -94,8 +94,7 @@ func (m *MetricStorage) GaugeSet(metric string, value float64, labels map[string } defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricsStorage"). - Errorf("Metric gauge set %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) + m.logger.Errorf("Metric gauge set %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) } }() @@ -108,8 +107,7 @@ func (m *MetricStorage) GaugeAdd(metric string, value float64, labels map[string } defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricsStorage"). - Errorf("Metric gauge add %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) + m.logger.Errorf("Metric gauge add %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) } }() @@ -134,8 +132,7 @@ func (m *MetricStorage) RegisterGauge(metric string, labels map[string]string) * defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricStorage"). - Errorf("Create metric gauge %s %v with %v: %v", metricName, LabelNames(labels), labels, r) + m.logger.Errorf("Create metric gauge %s %v with %v: %v", metricName, LabelNames(labels), labels, r) } }() @@ -147,8 +144,7 @@ func (m *MetricStorage) RegisterGauge(metric string, labels map[string]string) * return vec } - m.logger.With("operator.component", "metricStorage"). - Infof("Create metric gauge %s", metricName) + m.logger.Infof("Create metric gauge %s", metricName) vec = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: metricName, @@ -169,8 +165,7 @@ func (m *MetricStorage) CounterAdd(metric string, value float64, labels map[stri } defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricsStorage"). - Errorf("Metric counter add %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) + m.logger.Errorf("Metric counter add %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) } }() m.Counter(metric, labels).With(labels).Add(value) @@ -194,8 +189,7 @@ func (m *MetricStorage) RegisterCounter(metric string, labels map[string]string) defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricStorage"). - Errorf("Create metric counter %s %v with %v: %v", metricName, LabelNames(labels), labels, r) + m.logger.Errorf("Create metric counter %s %v with %v: %v", metricName, LabelNames(labels), labels, r) } }() @@ -207,8 +201,7 @@ func (m *MetricStorage) RegisterCounter(metric string, labels map[string]string) return vec } - m.logger.With("operator.component", "metricStorage"). - Infof("Create metric counter %s", metricName) + m.logger.Infof("Create metric counter %s", metricName) vec = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: metricName, @@ -228,8 +221,7 @@ func (m *MetricStorage) HistogramObserve(metric string, value float64, labels ma } defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricsStorage"). - Errorf("Metric histogram observe %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) + m.logger.Errorf("Metric histogram observe %s %v with %v: %v", m.resolveMetricName(metric), LabelNames(labels), labels, r) } }() m.Histogram(metric, labels, buckets).With(labels).Observe(value) @@ -250,8 +242,7 @@ func (m *MetricStorage) RegisterHistogram(metric string, labels map[string]strin defer func() { if r := recover(); r != nil { - m.logger.With("operator.component", "metricsStorage"). - Errorf("Create metric histogram %s %v with %v: %v", metricName, LabelNames(labels), labels, r) + m.logger.Errorf("Create metric histogram %s %v with %v: %v", metricName, LabelNames(labels), labels, r) } }() @@ -263,8 +254,7 @@ func (m *MetricStorage) RegisterHistogram(metric string, labels map[string]strin return vec } - m.logger.With("operator.component", "metricsStorage"). - Infof("Create metric histogram %s", metricName) + m.logger.Infof("Create metric histogram %s", metricName) b, has := m.HistogramBuckets[metric] // This shouldn't happen except when entering this concurrently // If there are buckets for this histogram about to be registered, keep them