Skip to content

Commit

Permalink
Merge pull request #2 from amrutprabhu/spring-boot-3-opentelemetry-ja…
Browse files Browse the repository at this point in the history
…eger

Adding spring boot 3 telemetry with jaeger
  • Loading branch information
amrutprabhu committed May 7, 2023
2 parents e4ed5a9 + f2f9955 commit 9cee311
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/project-build.yaml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions distributed-tracing-spring-boot-opentelemetry-jaeger/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions distributed-tracing-spring-boot-opentelemetry-jaeger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.amrut.prabhu</groupId>
<artifactId>distributed-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Distributed Tracing Spring Boot 3 OpenTelemetry Jaeger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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 ");
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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:-}]"

Original file line number Diff line number Diff line change
@@ -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() {
}

}

0 comments on commit 9cee311

Please sign in to comment.