|
5 | 5 |
|
6 | 6 | package io.opentelemetry.contrib.sdk.autoconfigure;
|
7 | 7 |
|
| 8 | +import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty; |
| 9 | + |
8 | 10 | import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
|
9 | 11 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
|
10 |
| - |
11 |
| -import javax.annotation.Nullable; |
12 | 12 | import java.time.Duration;
|
13 | 13 | import java.util.Collections;
|
14 | 14 | import java.util.HashMap;
|
15 | 15 | import java.util.List;
|
16 | 16 | import java.util.Map;
|
17 | 17 | import java.util.function.BiFunction;
|
18 |
| - |
19 |
| -import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty; |
| 18 | +import javax.annotation.Nullable; |
20 | 19 |
|
21 | 20 | /**
|
22 | 21 | * A {@link ConfigProperties} which resolves properties based on {@link
|
|
49 | 48 | */
|
50 | 49 | final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
|
51 | 50 |
|
52 |
| - private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation."; |
53 |
| - |
54 |
| - // The node at .instrumentation.java |
55 |
| - private final DeclarativeConfigProperties instrumentationJavaNode; |
56 |
| - |
57 |
| - DeclarativeConfigPropertiesBridge(DeclarativeConfigProperties instrumentationNode) { |
58 |
| - instrumentationJavaNode = instrumentationNode.getStructured("java", empty()); |
| 51 | + private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation."; |
| 52 | + |
| 53 | + // The node at .instrumentation.java |
| 54 | + private final DeclarativeConfigProperties instrumentationJavaNode; |
| 55 | + |
| 56 | + DeclarativeConfigPropertiesBridge(DeclarativeConfigProperties instrumentationNode) { |
| 57 | + instrumentationJavaNode = instrumentationNode.getStructured("java", empty()); |
| 58 | + } |
| 59 | + |
| 60 | + @Nullable |
| 61 | + @Override |
| 62 | + public String getString(String propertyName) { |
| 63 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getString); |
| 64 | + } |
| 65 | + |
| 66 | + @Nullable |
| 67 | + @Override |
| 68 | + public Boolean getBoolean(String propertyName) { |
| 69 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean); |
| 70 | + } |
| 71 | + |
| 72 | + @Nullable |
| 73 | + @Override |
| 74 | + public Integer getInt(String propertyName) { |
| 75 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt); |
| 76 | + } |
| 77 | + |
| 78 | + @Nullable |
| 79 | + @Override |
| 80 | + public Long getLong(String propertyName) { |
| 81 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
| 82 | + } |
| 83 | + |
| 84 | + @Nullable |
| 85 | + @Override |
| 86 | + public Double getDouble(String propertyName) { |
| 87 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble); |
| 88 | + } |
| 89 | + |
| 90 | + @Nullable |
| 91 | + @Override |
| 92 | + public Duration getDuration(String propertyName) { |
| 93 | + Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
| 94 | + if (millis == null) { |
| 95 | + return null; |
59 | 96 | }
|
60 |
| - |
61 |
| - @Nullable |
62 |
| - @Override |
63 |
| - public String getString(String propertyName) { |
64 |
| - return getPropertyValue(propertyName, DeclarativeConfigProperties::getString); |
| 97 | + return Duration.ofMillis(millis); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public List<String> getList(String propertyName) { |
| 102 | + List<String> propertyValue = |
| 103 | + getPropertyValue( |
| 104 | + propertyName, |
| 105 | + (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); |
| 106 | + return propertyValue == null ? Collections.emptyList() : propertyValue; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Map<String, String> getMap(String propertyName) { |
| 111 | + DeclarativeConfigProperties propertyValue = |
| 112 | + getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured); |
| 113 | + if (propertyValue == null) { |
| 114 | + return Collections.emptyMap(); |
65 | 115 | }
|
66 |
| - |
67 |
| - @Nullable |
68 |
| - @Override |
69 |
| - public Boolean getBoolean(String propertyName) { |
70 |
| - return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean); |
| 116 | + Map<String, String> result = new HashMap<>(); |
| 117 | + propertyValue |
| 118 | + .getPropertyKeys() |
| 119 | + .forEach( |
| 120 | + key -> { |
| 121 | + String value = propertyValue.getString(key); |
| 122 | + if (value == null) { |
| 123 | + return; |
| 124 | + } |
| 125 | + result.put(key, value); |
| 126 | + }); |
| 127 | + return Collections.unmodifiableMap(result); |
| 128 | + } |
| 129 | + |
| 130 | + @Nullable |
| 131 | + private <T> T getPropertyValue( |
| 132 | + String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) { |
| 133 | + if (instrumentationJavaNode == null) { |
| 134 | + return null; |
71 | 135 | }
|
72 | 136 |
|
73 |
| - @Nullable |
74 |
| - @Override |
75 |
| - public Integer getInt(String propertyName) { |
76 |
| - return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt); |
| 137 | + String[] segments = getSegments(property); |
| 138 | + if (segments.length == 0) { |
| 139 | + return null; |
77 | 140 | }
|
78 | 141 |
|
79 |
| - @Nullable |
80 |
| - @Override |
81 |
| - public Long getLong(String propertyName) { |
82 |
| - return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
| 142 | + // Extract the value by walking to the N-1 entry |
| 143 | + DeclarativeConfigProperties target = instrumentationJavaNode; |
| 144 | + if (segments.length > 1) { |
| 145 | + for (int i = 0; i < segments.length - 1; i++) { |
| 146 | + target = target.getStructured(segments[i], empty()); |
| 147 | + } |
83 | 148 | }
|
| 149 | + String lastPart = segments[segments.length - 1]; |
84 | 150 |
|
85 |
| - @Nullable |
86 |
| - @Override |
87 |
| - public Double getDouble(String propertyName) { |
88 |
| - return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble); |
89 |
| - } |
90 |
| - |
91 |
| - @Nullable |
92 |
| - @Override |
93 |
| - public Duration getDuration(String propertyName) { |
94 |
| - Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
95 |
| - if (millis == null) { |
96 |
| - return null; |
97 |
| - } |
98 |
| - return Duration.ofMillis(millis); |
99 |
| - } |
100 |
| - |
101 |
| - @Override |
102 |
| - public List<String> getList(String propertyName) { |
103 |
| - List<String> propertyValue = |
104 |
| - getPropertyValue( |
105 |
| - propertyName, |
106 |
| - (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); |
107 |
| - return propertyValue == null ? Collections.emptyList() : propertyValue; |
108 |
| - } |
| 151 | + return extractor.apply(target, lastPart); |
| 152 | + } |
109 | 153 |
|
110 |
| - @Override |
111 |
| - public Map<String, String> getMap(String propertyName) { |
112 |
| - DeclarativeConfigProperties propertyValue = |
113 |
| - getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured); |
114 |
| - if (propertyValue == null) { |
115 |
| - return Collections.emptyMap(); |
116 |
| - } |
117 |
| - Map<String, String> result = new HashMap<>(); |
118 |
| - propertyValue |
119 |
| - .getPropertyKeys() |
120 |
| - .forEach( |
121 |
| - key -> { |
122 |
| - String value = propertyValue.getString(key); |
123 |
| - if (value == null) { |
124 |
| - return; |
125 |
| - } |
126 |
| - result.put(key, value); |
127 |
| - }); |
128 |
| - return Collections.unmodifiableMap(result); |
| 154 | + private static String[] getSegments(String property) { |
| 155 | + if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) { |
| 156 | + property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length()); |
129 | 157 | }
|
130 |
| - |
131 |
| - @Nullable |
132 |
| - private <T> T getPropertyValue( |
133 |
| - String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) { |
134 |
| - if (instrumentationJavaNode == null) { |
135 |
| - return null; |
136 |
| - } |
137 |
| - |
138 |
| - String[] segments = getSegments(property); |
139 |
| - if (segments.length == 0) { |
140 |
| - return null; |
141 |
| - } |
142 |
| - |
143 |
| - // Extract the value by walking to the N-1 entry |
144 |
| - DeclarativeConfigProperties target = instrumentationJavaNode; |
145 |
| - if (segments.length > 1) { |
146 |
| - for (int i = 0; i < segments.length - 1; i++) { |
147 |
| - target = target.getStructured(segments[i], empty()); |
148 |
| - } |
149 |
| - } |
150 |
| - String lastPart = segments[segments.length - 1]; |
151 |
| - |
152 |
| - return extractor.apply(target, lastPart); |
153 |
| - } |
154 |
| - |
155 |
| - private static String[] getSegments(String property) { |
156 |
| - if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) { |
157 |
| - property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length()); |
158 |
| - } |
159 |
| - // Split the remainder of the property on "." |
160 |
| - return property.split("\\."); |
| 158 | + // Split the remainder of the property on "." |
| 159 | + return property.split("\\."); |
| 160 | + } |
| 161 | + |
| 162 | + static String yamlPath(String property) { |
| 163 | + String[] segments = getSegments(property); |
| 164 | + if (segments.length == 0) { |
| 165 | + throw new IllegalArgumentException("Invalid property: " + property); |
161 | 166 | }
|
162 | 167 |
|
163 |
| - static String yamlPath(String property) { |
164 |
| - String[] segments = getSegments(property); |
165 |
| - if (segments.length == 0) { |
166 |
| - throw new IllegalArgumentException("Invalid property: " + property); |
167 |
| - } |
168 |
| - |
169 |
| - return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'"; |
170 |
| - } |
| 168 | + return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'"; |
| 169 | + } |
171 | 170 | }
|
0 commit comments