Skip to content

Commit 9886944

Browse files
committed
PR comments addressed
1 parent 64a7799 commit 9886944

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -652,24 +652,23 @@ private static String toLabelValue(AttributeType type, Object attributeValue) {
652652
case BOOLEAN:
653653
case LONG:
654654
case DOUBLE:
655+
return attributeValue.toString();
655656
case BOOLEAN_ARRAY:
656657
case LONG_ARRAY:
657658
case DOUBLE_ARRAY:
658-
return attributeValue.toString();
659-
660659
case STRING_ARRAY:
661660
if (attributeValue instanceof List) {
662661
return ((List<?>) attributeValue)
663662
.stream()
664-
.map(Object::toString)
665-
.map(Otel2PrometheusConverter::toJsonValidStr)
663+
.map(String::valueOf)
664+
.map(it -> AttributeType.STRING_ARRAY.equals(type) ? toJsonValidStr(it) : it)
666665
.collect(Collectors.toList())
667666
.toString();
668667
} else {
669668
LOGGER.log(
670669
Level.WARNING,
671-
"Unexpected label value of {0} for AttributeType.STRING_ARRAY, toString() is being used as fallback value...",
672-
attributeValue.getClass());
670+
"Unexpected label value of {0} for {1}, toString() is being used as fallback value...",
671+
new Object[] {attributeValue.getClass(), type.name()});
673672
return attributeValue.toString();
674673
}
675674
}

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import static org.assertj.core.api.Assertions.assertThat;
1010
import static org.assertj.core.api.Assertions.assertThatCode;
1111

12+
import com.fasterxml.jackson.core.JsonProcessingException;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
1214
import io.opentelemetry.api.common.AttributeKey;
1315
import io.opentelemetry.api.common.AttributeType;
1416
import io.opentelemetry.api.common.Attributes;
@@ -164,29 +166,41 @@ void labelValueSerialization_Primitives() {
164166
}
165167

166168
@Test
167-
void labelValueSerialization_NonPrimitives() {
169+
void labelValueSerialization_NonPrimitives() throws JsonProcessingException {
170+
List<String> stringArrayValue =
171+
Arrays.asList("stringValue1", "\"+\\\\\\+\b+\f+\n+\r+\t+" + (char) 0);
172+
List<Boolean> booleanArrayValue = Arrays.asList(true, false);
173+
List<Long> longArrayValue = Arrays.asList(Long.MIN_VALUE, Long.MAX_VALUE);
174+
List<Double> doubleArrayValue = Arrays.asList(Double.MIN_VALUE, Double.MAX_VALUE);
168175
Attributes attributes =
169176
Attributes.builder()
170-
.put(
171-
AttributeKey.stringArrayKey("stringKey"),
172-
Arrays.asList("stringValue1", "\"+\\\\\\+\b+\f+\n+\r+\t+" + (char) 0))
173-
.put(AttributeKey.booleanArrayKey("booleanKey"), Arrays.asList(true, false))
174-
.put(AttributeKey.longArrayKey("longKey"), Arrays.asList(12345L, 6789L))
175-
.put(AttributeKey.doubleArrayKey("doubleKey"), Arrays.asList(0.12345, 0.6789))
177+
.put(AttributeKey.stringArrayKey("stringKey"), stringArrayValue)
178+
.put(AttributeKey.booleanArrayKey("booleanKey"), booleanArrayValue)
179+
.put(AttributeKey.longArrayKey("longKey"), longArrayValue)
180+
.put(AttributeKey.doubleArrayKey("doubleKey"), doubleArrayValue)
176181
.build();
177182
MetricData metricData =
178183
createSampleMetricData("sample", "1", MetricDataType.LONG_SUM, attributes, null);
179184

180185
MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData));
181186

182-
assertThat(snapshots.get(0).getDataPoints().get(0).getLabels().get("stringKey"))
183-
.isEqualTo("[\"stringValue1\", \"\\\"+\\\\\\\\\\\\+\\b+\\f+\\n+\\r+\\t+\\u0000\"]");
184-
assertThat(snapshots.get(0).getDataPoints().get(0).getLabels().get("booleanKey"))
185-
.isEqualTo("[true, false]");
186-
assertThat(snapshots.get(0).getDataPoints().get(0).getLabels().get("longKey"))
187-
.isEqualTo("[12345, 6789]");
188-
assertThat(snapshots.get(0).getDataPoints().get(0).getLabels().get("doubleKey"))
189-
.isEqualTo("[0.12345, 0.6789]");
187+
ObjectMapper objectMapper = new ObjectMapper();
188+
assertThat(
189+
objectMapper.readTree(
190+
snapshots.get(0).getDataPoints().get(0).getLabels().get("stringKey")))
191+
.isEqualTo(objectMapper.valueToTree(stringArrayValue));
192+
assertThat(
193+
objectMapper.readTree(
194+
snapshots.get(0).getDataPoints().get(0).getLabels().get("booleanKey")))
195+
.isEqualTo(objectMapper.valueToTree(booleanArrayValue));
196+
assertThat(
197+
objectMapper.readTree(
198+
snapshots.get(0).getDataPoints().get(0).getLabels().get("longKey")))
199+
.isEqualTo(objectMapper.valueToTree(longArrayValue));
200+
assertThat(
201+
objectMapper.readTree(
202+
snapshots.get(0).getDataPoints().get(0).getLabels().get("doubleKey")))
203+
.isEqualTo(objectMapper.valueToTree(doubleArrayValue));
190204
}
191205

192206
@Test

0 commit comments

Comments
 (0)