Skip to content

Commit

Permalink
Normalize configuration properties value before send to Otel
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Jun 10, 2023
1 parent 55fb3b2 commit 84b2225
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ private Map<String, String> 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;
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
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 {
public static final String TRACE_ID = "traceId";
public static final String SPAN_ID = "spanId";
public static final String SAMPLED = "sampled";
public static final String PARENT_ID = "parentId";
public static final Map<String, Function<String, String>> 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() {
}
Expand Down Expand Up @@ -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<String, String> converter = OpenTelemetryUtil.OTEL_PROPERTIES_NORMALIZABLE.get(propertyName);
if (converter == null) {
return propertyValue;
} else {
return converter.apply(propertyValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,4 +57,32 @@ public void testConvertKeyValueListToMap_empty_value() {
.convertKeyValueListToMap(Collections.emptyList());
Assertions.assertThat(actual).containsExactly();
}

@Test
public void testNormalizeDuration_correct_duration_value() {
List<String> 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/");
}
}

0 comments on commit 84b2225

Please sign in to comment.