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-attacks

extensions:
  health_check: 

receivers: #接收器
  otlp:
    protocols:
      grpc:
        endpoint: 127.0.0.1:4317

processors:
  batch:
    send_batch_size: 100
  filter:
    metric_conditions: #删除无用的指标
      - conditions:
          - metric.name == "http.client.request.duration"
          - IsMatch(metric.name, "http.server.request.duration.*")
  resource: #删除无用的资源属性
    attributes:
    - key: os.type
      action: delete
    - key: os.version
      action: delete
    - key: process.command_line
      action: delete
    - key: process.executable.path
      action: delete
    - key: process.runtime.description
      action: delete
    - key: process.runtime.name
      action: delete
    - key: process.runtime.version
      action: delete
    - key: telemetry.distro.name
      action: delete
    - key: telemetry.sdk.language
      action: delete
    - key: telemetry.distro.version
      action: delete
    - key: telemetry.sdk.name
      action: delete
    - key: telemetry.sdk.version
      action: delete
exporters: #导出器
  debug:
    verbosity: basic
    sampling_initial: 5
    sampling_thereafter: 200
  otlp_grpc:
    endpoint: http://127.0.0.1:4318
    sending_queue:
      queue_size: 100
    timeout: 20s
    tls:
      insecure: true
  otlp_http:
    endpoint: https://loki/otlp
    headers:
        authorization: "xxx"
        X-Scope-OrgID: "yuanqu_zs"
  otlp_grpc/tmpo:
    endpoint: https://tmpo
    headers:
        authorization: "xxx"
  prometheusremotewrite:
    endpoint: "https://prometheus/api/v1/write"
    resource_to_telemetry_conversion:
      enabled: false # Convert resource attributes to metric labels
    headers:
        authorization: "xxx"

service:
  telemetry:
    metrics:
      level: none
  #telemetry:
    #logs:
      #level: debug
  pipelines: #管道
    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

posted @ 2026-06-04 11:46  Hekk丶  阅读(21)  评论(0)    收藏  举报