Skip to content

Commit

Permalink
Further reduce Labes() calls in the metrics registry (#4283)
Browse files Browse the repository at this point in the history
  • Loading branch information
zalegrala authored Nov 5, 2024
1 parent f8a1dbb commit c0fb6be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 45 deletions.
34 changes: 12 additions & 22 deletions modules/generator/registry/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ type counter struct {
}

type counterSeries struct {
labels LabelPair
labels labels.Labels
value *atomic.Float64
lastUpdated *atomic.Int64
// firstSeries is used to track if this series is new to the counter. This
// is used to ensure that new counters being with 0, and then are incremented
// to the desired value. This avoids Prometheus throwing away the first
// value in the series, due to the transition from null -> x.
firstSeries *atomic.Bool

lb *labels.Builder
baseLabels labels.Labels
}

var (
Expand Down Expand Up @@ -104,24 +101,24 @@ func (c *counter) Inc(labelValueCombo *LabelValueCombo, value float64) {
}

func (c *counter) newSeries(labelValueCombo *LabelValueCombo, value float64) *counterSeries {
// base labels
baseLabels := make(labels.Labels, 0, 1+len(c.externalLabels))
lbls := labelValueCombo.getLabelPair()
lb := labels.NewBuilder(make(labels.Labels, 1+len(lbls.names)+len(c.externalLabels)))

for i, name := range lbls.names {
lb.Set(name, lbls.values[i])
}

// add external labels
for name, value := range c.externalLabels {
baseLabels = append(baseLabels, labels.Label{Name: name, Value: value})
lb.Set(name, value)
}

// add metric name
baseLabels = append(baseLabels, labels.Label{Name: labels.MetricName, Value: c.metricName})
lb.Set(labels.MetricName, c.metricName)

return &counterSeries{
labels: labelValueCombo.getLabelPair(),
labels: lb.Labels(),
value: atomic.NewFloat64(value),
lastUpdated: atomic.NewInt64(time.Now().UnixMilli()),
firstSeries: atomic.NewBool(true),
lb: labels.NewBuilder(baseLabels),
baseLabels: baseLabels,
}
}

Expand All @@ -141,28 +138,21 @@ func (c *counter) collectMetrics(appender storage.Appender, timeMs int64) (activ
activeSeries = len(c.series)

for _, s := range c.series {
s.lb.Reset(s.baseLabels)

// set series-specific labels
for i, name := range s.labels.names {
s.lb.Set(name, s.labels.values[i])
}

// If we are about to call Append for the first time on a series, we need
// to first insert a 0 value to allow Prometheus to start from a non-null
// value.
if s.isNew() {
// We set the timestamp of the init serie at the end of the previous minute, that way we ensure it ends in a
// different aggregation interval to avoid be downsampled.
endOfLastMinuteMs := getEndOfLastMinuteMs(timeMs)
_, err = appender.Append(0, s.lb.Labels(), endOfLastMinuteMs, 0)
_, err = appender.Append(0, s.labels, endOfLastMinuteMs, 0)
if err != nil {
return
}
s.registerSeenSeries()
}

_, err = appender.Append(0, s.lb.Labels(), timeMs, s.value.Load())
_, err = appender.Append(0, s.labels, timeMs, s.value.Load())
if err != nil {
return
}
Expand Down
34 changes: 11 additions & 23 deletions modules/generator/registry/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ type gauge struct {
}

type gaugeSeries struct {
// labelValueCombo should not be modified after creation
labels LabelPair
labels labels.Labels
value *atomic.Float64
lastUpdated *atomic.Int64
lb *labels.Builder
baseLabels labels.Labels
}

var (
Expand Down Expand Up @@ -112,23 +109,23 @@ func (g *gauge) updateSeries(labelValueCombo *LabelValueCombo, value float64, op
}

func (g *gauge) newSeries(labelValueCombo *LabelValueCombo, value float64) *gaugeSeries {
// base labels
baseLabels := make(labels.Labels, 1+len(g.externalLabels))
lbls := labelValueCombo.getLabelPair()
lb := labels.NewBuilder(make(labels.Labels, 1+len(lbls.names)+len(g.externalLabels)))

// add metric name
baseLabels = append(baseLabels, labels.Label{Name: labels.MetricName, Value: g.metricName})
for i, name := range lbls.names {
lb.Set(name, lbls.values[i])
}

// add external labels
for name, value := range g.externalLabels {
baseLabels = append(baseLabels, labels.Label{Name: name, Value: value})
lb.Set(name, value)
}

lb.Set(labels.MetricName, g.metricName)

return &gaugeSeries{
labels: labelValueCombo.getLabelPair(),
labels: lb.Labels(),
value: atomic.NewFloat64(value),
lastUpdated: atomic.NewInt64(time.Now().UnixMilli()),
lb: labels.NewBuilder(baseLabels),
baseLabels: baseLabels,
}
}

Expand All @@ -153,16 +150,7 @@ func (g *gauge) collectMetrics(appender storage.Appender, timeMs int64) (activeS

for _, s := range g.series {
t := time.UnixMilli(timeMs)

// reset labels for every series
s.lb.Reset(s.baseLabels)

// set series-specific labels
for i, name := range s.labels.names {
s.lb.Set(name, s.labels.values[i])
}

_, err = appender.Append(0, s.lb.Labels(), t.UnixMilli(), s.value.Load())
_, err = appender.Append(0, s.labels, t.UnixMilli(), s.value.Load())
if err != nil {
return
}
Expand Down

0 comments on commit c0fb6be

Please sign in to comment.