Skip to content

Commit

Permalink
[receiver/prometheusreceiver] Use Prometheus Labels public method API (
Browse files Browse the repository at this point in the history
…#31908)

**Description:**
By only using the public method API for Prometheus labels (rather than
assuming `labels.Labels` is an alias of a slice) it opens up the
possibility to build a collector with the `stringlabels` tag, so we can
use the more memory efficient labels implementation.

**Link to tracking Issue:** #31907

**Testing:** 
I had trouble running all of the tests locally, so I think I will need
some help with making that work. I did run all the tests I changed with
`-tags=stringlabels` and without it.
  • Loading branch information
braydonk authored Mar 27, 2024
1 parent 73eabd8 commit 89b20d5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 70 deletions.
22 changes: 11 additions & 11 deletions receiver/prometheusreceiver/internal/metricfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,19 @@ func populateAttributes(mType pmetric.MetricType, ls labels.Labels, dest pcommon
dest.EnsureCapacity(ls.Len())
names := getSortedNotUsefulLabels(mType)
j := 0
for i := range ls {
for j < len(names) && names[j] < ls[i].Name {
ls.Range(func(l labels.Label) {
for j < len(names) && names[j] < l.Name {
j++
}
if j < len(names) && ls[i].Name == names[j] {
continue
if j < len(names) && l.Name == names[j] {
return
}
if ls[i].Value == "" {
if l.Value == "" {
// empty label values should be omitted
continue
return
}
dest.PutStr(ls[i].Name, ls[i].Value)
}
dest.PutStr(l.Name, l.Value)
})
}

func (mf *metricFamily) loadMetricGroupOrCreate(groupKey uint64, ls labels.Labels, ts int64) *metricGroup {
Expand Down Expand Up @@ -382,8 +382,8 @@ func (mf *metricFamily) addExemplar(seriesRef uint64, e exemplar.Exemplar) {
func convertExemplar(pe exemplar.Exemplar, e pmetric.Exemplar) {
e.SetTimestamp(timestampFromMs(pe.Ts))
e.SetDoubleValue(pe.Value)
e.FilteredAttributes().EnsureCapacity(len(pe.Labels))
for _, lb := range pe.Labels {
e.FilteredAttributes().EnsureCapacity(pe.Labels.Len())
pe.Labels.Range(func(lb labels.Label) {
switch strings.ToLower(lb.Name) {
case traceIDKey:
var tid [16]byte
Expand All @@ -404,7 +404,7 @@ func convertExemplar(pe exemplar.Exemplar, e pmetric.Exemplar) {
default:
e.FilteredAttributes().PutStr(lb.Name, lb.Value)
}
}
})
}

/*
Expand Down
34 changes: 18 additions & 16 deletions receiver/prometheusreceiver/internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"errors"
"fmt"
"sort"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar"
Expand Down Expand Up @@ -91,9 +90,12 @@ func (t *transaction) Append(_ storage.SeriesRef, ls labels.Labels, atMs int64,
default:
}

if len(t.externalLabels) != 0 {
ls = append(ls, t.externalLabels...)
sort.Sort(ls)
if t.externalLabels.Len() != 0 {
b := labels.NewBuilder(ls)
t.externalLabels.Range(func(l labels.Label) {
b.Set(l.Name, l.Value)
})
ls = b.Labels()
}

if t.isNew {
Expand Down Expand Up @@ -259,14 +261,14 @@ func (t *transaction) getMetrics(resource pcommon.Resource) (pmetric.Metrics, er

func getScopeID(ls labels.Labels) scopeID {
var scope scopeID
for _, lbl := range ls {
ls.Range(func(lbl labels.Label) {
if lbl.Name == scopeNameLabel {
scope.name = lbl.Value
}
if lbl.Name == scopeVersionLabel {
scope.version = lbl.Value
}
}
})
return scope
}

Expand Down Expand Up @@ -325,33 +327,33 @@ func (t *transaction) UpdateMetadata(_ storage.SeriesRef, _ labels.Labels, _ met
return 0, nil
}

func (t *transaction) AddTargetInfo(labels labels.Labels) {
func (t *transaction) AddTargetInfo(ls labels.Labels) {
attrs := t.nodeResource.Attributes()
for _, lbl := range labels {
ls.Range(func(lbl labels.Label) {
if lbl.Name == model.JobLabel || lbl.Name == model.InstanceLabel || lbl.Name == model.MetricNameLabel {
continue
return
}
attrs.PutStr(lbl.Name, lbl.Value)
}
})
}

func (t *transaction) addScopeInfo(labels labels.Labels) {
func (t *transaction) addScopeInfo(ls labels.Labels) {
attrs := pcommon.NewMap()
scope := scopeID{}
for _, lbl := range labels {
ls.Range(func(lbl labels.Label) {
if lbl.Name == model.JobLabel || lbl.Name == model.InstanceLabel || lbl.Name == model.MetricNameLabel {
continue
return
}
if lbl.Name == scopeNameLabel {
scope.name = lbl.Value
continue
return
}
if lbl.Name == scopeVersionLabel {
scope.version = lbl.Value
continue
return
}
attrs.PutStr(lbl.Name, lbl.Value)
}
})
t.scopeAttributes[scope] = attrs
}

Expand Down
Loading

0 comments on commit 89b20d5

Please sign in to comment.