Skip to content

Commit 6ee54e5

Browse files
Fix serialization of array-valued scope and resource attributes in Prometheus exporter (#8497)
Co-authored-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 0004c7e commit 6ee54e5

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
### SDK
66

7+
#### Exporters
8+
9+
* Prometheus: Fix serialization of array-valued scope and resource attributes to JSON strings
10+
711
#### Traces
812

913
* Add `BatchSpanProcessor.create(SpanExporter)` convenience factory to mirror

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ private Labels convertAttributes(
561561
.forEach(
562562
(key, value) ->
563563
labelNameToValue.putIfAbsent(
564-
OTEL_SCOPE_ATTRIBUTE_PREFIX + key.getKey(), value.toString()));
564+
OTEL_SCOPE_ATTRIBUTE_PREFIX + key.getKey(),
565+
toLabelValue(key.getType(), value)));
565566
}
566567

567568
if (resource != null) {
@@ -570,7 +571,8 @@ private Labels convertAttributes(
570571
Object attributeValue = resourceAttributes.get(attributeKey);
571572
if (attributeValue != null) {
572573
labelNameToValue.putIfAbsent(
573-
convertLabelName(attributeKey.getKey()), attributeValue.toString());
574+
convertLabelName(attributeKey.getKey()),
575+
toLabelValue(attributeKey.getType(), attributeValue));
574576
}
575577
}
576578
}

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverterTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,56 @@ private static Stream<Arguments> resourceAttributesAdditionArgs() {
684684
"my_metric_units",
685685
"cluster=\"mycluster\",otel_scope_foo=\"bar\",otel_scope_name=\"scope\",otel_scope_schema_url=\"schemaUrl\",otel_scope_version=\"version\""));
686686

687+
// Array-valued resource attribute is serialized as a JSON string, matching the point attribute
688+
// path
689+
arguments.add(
690+
Arguments.argumentSet(
691+
"array-valued resource attribute serialized as json",
692+
createSampleMetricData(
693+
"my.metric",
694+
"units",
695+
MetricDataType.LONG_SUM,
696+
Attributes.empty(),
697+
Resource.create(
698+
Attributes.of(stringArrayKey("clusters"), Arrays.asList("a", "b")))),
699+
/* allowedResourceAttributesFilter= */ Predicates.startsWith("clu"),
700+
"my_metric_units",
701+
"clusters=\"[\\\"a\\\",\\\"b\\\"]\",otel_scope_foo=\"bar\",otel_scope_name=\"scope\",otel_scope_schema_url=\"schemaUrl\",otel_scope_version=\"version\""));
702+
687703
return arguments.stream();
688704
}
689705

706+
@Test
707+
void arrayValuedScopeAttributeSerializedAsJson() {
708+
// Array-valued scope attribute is serialized as a JSON string, matching the point attribute
709+
// path
710+
InstrumentationScopeInfo scope =
711+
InstrumentationScopeInfo.builder("scope")
712+
.setAttributes(Attributes.of(stringArrayKey("foo"), Arrays.asList("a", "b")))
713+
.build();
714+
MetricData metricData =
715+
ImmutableMetricData.createLongSum(
716+
Resource.getDefault(),
717+
scope,
718+
"sample",
719+
"description",
720+
"1",
721+
ImmutableSumData.create(
722+
/* isMonotonic= */ true,
723+
AggregationTemporality.CUMULATIVE,
724+
Collections.singletonList(
725+
ImmutableLongPointData.create(0, 1, Attributes.empty(), 1L))));
726+
727+
MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData));
728+
729+
Optional<MetricSnapshot> metricSnapshot =
730+
snapshots.stream().filter(snapshot -> snapshot instanceof CounterSnapshot).findFirst();
731+
assertThat(metricSnapshot).isPresent();
732+
733+
Labels labels = metricSnapshot.get().getDataPoints().get(0).getLabels();
734+
assertThat(labels.get("otel_scope_foo")).isEqualTo("[\"a\",\"b\"]");
735+
}
736+
690737
@Test
691738
void metricNameCollisionTest_Issue6277() {
692739
// NOTE: Metrics with the same resolved prometheus name should merge. However,

0 commit comments

Comments
 (0)