diff --git a/.github/workflows/project-build.yaml b/.github/workflows/project-build.yaml new file mode 100644 index 0000000..f27aea4 --- /dev/null +++ b/.github/workflows/project-build.yaml @@ -0,0 +1,39 @@ +name: Build Project + +on: + push: + branches: + - main + pull_request: + branches: + - '*' + +jobs: + verify-project-build: + permissions: # should be removed by this MR: https://github.com/dorny/test-reporter/pull/174/ + statuses: write + checks: write + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: 17.0 + distribution: 'adopt' + java-package: 'jdk' + + - name: Build the project + working-directory: distributed-tracing-spring-boot-opentelemetry-jaeger + run: mvn clean verify + + - name: Report + uses: dorny/test-reporter@v1 + if: always() + with: + name: Tests Results + path: "**/surefire-reports/*.xml" + reporter: java-junit + fail-on-error: true \ No newline at end of file diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/README.md b/distributed-tracing-spring-boot-opentelemetry-jaeger/README.md new file mode 100644 index 0000000..11202d5 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/README.md @@ -0,0 +1,24 @@ +# Distributed Tracing with OpenTelemetry and Jaeger + +In this project I have figured out how we can integrate distributed tracing in a Spring Boot application. +We are using OpenTelemetry to export traces to Jaeger. + +You can read about this on my website [https://refactorfirst.com](https://refactorfirst.com) + +Once you build the application using `mvn clean verify`, You can start the application as two service instances. + +Service 1 +``` +java -jar \ +target/Distributed-Service-0.0.1-SNAPSHOT.jar \ +--spring.application.name=Service-1 \ +--server.port=8080 +``` + +Service 2 +``` +java -jar \ +target/Distributed-Service-0.0.1-SNAPSHOT.jar \ +--spring.application.name=Service-2 \ +--server.port=8090 +``` \ No newline at end of file diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/docker-compose.yaml b/distributed-tracing-spring-boot-opentelemetry-jaeger/docker-compose.yaml new file mode 100644 index 0000000..e22b3ea --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/docker-compose.yaml @@ -0,0 +1,9 @@ +version: "3.9" +services: + jaeger: + image: jaegertracing/all-in-one:latest + ports: + - 4318:4318 + - 16686:16686 + environment: + - COLLECTOR_OTLP_ENABLED=true diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/pom.xml b/distributed-tracing-spring-boot-opentelemetry-jaeger/pom.xml new file mode 100644 index 0000000..ccfbe62 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.5 + + + com.amrut.prabhu + distributed-service + 0.0.1-SNAPSHOT + Distributed Tracing Spring Boot 3 OpenTelemetry Jaeger + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + io.micrometer + micrometer-tracing-bridge-otel + + + io.opentelemetry + opentelemetry-exporter-otlp + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/Controller.java b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/Controller.java new file mode 100644 index 0000000..4652e96 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/Controller.java @@ -0,0 +1,39 @@ +package com.amrut.prabhu; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +@RequestMapping("/service") +public class Controller { + + private static final Logger logger = LoggerFactory.getLogger(Controller.class); + private RestTemplate restTemplate; + + @Value("${spring.application.name}") + private String applicationName; + + public Controller(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + @GetMapping("/path1") + public ResponseEntity path1() { + + logger.info("Incoming request at {} for request /path1 ", applicationName); + String response = restTemplate.getForObject("http://localhost:8090/service/path2", String.class); + return ResponseEntity.ok("response from /path1 + " + response); + } + + @GetMapping("/path2") + public ResponseEntity path2() { + logger.info("Incoming request at {} at /path2", applicationName); + return ResponseEntity.ok("response from /path2 "); + } +} \ No newline at end of file diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/DistributedTracingApplication.java b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/DistributedTracingApplication.java new file mode 100644 index 0000000..5bc0724 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/DistributedTracingApplication.java @@ -0,0 +1,21 @@ +package com.amrut.prabhu; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@SpringBootApplication +public class DistributedTracingApplication { + + public static void main(String[] args) { + SpringApplication.run(DistributedTracingApplication.class, args); + } + + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder. + build(); + } +} diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/OtlpConfiguration.java b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/OtlpConfiguration.java new file mode 100644 index 0000000..f83f0b5 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/java/com/amrut/prabhu/OtlpConfiguration.java @@ -0,0 +1,19 @@ +package com.amrut.prabhu; + +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OtlpConfiguration { + + @Bean + OtlpHttpSpanExporter otlpHttpSpanExporter(@Value("${tracing.url}") String url) { + return OtlpHttpSpanExporter.builder() + .setEndpoint(url) + .build(); + } +} diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/resources/application.yaml b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/resources/application.yaml new file mode 100644 index 0000000..c5871c5 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/main/resources/application.yaml @@ -0,0 +1,19 @@ +server: + port: 8090 + +spring: + application: + name: app + +tracing: + url: http://localhost:4318/v1/traces + +management: + tracing: + sampling: + probability: 1.0 + +logging: + pattern: + level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]" + diff --git a/distributed-tracing-spring-boot-opentelemetry-jaeger/src/test/java/com/amrut/prabhu/DistributedTracingApplicationTests.java b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/test/java/com/amrut/prabhu/DistributedTracingApplicationTests.java new file mode 100644 index 0000000..425dce7 --- /dev/null +++ b/distributed-tracing-spring-boot-opentelemetry-jaeger/src/test/java/com/amrut/prabhu/DistributedTracingApplicationTests.java @@ -0,0 +1,13 @@ +package com.amrut.prabhu; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DistributedTracingApplicationTests { + + @Test + void contextLoads() { + } + +}