|
| 1 | +# Java Agent Declarative Configuration Example |
| 2 | + |
| 3 | +This example demonstrates how to use [declarative configuration](https://opentelemetry.io/docs/specs/otel/configuration/#declarative-configuration) with the OpenTelemetry Java Agent to configure tracing behavior. |
| 4 | + |
| 5 | +The configuration file is located at [otel-agent-config.yaml](./otel-agent-config.yaml). |
| 6 | + |
| 7 | +This Spring Boot application includes two endpoints: |
| 8 | +- `/actuator/health` - A health check endpoint (from Spring Boot Actuator) that is configured to be excluded from tracing |
| 9 | +- `/api/example` - A simple API endpoint that will be traced normally |
| 10 | + |
| 11 | +## End-to-End Instructions |
| 12 | + |
| 13 | +### Prerequisites |
| 14 | +* Java 17 or higher |
| 15 | +* OpenTelemetry Java Agent JAR file (see next step) |
| 16 | + |
| 17 | +Download the OpenTelemetry Java Agent: |
| 18 | +```bash |
| 19 | +# Download the latest OpenTelemetry Java Agent |
| 20 | +curl -L -o opentelemetry-javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar |
| 21 | +``` |
| 22 | + |
| 23 | +### Step 1: Build the Application |
| 24 | + |
| 25 | +```bash |
| 26 | +# Build the JAR - Run from the javaagent-declarative-configuration directory |
| 27 | +../gradlew bootJar |
| 28 | +``` |
| 29 | + |
| 30 | +### Step 2: Run with OpenTelemetry Java Agent |
| 31 | + |
| 32 | +```bash |
| 33 | +# From the javaagent-declarative-configuration directory |
| 34 | + |
| 35 | +# Run with the OpenTelemetry Java Agent and contrib extension |
| 36 | +java -javaagent:opentelemetry-javaagent.jar \ |
| 37 | + -Dotel.experimental.config.file=$(pwd)/otel-agent-config.yaml \ |
| 38 | + -jar build/libs/javaagent-declarative-configuration.jar |
| 39 | +``` |
| 40 | + |
| 41 | +### Step 3: Test the Endpoints |
| 42 | + |
| 43 | +Open a new terminal and test both endpoints: |
| 44 | + |
| 45 | +```bash |
| 46 | +# This endpoint will NOT be traced (excluded by configuration) |
| 47 | +curl http://localhost:8080/actuator/health |
| 48 | + |
| 49 | +# This endpoint WILL be traced normally |
| 50 | +curl http://localhost:8080/api/example |
| 51 | +``` |
| 52 | + |
| 53 | +### Step 4: Verify Tracing Behavior |
| 54 | + |
| 55 | +Check the application logs to see: |
| 56 | +- Health check requests (`/actuator/health`) should NOT generate traces (excluded by configuration) |
| 57 | +- API requests (`/api/example`) should generate traces with console output |
| 58 | + |
| 59 | +## Configuration |
| 60 | + |
| 61 | +The `otel-agent-config.yaml` file demonstrates rule-based sampling using declarative configuration to exclude health checks from tracing: |
| 62 | + |
| 63 | +```yaml |
| 64 | +tracer_provider: |
| 65 | + sampler: |
| 66 | + rule_based_routing: |
| 67 | + fallback_sampler: |
| 68 | + always_on: |
| 69 | + span_kind: SERVER |
| 70 | + rules: |
| 71 | + - action: DROP |
| 72 | + attribute: url.path |
| 73 | + pattern: /actuator.* |
| 74 | +``` |
| 75 | +
|
| 76 | +This configuration: |
| 77 | +- Uses the `rule_based_routing` sampler from the OpenTelemetry contrib extension |
| 78 | +- Excludes health check endpoints (`/actuator.*`) from tracing using the `DROP` action |
| 79 | +- Samples all other requests using the `always_on` fallback sampler |
| 80 | +- Only applies to `SERVER` span kinds |
0 commit comments