Skip to content

Commit

Permalink
Merge pull request #35139 from geoand/#33381
Browse files Browse the repository at this point in the history
Convert duration properties into a format OTel can parse
  • Loading branch information
geoand authored Aug 2, 2023
2 parents c07c61e + 2014dc9 commit f78c8a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DurationConverter implements Converter<Duration>, Serializable {
private static final long serialVersionUID = 7499347081928776532L;
private static final String PERIOD = "P";
private static final String PERIOD_OF_TIME = "PT";
private static final Pattern DIGITS = Pattern.compile("^[-+]?\\d+$");
public static final Pattern DIGITS = Pattern.compile("^[-+]?\\d+$");
private static final Pattern DIGITS_AND_UNIT = Pattern.compile("^[-+]?\\d+(?:\\.\\d+)?(?i)[hms]$");
private static final Pattern DAYS = Pattern.compile("^[-+]?\\d+(?i)d$");
private static final Pattern MILLIS = Pattern.compile("^[-+]?\\d+(?i)ms$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class OtlpExporterConfigTest {
.overrideConfigKey("otel.exporter.otlp.traces.protocol", "http/protobuf")
.overrideConfigKey("quarkus.opentelemetry.tracer.exporter.otlp.endpoint", "http://localhost ")
.overrideConfigKey("quarkus.otel.bsp.schedule.delay", "50")
.overrideConfigKey("quarkus.otel.bsp.export.timeout", "1s");
.overrideConfigKey("quarkus.otel.bsp.export.timeout", "PT1S");

@Inject
OtlpExporterRuntimeConfig config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.opentelemetry.runtime;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -18,6 +19,7 @@
import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.runtime.configuration.DurationConverter;
import io.smallrye.config.ConfigValue;
import io.smallrye.config.NameIterator;
import io.smallrye.config.SmallRyeConfig;
Expand Down Expand Up @@ -91,12 +93,41 @@ 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());
oTelConfigs.put(name.getName().substring(name.getPosition() + 1), getValue(configValue));
}
}
}
return oTelConfigs;
}

/**
* Transforms the value to what OTel expects
* TODO: this is super simplistic, and should be more modular if needed
*/
private String getValue(ConfigValue configValue) {
String name = configValue.getName();
if (name.endsWith("timeout") || name.endsWith("delay")) {
String value = configValue.getValue();
if (DurationConverter.DIGITS.asPredicate().test(value)) {
// OTel regards values without a unit to me milliseconds instead of seconds
// that java.time.Duration assumes, so let's just not do any conversion and let OTel handle with it
return value;
}
Duration duration;
try {
duration = DurationConverter.parseDuration(value);
} catch (Exception ignored) {
// it's not a Duration, so we can't do much
return configValue.getValue();
}
try {
return duration.toMillis() + "ms";
} catch (Exception ignored) {
return duration.toSeconds() + "s";
}
}
return configValue.getValue();
}
};
}

Expand Down

0 comments on commit f78c8a9

Please sign in to comment.