Opentelemetry在Java中的实践

📅 2026/7/23 4:49:19 👁️ 阅读次数 📝 编程学习
Opentelemetry在Java中的实践

简介

本文介绍使用Opentelemetry的javaagent jar包进行零代码插桩,并通过Collector收集和发送日志、指标、跟踪。每种数据都存储在不同的后端:

数据类型 对应后端
日志 Loki
指标 Prometheus
跟踪 Tempo

以及通过Grafana展示数据。
前提:Loki、Tempo、Grafana、Prometheus(write模式)的安装

安装

  1. 下载jar包,地址: https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases
  2. 配置环境参数
JAVA_TOOL_OPTIONS="-javaagent:/opt/runjar/opentelemetry/opentelemetry-javaagent.jar"
OTEL_SERVICE_NAME="energy"
OTEL_TRACES_EXPORTER="otlp"
OTEL_METRICS_EXPORTER="otlp"
OTEL_LOGS_EXPORTER="otlp"
OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317"
OTEL_EXPORTER_OTLP_PROTOCOL="grpc"
  • JAVA_TOOL_OPTIONS ‌是 Java 官方标准的环境变量‌,用于在 Java 程序启动时自动传递 JVM 参数,无需修改每个应用的启动命令
  • OTEL_SERVICE_NAME 指定服务名称,这个名称之后要用于搜索数据
  • OTEL_TRACES_EXPORTEROTEL_METRICS_EXPORTEROTEL_METRICS_EXPORTER 指定数据导出的格式,此处为otlp
  • OTEL_EXPORTER_OTLP_ENDPOINT Collector的地址
  • OTEL_EXPORTER_OTLP_PROTOCOL 数据传输的协议
    其他配置可查看官方文档:
  • https://opentelemetry.io/docs/languages/sdk-configuration/
  • https://opentelemetry.io/docs/languages/java/configuration/

以上环境变量可放入一个单独的文件.env中,然后在启动脚本开头加入以下命令以使环境变量生效:

set -a
source .env
set +a

Collector安装

此处使用DEB安装包进行安装,其他安装方式参考:https://opentelemetry.io/zh/docs/collector/install/

  1. 下载deb安装包,地址https://github.com/open-telemetry/opentelemetry-collector-releases/releases
  2. 安装命令 dpkg -i otelcol_0.153.0_linux_amd64.deb
  3. 修改配置文件
    安装完成后,会生成一个默认配置文件:/etc/otelcol/config.yaml
    改成如下配置:
# To limit exposure to denial of service attacks, change the host in endpoints below from 0.0.0.0 to a specific network interface.
# See https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/security-best-practices.md#safeguards-against-denial-of-service-attacksextensions:health_check: receivers: #接收器otlp:protocols:grpc:endpoint: 127.0.0.1:4317processors:batch:send_batch_size: 100filter:metric_conditions: #删除无用的指标- conditions:- metric.name == "http.client.request.duration"- IsMatch(metric.name, "http.server.request.duration.*")resource: #删除无用的资源属性attributes:- key: os.typeaction: delete- key: os.versionaction: delete- key: process.command_lineaction: delete- key: process.executable.pathaction: delete- key: process.runtime.descriptionaction: delete- key: process.runtime.nameaction: delete- key: process.runtime.versionaction: delete- key: telemetry.distro.nameaction: delete- key: telemetry.sdk.languageaction: delete- key: telemetry.distro.versionaction: delete- key: telemetry.sdk.nameaction: delete- key: telemetry.sdk.versionaction: delete
exporters: #导出器debug:verbosity: basicsampling_initial: 5sampling_thereafter: 200otlp_grpc:endpoint: http://127.0.0.1:4318sending_queue:queue_size: 100timeout: 20stls:insecure: trueotlp_http:endpoint: https://loki/otlpheaders:authorization: "xxx"X-Scope-OrgID: "yuanqu_zs"otlp_grpc/tmpo:endpoint: https://tmpoheaders:authorization: "xxx"prometheusremotewrite:endpoint: "https://prometheus/api/v1/write"resource_to_telemetry_conversion:enabled: false # Convert resource attributes to metric labelsheaders:authorization: "xxx"service:telemetry:metrics:level: none#telemetry:#logs:#level: debugpipelines: #管道traces:receivers: [otlp]processors: [resource,batch]exporters: [otlp_grpc/tmpo]metrics:receivers: [otlp]processors: [filter,resource,batch]exporters: [prometheusremotewrite]logs:receivers: [otlp]processors: [resource,batch]exporters: [otlp_http]extensions: [health_check]
  1. 启动命令
  • systemctl daemon-reload
  • systemctl restart otelcol

数据展示

上述都配置成功后,可从Grafana中参看数据
image
image
image
image