JaegerでSpringBootアプリケーションをトレーシング
概要
Jeagerを触ってみたので導入方法をメモしておきます
トレーシング対象のアプリケーションはSpringBootです。
導入
Jaeger起動
今回はお試しということでall in oneで起動します
公式ドキュメントを参考にdocker-composeで起動できるようにします。
version: '2'
services:
jeager:
image: jaegertracing/all-in-one:1.32
ports:
- "6831:6831/udp" # accept jaeger.thrift over compact thrift protocol
- "6832:6832/udp" # accept jaeger.thrift over binary thrift protocol
- "5778:5778" # serve configs
- "16686:16686" # serve frontend
- "14250:14250" # accept model.proto
- "14268:14268" # accept jaeger.thrift directly from clients
- "14269:14269" #
- "9411:9411" # Zipkin compatible endpoint (optional)
environment:
COLLECTOR_ZIPKIN_HOST_PORT: ":9411"
起動!
docker-compose up -d
http://localhost:16686/ にアクセスしたらUIが表示されます🎉
アプリケーション実装
SpringBootアプリケーションのベースは https://start.spring.io/ で作成します。
ここではSpring Webのみ追加してベースを作成します。
Zipをダウンロード&解答したらJeagerとの連携に必要なライブラリを追加します。
dependencies {
implementation 'io.opentracing.contrib:opentracing-spring-jaeger-web-starter:3.3.1'
implementation 'io.opentracing.contrib:opentracing-spring-web-starter:4.1.0'
}
補足: 追加するライブラリは以下から探します
※ここは結構試行錯誤必要かも。。。
サンプルで動作させるAPIを実装します。
@RestController
@RequiredArgsConstructor
public class SampleController {
private final RestTemplate restTemplate;
@GetMapping("/proxy/hello")
public String proxyToHello() {
return restTemplate.getForObject("/hello", String.class);
}
@GetMapping("/hello")
public String hello() {
return "Hello, jaeger.";
}
}
RestTemplateはBean登録している必要があるので、コンストラクタインジェクションで注入するようにしています。
最後にRestTemplateのBean登録です。
@Bean
public RestTemplate restTemplate() {
return new RestTemplateBuilder()
.rootUri("http://localhost:8080")
.build();
}
動作確認
APIにリクエストを送って
$ curl localhost:8080/proxy/hello
Hello, jaeger.
UIで検索してみると
トレーシングできてる!!!🎉🎉🎉
DBもトレーシング
DBまでトレースできるようにしてみます。
DBとしてH2DBを使い、jpaが使えるよう依存を追加します。
またDBのトレースができるよう依存を追加します。
dependencies {
implementation 'io.opentracing.contrib:opentracing-jdbc:0.2.15'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
}
DBのトレースをするには、Driverとurlの指定を普段を変える必要があります。
spring:
datasource:
# driver-class-name: "org.h2.Driver"
driver-class-name: "io.opentracing.contrib.jdbc.TracingDriver"
# url: "jdbc:h2:./h2db/sample"
url: "jdbc:tracing:h2:./h2db/sample"
APIをDBアクセスしてidに紐づくnameに対して挨拶するような感じにしてみます
※DB、jpa周りは省略🙏
@RestController
@RequiredArgsConstructor
public class SampleController {
private final RestTemplate restTemplate;
private final UserRepository userRepository;
@GetMapping("/proxy/hello/{id}")
public String proxyToHello(@PathVariable String id) {
return restTemplate.getForObject("/hello/{id}", String.class, id);
}
@GetMapping("/hello/{id}")
public String hello(@PathVariable String id) {
var user = userRepository.findById(id);
return "Hello, " + user.map(User::getName).orElse("jaeger") + ".";
}
}
curlでAPIにリクエストを送ってみると
$ curl localhost:8080/proxy/hello/1
Hello, Ryo.
UIでDBの情報も見れるようになりました🎉🎉🎉
Author And Source
この問題について(JaegerでSpringBootアプリケーションをトレーシング), 我々は、より多くの情報をここで見つけました https://qiita.com/rhirabay/items/922d0b0a50ce43afa922著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .