-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarativeConfigPropertiesApiBridge.java
More file actions
147 lines (128 loc) · 4.61 KB
/
DeclarativeConfigPropertiesApiBridge.java
File metadata and controls
147 lines (128 loc) · 4.61 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.config.bridge;
import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
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.function.BiFunction;
import javax.annotation.Nullable;
public class DeclarativeConfigPropertiesApiBridge {
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
protected final DeclarativeConfigProperties baseNode;
// lookup order matters - we choose the first match
protected final Map<String, String> mappings;
protected final Map<String, Object> overrideValues;
public DeclarativeConfigPropertiesApiBridge(
DeclarativeConfigProperties baseNode,
Map<String, String> mappings,
Map<String, Object> overrideValues) {
this.baseNode = Objects.requireNonNull(baseNode);
this.mappings = mappings;
this.overrideValues = overrideValues;
}
@Nullable
public String getString(String propertyName) {
return getPropertyValue(propertyName, String.class, DeclarativeConfigProperties::getString);
}
@Nullable
public Boolean getBoolean(String propertyName) {
return getPropertyValue(propertyName, Boolean.class, DeclarativeConfigProperties::getBoolean);
}
@Nullable
public Integer getInt(String propertyName) {
return getPropertyValue(propertyName, Integer.class, DeclarativeConfigProperties::getInt);
}
@Nullable
public Long getLong(String propertyName) {
return getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong);
}
@Nullable
public Double getDouble(String propertyName) {
return getPropertyValue(propertyName, Double.class, DeclarativeConfigProperties::getDouble);
}
@Nullable
public Duration getDuration(String propertyName) {
Long millis = getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong);
if (millis == null) {
return null;
}
return Duration.ofMillis(millis);
}
@SuppressWarnings("unchecked")
public List<String> getList(String propertyName) {
List<String> propertyValue =
getPropertyValue(
propertyName,
List.class,
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
return propertyValue == null ? Collections.emptyList() : propertyValue;
}
@SuppressWarnings("unchecked")
public Map<String, String> getMap(String propertyName) {
DeclarativeConfigProperties propertyValue =
getPropertyValue(
propertyName,
DeclarativeConfigProperties.class,
DeclarativeConfigProperties::getStructured);
if (propertyValue == null) {
return Collections.emptyMap();
}
Map<String, String> result = new HashMap<>();
propertyValue
.getPropertyKeys()
.forEach(
key -> {
String value = propertyValue.getString(key);
if (value == null) {
return;
}
result.put(key, value);
});
return Collections.unmodifiableMap(result);
}
@Nullable
private <T> T getPropertyValue(
String property,
Class<T> clazz,
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
T override = clazz.cast(overrideValues.get(property));
if (override != null) {
return override;
}
String[] segments = getSegments(translateProperty(property));
if (segments.length == 0) {
return null;
}
// Extract the value by walking to the N-1 entry
DeclarativeConfigProperties target = baseNode;
if (segments.length > 1) {
for (int i = 0; i < segments.length - 1; i++) {
target = target.getStructured(segments[i], empty());
}
}
String lastPart = segments[segments.length - 1];
return extractor.apply(target, lastPart);
}
static String[] getSegments(String property) {
if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
}
// Split the remainder of the property on "."
return property.replace('-', '_').split("\\.");
}
private String translateProperty(String property) {
for (Map.Entry<String, String> entry : mappings.entrySet()) {
if (property.startsWith(entry.getKey())) {
return entry.getValue() + property.substring(entry.getKey().length());
}
}
return property;
}
}