|
9 | 9 |
|
10 | 10 | import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
11 | 11 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 12 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
12 | 13 | import java.time.Duration; |
13 | 14 | import java.util.Collections; |
14 | 15 | import java.util.HashMap; |
15 | 16 | import java.util.List; |
16 | 17 | import java.util.Map; |
17 | 18 | import java.util.Objects; |
| 19 | +import java.util.concurrent.TimeUnit; |
18 | 20 | import java.util.function.BiFunction; |
19 | 21 | import javax.annotation.Nullable; |
20 | 22 |
|
@@ -99,11 +101,77 @@ public Double getDouble(String propertyName) { |
99 | 101 | @Nullable |
100 | 102 | @Override |
101 | 103 | 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) { |
104 | 113 | return null; |
105 | 114 | } |
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); |
107 | 175 | } |
108 | 176 |
|
109 | 177 | @SuppressWarnings("unchecked") |
|
0 commit comments