Skip to content

Commit c5b6746

Browse files
jmichalek132ArthurSensdashpole
authored
feat: prom translation rw2 add support for labels (#35751)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Adding full support for handling labels. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue #33661 Fixes <!--Describe what testing was performed and which tests were added.--> #### Testing <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.--> --------- Signed-off-by: Juraj Michalek <[email protected]> Co-authored-by: Arthur Silva Sens <[email protected]> Co-authored-by: David Ashpole <[email protected]>
1 parent 7daddba commit c5b6746

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/translator/prometheusremotewrite
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "`FromMetricsV2` now transforms attributes into labels."
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33661]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/translator/prometheusremotewrite/metrics_to_prw_v2.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"strconv"
1010

11-
"github.com/prometheus/prometheus/model/labels"
11+
"github.com/prometheus/prometheus/prompb"
1212
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
1313
"go.opentelemetry.io/collector/pdata/pcommon"
1414
"go.opentelemetry.io/collector/pdata/pmetric"
@@ -49,6 +49,7 @@ func (c *prometheusConverterV2) fromMetrics(md pmetric.Metrics, settings Setting
4949
resourceMetricsSlice := md.ResourceMetrics()
5050
for i := 0; i < resourceMetricsSlice.Len(); i++ {
5151
resourceMetrics := resourceMetricsSlice.At(i)
52+
resource := resourceMetrics.Resource()
5253
scopeMetricsSlice := resourceMetrics.ScopeMetrics()
5354
// keep track of the most recent timestamp in the ResourceMetrics for
5455
// use with the "target" info metric
@@ -77,7 +78,7 @@ func (c *prometheusConverterV2) fromMetrics(md pmetric.Metrics, settings Setting
7778
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name()))
7879
break
7980
}
80-
c.addGaugeNumberDataPoints(dataPoints, promName)
81+
c.addGaugeNumberDataPoints(dataPoints, resource, settings, promName)
8182
case pmetric.MetricTypeSum:
8283
// TODO implement
8384
case pmetric.MetricTypeHistogram:
@@ -107,16 +108,25 @@ func (c *prometheusConverterV2) timeSeries() []writev2.TimeSeries {
107108
return allTS
108109
}
109110

110-
func (c *prometheusConverterV2) addSample(sample *writev2.Sample, lbls labels.Labels) *writev2.TimeSeries {
111+
func (c *prometheusConverterV2) addSample(sample *writev2.Sample, lbls []prompb.Label) *writev2.TimeSeries {
111112
if sample == nil || len(lbls) == 0 {
112113
// This shouldn't happen
113114
return nil
114115
}
115-
ts := &writev2.TimeSeries{}
116-
ts.LabelsRefs = c.symbolTable.SymbolizeLabels(lbls, ts.LabelsRefs)
117-
ts.Samples = append(ts.Samples, *sample)
118116

119-
c.unique[lbls.Hash()] = ts
117+
buf := make([]uint32, 0, len(lbls)*2)
118+
var off uint32
119+
for _, l := range lbls {
120+
off = c.symbolTable.Symbolize(l.Name)
121+
buf = append(buf, off)
122+
off = c.symbolTable.Symbolize(l.Value)
123+
buf = append(buf, off)
124+
}
125+
ts := writev2.TimeSeries{
126+
LabelsRefs: buf,
127+
Samples: []writev2.Sample{*sample},
128+
}
129+
c.unique[timeSeriesSignature(lbls)] = &ts
120130

121-
return ts
131+
return &ts
122132
}

pkg/translator/prometheusremotewrite/metrics_to_prw_v2_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ func TestFromMetricsV2(t *testing.T) {
2727
want := func() map[string]*writev2.TimeSeries {
2828
return map[string]*writev2.TimeSeries{
2929
"0": {
30-
LabelsRefs: []uint32{1, 2},
30+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8},
3131
Samples: []writev2.Sample{
3232
{Timestamp: convertTimeStamp(pcommon.Timestamp(ts)), Value: 1.23},
3333
},
3434
},
3535
}
3636
}
37+
wantedSymbols := []string{"", "series_name_2", "value-2", "series_name_3", "value-3", "__name__", "gauge_1", "series_name_1", "value-1"}
3738
tsMap, symbolsTable, err := FromMetricsV2(payload.Metrics(), settings)
38-
wanted := want()
3939
require.NoError(t, err)
40-
require.NotNil(t, tsMap)
41-
require.Equal(t, wanted, tsMap)
42-
require.NotNil(t, symbolsTable)
43-
40+
require.Equal(t, want(), tsMap)
41+
require.ElementsMatch(t, wantedSymbols, symbolsTable.Symbols())
4442
}

