Skip to content
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 @@ -9,12 +9,14 @@

import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -99,11 +101,77 @@ public Double getDouble(String propertyName) {
@Nullable
@Override
public Duration getDuration(String propertyName) {
Long millis = getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong);
if (millis == null) {
// If this is a raw integer number then assume it is the number of milliseconds
Long millis = getLong(propertyName);
if (millis != null) {
return Duration.ofMillis(millis);
}

// If this is a string than it consists of value and time unit
String value = getString(propertyName);
if (value == null) {
return null;
}
return Duration.ofMillis(millis);
String unitString = getUnitString(value);
String numberString = value.substring(0, value.length() - unitString.length());
try {
long rawNumber = Long.parseLong(numberString.trim());
TimeUnit unit = getDurationUnit(unitString.trim());
return Duration.ofNanos(TimeUnit.NANOSECONDS.convert(rawNumber, unit));
} catch (NumberFormatException ex) {
throw new ConfigurationException(
"Invalid duration property "
+ propertyName
+ "="
+ value
+ ". Expected number, found: "
+ numberString,
ex);
} catch (ConfigurationException ex) {
throw new ConfigurationException(
"Invalid duration property " + propertyName + "=" + value + ". " + ex.getMessage());
}
}

/** Returns the TimeUnit associated with a unit string. Defaults to milliseconds. */
private static TimeUnit getDurationUnit(String unitString) {
switch (unitString) {
case "us":
return TimeUnit.MICROSECONDS;
case "ns":
return TimeUnit.NANOSECONDS;
case "": // Fallthrough expected
case "ms":
return TimeUnit.MILLISECONDS;
case "s":
return TimeUnit.SECONDS;
case "m":
return TimeUnit.MINUTES;
case "h":
return TimeUnit.HOURS;
case "d":
return TimeUnit.DAYS;
default:
throw new ConfigurationException("Invalid duration string, found: " + unitString);
}
}

/**
* Fragments the 'units' portion of a config value from the 'value' portion.
*
* <p>E.g. "1ms" would return the string "ms".
*/
private static String getUnitString(String rawValue) {
int lastDigitIndex = rawValue.length() - 1;
while (lastDigitIndex >= 0) {
char c = rawValue.charAt(lastDigitIndex);
if (Character.isDigit(c)) {
break;
}
lastDigitIndex--;
}
// Pull everything after the last digit.
return rawValue.substring(lastDigitIndex + 1);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ void getProperties() {
assertThat(bridge.getInt("otel.instrumentation.example-instrumentation.int_key")).isEqualTo(1);
assertThat(bridge.getLong("otel.instrumentation.example-instrumentation.int_key"))
.isEqualTo(1L);
assertThat(bridge.getDuration("otel.instrumentation.example-instrumentation.int_key"))
.isEqualTo(Duration.ofMillis(1));
assertThat(bridge.getDuration("otel.instrumentation.example-instrumentation.duration_key1"))
.isEqualTo(Duration.ofMillis(123));
assertThat(bridge.getDuration("otel.instrumentation.example-instrumentation.duration_key2"))
.isEqualTo(Duration.ofNanos(987));
assertThat(bridge.getDouble("otel.instrumentation.example-instrumentation.double_key"))
.isEqualTo(1.1);
assertThat(bridge.getList("otel.instrumentation.example-instrumentation.list_key"))
Expand Down
2 changes: 2 additions & 0 deletions declarative-config-bridge/src/test/resources/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ instrumentation/development:
bool_key: true
int_key: 1
double_key: 1.1
duration_key1: 123
duration_key2: 987ns
list_key:
- value1
- value2
Expand Down