Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/msfidelis/opentelemetry-java-autoconfigure
Educational example explaining how to use OpenTelemetry's auto-instrumentation with Java and Spring Boot.
https://github.com/msfidelis/opentelemetry-java-autoconfigure
Last synced: 28 days ago
JSON representation
Educational example explaining how to use OpenTelemetry's auto-instrumentation with Java and Spring Boot.
- Host: GitHub
- URL: https://github.com/msfidelis/opentelemetry-java-autoconfigure
- Owner: msfidelis
- Created: 2023-10-05T01:20:14.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-19T16:02:29.000Z (5 months ago)
- Last Synced: 2024-10-02T09:17:54.970Z (about 1 month ago)
- Language: Java
- Size: 1020 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Open Telemetry Auto Traces - Example
* [Feign HTTP Client](https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/)
* [HTTP Client - Spring Cloud Gateway](https://spring.io/projects/spring-cloud-gateway)
* [@WithSpan and @SpanAttribute Annotations](https://opentelemetry.io/docs/instrumentation/java/automatic/annotations/)
* [Spring Data Redis Operations / Jedis](https://spring.io/projects/spring-data-redis)
* [Hibernate Operations / Spring Data JPA](https://spring.io/projects/spring-data-jpa)# Project Architecture
## Isolated Services - Spring Cloud Gateway Routes
![Spring Cloud Gateway](.github/img/otel_example-Simple.drawio.png)
## Isolated Services - REST Methods
![REST](.github/img/otel_example-Página-2.drawio.png)
## Isolated Services - Data Layers
![Data Layers](.github/img/otel_example-Página-3.drawio.png)
## Integrated Services - Books / Reviews
![Data Layers](.github/img/otel_example-Página-4.drawio.png)
# Example Trace - Autoconfiguration
## Dockerfile
```Dockerfile
FROM openjdk:21-jdkWORKDIR /app
COPY .mvn .mvn
COPY mvnw .
COPY pom.xml .
COPY src srcRUN ./mvnw clean package
# ADD OTEL Java Agent
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar opentelemetry-javaagent.jar# Add Agent Path on JAVA_TOOL_OPTIONS
ENV JAVA_TOOL_OPTIONS="-javaagent:./opentelemetry-javaagent.jar"RUN mv target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
```## Maven Dependencies
```xml
io.opentelemetry.instrumentation
opentelemetry-instrumentation-annotations
1.30.0
```## Environment Variables
```yaml
// ...
environment:
OTEL_SERVICE_NAME: books-api
OTEL_TRACES_EXPORTER: zipkin
OTEL_EXPORTER_ZIPKIN_ENDPOINT: http://jaeger:9411/api/v2/spans
OTEL_METRICS_EXPORTER: none
// ...
```## Annotate with @WithSpan and @SpanAttribute
```java
@WithSpan
public BookResponse fetchBook(@SpanAttribute("id_book") Long id_book) {
try {
return bookClient.getBookById(id_book);
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}@WithSpan
@Cacheable(value = "Reviews", key = "#id_book")
public List getReviews(@SpanAttribute("id_book") Long id_book) {
List reviews = repository.findByIdBook(id_book);
return reviews;
}
```## Test Requests
```bash
❯ curl --request POST \
--url http://0.0.0.0:9000/author \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/8.2.0' \
--data '{
"name": "Matheus Fidelis",
"location": "Brazil",
"birth": 1995
}' -iHTTP/1.1 201 Created
transfer-encoding: chunked
Content-Type: application/json
Date: Mon, 09 Oct 2023 21:24:03 GMT{"id":4,"name":"Matheus Fidelis","location":"Brazil","birth":1995,"active":true}
``````bash
curl --request POST \
--url http://0.0.0.0:9000/books \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/8.2.0' \
--data '{
"name": "As melhores do Ari Toledo",
"year": 2012,
"author_id": 1
}' -iHTTP/1.1 201 Created
transfer-encoding: chunked
Content-Type: application/json
Date: Mon, 09 Oct 2023 21:26:42 GMT{"id":28,"name":"As melhores do Ari Toledo","year":2012,"active":true,"author":{"id":4,"name":"Matheus Fidelis","location":"Brazil","birth":1995,"active":true}}
``````bash
curl --request POST \
--url http://0.0.0.0:9000/reviews \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/8.2.0' \
--data '{
"book_id": 1 ,
"rate": 5,
"review": "kkkkkkkk top dms slc man"
}' -i
HTTP/1.1 201 Created
transfer-encoding: chunked
Content-Type: application/json
Date: Mon, 09 Oct 2023 21:29:12 GMT{"id":181,"rate":5.0,"book_id":28,"review":"kkkkkkkk top dms slc man","active":true
``````bash
❯ curl --request GET \
--url 'http://0.0.0.0:9000/reviews?book=28' \
--header 'User-Agent: insomnia/8.2.0' -i
HTTP/1.1 200 OK
transfer-encoding: chunked
Content-Type: application/json
Date: Mon, 09 Oct 2023 21:30:15 GMT[{"id":180,"rate":5.0,"review":"kkkkkkkk top dms slc man"},{"id":181,"rate":5.0,"review":"kkkkkkkk top dms slc man"}]
```## Example Traces
![Simple Trace](.github/img/trace_1.png)
![Simple Trace](.github/img/trace_2.png)
![Simple Trace](.github/img/trace_3.png)
![Simple Trace](.github/img/trace_4.png)