pkg/translator/prometheusremotewrite/number_data_points_v2.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@ import (
77
"math"
88

99
"github.com/prometheus/common/model"
10-
"github.com/prometheus/prometheus/model/labels"
1110
"github.com/prometheus/prometheus/model/value"
1211
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
12+
"go.opentelemetry.io/collector/pdata/pcommon"
1313
"go.opentelemetry.io/collector/pdata/pmetric"
1414
)
1515

16-
func (c *prometheusConverterV2) addGaugeNumberDataPoints(dataPoints pmetric.NumberDataPointSlice, name string) {
16+
func (c *prometheusConverterV2) addGaugeNumberDataPoints(dataPoints pmetric.NumberDataPointSlice,
17+
resource pcommon.Resource, settings Settings, name string) {
1718
for x := 0; x < dataPoints.Len(); x++ {
1819
pt := dataPoints.At(x)
19-
// TODO implement support for labels
2020

21-
labels := labels.Labels{
22-
labels.Label{
23-
Name: model.MetricNameLabel,
24-
Value: name,
25-
},
26-
}
21+
labels := createAttributes(
22+
resource,
23+
pt.Attributes(),
24+
settings.ExternalLabels,
25+
nil,
26+
true,
27+
model.MetricNameLabel,
28+
name,
29+
)
2730

2831
sample := &writev2.Sample{
2932
// convert ns to ms

pkg/translator/prometheusremotewrite/number_data_points_v2_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,16 @@ func TestPrometheusConverterV2_addGaugeNumberDataPoints(t *testing.T) {
108108
for _, tt := range tests {
109109
t.Run(tt.name, func(t *testing.T) {
110110
metric := tt.metric()
111+
settings := Settings{
112+
Namespace: "",
113+
ExternalLabels: nil,
114+
DisableTargetInfo: false,
115+
ExportCreatedMetric: false,
116+
AddMetricSuffixes: false,
117+
SendMetadata: false,
118+
}
111119
converter := newPrometheusConverterV2()
112-
converter.addGaugeNumberDataPoints(metric.Gauge().DataPoints(), metric.Name())
120+
converter.addGaugeNumberDataPoints(metric.Gauge().DataPoints(), pcommon.NewResource(), settings, metric.Name())
113121
w := tt.want()
114122

115123
diff := cmp.Diff(w, converter.unique, cmpopts.EquateNaNs())
@@ -150,9 +158,18 @@ func TestPrometheusConverterV2_addGaugeNumberDataPointsDuplicate(t *testing.T) {
150158
}
151159
}
152160

161+
settings := Settings{
162+
Namespace: "",
163+
ExternalLabels: nil,
164+
DisableTargetInfo: false,
165+
ExportCreatedMetric: false,
166+
AddMetricSuffixes: false,
167+
SendMetadata: false,
168+
}
169+
153170
converter := newPrometheusConverterV2()
154-
converter.addGaugeNumberDataPoints(metric1.Gauge().DataPoints(), metric1.Name())
155-
converter.addGaugeNumberDataPoints(metric2.Gauge().DataPoints(), metric2.Name())
171+
converter.addGaugeNumberDataPoints(metric1.Gauge().DataPoints(), pcommon.NewResource(), settings, metric1.Name())
172+
converter.addGaugeNumberDataPoints(metric2.Gauge().DataPoints(), pcommon.NewResource(), settings, metric2.Name())
156173

157174
assert.Equal(t, want(), converter.unique)
158175

0 commit comments

Comments
 (0)