Skip to content

Commit

Permalink
opentelemetry-spring-boot-starterで事足りるっぽい。ただJVMのメトリクスを取るにはmicrometer-…
Browse files Browse the repository at this point in the history
…registry-otlpが必要っぽいかな?
  • Loading branch information
backpaper0 committed Mar 22, 2024
1 parent 609b86b commit 8a85050
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 13 deletions.
8 changes: 5 additions & 3 deletions telemetry-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ mvn spring-boot:run
リクエストを送信する。

```
while true; do (curl -s localhost:8080/count|jq -cS); sleep 1; done
while true; do (curl -s localhost:8080/rolldice -G -d rolls=3|jq -cS); sleep 1; done
```

[prometheus](http://localhost:9090/graph?g0.expr=example_count_1&g0.tab=0&g0.stacked=1&g0.show_exemplars=0&g0.range_input=5m&g1.expr=example_count_2&g1.tab=0&g1.stacked=1&g1.show_exemplars=0&g1.range_input=5m&g2.expr=example_count_3_total&g2.tab=0&g2.stacked=1&g2.show_exemplars=0&g2.range_input=5m)でメトリクスを確認する
SigNozでテレメトリーを確認する

## その他

Expand All @@ -27,4 +27,6 @@ while true; do (curl -s localhost:8080/count|jq -cS); sleep 1; done
## 参考

- https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics.supported

- https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/v2.2.0/instrumentation/spring
- https://github.com/open-telemetry/opentelemetry-java/blob/v1.36.0/sdk-extensions/autoconfigure/README.md
- https://opentelemetry.io/docs/languages/java/automatic/spring-boot/
26 changes: 19 additions & 7 deletions telemetry-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>metrics-example</artifactId>
<artifactId>telemetry-example</artifactId>

<properties>
<otel-inst.version>2.2.0-alpha</otel-inst.version>
</properties>

<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -41,6 +41,18 @@
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
<version>${otel-inst.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>app</finalName>
<plugins>
Expand Down
22 changes: 22 additions & 0 deletions telemetry-example/src/main/java/com/example/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

import org.springframework.stereotype.Component;

import io.opentelemetry.instrumentation.annotations.WithSpan;

@Component
public class Dice {

@WithSpan
public List<Integer> rollTheDice(int rolls) {
return IntStream.range(0, rolls).map(a -> rollOnce()).boxed().toList();
}

private int rollOnce() {
return ThreadLocalRandom.current().nextInt(1, 7);
}
}
33 changes: 33 additions & 0 deletions telemetry-example/src/main/java/com/example/DiceController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DiceController {

private final Logger logger = LoggerFactory.getLogger(getClass());

private final Dice dice;

public DiceController(Dice dice) {
this.dice = dice;
}

@GetMapping("/rolldice")
public List<Integer> rolldice(@RequestParam(required = false) String player,
@RequestParam int rolls) {
List<Integer> result = dice.rollTheDice(rolls);
if (player != null) {
logger.info("{} is rolling the dice: {}", player, result);
} else {
logger.info("Anonymous player is rolling the dice: {}", result);
}
return result;
}
}
6 changes: 3 additions & 3 deletions telemetry-example/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.application.name=telemetry-example

management.tracing.sampling.probability=1.0
# management.otlp.metrics.export.url=http://localhost:4318/v1/metrics
management.otlp.tracing.endpoint=http://localhost:4318/v1/traces
otel.exporter.otlp.traces.endpoint=http://localhost:4318/v1/traces
otel.exporter.otlp.metrics.endpoint=http://localhost:4318/v1/metrics
otel.exporter.otlp.logs.endpoint=http://localhost:4318/v1/logs

0 comments on commit 8a85050

Please sign in to comment.