fix(metric_aggregation): Fix duplicate metrics registration (#15142)

pull/15103/head
Shantanu Alshi 1 year ago committed by GitHub
parent 716d54e2a9
commit 215f994d0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      pkg/pattern/aggregation/metrics.go
  2. 9
      pkg/pattern/aggregation/push.go
  3. 4
      pkg/pattern/aggregation/push_test.go
  4. 3
      pkg/pattern/ingester.go

@ -1,12 +1,19 @@
package aggregation package aggregation
import ( import (
"sync"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/loki/v3/pkg/util/constants" "github.com/grafana/loki/v3/pkg/util/constants"
) )
var (
aggMetrics *Metrics
metricsOnce sync.Once
)
type Metrics struct { type Metrics struct {
reg prometheus.Registerer reg prometheus.Registerer
@ -28,10 +35,8 @@ type Metrics struct {
} }
func NewMetrics(r prometheus.Registerer) *Metrics { func NewMetrics(r prometheus.Registerer) *Metrics {
var m Metrics metricsOnce.Do(func() {
m.reg = r aggMetrics = &Metrics{
m = Metrics{
chunks: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{ chunks: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{
Namespace: constants.Loki, Namespace: constants.Loki,
Subsystem: "pattern_ingester", Subsystem: "pattern_ingester",
@ -103,21 +108,7 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
Help: "Total number of write timeouts.", Help: "Total number of write timeouts.",
}, []string{"tenant_id"}), }, []string{"tenant_id"}),
} }
})
if m.reg != nil { return aggMetrics
m.reg.MustRegister(
m.chunks,
m.samples,
m.pushErrors,
m.pushRetries,
m.pushSuccesses,
m.payloadSize,
m.streamsPerPush,
m.entriesPerPush,
m.servicesTracked,
m.writeTimeout,
)
}
return &m
} }

@ -17,7 +17,6 @@ import (
"github.com/go-kit/log/level" "github.com/go-kit/log/level"
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/config" "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
@ -112,7 +111,7 @@ func NewPush(
useTLS bool, useTLS bool,
backoffCfg *backoff.Config, backoffCfg *backoff.Config,
logger log.Logger, logger log.Logger,
registrer prometheus.Registerer, metrics *Metrics,
) (*Push, error) { ) (*Push, error) {
client, err := config.NewClientFromConfig(cfg, "pattern-ingester-push", config.WithHTTP2Disabled()) client, err := config.NewClientFromConfig(cfg, "pattern-ingester-push", config.WithHTTP2Disabled())
if err != nil { if err != nil {
@ -147,7 +146,7 @@ func NewPush(
entries: entries{ entries: entries{
entries: make([]entry, 0), entries: make([]entry, 0),
}, },
metrics: NewMetrics(registrer), metrics: metrics,
} }
go p.run(pushPeriod) go p.run(pushPeriod)
@ -284,12 +283,12 @@ func (p *Push) run(pushPeriod time.Duration) {
} }
if !backoff.Ongoing() { if !backoff.Ongoing() {
level.Error(p.logger).Log("msg", "failed to send entry, retries exhausted, entry will be dropped", "entry", "status", status, "error", err) level.Error(p.logger).Log("msg", "failed to send entry, retries exhausted, entry will be dropped", "status", status, "error", err)
pushTicker.Reset(pushPeriod) pushTicker.Reset(pushPeriod)
break break
} }
level.Warn(p.logger). level.Warn(p.logger).
Log("msg", "failed to send entry, retrying", "entry", "status", status, "error", err) Log("msg", "failed to send entry, retrying", "status", status, "error", err)
backoff.Wait() backoff.Wait()
} }

@ -58,7 +58,7 @@ func Test_Push(t *testing.T) {
false, false,
&backoff, &backoff,
log.NewNopLogger(), log.NewNopLogger(),
nil, NewMetrics(nil),
) )
require.NoError(t, err) require.NoError(t, err)
ts, payload := testPayload() ts, payload := testPayload()
@ -83,7 +83,7 @@ func Test_Push(t *testing.T) {
"user", "secret", "user", "secret",
false, false,
&backoff, &backoff,
log.NewNopLogger(), nil, log.NewNopLogger(), NewMetrics(nil),
) )
require.NoError(t, err) require.NoError(t, err)
ts, payload := testPayload() ts, payload := testPayload()

@ -392,6 +392,7 @@ func (i *Ingester) GetOrCreateInstance(instanceID string) (*instance, error) { /
aggCfg := i.cfg.MetricAggregation aggCfg := i.cfg.MetricAggregation
if i.limits.MetricAggregationEnabled(instanceID) { if i.limits.MetricAggregationEnabled(instanceID) {
metricAggregationMetrics := aggregation.NewMetrics(i.registerer)
writer, err = aggregation.NewPush( writer, err = aggregation.NewPush(
aggCfg.LokiAddr, aggCfg.LokiAddr,
instanceID, instanceID,
@ -403,7 +404,7 @@ func (i *Ingester) GetOrCreateInstance(instanceID string) (*instance, error) { /
aggCfg.UseTLS, aggCfg.UseTLS,
&aggCfg.BackoffConfig, &aggCfg.BackoffConfig,
i.logger, i.logger,
i.registerer, metricAggregationMetrics,
) )
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save