对于 Java 应用,OpenTelemetry 提供了自动埋点的功能,OpenTelemetry 提供的 Java 自动检测功能可附加到任何 Java 8+ 应用程序的 Java agent JAR,它动态注入字节码自动从很多流行的库和框架捕获遥测数据。它可用于捕获应用程序或服务“边缘”的遥测数据,例如入站请求、出站 HTTP 调用、数据库调用等。

对于 Java 应用,OpenTelemetry 提供了 3 个代码仓库:

  • opentelemetry-java:这个仓库是主要的 OpenTelemetry Java SDK,提供手动埋点的组件。顶级组件包括 OpenTelemetry API,扩展,SDK,用于 OpenTracing 和 OpenCensus 的桥接层。
  • opentelemetry-java-instrumentation:这是 opentelemetry-java的姐妹项目,提供一体化、易于安装的自动埋点 Java Agent。OpenTelemetry Java 代理使您能够从许多流行的库和框架中捕获遥测数据。您需要将它附加到任何 Java 8+ 应用程序上。
  • opentelemetry-java-contrib:用于覆盖不适合 opentelemetry-javaopentelemetry-java-instrumentation 范围的基于 JVM 的应用程序和工作流程。

trace自动埋点

可以从官方的 opentelemetry-java-instrumentation 仓库的 releases 页面下载最新的

这里在码云上也存放了一份opentelemetry-javaagent.jar

本地调试

  • 这里以服务 dj-scrm 为例
    • -javaagent: 这里填入agent路径
    • -Dotel.service.name 填入服务名
    • -Dotel.traces.exporter 输出trace协议为otlp
    • -Dotel.metrics.exporter 此处关闭metrics
    • -Dotel.logs.exporter 此处关闭logs
    • -Dotel.exporter.otlp.endpoint 这里填写otlp服务端地址,此处连接vpn后填入测试环境的内网IP b

具体参数如下所示

java -javaagent:path/to/opentelemetry-javaagent.jar \
     -Dotel.service.name=dj-scrm \  
     -Dotel.traces.exporter=otlp \
     -Dotel.metrics.exporter="none" \
     -Dotel.logs.exporter="none" \
     -Dotel.exporter.otlp.endpoint=http://192.168.0.149:4318 \   
     -jar myapp.jar

日志打印TraceId与SpanId

参考文档

实现效果如下

2024-06-26 10:56:31.200 trace_id=8f7ebd8a73f9a8f50e6a00a87a20952a span_id=1b08f18b8858bb9a  INFO 53724 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-26 10:56:31.201 trace_id=8f7ebd8a73f9a8f50e6a00a87a20952a span_id=1b08f18b8858bb9a  INFO 53724 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-26 10:56:31.209 trace_id=8f7ebd8a73f9a8f50e6a00a87a20952a span_id=1b08f18b8858bb9a  INFO 53724 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms
2024-06-26 10:56:31.296 trace_id=8f7ebd8a73f9a8f50e6a00a87a20952a span_id=5743699405074f4e  INFO 53724 --- [nio-8081-exec-1] com.example.httpserver.ot.OTServer       : hello world

引入Metrics接口

Spring Boot Actuator

Spring Boot 作为最主流的 Java Web 框架, 自然也少不了对监控的支持, 那就是 Actuator, 要使用 Actuator 需要先添加依赖:

参考文档

server:
  port: 8080
spring:
  application:
    name: spring-demo
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus' # 暴露/actuator/prometheus
  metrics:
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application label

然后启动应用, 访问 http://localhost:8080/actuator/prometheus 应该会得到如下结果:

# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Eden Space",} 1.77733632E8
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Metaspace",} 3.6880384E7
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Old Gen",} 1.53092096E8
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Survivor Space",} 1.4680064E7
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Compressed Class Space",} 5160960.0
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Code Cache",} 7798784.0
# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_classes_total counter
jvm_classes_unloaded_classes_total{application="spring-demo",} 0.0
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
jvm_memory_max_bytes{application="spring-demo",area="nonheap",id="Code Cache",} 2.5165824E8
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="spring-demo",} 7010.0
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="spring-demo",} 24.0
# HELP jvm_threads_states_threads The current number of threads having NEW state

# 太长, 后面省略

这就是 Prometheus exporter 的格式