You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example demonstrates how to use [declarative configuration](https://opentelemetry.io/docs/specs/otel/configuration/#declarative-configuration) with the OpenTelemetry Spring Boot Starter (requires version 2.22.0+). Declarative configuration allows you to control tracing and metrics behavior using a YAML file, without code changes.
3
+
This example demonstrates how to use [declarative configuration](https://opentelemetry.io/docs/specs/otel/configuration/#declarative-configuration)
4
+
with the OpenTelemetry Spring Boot Starter to configure tracing, metrics, and logging for a Spring Boot application.
4
5
5
-
The configuration file is located at [`application.yaml`](./application.yaml).
6
+
Instead of using the OpenTelemetry Java Agent and an `otel-agent-config.yaml` file, this module uses the
7
+
[OpenTelemetry Spring Boot Starter](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/spring/spring-boot-autoconfigure)
8
+
and configures declarative behavior via standard Spring Boot configuration in `application.yaml`.
9
+
10
+
> **Requirement:** Declarative configuration via the Spring Boot Starter requires version **2.22.0 or newer** of the
- Read declarative configuration under the `otel:` root in `application.yaml`
58
+
- Apply exporters, processors, and sampler rules defined there
59
+
38
60
### Step 3: Test the Endpoints
39
61
40
-
Open a new terminal and test both endpoints:
62
+
In a separate terminal, call both endpoints:
41
63
42
64
```bash
43
-
# This endpoint will NOT be traced (excluded by configuration)
65
+
# This endpoint will NOT be traced (excluded by declarative configuration)
44
66
curl http://localhost:8080/actuator/health
45
67
46
68
# This endpoint WILL be traced normally
47
69
curl http://localhost:8080/api/example
48
70
```
49
71
50
-
### Step 4: Verify Tracing Behavior
72
+
### Step 4: Verify Tracing, Metrics, and Logs
73
+
74
+
By default, this example configures:
51
75
52
-
Check your tracing backend or logs to see:
53
-
- Health check requests (`/actuator/health`) should NOT generate traces (excluded by configuration)
54
-
- API requests (`/api/example`) should generate traces
76
+
- An OTLP HTTP exporter for traces, metrics, and logs
77
+
- A console exporter for traces for easy local inspection
55
78
56
-
## Declarative Configuration
79
+
Check the application logs to see that:
57
80
58
-
Declarative configuration is enabled by setting the `file_format` property in your `application.yaml` under the `otel:` root node. All configuration must be indented under `otel:` for Spring Boot.
81
+
- Health check requests (`/actuator/health`) **do not** generate spans (dropped by sampler rules)
82
+
- Requests to `/api/example`**do** generate spans and are exported to console and/or OTLP
59
83
60
-
See [OpenTelemetry Configuration Schema and Examples](https://github.com/open-telemetry/opentelemetry-configuration) for details.
84
+
If you have an OTLP-compatible backend (e.g., the OpenTelemetry Collector, Jaeger, Tempo, etc.) listening on
85
+
`http://localhost:4318`, you can inspect the exported telemetry there as well.
61
86
62
-
Example configuration to exclude health checks from tracing:
87
+
## Declarative Configuration with Spring Boot
88
+
89
+
The declarative configuration used by this example lives in [`application.yaml`](./src/main/resources/application.yaml)
90
+
under the `otel:` root key.
63
91
64
92
```yaml
65
93
otel:
66
-
file_format: declarative
94
+
# ... see src/main/resources/application.yaml for the full configuration
95
+
```
96
+
97
+
This layout follows the declarative configuration schema defined in the
98
+
[opentelemetry-configuration](https://github.com/open-telemetry/opentelemetry-configuration) repository, but adapted
99
+
for Spring Boot:
100
+
101
+
- All OpenTelemetry configuration keys live under the `otel:` root
102
+
- Configuration blocks from the reference repo (such as `tracer_provider`, `meter_provider`, `logger_provider`, etc.)
103
+
are indented by **two spaces** beneath `otel:`
104
+
- The configuration is loaded via the OpenTelemetry Spring Boot Starter instead of the Java Agent
105
+
106
+
### Opting In with `file_format`
107
+
108
+
Declarative configuration is **opt-in**. In this Spring Boot example, you enable declarative configuration by setting
109
+
`file_format` under `otel:` in `application.yaml`:
110
+
111
+
```yaml
112
+
otel:
113
+
file_format: "1.0-rc.2"
114
+
# ... other configuration
115
+
```
116
+
117
+
The `file_format` value follows the versions defined in the
- Spring Boot uses `:` as the separator for default values in property expressions, e.g. `${OTEL_EXPORTER_OTLP_ENDPOINT:http://localhost:4318}/v1/traces` (not `:-`).
81
-
- The auto-suggested properties in IDEs may be incorrect, as they are based on the non-declarative config schema.
82
-
- The `file_format` property is required to opt-in to declarative configuration.
83
-
- Refer to the [`application.yaml`](./application.yaml) configuration file in this module for the complete list of available properties and their descriptions.
178
+
This configuration is conceptually equivalent to the Java Agent example’s `rule_based_routing` sampler (from
- All other server requests are sampled by the `always_on` fallback sampler
185
+
186
+
## Spring Boot Starter–Specific Notes
187
+
188
+
### Spring Boot Starter Version
189
+
190
+
- Declarative configuration is supported by the OpenTelemetry Spring Boot Starter starting with version **2.22.0**
191
+
- Ensure your dependencies use at least this version; otherwise, `file_format` and other declarative config features
192
+
may be ignored
193
+
194
+
### Property Metadata and IDE Auto-Completion
84
195
85
-
## Programmatic Configuration Example
196
+
Most IDEs derive auto-completion for Spring properties from Spring Boot configuration metadata. At the time of this
197
+
example, that metadata is primarily based on the **non-declarative** configuration schema.
86
198
87
-
If you need to filter out health checks programmatically (instead of declarative config), see [`OpenTelemetryConfig.java`](../spring-native/src/main/java/io/opentelemetry/example/graal/OpenTelemetryConfig.java) in the `spring-native` module. The `configureSampler` method demonstrates how to drop actuator endpoints from tracing using the RuleBasedRoutingSampler:
0 commit comments