|
5 | 5 |
|
6 | 6 | package io.opentelemetry.javaagent.tooling; |
7 | 7 |
|
| 8 | +import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty; |
| 9 | + |
8 | 10 | import io.opentelemetry.api.incubator.config.ConfigProvider; |
| 11 | +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
9 | 12 | import io.opentelemetry.instrumentation.config.bridge.DeclarativeConfigPropertiesBridgeBuilder; |
10 | 13 | import io.opentelemetry.javaagent.bootstrap.OpenTelemetrySdkAccess; |
11 | 14 | import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig; |
12 | 15 | import io.opentelemetry.sdk.OpenTelemetrySdk; |
13 | 16 | import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
14 | 17 | import io.opentelemetry.sdk.autoconfigure.SdkAutoconfigureAccess; |
15 | 18 | import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil; |
| 19 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 20 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
16 | 21 | import io.opentelemetry.sdk.common.CompletableResultCode; |
17 | 22 | import java.util.Arrays; |
18 | 23 |
|
@@ -44,22 +49,49 @@ public static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk( |
44 | 49 | return SdkAutoconfigureAccess.create( |
45 | 50 | sdk, |
46 | 51 | SdkAutoconfigureAccess.getResource(autoConfiguredSdk), |
47 | | - new DeclarativeConfigPropertiesBridgeBuilder() |
48 | | - .addMapping("otel.javaagent", "agent") |
49 | | - // these properties are used to initialize the SDK before the configuration file |
50 | | - // is loaded for consistency, we pass them to the bridge, so that they can be read |
51 | | - // later with the same value from the {@link DeclarativeConfigPropertiesBridge} |
52 | | - .addOverride( |
53 | | - "otel.javaagent.debug", earlyConfig.getBoolean("otel.javaagent.debug", false)) |
54 | | - .addOverride( |
55 | | - "otel.javaagent.logging", earlyConfig.getString("otel.javaagent.logging")) |
56 | | - .buildFromInstrumentationConfig(configProvider.getInstrumentationConfig()), |
| 52 | + getDeclarativeConfigBridgedProperties(earlyConfig, configProvider), |
57 | 53 | configProvider); |
58 | 54 | } |
59 | 55 |
|
60 | 56 | return autoConfiguredSdk; |
61 | 57 | } |
62 | 58 |
|
| 59 | + // Visible for testing |
| 60 | + static ConfigProperties getDeclarativeConfigBridgedProperties( |
| 61 | + EarlyInitAgentConfig earlyConfig, ConfigProvider configProvider) { |
| 62 | + return new DeclarativeConfigPropertiesBridgeBuilder() |
| 63 | + .addMapping("otel.javaagent", "agent") |
| 64 | + .addOverride("otel.instrumentation.common.default-enabled", defaultEnabled(configProvider)) |
| 65 | + // these properties are used to initialize the SDK before the configuration file |
| 66 | + // is loaded for consistency, we pass them to the bridge, so that they can be read |
| 67 | + // later with the same value from the {@link DeclarativeConfigPropertiesBridge} |
| 68 | + .addOverride("otel.javaagent.debug", earlyConfig.getBoolean("otel.javaagent.debug", false)) |
| 69 | + .addOverride("otel.javaagent.logging", earlyConfig.getString("otel.javaagent.logging")) |
| 70 | + .buildFromInstrumentationConfig(configProvider.getInstrumentationConfig()); |
| 71 | + } |
| 72 | + |
| 73 | + private static boolean defaultEnabled(ConfigProvider configProvider) { |
| 74 | + DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig(); |
| 75 | + if (instrumentationConfig == null) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + String profile = |
| 80 | + instrumentationConfig |
| 81 | + .getStructured("java", empty()) |
| 82 | + .getStructured("agent", empty()) |
| 83 | + .getString("instrumentation_mode", "default"); |
| 84 | + |
| 85 | + switch (profile) { |
| 86 | + case "none": |
| 87 | + return false; |
| 88 | + case "default": |
| 89 | + return true; |
| 90 | + default: |
| 91 | + throw new ConfigurationException("Unknown instrumentation profile: " + profile); |
| 92 | + } |
| 93 | + } |
| 94 | + |
63 | 95 | private static void setForceFlush(OpenTelemetrySdk sdk) { |
64 | 96 | OpenTelemetrySdkAccess.internalSetForceFlush( |
65 | 97 | (timeout, unit) -> { |
|
0 commit comments