Skip to content

Commit eb97ffe

Browse files
committed
Spring Boot 4のOpenTelemetry対応のやつ
1 parent 0357525 commit eb97ffe

File tree

9 files changed

+71
-77
lines changed

9 files changed

+71
-77
lines changed

observability-example/compose.yaml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
services:
2-
zipkin:
3-
image: openzipkin/zipkin
4-
ports:
5-
- "9411:9411"
6-
7-
prometheus:
8-
image: prom/prometheus
9-
ports:
10-
- "9090:9090"
11-
volumes:
12-
- ./docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
13-
14-
grafana:
15-
image: grafana/grafana-oss
16-
ports:
17-
- "3000:3000"
18-
2+
grafana-lgtm:
3+
image: grafana/otel-lgtm
4+
ports: ["3000:3000", "4317:4317", "4318:4318"]

observability-example/observability-example-web/pom.xml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<parent>
67
<groupId>com.example</groupId>
78
<artifactId>spring-boot-sandbox-parent</artifactId>
89
<version>0.0.1-SNAPSHOT</version>
9-
<relativePath>../../</relativePath>
10+
<relativePath>../../</relativePath>
1011
</parent>
1112
<artifactId>observability-example-web</artifactId>
1213
<dependencies>
1314
<dependency>
1415
<groupId>org.springframework.boot</groupId>
1516
<artifactId>spring-boot-starter-actuator</artifactId>
1617
</dependency>
17-
<dependency>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-aspectj</artifactId>
21+
</dependency>
22+
<dependency>
1823
<groupId>org.springframework.boot</groupId>
1924
<artifactId>spring-boot-starter-security</artifactId>
2025
</dependency>
@@ -23,12 +28,17 @@
2328
<artifactId>spring-boot-starter-webmvc</artifactId>
2429
</dependency>
2530
<dependency>
26-
<groupId>io.micrometer</groupId>
27-
<artifactId>micrometer-tracing-bridge-brave</artifactId>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-opentelemetry</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-micrometer-metrics</artifactId>
2837
</dependency>
2938
<dependency>
30-
<groupId>io.zipkin.reporter2</groupId>
31-
<artifactId>zipkin-reporter-brave</artifactId>
39+
<groupId>io.opentelemetry.instrumentation</groupId>
40+
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
41+
<version>2.21.0-alpha</version>
3242
</dependency>
3343

3444
<dependency>
@@ -63,4 +73,4 @@
6373
</plugins>
6474
</build>
6575

66-
</project>
76+
</project>

observability-example/observability-example-web/src/main/java/com/example/App.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
@EnableScheduling
99
public class App {
1010

11-
@SuppressWarnings("resource")
1211
public static void main(String[] args) {
1312
SpringApplication.run(App.class, args);
1413
}
Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
11
package com.example;
22

3-
import io.micrometer.observation.Observation;
4-
import io.micrometer.observation.ObservationRegistry;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.scheduling.annotation.Scheduled;
67
import org.springframework.stereotype.Component;
78

89
@Component
910
public class Example {
1011

11-
private final ObservationRegistry registry;
12+
private final Logger logger = LoggerFactory.getLogger(Example.class);
1213

13-
public Example(ObservationRegistry registry) {
14-
this.registry = registry;
15-
}
14+
@Autowired
15+
private ExampleService service;
1616

1717
@Scheduled(cron = "*/10 * * * * *")
1818
public void run() {
19-
Observation.createNotStarted("foo", registry)
20-
.lowCardinalityKeyValue("lowTag", "lowTagValue")
21-
.highCardinalityKeyValue("highTag", "highTagValue")
22-
.observe(() -> {
23-
sleep(100);
24-
runInternal();
25-
sleep(100);
26-
});
27-
}
28-
29-
private void runInternal() {
30-
Observation.createNotStarted("bar", registry)
31-
.lowCardinalityKeyValue("lowTag", "lowTagValue")
32-
.highCardinalityKeyValue("highTag", "highTagValue")
33-
.observe(() -> {
34-
sleep(100);
35-
System.out.println("Hello");
36-
sleep(100);
37-
});
38-
}
39-
40-
static void sleep(long millis) {
41-
try {
42-
Thread.sleep(millis);
43-
} catch (InterruptedException e) {
44-
e.printStackTrace();
45-
}
19+
logger.info(service.sayHello("world"));
4620
}
4721
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example;
2+
3+
import io.opentelemetry.api.OpenTelemetry;
4+
import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
5+
import org.springframework.beans.factory.InitializingBean;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
class InstallOpenTelemetryAppender implements InitializingBean {
10+
11+
private final OpenTelemetry openTelemetry;
12+
13+
InstallOpenTelemetryAppender(OpenTelemetry openTelemetry) {
14+
this.openTelemetry = openTelemetry;
15+
}
16+
17+
@Override
18+
public void afterPropertiesSet() {
19+
OpenTelemetryAppender.install(this.openTelemetry);
20+
}
21+
}

observability-example/observability-example-web/src/main/java/com/example/ObservedConfig.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

observability-example/observability-example-web/src/main/java/com/example/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class WebSecurityConfig {
1010

1111
@Bean
12-
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
12+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
1313
return http.authorizeHttpRequests(c -> c.anyRequest().permitAll()).build();
1414
}
1515
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
spring.application.name=observability-example-web
12
spring.security.user.password=pass
23

3-
management.endpoints.web.exposure.include=health, metrics, prometheus
4+
# management.endpoints.web.exposure.include=health, metrics, prometheus
45

5-
# すべてのリクエストがバックエンド(ここではZipkin)へ送信されるようにする
6+
# すべてのリクエストのトレースが送信されるようにする
67
# (デフォルトでは過負荷を避けるためリクエストの10%だけを送信するようになっている)
78
management.tracing.sampling.probability=1.0
9+
10+
management.observations.annotations.enabled=true
11+
12+
management.opentelemetry.tracing.export.otlp.endpoint=http://localhost:4318/v1/traces
13+
management.opentelemetry.logging.export.otlp.endpoint=http://localhost:4318/v1/logs
14+
management.otlp.metrics.export.url=http://localhost:4318/v1/metrics
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
4+
5+
<appender name="OTEL" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
6+
</appender>
7+
8+
<root level="INFO">
9+
<appender-ref ref="CONSOLE"/>
10+
<appender-ref ref="OTEL"/>
11+
</root>
12+
</configuration>

0 commit comments

Comments
 (0)