|
| 1 | +--- |
| 2 | +title: Declarative configuration |
| 3 | +weight: 25 |
| 4 | +cSpell:ignore: Customizer Dotel |
| 5 | +--- |
| 6 | + |
| 7 | +Declarative configuration uses the |
| 8 | +[OpenTelemetry declarative configuration schema](/docs/languages/sdk-configuration/declarative-configuration/) |
| 9 | +inside your `application.yaml`. |
| 10 | + |
| 11 | +This approach is useful when: |
| 12 | + |
| 13 | +- You have many configuration options to set |
| 14 | +- You want to use configuration options that are not available with |
| 15 | + `application.properties` or `application.yaml` |
| 16 | +- You want to use the same configuration format as the |
| 17 | + [Java agent](/docs/zero-code/java/agent/declarative-configuration/) |
| 18 | + |
| 19 | +> [!WARNING] |
| 20 | +> |
| 21 | +> Declarative configuration is experimental. |
| 22 | +
|
| 23 | +## Supported versions |
| 24 | + |
| 25 | +Declarative configuration is supported in the **OpenTelemetry Spring Boot |
| 26 | +starter version 2.26.0 and later**. |
| 27 | + |
| 28 | +## Getting started |
| 29 | + |
| 30 | +Add `otel.file_format: "1.0"` (or the current or desired version) to your |
| 31 | +`application.yaml` to opt in to declarative configuration: |
| 32 | + |
| 33 | +```yaml |
| 34 | +otel: |
| 35 | + file_format: '1.0' |
| 36 | + |
| 37 | + resource: |
| 38 | + detection/development: |
| 39 | + detectors: |
| 40 | + - service: |
| 41 | + attributes: |
| 42 | + - name: service.name |
| 43 | + value: my-spring-app |
| 44 | + |
| 45 | + propagator: |
| 46 | + composite: |
| 47 | + - tracecontext: |
| 48 | + - baggage: |
| 49 | + |
| 50 | + tracer_provider: |
| 51 | + processors: |
| 52 | + - batch: |
| 53 | + exporter: |
| 54 | + otlp_http: |
| 55 | + endpoint: ${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:http://localhost:4318/v1/traces} |
| 56 | + |
| 57 | + meter_provider: |
| 58 | + readers: |
| 59 | + - periodic: |
| 60 | + exporter: |
| 61 | + otlp_http: |
| 62 | + endpoint: ${OTEL_EXPORTER_OTLP_METRICS_ENDPOINT:http://localhost:4318/v1/metrics} |
| 63 | + |
| 64 | + logger_provider: |
| 65 | + processors: |
| 66 | + - batch: |
| 67 | + exporter: |
| 68 | + otlp_http: |
| 69 | + endpoint: ${OTEL_EXPORTER_OTLP_LOGS_ENDPOINT:http://localhost:4318/v1/logs} |
| 70 | +``` |
| 71 | +
|
| 72 | +Note that `${VAR:default}` uses a single colon (Spring syntax), not the |
| 73 | +`${VAR:-default}` syntax used in the agent's standalone YAML file. |
| 74 | + |
| 75 | +## Mapping of configuration options |
| 76 | + |
| 77 | +The following rules describe how `application.properties` / `application.yaml` |
| 78 | +configuration options map to their declarative configuration equivalents: |
| 79 | + |
| 80 | +### Instrumentation enable/disable |
| 81 | + |
| 82 | +In declarative configuration, instrumentation enable/disable uses centralized |
| 83 | +lists instead of individual properties. The instrumentation name uses `_` |
| 84 | +(snake_case), not `-` (kebab-case). |
| 85 | + |
| 86 | +| Properties | Declarative Configuration | |
| 87 | +| ----------------------------------------------------- | ------------------------------------------------------------------------------- | |
| 88 | +| `otel.instrumentation.jdbc.enabled=true` | `otel.distribution.spring_starter.instrumentation.enabled: [jdbc]` | |
| 89 | +| `otel.instrumentation.logback-appender.enabled=false` | `otel.distribution.spring_starter.instrumentation.disabled: [logback_appender]` | |
| 90 | +| `otel.instrumentation.common.default-enabled=false` | `otel.distribution.spring_starter.instrumentation.default_enabled: false` | |
| 91 | + |
| 92 | +Example: |
| 93 | + |
| 94 | +```yaml |
| 95 | +otel: |
| 96 | + distribution: |
| 97 | + spring_starter: |
| 98 | + instrumentation: |
| 99 | + default_enabled: false |
| 100 | + enabled: |
| 101 | + - jdbc |
| 102 | + - spring_web |
| 103 | + disabled: |
| 104 | + - logback_appender |
| 105 | +``` |
| 106 | + |
| 107 | +### Instrumentation configuration |
| 108 | + |
| 109 | +Configuration options under `otel.instrumentation.*` (other than enable/disable) |
| 110 | +map to `otel.instrumentation/development.java.*`: |
| 111 | + |
| 112 | +1. Strip the `otel.instrumentation.` prefix |
| 113 | +2. Per segment: replace `-` with `_` |
| 114 | +3. Place under `otel.instrumentation/development.java.` |
| 115 | +4. A `/development` suffix on a key indicates an experimental feature (see the |
| 116 | + `translateName` method in `ConfigPropertiesBackedDeclarativeConfigProperties` |
| 117 | + for the reverse mapping) |
| 118 | + |
| 119 | +For example: |
| 120 | + |
| 121 | +| Properties | Declarative Configuration | |
| 122 | +| ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ | |
| 123 | +| `otel.instrumentation.logback-appender.experimental-log-attributes` | `otel.instrumentation/development.java.logback_appender.experimental_log_attributes/development` | |
| 124 | + |
| 125 | +Some options have special mappings that don't follow the default algorithm: |
| 126 | + |
| 127 | +| Properties | Declarative Configuration | |
| 128 | +| ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | |
| 129 | +| `otel.instrumentation.common.db-statement-sanitizer.enabled` | `otel.instrumentation/development.java.common.database.statement_sanitizer.enabled` | |
| 130 | +| `otel.instrumentation.http.client.capture-request-headers` | `otel.instrumentation/development.general.http.client.request_captured_headers` | |
| 131 | +| `otel.instrumentation.http.client.capture-response-headers` | `otel.instrumentation/development.general.http.client.response_captured_headers` | |
| 132 | +| `otel.instrumentation.http.server.capture-request-headers` | `otel.instrumentation/development.general.http.server.request_captured_headers` | |
| 133 | +| `otel.instrumentation.http.server.capture-response-headers` | `otel.instrumentation/development.general.http.server.response_captured_headers` | |
| 134 | +| `otel.instrumentation.http.client.emit-experimental-telemetry` | `otel.instrumentation/development.java.common.http.client.emit_experimental_telemetry/development` | |
| 135 | +| `otel.instrumentation.http.server.emit-experimental-telemetry` | `otel.instrumentation/development.java.common.http.server.emit_experimental_telemetry/development` | |
| 136 | +| `otel.instrumentation.http.known-methods` | `otel.instrumentation/development.java.common.http.known_methods` | |
| 137 | +| `otel.instrumentation.messaging.experimental.receive-telemetry.enabled` | `otel.instrumentation/development.java.common.messaging.receive_telemetry/development.enabled` | |
| 138 | +| `otel.jmx.enabled` | `otel.instrumentation/development.java.jmx.enabled` | |
| 139 | + |
| 140 | +The `instrumentation/development` section has two top-level groups: |
| 141 | + |
| 142 | +- `general.*` — Cross-language configuration (HTTP headers, semantic convention |
| 143 | + stability) |
| 144 | +- `java.*` — Java-specific instrumentation configuration |
| 145 | + |
| 146 | +### Disable the SDK |
| 147 | + |
| 148 | +| Properties | Declarative Configuration | |
| 149 | +| ------------------------ | ------------------------- | |
| 150 | +| `otel.sdk.disabled=true` | `otel.disabled: true` | |
| 151 | + |
| 152 | +### SDK configuration |
| 153 | + |
| 154 | +SDK-level configuration (exporters, propagators, resources) follows the standard |
| 155 | +[declarative configuration schema](/docs/languages/sdk-configuration/declarative-configuration/) |
| 156 | +directly under `otel:`, as shown in the [Getting started](#getting-started) |
| 157 | +example. |
| 158 | + |
| 159 | +## Differences from agent declarative configuration |
| 160 | + |
| 161 | +| Aspect | Agent | Spring Boot starter | |
| 162 | +| --------------- | -------------------------------------------------------- | ------------------------------------------------------------- | |
| 163 | +| Config location | Separate file (`-Dotel.config.file=...`) | Inside `application.yaml` | |
| 164 | +| Variable syntax | `${VAR:-default}` (double-colon) | `${VAR:default}` (single colon, Spring) | |
| 165 | +| Profiles | Not supported | Spring profiles work normally | |
| 166 | +| Enable/disable | `distribution.javaagent.instrumentation.*` | `distribution.spring_starter.instrumentation.*` | |
| 167 | +| Default-enabled | `distribution.javaagent.instrumentation.default_enabled` | `distribution.spring_starter.instrumentation.default_enabled` | |
| 168 | + |
| 169 | +## Environment variable overrides |
| 170 | + |
| 171 | +Spring's relaxed binding lets you override any part of the declarative |
| 172 | +configuration YAML via environment variables: |
| 173 | + |
| 174 | +```shell |
| 175 | +# Override a scalar under instrumentation/development |
| 176 | +OTEL_INSTRUMENTATION/DEVELOPMENT_JAVA_FOO_STRING_KEY=new_value |
| 177 | +
|
| 178 | +# Override an indexed list element (e.g. exporter endpoint) |
| 179 | +OTEL_TRACER_PROVIDER_PROCESSORS_0_BATCH_EXPORTER_OTLP_HTTP_ENDPOINT=http://custom:4318/v1/traces |
| 180 | +``` |
| 181 | + |
| 182 | +Rules: uppercase, replace `.` with `_`, keep `/` as-is (e.g. |
| 183 | +`INSTRUMENTATION/DEVELOPMENT`), use `_0_`, `_1_` for list indices. |
| 184 | + |
| 185 | +This is a standard Spring feature — it works for any key in `application.yaml`. |
| 186 | + |
| 187 | +## Duration format |
| 188 | + |
| 189 | +Declarative configuration **only supports durations in milliseconds** (e.g. |
| 190 | +`5000` for 5 seconds). You will get an error if you use a duration string like |
| 191 | +`5s`. |
| 192 | + |
| 193 | +## Programmatic configuration |
| 194 | + |
| 195 | +With declarative configuration, `AutoConfigurationCustomizerProvider` (see |
| 196 | +[Programmatic configuration](../programmatic-configuration/)) is replaced by |
| 197 | +`DeclarativeConfigurationCustomizerProvider`. Components such as span exporters |
| 198 | +use the `ComponentProvider` API. See the |
| 199 | +[agent Extension API section](/docs/zero-code/java/agent/declarative-configuration/) |
| 200 | +for details and examples — the same APIs apply to the Spring Boot starter. |
0 commit comments