Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert duration properties before send to OTel #33916

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ 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
void testNormalizeProperty_property_without_time_unit() {
String normalizeProperty = OpenTelemetryUtil.normalizeProperty("quarkus.otel.bsp.schedule.delay", "50");
Assertions.assertThat(normalizeProperty).isEqualTo("50000ms");
}

@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/");
}
}
Loading