7
7
8
8
import static io .opentelemetry .api .incubator .config .DeclarativeConfigProperties .empty ;
9
9
10
- import io .opentelemetry .api .incubator .config .ConfigProvider ;
11
10
import io .opentelemetry .api .incubator .config .DeclarativeConfigProperties ;
12
11
import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
13
12
import java .time .Duration ;
50
49
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
51
50
* at any time.
52
51
*/
53
- public final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
52
+ final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
54
53
55
54
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation." ;
56
- private static final String OTEL_JAVA_AGENT_PREFIX = "otel.javaagent." ;
57
55
58
- private static final Map <String , String > JAVA_MAPPING_RULES = new HashMap <>();
56
+ private final PropertyTranslator translator ;
57
+ @ Nullable private final DeclarativeConfigProperties baseNode ;
59
58
60
- // The node at .instrumentation.java
61
- private final DeclarativeConfigProperties instrumentationJavaNode ;
62
-
63
- static {
64
- JAVA_MAPPING_RULES .put ("otel.instrumentation.common.default-enabled" , "common.default.enabled" );
59
+ static DeclarativeConfigPropertiesBridge fromInstrumentationConfig (
60
+ @ Nullable DeclarativeConfigProperties instrumentationConfig , PropertyTranslator translator ) {
61
+ if (instrumentationConfig == null ) {
62
+ instrumentationConfig = DeclarativeConfigProperties .empty ();
63
+ }
64
+ return new DeclarativeConfigPropertiesBridge (
65
+ instrumentationConfig .getStructured ("java" , empty ()), translator );
65
66
}
66
67
67
- private final Map <String , Object > earlyInitProperties ;
68
+ static DeclarativeConfigPropertiesBridge create (
69
+ @ Nullable DeclarativeConfigProperties node , PropertyTranslator translator ) {
70
+ return new DeclarativeConfigPropertiesBridge (node , translator );
71
+ }
68
72
69
- public DeclarativeConfigPropertiesBridge (
70
- ConfigProvider configProvider , Map <String , Object > earlyInitProperties ) {
71
- this .earlyInitProperties = earlyInitProperties ;
72
- DeclarativeConfigProperties inst = configProvider .getInstrumentationConfig ();
73
- if (inst == null ) {
74
- inst = DeclarativeConfigProperties .empty ();
75
- }
76
- instrumentationJavaNode = inst .getStructured ("java" , empty ());
73
+ private DeclarativeConfigPropertiesBridge (
74
+ @ Nullable DeclarativeConfigProperties baseNode , PropertyTranslator translator ) {
75
+ this .baseNode = baseNode ;
76
+ this .translator = translator ;
77
77
}
78
78
79
79
@ Nullable
80
80
@ Override
81
81
public String getString (String propertyName ) {
82
- Object value = earlyInitProperties .get (propertyName );
82
+ Object value = translator .get (propertyName );
83
83
if (value != null ) {
84
84
return value .toString ();
85
85
}
@@ -89,7 +89,7 @@ public String getString(String propertyName) {
89
89
@ Nullable
90
90
@ Override
91
91
public Boolean getBoolean (String propertyName ) {
92
- Object value = earlyInitProperties .get (propertyName );
92
+ Object value = translator .get (propertyName );
93
93
if (value != null ) {
94
94
return (Boolean ) value ;
95
95
}
@@ -99,42 +99,68 @@ public Boolean getBoolean(String propertyName) {
99
99
@ Nullable
100
100
@ Override
101
101
public Integer getInt (String propertyName ) {
102
+ Object value = translator .get (propertyName );
103
+ if (value != null ) {
104
+ return (Integer ) value ;
105
+ }
102
106
return getPropertyValue (propertyName , DeclarativeConfigProperties ::getInt );
103
107
}
104
108
105
109
@ Nullable
106
110
@ Override
107
111
public Long getLong (String propertyName ) {
112
+ Object value = translator .get (propertyName );
113
+ if (value != null ) {
114
+ return (Long ) value ;
115
+ }
108
116
return getPropertyValue (propertyName , DeclarativeConfigProperties ::getLong );
109
117
}
110
118
111
119
@ Nullable
112
120
@ Override
113
121
public Double getDouble (String propertyName ) {
122
+ Object value = translator .get (propertyName );
123
+ if (value != null ) {
124
+ return (Double ) value ;
125
+ }
114
126
return getPropertyValue (propertyName , DeclarativeConfigProperties ::getDouble );
115
127
}
116
128
117
129
@ Nullable
118
130
@ Override
119
131
public Duration getDuration (String propertyName ) {
132
+ Object value = translator .get (propertyName );
133
+ if (value != null ) {
134
+ return (Duration ) value ;
135
+ }
120
136
Long millis = getPropertyValue (propertyName , DeclarativeConfigProperties ::getLong );
121
137
if (millis == null ) {
122
138
return null ;
123
139
}
124
140
return Duration .ofMillis (millis );
125
141
}
126
142
143
+ @ SuppressWarnings ("unchecked" )
127
144
@ Override
128
145
public List <String > getList (String propertyName ) {
146
+ Object value = translator .get (propertyName );
147
+ if (value != null ) {
148
+ return (List <String >) value ;
149
+ }
129
150
List <String > propertyValue =
130
151
getPropertyValue (
131
152
propertyName ,
132
153
(properties , lastPart ) -> properties .getScalarList (lastPart , String .class ));
133
154
return propertyValue == null ? Collections .emptyList () : propertyValue ;
134
155
}
135
156
157
+ @ SuppressWarnings ("unchecked" )
136
158
@ Override
137
159
public Map <String , String > getMap (String propertyName ) {
160
+ Object fixed = translator .get (propertyName );
161
+ if (fixed != null ) {
162
+ return (Map <String , String >) fixed ;
163
+ }
138
164
DeclarativeConfigProperties propertyValue =
139
165
getPropertyValue (propertyName , DeclarativeConfigProperties ::getStructured );
140
166
if (propertyValue == null ) {
@@ -157,38 +183,41 @@ public Map<String, String> getMap(String propertyName) {
157
183
@ Nullable
158
184
private <T > T getPropertyValue (
159
185
String property , BiFunction <DeclarativeConfigProperties , String , T > extractor ) {
160
- return splitOnDot (getJavaPath (property ), instrumentationJavaNode , extractor );
161
- }
186
+ if (baseNode == null ) {
187
+ return null ;
188
+ }
162
189
163
- private static <T > T splitOnDot (
164
- String path ,
165
- DeclarativeConfigProperties target ,
166
- BiFunction <DeclarativeConfigProperties , String , T > extractor ) {
167
- // Split the remainder of the property on ".", and walk to the N-1 entry
168
- String [] segments = path .split ("\\ ." );
190
+ String [] segments = getSegments (translator .translateProperty (property ));
169
191
if (segments .length == 0 ) {
170
192
return null ;
171
193
}
194
+
195
+ // Extract the value by walking to the N-1 entry
196
+ DeclarativeConfigProperties target = baseNode ;
172
197
if (segments .length > 1 ) {
173
198
for (int i = 0 ; i < segments .length - 1 ; i ++) {
174
199
target = target .getStructured (segments [i ], empty ());
175
200
}
176
201
}
177
202
String lastPart = segments [segments .length - 1 ];
203
+
178
204
return extractor .apply (target , lastPart );
179
205
}
180
206
181
- private static String getJavaPath (String property ) {
182
- String special = JAVA_MAPPING_RULES .get (property );
183
- if (special != null ) {
184
- return special ;
207
+ private static String [] getSegments (String property ) {
208
+ if (property .startsWith (OTEL_INSTRUMENTATION_PREFIX )) {
209
+ property = property .substring (OTEL_INSTRUMENTATION_PREFIX .length ());
185
210
}
211
+ // Split the remainder of the property on "."
212
+ return property .replace ('-' , '_' ).split ("\\ ." );
213
+ }
186
214
187
- if ( property . startsWith ( OTEL_INSTRUMENTATION_PREFIX ) ) {
188
- return property . substring ( OTEL_INSTRUMENTATION_PREFIX . length ()). replace ( '-' , '_' );
189
- } else if (property . startsWith ( OTEL_JAVA_AGENT_PREFIX ) ) {
190
- return "agent. " + property . substring ( OTEL_JAVA_AGENT_PREFIX . length ()). replace ( '-' , '_' );
215
+ static String yamlPath ( String property ) {
216
+ String [] segments = getSegments ( property );
217
+ if (segments . length == 0 ) {
218
+ throw new IllegalArgumentException ( "Invalid property: " + property );
191
219
}
192
- return property ;
220
+
221
+ return "'instrumentation/development' / 'java' / '" + String .join ("' / '" , segments ) + "'" ;
193
222
}
194
223
}
0 commit comments