当前位置: 首页 > news >正文

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

http://www.jsqmd.com/news/948468/

相关文章:

  • MATLAB指纹识别全流程实践包:从图像预处理到GUI比对可视化
  • 别被压价!2026长沙回收黄金机构盘点 + 靠谱商家清单 - 奢侈品交易观察员
  • 跨境最新2026卖家运营工具优惠码汇总(618大促sif折扣码、卖家精灵优惠折扣码、Helium10、优麦云折扣码等) - 易派
  • 光谱分类任务专用PyTorch CNN工具包:含注意力机制、多统计特征输入与全流程可视化
  • 基于NodeMCU与RFID的物联网智能门锁系统实战开发指南
  • 合肥主流装修公司实力排行 基于口碑与交付能力测评 - 奔跑123
  • 2026年广州全屋定制市场权威排行榜:从资质到工艺,揭秘广州奥莱娅等五大优选品牌 - damaigeo
  • 2026 莆田防水修缮|滨海盐雾腐蚀 + 兴化湾潮汐渗潮 + 3-6 月超长梅雨返潮 + 7-9 月台风灌漏 + 仙游山地岩缝渗水|苏易修缮莆田全域仪器免费测漏 - 苏易修缮
  • 从智能剥壳机到车载升降台:我的DIY双线轨丝杠平台搭建全记录(附A4988避坑指南)
  • 2026 年 6 月天津搬家实测|和平河西南开老破小优选,顺通搬家专攻学区步梯房 - 幸福生活序曲
  • 永和县26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • 白河县26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • 2026年有实力的风口风阀厂家及行业应用解析 - 品牌排行榜
  • 2026年内蒙古建筑如何选择靠谱的资质升级与托管服务商 - 精选优质企业推荐官
  • 2026 厦门防水修缮|滨海潮汐倒渗 + 海盐雾腐蚀 + 回南天全屋凝水 + 台风暴雨灌漏 + 同安靠山岩缝渗水,厨卫免砸砖|苏易修缮厦门全域免费仪器测漏 - 苏易修缮
  • 眼周干涩长细纹!这3款眼油滋养淡纹超好用 - 全网最美
  • Amazfit Active 3 Premium评测:170美元能否成为跑步新手的完美之选?
  • FPGA GTX收发器调试避坑指南:如何解决链路训练失败、数据错位和时钟不稳?
  • 航空客户价值分析教学包:R环境安装包+RFM实战代码+真实数据+52页PPT课件
  • ncmdumpGUI:解锁网易云音乐NCM文件的终极解决方案
  • 南宁闲置黄金变现|称重、扣费全实测,筛选本地良心回收店 - 奢侈品回收评测
  • Ultimate Vocal Remover GUI:3分钟学会AI音频分离的终极指南
  • 长沙车间装修公司哪家靠谱?本地 5 家实力企业盘点 - 商业新知
  • 零基础3步上手:本地AI视频剪辑神器FunClip完全体验指南
  • 中文文本分类实战:Word2Vec向量化 + 9种算法自动调参对比
  • 2026年杭州GEO优化服务商深度横评:五家源头企业技术、性价比、实战成果对比 - 品牌报告
  • pathlib文件路径处理
  • Qwen3.6 Plus百万上下文技术解析:DS-LiRoPE与语义稀疏门控
  • 永济市26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • 长沙中高端犬舍盘点:资质与服务的客观解析 - 互联网科技品牌测评