Skip to content

EightAugusto/spring-boot-trace-metric-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Trace and Metric

Spring Boot 3 demonstration using Observability implementing:

  • Tracing with Jaeger and micrometer-tracing-bridge-otel/opentelemetry-exporter-zipkin
  • Metrics with Prometheus and micrometer-registry-prometheus

Justification

Implementing Jaeger for tracing, Prometheus for metrics in a Spring Boot project oriented toward production support is essential for enhancing visibility, diagnosing issues, proactively monitoring performance, optimizing resource utilization, ensuring security and compliance, and scaling effectively. These open-source tools empower real-time alerts, historical analysis, and integration with various services while remaining cost-effective and benefiting from robust community support. This comprehensive approach is crucial for maintaining a high level of system reliability and efficiency in a production environment.


Requirements


Run

source .env; \
mvn clean install; \
java -Dserver.port=${APPLICATION_PORT} -jar target/${APPLICATION}.jar;

or

mvn clean install; \
make docker.start;

Stop

make docker.stop

Demonstration

Tracing

Once the application is running you can execute the following command to trigger a simple REST call from the local service to the local service.

curl -X POST 'http://localhost:8080/v1/message'

The previews command will allow Jaeger, based on the application.yml file with the following configuration.

management.zipkin.tracing.endpoint: http://localhost:9411/api/v2/spans

Create a complete trace of the 2 flows (POST /v1/message and GET /v1/message/{message}). SpringBootJaegerTrace

Additionally, in logs, you can see the traceId beeing shared between each flow.

 2024-01-01T00:00:00,000 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-1] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=ebc78e59b8c969c1}] c.e.s.b.t.m.m.c.MessageController.postMessage:22 - Enter ()
 2024-01-01T00:00:00,001 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-1] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=ebc78e59b8c969c1}] c.e.s.b.t.m.m.s.i.MessageServiceImpl.postMessage:42 - Enter ()
 2024-01-01T00:00:00,002 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-2] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=a6c8a30280af0040}] c.e.s.b.t.m.m.c.MessageController.getMessage:29 - Enter ()
 2024-01-01T00:00:00,003 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-2] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=a6c8a30280af0040}] c.e.s.b.t.m.m.s.i.MessageServiceImpl.getMessage:34 - Enter (e20bcf63-35d9-460b-b819-402a634b40f1)
 2024-01-01T00:00:00,004 [spring-boot-trace-metric-example] INFO  [http-nio-8080-exec-2] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=a6c8a30280af0040}] c.e.s.b.t.m.m.s.i.MessageServiceImpl.getMessage:35 - Received message: 'e20bcf63-35d9-460b-b819-402a634b40f1'
 2024-01-01T00:00:00,005 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-2] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=a6c8a30280af0040}] c.e.s.b.t.m.m.s.i.MessageServiceImpl.getMessage:36 - Exit
 2024-01-01T00:00:00,006 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-2] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=a6c8a30280af0040}] c.e.s.b.t.m.m.c.MessageController.getMessage:31 - Exit
 2024-01-01T00:00:00,007 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-1] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=ebc78e59b8c969c1}] c.e.s.b.t.m.m.s.i.MessageServiceImpl.postMessage:45 - Exit
 2024-01-01T00:00:00,008 [spring-boot-trace-metric-example] TRACE [http-nio-8080-exec-1] [{traceId=821c9f9fbed2ce64aed490c2f675f961, spanId=ebc78e59b8c969c1}] c.e.s.b.t.m.m.c.MessageController.postMessage:24 - Exit

Metrics

Spring Boot will expose prometheus endpoint based on the use of actuators and the micrometer-registry-prometheus implementation.

The following configuration will allow Spring to expose prometheus actuator.

management:
  endpoint.prometheus.enabled: true
  endpoints.web.exposure.include: prometheus

Once deployed, you can manually request the current service metrics using the following command.

curl 'http://localhost:8080/actuator/prometheus'

The previous configuration can be pulled using prometheus scraper configuring the prometheus.yml with the previous endpoint, i.e.:

scrape_configs:
  - job_name: 'Java Services'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['spring-boot-trace-metric-example:8080']

With the previous done, you can request metrics using Prometheus Dashboard, i.e.:

PrometheusDashboard