Skip to content

Commit 3cf8edf

Browse files
chore: otel prom metadata translation pass namespace config (#39826)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Fixing passing metric namespace to the otel metadata translation, so when provided metric name matches the metric name in metadata <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit test coverage --------- Co-authored-by: Arthur Silva Sens <[email protected]>
1 parent 8d5e2fe commit 3cf8edf

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
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: bug_fix
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: "Fix bug where metric metadata was sent with incorrect metric name if configuration includes 'namespace'"
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: [ 39826 ]
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: [user, api]

exporter/prometheusremotewriteexporter/exporter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (prwe *prwExporter) pushMetricsV1(ctx context.Context, md pmetric.Metrics)
220220

221221
var m []*prompb.MetricMetadata
222222
if prwe.exporterSettings.SendMetadata {
223-
m = prometheusremotewrite.OtelMetricsToMetadata(md, prwe.exporterSettings.AddMetricSuffixes)
223+
m = prometheusremotewrite.OtelMetricsToMetadata(md, prwe.exporterSettings.AddMetricSuffixes, prwe.exporterSettings.Namespace)
224224
}
225225
if err != nil {
226226
prwe.telemetry.recordTranslationFailure(ctx)
@@ -253,7 +253,6 @@ func (prwe *prwExporter) PushMetrics(ctx context.Context, md pmetric.Metrics) er
253253
return prwe.pushMetricsV1(ctx, md)
254254
case config.RemoteWriteProtoMsgV2:
255255
return prwe.pushMetricsV2(ctx, md)
256-
257256
default:
258257
return fmt.Errorf("unsupported remote-write protobuf message: %v", prwe.RemoteWriteProtoMsg)
259258
}

pkg/translator/prometheusremotewrite/otlp_to_openmetrics_metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func otelMetricTypeToPromMetricType(otelMetric pmetric.Metric) prompb.MetricMeta
4242
return prompb.MetricMetadata_UNKNOWN
4343
}
4444

45-
func OtelMetricsToMetadata(md pmetric.Metrics, addMetricSuffixes bool) []*prompb.MetricMetadata {
45+
func OtelMetricsToMetadata(md pmetric.Metrics, addMetricSuffixes bool, namespace string) []*prompb.MetricMetadata {
4646
resourceMetricsSlice := md.ResourceMetrics()
4747

4848
metadataLength := 0
@@ -64,7 +64,7 @@ func OtelMetricsToMetadata(md pmetric.Metrics, addMetricSuffixes bool) []*prompb
6464
metric := scopeMetrics.Metrics().At(k)
6565
entry := prompb.MetricMetadata{
6666
Type: otelMetricTypeToPromMetricType(metric),
67-
MetricFamilyName: prometheustranslator.BuildCompliantName(metric, "", addMetricSuffixes),
67+
MetricFamilyName: prometheustranslator.BuildCompliantName(metric, namespace, addMetricSuffixes),
6868
Unit: prometheustranslator.BuildCompliantPrometheusUnit(metric.Unit()),
6969
Help: metric.Description(),
7070
}

pkg/translator/prometheusremotewrite/otlp_to_openmetrics_metadata_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ func TestOtelMetricTypeToPromMetricType(t *testing.T) {
152152
func TestOtelMetricsToMetadata(t *testing.T) {
153153
ts := uint64(time.Now().UnixNano())
154154
tests := []struct {
155-
name string
156-
metrics pmetric.Metrics
157-
want []*prompb.MetricMetadata
155+
name string
156+
metrics pmetric.Metrics
157+
want []*prompb.MetricMetadata
158+
namespace string
158159
}{
159160
{
160161
name: "all types§",
@@ -222,10 +223,27 @@ func TestOtelMetricsToMetadata(t *testing.T) {
222223
},
223224
},
224225
},
226+
{
227+
name: "gauge_namespace",
228+
metrics: GenerateMetricsGauge(),
229+
want: []*prompb.MetricMetadata{
230+
{
231+
Type: prompb.MetricMetadata_GAUGE,
232+
MetricFamilyName: prometheustranslator.BuildCompliantName(getIntGaugeMetric(
233+
testdata.TestGaugeDoubleMetricName,
234+
pcommon.NewMap(),
235+
1, ts,
236+
), "ns", false),
237+
Unit: "bytes_per_second",
238+
Help: "gauge description",
239+
},
240+
},
241+
namespace: "ns",
242+
},
225243
}
226244
for _, tt := range tests {
227245
t.Run(tt.name, func(t *testing.T) {
228-
metaData := OtelMetricsToMetadata(tt.metrics, false)
246+
metaData := OtelMetricsToMetadata(tt.metrics, false, tt.namespace)
229247

230248
for i := 0; i < len(metaData); i++ {
231249
assert.Equal(t, tt.want[i].Type, metaData[i].Type)
@@ -237,6 +255,14 @@ func TestOtelMetricsToMetadata(t *testing.T) {
237255
}
238256
}
239257

258+
func GenerateMetricsGauge() pmetric.Metrics {
259+
md := testdata.GenerateMetricsOneEmptyInstrumentationLibrary()
260+
ilm0 := md.ResourceMetrics().At(0).ScopeMetrics().At(0)
261+
ms := ilm0.Metrics()
262+
initMetric(ms.AppendEmpty(), testdata.TestGaugeDoubleMetricName, pmetric.MetricTypeGauge, "By/s", "gauge description")
263+
return md
264+
}
265+
240266
func GenerateMetricsAllTypesNoDataPointsHelp() pmetric.Metrics {
241267
md := testdata.GenerateMetricsOneEmptyInstrumentationLibrary()
242268
ilm0 := md.ResourceMetrics().At(0).ScopeMetrics().At(0)

0 commit comments

Comments
 (0)