-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarativeConfigPropertiesDurationUtil.java
More file actions
53 lines (46 loc) · 1.87 KB
/
Copy pathDeclarativeConfigPropertiesDurationUtil.java
File metadata and controls
53 lines (46 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.config.bridge;
import static java.util.Collections.singletonMap;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import java.time.Duration;
import javax.annotation.Nullable;
/**
* Helpers for reading duration values from {@link DeclarativeConfigProperties}.
*
* <p>When the config is backed by flat {@link
* io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties}, duration strings such as {@code 42ms}
* are supported by delegating to the SDK's standard duration parser.
*
* <p>For other declarative-config implementations, this utility expects duration values to already
* be normalized to integer milliseconds, which means string durations in declarative YAML are not
* accepted.
*/
public final class DeclarativeConfigPropertiesDurationUtil {
private DeclarativeConfigPropertiesDurationUtil() {}
/**
* Reads a duration from declarative config.
*
* <p>String duration values are only supported when {@code properties} is a {@link
* ConfigPropertiesBackedDeclarativeConfigProperties}. Other implementations must provide integer
* milliseconds for the same key.
*/
@Nullable
public static Duration parseDuration(DeclarativeConfigProperties properties, String key) {
if (properties instanceof ConfigPropertiesBackedDeclarativeConfigProperties) {
String rawValue = properties.getString(key);
if (rawValue == null || rawValue.isEmpty()) {
return null;
}
return DefaultConfigProperties.createFromMap(singletonMap(key, rawValue)).getDuration(key);
}
Long rawLongValue = properties.getLong(key);
if (rawLongValue == null) {
return null;
}
return Duration.ofMillis(rawLongValue);
}
}