|
| 1 | +# Java API with OpenTelemetry SDK |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | +- [Introduction](#introduction) |
| 10 | +- [Architecture](#architecture) |
| 11 | + - [Components](#components) |
| 12 | + - [Data Flow](#data-flow) |
| 13 | +- [Project Structure](#project-structure) |
| 14 | +- [Prerequisites](#prerequisites) |
| 15 | +- [Installation](#installation) |
| 16 | +- [Usage](#usage) |
| 17 | + - [Running the API](#running-the-api) |
| 18 | + - [API Endpoints](#api-endpoints) |
| 19 | + - [OpenTelemetry Integration](#opentelemetry-integration) |
| 20 | +- [Components in Detail](#components-in-detail) |
| 21 | + - [Spring Boot API](#spring-boot-api) |
| 22 | + - [OpenTelemetry SDK](#opentelemetry-sdk) |
| 23 | + - [H2 Database](#h2-database) |
| 24 | + - [Swagger Documentation](#swagger-documentation) |
| 25 | +- [Configurations](#configurations) |
| 26 | + - [Application Properties](#application-properties) |
| 27 | + - [OpenTelemetry Configuration](#opentelemetry-configuration) |
| 28 | +- [Troubleshooting](#troubleshooting) |
| 29 | +- [Contribution](#contribution) |
| 30 | +- [License](#license) |
| 31 | + |
| 32 | +## Introduction |
| 33 | + |
| 34 | +**Java API with OpenTelemetry SDK** is a complete REST API built with Spring Boot that demonstrates a production-ready implementation of the OpenTelemetry SDK for observability. This project provides a robust foundation for developing microservices with built-in telemetry capabilities, allowing seamless integration with modern observability platforms. |
| 35 | + |
| 36 | +The API includes full CRUD operations for user management, follows a layered architecture pattern (Controller-Service-Repository), and uses JPA/Hibernate for persistence with an in-memory H2 database. The OpenTelemetry integration provides automatic instrumentation for metrics, traces, and logs. |
| 37 | + |
| 38 | +## Architecture |
| 39 | + |
| 40 | +### Components |
| 41 | + |
| 42 | +The application consists of the following main components: |
| 43 | + |
| 44 | +- **Spring Boot API**: REST API with CRUD operations |
| 45 | +- **OpenTelemetry SDK**: For collecting and exporting telemetry data |
| 46 | +- **H2 Database**: In-memory database for persistence |
| 47 | +- **Swagger UI**: API documentation and testing interface |
| 48 | + |
| 49 | +### Data Flow |
| 50 | + |
| 51 | +``` |
| 52 | +┌─────────────┐ ┌─────────────────┐ ┌──────────────────┐ |
| 53 | +│ HTTP Client │──────▶ Spring Boot API │──────▶ Business Services │ |
| 54 | +└─────────────┘ └────────┬────────┘ └─────────┬────────┘ |
| 55 | + │ │ |
| 56 | + │ │ |
| 57 | + ┌────────▼────────┐ ┌──────────▼────────┐ |
| 58 | + │ OpenTelemetry │ │ H2 Database │ |
| 59 | + │ SDK │ │ │ |
| 60 | + └────────┬────────┘ └───────────────────┘ |
| 61 | + │ |
| 62 | + │ |
| 63 | + ┌────────▼────────┐ |
| 64 | + │ Telemetry Data │ |
| 65 | + │ (OTLP Format) │ |
| 66 | + └────────┬────────┘ |
| 67 | + │ |
| 68 | + ▼ |
| 69 | + ┌──────────────────────┐ |
| 70 | + │ Observability Backend │ |
| 71 | + │ (Collector, Jaeger, │ |
| 72 | + │ Prometheus, etc.) │ |
| 73 | + └──────────────────────┘ |
| 74 | +``` |
| 75 | + |
| 76 | +## Project Structure |
| 77 | + |
| 78 | +``` |
| 79 | +java-api-with-otlp-sdk/ |
| 80 | +├── src/ |
| 81 | +│ ├── main/ |
| 82 | +│ │ ├── java/ |
| 83 | +│ │ │ └── com/ |
| 84 | +│ │ │ └── example/ |
| 85 | +│ │ │ ├── controller/ # REST API controllers |
| 86 | +│ │ │ │ └── HealthController.java |
| 87 | +│ │ │ ├── service/ # Business logic services |
| 88 | +│ │ │ ├── repository/ # Data access layer |
| 89 | +│ │ │ ├── model/ # Domain entities |
| 90 | +│ │ │ ├── config/ # Application configs |
| 91 | +│ │ │ │ └── OpenTelemetryConfig.java |
| 92 | +│ │ │ └── Application.java # Main application class |
| 93 | +│ │ └── resources/ |
| 94 | +│ │ ├── application.properties # App configuration |
| 95 | +│ │ └── otel-config.yaml # OpenTelemetry config |
| 96 | +│ └── test/ # Unit and integration tests |
| 97 | +├── pom.xml # Maven dependencies |
| 98 | +├── Dockerfile # Container definition |
| 99 | +└── README.md # Project documentation |
| 100 | +``` |
| 101 | + |
| 102 | +## Prerequisites |
| 103 | + |
| 104 | +To run this project, you need: |
| 105 | + |
| 106 | +- JDK 17 or higher |
| 107 | +- Maven 3.6 or higher |
| 108 | +- OpenTelemetry Collector (optional, for exporting telemetry data) |
| 109 | +- Docker (optional, for containerization) |
| 110 | + |
| 111 | +## Installation |
| 112 | + |
| 113 | +1. Clone the repository: |
| 114 | + ```bash |
| 115 | + git clone https://github.com/yourusername/java-api-with-otlp-sdk.git |
| 116 | + cd java-api-with-otlp-sdk |
| 117 | + ``` |
| 118 | + |
| 119 | +2. Build the project with Maven: |
| 120 | + ```bash |
| 121 | + mvn clean package |
| 122 | + ``` |
| 123 | + |
| 124 | +3. (Optional) Build Docker container: |
| 125 | + ```bash |
| 126 | + docker build -t java-api-with-otlp:latest . |
| 127 | + ``` |
| 128 | + |
| 129 | +## Usage |
| 130 | + |
| 131 | +### Running the API |
| 132 | + |
| 133 | +Run the application locally: |
| 134 | + |
| 135 | +```bash |
| 136 | +mvn spring-boot:run |
| 137 | +``` |
| 138 | + |
| 139 | +Or using the JAR file: |
| 140 | + |
| 141 | +```bash |
| 142 | +java -jar target/java-api-with-otlp-sdk-1.0.0.jar |
| 143 | +``` |
| 144 | + |
| 145 | +With Docker: |
| 146 | + |
| 147 | +```bash |
| 148 | +docker run -p 8080:8080 java-api-with-otlp:latest |
| 149 | +``` |
| 150 | + |
| 151 | +### API Endpoints |
| 152 | + |
| 153 | +Once the application is running, you can access: |
| 154 | + |
| 155 | +- API Base URL: `http://localhost:8080` |
| 156 | +- Swagger UI: `http://localhost:8080/swagger-ui.html` |
| 157 | +- Health Check: `http://localhost:8080/health` |
| 158 | + |
| 159 | +Main endpoints include: |
| 160 | + |
| 161 | +- `GET /users` - List all users |
| 162 | +- `GET /users/{id}` - Get user by ID |
| 163 | +- `POST /users` - Create new user |
| 164 | +- `PUT /users/{id}` - Update existing user |
| 165 | +- `DELETE /users/{id}` - Delete user |
| 166 | + |
| 167 | +### OpenTelemetry Integration |
| 168 | + |
| 169 | +The application is configured to send telemetry data to an OpenTelemetry Collector. By default, it uses the following settings: |
| 170 | + |
| 171 | +- OTLP Export Protocol: gRPC |
| 172 | +- Collector Endpoint: `http://localhost:4317` |
| 173 | +- Service Name: `java-api-service` |
| 174 | + |
| 175 | +These settings can be modified in the `application.properties` file. |
| 176 | + |
| 177 | +## Components in Detail |
| 178 | + |
| 179 | +### Spring Boot API |
| 180 | + |
| 181 | +The API is built using Spring Boot 3.1.0 with the following features: |
| 182 | + |
| 183 | +- RESTful endpoints with proper HTTP status codes |
| 184 | +- Controller-Service-Repository architecture |
| 185 | +- Custom exception handling with appropriate error responses |
| 186 | +- Request validation |
| 187 | +- Pagination and sorting capabilities |
| 188 | + |
| 189 | +### OpenTelemetry SDK |
| 190 | + |
| 191 | +The application uses OpenTelemetry Java SDK for: |
| 192 | + |
| 193 | +- **Automatic Instrumentation**: Traces HTTP requests, database queries, and internal method calls |
| 194 | +- **Manual Instrumentation**: Custom spans for business logic |
| 195 | +- **Metrics Collection**: JVM metrics, API endpoint metrics, and custom business metrics |
| 196 | +- **Context Propagation**: Maintains trace context across asynchronous boundaries |
| 197 | +- **Attribute Enrichment**: Adds metadata to spans for better analysis |
| 198 | + |
| 199 | +### H2 Database |
| 200 | + |
| 201 | +An in-memory H2 database is used for data persistence: |
| 202 | + |
| 203 | +- Auto-configured by Spring Boot |
| 204 | +- Console available at `http://localhost:8080/h2-console` |
| 205 | +- Default credentials: username="sa", password="" (empty) |
| 206 | +- JDBC URL: `jdbc:h2:mem:testdb` |
| 207 | + |
| 208 | +### Swagger Documentation |
| 209 | + |
| 210 | +The API is documented using SpringDoc OpenAPI: |
| 211 | + |
| 212 | +- Interactive API documentation |
| 213 | +- Try-out functionality for all endpoints |
| 214 | +- Model schema definitions |
| 215 | +- Authentication documentation |
| 216 | + |
| 217 | +## Configurations |
| 218 | + |
| 219 | +### Application Properties |
| 220 | + |
| 221 | +Key application properties (`application.properties`): |
| 222 | + |
| 223 | +```properties |
| 224 | +# Server configuration |
| 225 | +server.port=8080 |
| 226 | +
|
| 227 | +# H2 Database |
| 228 | +spring.datasource.url=jdbc:h2:mem:testdb |
| 229 | +spring.datasource.driverClassName=org.h2.Driver |
| 230 | +spring.datasource.username=sa |
| 231 | +spring.datasource.password= |
| 232 | +spring.h2.console.enabled=true |
| 233 | +
|
| 234 | +# JPA/Hibernate |
| 235 | +spring.jpa.hibernate.ddl-auto=update |
| 236 | +spring.jpa.show-sql=true |
| 237 | +
|
| 238 | +# OpenTelemetry |
| 239 | +otel.service.name=java-api-service |
| 240 | +otel.exporter.otlp.endpoint=http://localhost:4317 |
| 241 | +``` |
| 242 | + |
| 243 | +### OpenTelemetry Configuration |
| 244 | + |
| 245 | +The OpenTelemetry SDK is configured in `OpenTelemetryConfig.java`: |
| 246 | + |
| 247 | +- Automatic instrumentation of Spring Web, JDBC, and JPA |
| 248 | +- Resource attributes including service name and version |
| 249 | +- Span processors for batching and exporting telemetry data |
| 250 | +- Integration with Spring's logging framework |
| 251 | +
|
| 252 | +## Troubleshooting |
| 253 | +
|
| 254 | +### Common Issues and Solutions |
| 255 | +
|
| 256 | +1. **Application fails to start**: |
| 257 | + - Verify Java version (`java -version`) |
| 258 | + - Check the application logs for specific error messages |
| 259 | + - Ensure required ports are available (8080 for API) |
| 260 | +
|
| 261 | +2. **Telemetry data not showing up**: |
| 262 | + - Verify the OpenTelemetry Collector is running |
| 263 | + - Check the collector endpoint configuration |
| 264 | + - Look for connection errors in the application logs |
| 265 | +
|
| 266 | +3. **Database connection issues**: |
| 267 | + - Check H2 console for database state |
| 268 | + - Verify entity mappings and relationships |
| 269 | + - Review JPA configuration properties |
| 270 | +
|
| 271 | +## Contribution |
| 272 | +
|
| 273 | +Contributions are welcome! To contribute: |
| 274 | +
|
| 275 | +1. Fork the repository |
| 276 | +2. Create a feature branch (`git checkout -b feature/new-feature`) |
| 277 | +3. Commit your changes (`git commit -m 'Add new feature'`) |
| 278 | +4. Push to the branch (`git push origin feature/new-feature`) |
| 279 | +5. Open a Pull Request |
| 280 | +
|
| 281 | +Please ensure your code follows the existing code style and includes appropriate tests. |
| 282 | +
|
| 283 | +## License |
| 284 | +
|
| 285 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 286 | +
|
| 287 | +--- |
| 288 | +Developed with ❤️ to demonstrate Spring Boot and OpenTelemetry integration. |
0 commit comments