|
13 | 13 | import io.opentelemetry.instrumentation.spring.autoconfigure.internal.EarlyConfig; |
14 | 14 | import java.util.Iterator; |
15 | 15 | import java.util.Optional; |
| 16 | +import javax.annotation.Nullable; |
16 | 17 | import org.slf4j.ILoggerFactory; |
17 | 18 | import org.slf4j.Logger; |
18 | 19 | import org.slf4j.LoggerFactory; |
@@ -168,11 +169,9 @@ private static void initializeOpenTelemetryAppenderFromProperties( |
168 | 169 | } |
169 | 170 |
|
170 | 171 | String mdcAttributeProperty = |
171 | | - applicationEnvironmentPreparedEvent |
172 | | - .getEnvironment() |
173 | | - .getProperty( |
174 | | - "otel.instrumentation.logback-appender.experimental.capture-mdc-attributes", |
175 | | - String.class); |
| 172 | + getLoggingProperty( |
| 173 | + applicationEnvironmentPreparedEvent.getEnvironment(), |
| 174 | + "otel.instrumentation.logback-appender.experimental.capture-mdc-attributes"); |
176 | 175 | if (mdcAttributeProperty != null) { |
177 | 176 | openTelemetryAppender.setCaptureMdcAttributes(mdcAttributeProperty); |
178 | 177 | } |
@@ -217,48 +216,77 @@ private static void initializeMdcAppenderFromProperties( |
217 | 216 | } |
218 | 217 |
|
219 | 218 | String traceIdKey = |
220 | | - applicationEnvironmentPreparedEvent |
221 | | - .getEnvironment() |
222 | | - .getProperty("otel.instrumentation.common.logging.trace-id", String.class); |
| 219 | + getLoggingProperty( |
| 220 | + applicationEnvironmentPreparedEvent.getEnvironment(), |
| 221 | + "otel.instrumentation.common.logging.trace-id"); |
223 | 222 | if (traceIdKey != null) { |
224 | 223 | openTelemetryAppender.setTraceIdKey(traceIdKey); |
225 | 224 | } |
226 | 225 |
|
227 | 226 | String spanIdKey = |
228 | | - applicationEnvironmentPreparedEvent |
229 | | - .getEnvironment() |
230 | | - .getProperty("otel.instrumentation.common.logging.span-id", String.class); |
| 227 | + getLoggingProperty( |
| 228 | + applicationEnvironmentPreparedEvent.getEnvironment(), |
| 229 | + "otel.instrumentation.common.logging.span-id"); |
231 | 230 | if (spanIdKey != null) { |
232 | 231 | openTelemetryAppender.setSpanIdKey(spanIdKey); |
233 | 232 | } |
234 | 233 |
|
235 | 234 | String traceFlagsKey = |
236 | | - applicationEnvironmentPreparedEvent |
237 | | - .getEnvironment() |
238 | | - .getProperty("otel.instrumentation.common.logging.trace-flags", String.class); |
| 235 | + getLoggingProperty( |
| 236 | + applicationEnvironmentPreparedEvent.getEnvironment(), |
| 237 | + "otel.instrumentation.common.logging.trace-flags"); |
239 | 238 | if (traceFlagsKey != null) { |
240 | 239 | openTelemetryAppender.setTraceFlagsKey(traceFlagsKey); |
241 | 240 | } |
242 | 241 | } |
243 | 242 |
|
| 243 | + @Nullable |
| 244 | + private static String getLoggingProperty(ConfigurableEnvironment environment, String property) { |
| 245 | + return environment.getProperty(getEnvironmentPropertyName(environment, property), String.class); |
| 246 | + } |
| 247 | + |
244 | 248 | /** Evaluates a boolean property, taking into account whether declarative config is in use. */ |
| 249 | + @Nullable |
245 | 250 | private static Boolean evaluateBooleanProperty( |
246 | 251 | ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent, String property) { |
247 | 252 | ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getEnvironment(); |
248 | | - String key = property; |
| 253 | + return environment.getProperty( |
| 254 | + getEnvironmentPropertyName(environment, property), Boolean.class); |
| 255 | + } |
| 256 | + |
| 257 | + private static String getEnvironmentPropertyName( |
| 258 | + ConfigurableEnvironment environment, String property) { |
249 | 259 | if (EarlyConfig.isDeclarativeConfig(environment)) { |
250 | 260 | if (property.startsWith("otel.instrumentation.")) { |
251 | | - key = |
252 | | - String.format( |
253 | | - "otel.instrumentation/development.java.%s", |
254 | | - property.substring("otel.instrumentation.".length())) |
255 | | - .replace('-', '_'); |
| 261 | + return "otel.instrumentation/development.java." |
| 262 | + + toDeclarativeInstrumentationPropertyName( |
| 263 | + property.substring("otel.instrumentation.".length())); |
256 | 264 | } else { |
257 | 265 | throw new IllegalStateException( |
258 | 266 | "No mapping found for property name: " + property + ". Please report this bug."); |
259 | 267 | } |
260 | 268 | } |
261 | | - return environment.getProperty(key, Boolean.class); |
| 269 | + return property; |
| 270 | + } |
| 271 | + |
| 272 | + private static String toDeclarativeInstrumentationPropertyName(String instrumentationProperty) { |
| 273 | + StringBuilder declarativeProperty = new StringBuilder(); |
| 274 | + boolean nextSegmentIsDevelopment = false; |
| 275 | + for (String segment : instrumentationProperty.split("\\.")) { |
| 276 | + if (segment.equals("experimental")) { |
| 277 | + nextSegmentIsDevelopment = true; |
| 278 | + continue; |
| 279 | + } |
| 280 | + if (declarativeProperty.length() > 0) { |
| 281 | + declarativeProperty.append('.'); |
| 282 | + } |
| 283 | + declarativeProperty.append(segment.replace('-', '_')); |
| 284 | + if (nextSegmentIsDevelopment || segment.startsWith("experimental-")) { |
| 285 | + declarativeProperty.append("/development"); |
| 286 | + nextSegmentIsDevelopment = false; |
| 287 | + } |
| 288 | + } |
| 289 | + return declarativeProperty.toString(); |
262 | 290 | } |
263 | 291 |
|
264 | 292 | private static <T> Optional<T> findAppender(Class<T> appenderClass) { |
|
0 commit comments