|
| 1 | +# OpenTelemetry Java Reference Application |
| 2 | + |
| 3 | +This reference application demonstrates comprehensive OpenTelemetry usage in Java, following the [OpenTelemetry Getting Started Reference Application Specification](https://opentelemetry.io/docs/getting-started/reference-application-specification/). |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +This application showcases: |
| 8 | + |
| 9 | +- **Traces**: Manual and automatic span creation, distributed tracing |
| 10 | +- **Metrics**: Custom metrics, performance monitoring |
| 11 | +- **Logs**: Structured logging with trace correlation |
| 12 | +- **Multiple exporters**: Console, OTLP, file-based exports |
| 13 | +- **Configuration**: Environment variables, programmatic setup, and declarative configuration |
| 14 | +- **Docker support**: Complete setup with OpenTelemetry Collector |
| 15 | + |
| 16 | +## Application Overview |
| 17 | + |
| 18 | +The reference application is a dice rolling service that simulates various scenarios to demonstrate OpenTelemetry capabilities: |
| 19 | + |
| 20 | +### Endpoints |
| 21 | + |
| 22 | +- `GET /rolldice` - Basic dice roll (returns random 1-6) |
| 23 | +- `GET /rolldice?player=<name>` - Dice roll for a specific player |
| 24 | +- `GET /rolldice?rolls=<n>` - Roll multiple dice |
| 25 | +- `GET /fibonacci?n=<number>` - Calculate fibonacci (demonstrates computation tracing) |
| 26 | +- `GET /health` - Health check endpoint |
| 27 | +- `GET /metrics` - Prometheus metrics endpoint (when enabled) |
| 28 | + |
| 29 | +### Scenarios Demonstrated |
| 30 | + |
| 31 | +1. **Basic HTTP instrumentation**: Automatic span creation for HTTP requests |
| 32 | +2. **Manual instrumentation**: Custom spans for business logic |
| 33 | +3. **Error handling**: Error span recording and exception tracking |
| 34 | +4. **Custom metrics**: Performance counters, histograms, gauges |
| 35 | +5. **Baggage propagation**: Cross-cutting concerns |
| 36 | +6. **Resource detection**: Automatic resource attribute detection |
| 37 | + |
| 38 | +## Quick Start |
| 39 | + |
| 40 | +### Prerequisites |
| 41 | + |
| 42 | +- Java 17 or later |
| 43 | +- Docker and Docker Compose (for collector setup) |
| 44 | + |
| 45 | +### Running with Console Output |
| 46 | + |
| 47 | +```shell |
| 48 | +# Build and run with console logging |
| 49 | +../gradlew bootRun |
| 50 | +``` |
| 51 | + |
| 52 | +Then test the endpoints: |
| 53 | +```shell |
| 54 | +curl http://localhost:8080/rolldice |
| 55 | +curl http://localhost:8080/rolldice?player=alice |
| 56 | +curl http://localhost:8080/fibonacci?n=10 |
| 57 | +``` |
| 58 | + |
| 59 | +### Running with OpenTelemetry Collector |
| 60 | + |
| 61 | +```shell |
| 62 | +# Start the collector and application |
| 63 | +docker-compose up --build |
| 64 | +``` |
| 65 | + |
| 66 | +This will: |
| 67 | +- Start the reference application on port 8080 |
| 68 | +- Start OpenTelemetry Collector on port 4317/4318 |
| 69 | +- Export telemetry data to the collector |
| 70 | +- Output structured telemetry data to console |
| 71 | + |
| 72 | +## Configuration |
| 73 | + |
| 74 | +The application supports multiple configuration approaches: |
| 75 | + |
| 76 | +### Environment Variables |
| 77 | + |
| 78 | +```shell |
| 79 | +export OTEL_SERVICE_NAME=dice-server |
| 80 | +export OTEL_SERVICE_VERSION=1.0.0 |
| 81 | +export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 |
| 82 | +export OTEL_TRACES_EXPORTER=otlp |
| 83 | +export OTEL_METRICS_EXPORTER=otlp |
| 84 | +export OTEL_LOGS_EXPORTER=otlp |
| 85 | +``` |
| 86 | + |
| 87 | +### Programmatic Configuration |
| 88 | + |
| 89 | +See `src/main/java/io/opentelemetry/example/config/` for examples of: |
| 90 | +- Manual SDK initialization |
| 91 | +- Custom span processors and exporters |
| 92 | +- Resource configuration |
| 93 | +- Sampling configuration |
| 94 | + |
| 95 | +### Declarative Configuration |
| 96 | + |
| 97 | +Use the included `otel-config.yaml` for file-based configuration: |
| 98 | + |
| 99 | +```shell |
| 100 | +export OTEL_EXPERIMENTAL_CONFIG_FILE=otel-config.yaml |
| 101 | +``` |
| 102 | + |
| 103 | +## Understanding the Output |
| 104 | + |
| 105 | +### Traces |
| 106 | + |
| 107 | +The application creates spans for: |
| 108 | +- HTTP requests (automatic) |
| 109 | +- Business logic operations (manual) |
| 110 | +- External calls and computations |
| 111 | +- Error scenarios |
| 112 | + |
| 113 | +### Metrics |
| 114 | + |
| 115 | +The application reports: |
| 116 | +- Request duration histograms |
| 117 | +- Request counters by endpoint |
| 118 | +- Error rates |
| 119 | +- Custom business metrics (dice roll distributions) |
| 120 | + |
| 121 | +### Logs |
| 122 | + |
| 123 | +All logs include: |
| 124 | +- Trace ID and Span ID for correlation |
| 125 | +- Structured fields |
| 126 | +- Different log levels |
| 127 | +- Business context |
| 128 | + |
| 129 | +## Development |
| 130 | + |
| 131 | +### Building |
| 132 | + |
| 133 | +```shell |
| 134 | +../gradlew build |
| 135 | +``` |
| 136 | + |
| 137 | +### Testing |
| 138 | + |
| 139 | +```shell |
| 140 | +../gradlew test |
| 141 | +``` |
| 142 | + |
| 143 | +### Running locally |
| 144 | + |
| 145 | +```shell |
| 146 | +../gradlew bootRun |
| 147 | +``` |
| 148 | + |
| 149 | +## Docker Images |
| 150 | + |
| 151 | +The application can be built as a Docker image: |
| 152 | + |
| 153 | +```shell |
| 154 | +../gradlew bootBuildImage |
| 155 | +``` |
| 156 | + |
| 157 | +## Troubleshooting |
| 158 | + |
| 159 | +### Common Issues |
| 160 | + |
| 161 | +1. **No telemetry data**: Check OTEL_* environment variables |
| 162 | +2. **Connection issues**: Verify collector endpoint configuration |
| 163 | +3. **Missing traces**: Ensure sampling is configured correctly |
| 164 | + |
| 165 | +### Debugging |
| 166 | + |
| 167 | +Enable debug logging: |
| 168 | +```shell |
| 169 | +export OTEL_JAVAAGENT_DEBUG=true |
| 170 | +``` |
| 171 | + |
| 172 | +Or set logging level: |
| 173 | +```shell |
| 174 | +export LOGGING_LEVEL_IO_OPENTELEMETRY=DEBUG |
| 175 | +``` |
| 176 | + |
| 177 | +## Learn More |
| 178 | + |
| 179 | +- [OpenTelemetry Java Documentation](https://opentelemetry.io/docs/languages/java/) |
| 180 | +- [OpenTelemetry Specification](https://opentelemetry.io/docs/specs/otel/) |
| 181 | +- [Semantic Conventions](https://opentelemetry.io/docs/specs/semconv/) |
0 commit comments