Skip to content

Commit b7d60fb

Browse files
committed
Handling unit in duration property
1 parent aa3bd49 commit b7d60fb

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

declarative-config-bridge/src/main/java/io/opentelemetry/instrumentation/config/bridge/DeclarativeConfigPropertiesBridge.java

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
1111
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
12+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
1213
import java.time.Duration;
1314
import java.util.Collections;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Objects;
19+
import java.util.concurrent.TimeUnit;
1820
import java.util.function.BiFunction;
1921
import javax.annotation.Nullable;
2022

@@ -99,11 +101,77 @@ public Double getDouble(String propertyName) {
99101
@Nullable
100102
@Override
101103
public Duration getDuration(String propertyName) {
102-
Long millis = getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong);
103-
if (millis == null) {
104+
// If this is a raw integer number then assume it is the number of milliseconds
105+
Long millis = getLong(propertyName);
106+
if (millis != null) {
107+
return Duration.ofMillis(millis);
108+
}
109+
110+
// If this is a string than it consists of value and time unit
111+
String value = getString(propertyName);
112+
if (value == null) {
104113
return null;
105114
}
106-
return Duration.ofMillis(millis);
115+
String unitString = getUnitString(value);
116+
String numberString = value.substring(0, value.length() - unitString.length());
117+
try {
118+
long rawNumber = Long.parseLong(numberString.trim());
119+
TimeUnit unit = getDurationUnit(unitString.trim());
120+
return Duration.ofNanos(TimeUnit.NANOSECONDS.convert(rawNumber, unit));
121+
} catch (NumberFormatException ex) {
122+
throw new ConfigurationException(
123+
"Invalid duration property "
124+
+ propertyName
125+
+ "="
126+
+ value
127+
+ ". Expected number, found: "
128+
+ numberString,
129+
ex);
130+
} catch (ConfigurationException ex) {
131+
throw new ConfigurationException(
132+
"Invalid duration property " + propertyName + "=" + value + ". " + ex.getMessage());
133+
}
134+
}
135+
136+
/** Returns the TimeUnit associated with a unit string. Defaults to milliseconds. */
137+
private static TimeUnit getDurationUnit(String unitString) {
138+
switch (unitString) {
139+
case "us":
140+
return TimeUnit.MICROSECONDS;
141+
case "ns":
142+
return TimeUnit.NANOSECONDS;
143+
case "": // Fallthrough expected
144+
case "ms":
145+
return TimeUnit.MILLISECONDS;
146+
case "s":
147+
return TimeUnit.SECONDS;
148+
case "m":
149+
return TimeUnit.MINUTES;
150+
case "h":
151+
return TimeUnit.HOURS;
152+
case "d":
153+
return TimeUnit.DAYS;
154+
default:
155+
throw new ConfigurationException("Invalid duration string, found: " + unitString);
156+
}
157+
}
158+
159+
/**
160+
* Fragments the 'units' portion of a config value from the 'value' portion.
161+
*
162+
* <p>E.g. "1ms" would return the string "ms".
163+
*/
164+
private static String getUnitString(String rawValue) {
165+
int lastDigitIndex = rawValue.length() - 1;
166+
while (lastDigitIndex >= 0) {
167+
char c = rawValue.charAt(lastDigitIndex);
168+
if (Character.isDigit(c)) {
169+
break;
170+
}
171+
lastDigitIndex--;
172+
}
173+
// Pull everything after the last digit.
174+
return rawValue.substring(lastDigitIndex + 1);
107175
}
108176

109177
@SuppressWarnings("unchecked")

declarative-config-bridge/src/test/java/io/opentelemetry/instrumentation/config/bridge/DeclarativeConfigPropertiesBridgeTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ void getProperties() {
7272
assertThat(bridge.getInt("otel.instrumentation.example-instrumentation.int_key")).isEqualTo(1);
7373
assertThat(bridge.getLong("otel.instrumentation.example-instrumentation.int_key"))
7474
.isEqualTo(1L);
75-
assertThat(bridge.getDuration("otel.instrumentation.example-instrumentation.int_key"))
76-
.isEqualTo(Duration.ofMillis(1));
75+
assertThat(bridge.getDuration("otel.instrumentation.example-instrumentation.duration_key1"))
76+
.isEqualTo(Duration.ofMillis(123));
77+
assertThat(bridge.getDuration("otel.instrumentation.example-instrumentation.duration_key2"))
78+
.isEqualTo(Duration.ofNanos(987));
7779
assertThat(bridge.getDouble("otel.instrumentation.example-instrumentation.double_key"))
7880
.isEqualTo(1.1);
7981
assertThat(bridge.getList("otel.instrumentation.example-instrumentation.list_key"))

declarative-config-bridge/src/test/resources/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ instrumentation/development:
1414
bool_key: true
1515
int_key: 1
1616
double_key: 1.1
17+
duration_key1: 123
18+
duration_key2: 987ns
1719
list_key:
1820
- value1
1921
- value2

0 commit comments

Comments
 (0)