From afbbaced4182f9fdfbf0a42a9c7ebd6f6c20b51e Mon Sep 17 00:00:00 2001
From: Georgios Andrianakis <geoand@gmail.com>
Date: Tue, 1 Aug 2023 15:23:47 +0300
Subject: [PATCH] Convert duration properties into a format OTel can parse

Fixes: #33381
---
 .../exporter/otlp/OtlpExporterConfigTest.java |  2 +-
 .../runtime/OpenTelemetryRecorder.java        | 27 ++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/exporter/otlp/OtlpExporterConfigTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/exporter/otlp/OtlpExporterConfigTest.java
index 498be3ade7c9d8..26a3eb5f277ec5 100644
--- a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/exporter/otlp/OtlpExporterConfigTest.java
+++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/exporter/otlp/OtlpExporterConfigTest.java
@@ -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;
diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java
index dab2f72463113f..41ee2ec6b1c431 100644
--- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java
+++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java
@@ -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;
@@ -19,6 +20,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;
@@ -93,12 +95,35 @@ 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")) {
+                    Duration duration;
+                    try {
+                        duration = DurationConverter.parseDuration(configValue.getValue());
+                    } 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();
+            }
         };
     }