From 84b22251492dac4e059bbdbbb0ddb8414d5019cb Mon Sep 17 00:00:00 2001 From: Matheus Cruz Date: Sat, 10 Jun 2023 15:09:35 -0300 Subject: [PATCH] Normalize configuration properties value before send to Otel --- .../runtime/OpenTelemetryProducer.java | 4 ++- .../runtime/OpenTelemetryUtil.java | 29 ++++++++++++--- .../runtime/OpenTelemetryUtilTest.java | 35 ++++++++++++++++--- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryProducer.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryProducer.java index 4df1f69659ba9..bb2f72deb81f9 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryProducer.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryProducer.java @@ -154,10 +154,12 @@ private Map getOtelConfigs() { if (configValue.getValue() != null) { NameIterator name = new NameIterator(propertyName); name.next(); - oTelConfigs.put(name.getName().substring(name.getPosition() + 1), configValue.getValue()); + String propertyValue = OpenTelemetryUtil.normalizeProperty(propertyName, configValue.getValue()); + oTelConfigs.put(name.getName().substring(name.getPosition() + 1), propertyValue); } } } return oTelConfigs; } + } diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtil.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtil.java index 6ab6ffac43112..df998f87fa50e 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtil.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtil.java @@ -1,14 +1,14 @@ package io.quarkus.opentelemetry.runtime; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.time.Duration; +import java.util.*; +import java.util.function.Function; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanContext; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.trace.ReadableSpan; +import io.quarkus.runtime.configuration.DurationConverter; import io.quarkus.vertx.core.runtime.VertxMDC; public final class OpenTelemetryUtil { @@ -16,6 +16,18 @@ public final class OpenTelemetryUtil { public static final String SPAN_ID = "spanId"; public static final String SAMPLED = "sampled"; public static final String PARENT_ID = "parentId"; + public static final Map> OTEL_PROPERTIES_NORMALIZABLE = Map.of( + "quarkus.otel.bsp.schedule.delay", + OpenTelemetryUtil::normalizeDuration, + "quarkus.otel.bsp.export.timeout", + OpenTelemetryUtil::normalizeDuration, + "quarkus.otel.exporter.otlp.traces.timeout", + OpenTelemetryUtil::normalizeDuration); + + static String normalizeDuration(String propertyValue) { + Duration duration = DurationConverter.parseDuration(propertyValue); + return Long.toString(duration.toMillis()).concat("ms"); + }; private OpenTelemetryUtil() { } @@ -84,4 +96,13 @@ public static void clearMDCData(io.vertx.core.Context vertxContext) { vertxMDC.remove(PARENT_ID, vertxContext); vertxMDC.remove(SAMPLED, vertxContext); } + + public static String normalizeProperty(String propertyName, String propertyValue) { + Function converter = OpenTelemetryUtil.OTEL_PROPERTIES_NORMALIZABLE.get(propertyName); + if (converter == null) { + return propertyValue; + } else { + return converter.apply(propertyValue); + } + } } diff --git a/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtilTest.java b/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtilTest.java index f1842ccc980a5..22cb779c45c4f 100644 --- a/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtilTest.java +++ b/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/OpenTelemetryUtilTest.java @@ -1,9 +1,8 @@ package io.quarkus.opentelemetry.runtime; -import java.util.AbstractMap; -import java.util.Arrays; -import java.util.Collections; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -58,4 +57,32 @@ public void testConvertKeyValueListToMap_empty_value() { .convertKeyValueListToMap(Collections.emptyList()); Assertions.assertThat(actual).containsExactly(); } + + @Test + public void testNormalizeDuration_correct_duration_value() { + List collect = Stream.of("10S", "5s", "PT20.345S") + .map(OpenTelemetryUtil::normalizeDuration).collect(Collectors.toList()); + Assertions.assertThat(collect).containsExactly( + "10000ms", + "5000ms", + "20345ms"); + } + + @Test + public void testNormalizeDuration_duration_wrong_value() { + Assertions.assertThatThrownBy(() -> OpenTelemetryUtil.normalizeDuration("10PTS3")); + } + + @Test + public void testNormalizeProperty_property_as_duration() { + String normalizeProperty = OpenTelemetryUtil.normalizeProperty("quarkus.otel.bsp.schedule.delay", "100S"); + Assertions.assertThat(normalizeProperty).isEqualTo("100000ms"); + } + + @Test + public void testNormalizeProperty_property_without_converter() { + String normalizedProperty = OpenTelemetryUtil.normalizeProperty("quarkus.otel.exporter.otlp.traces.endpoint", + "http://localhost:4317/"); + Assertions.assertThat(normalizedProperty).isEqualTo("http://localhost:4317/"); + } }