当前位置: 首页 > 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.gsyq.cn/news/1459992.html

相关文章:

  • 终极Steam成就管理指南:如何使用开源工具轻松解锁游戏成就 [特殊字符]
  • MATLAB指纹识别全流程实践包:从图像预处理到GUI比对可视化
  • 别被压价!2026长沙回收黄金机构盘点 + 靠谱商家清单 - 奢侈品交易观察员
  • 2026 莆田防水修缮|滨海盐雾腐蚀 + 兴化湾潮汐渗潮 + 3-6 月超长梅雨返潮 + 7-9 月台风灌漏 + 仙游山地岩缝渗水|苏易修缮莆田全域仪器免费测漏 - 苏易修缮
  • 2026 年 6 月天津搬家实测|和平河西南开老破小优选,顺通搬家专攻学区步梯房 - 幸福生活序曲
  • 永和县26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • 2026年有实力的风口风阀厂家及行业应用解析 - 品牌排行榜
  • FreeCAD完全指南:5个实用场景教你掌握开源3D建模软件
  • 2017年全国铁路线与客运站矢量数据包(WGS84坐标,含站名/等级/所属线路属性)
  • 3分钟上手:iFakeLocation让你的iOS设备自由穿梭全球位置
  • 白水县26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • 抖音批量下载助手:你的个人视频收藏管家
  • 3步快速部署:VdhCoApp在Mac系统上的完整安装与配置指南
  • 宝藏眼油推荐,养眼超厉害,这5款宝藏眼油淡纹提亮绝了 - 全网最美
  • 教育部新规下,论文抽检更严了!8款AI查重降重工具真诚评析,这款绝对是你的菜。 - 逢君学术-AI论文写作
  • 北京黄金回收认准这五家,本地老店零差评不压价 - 奢侈品回收测评
  • 2026西宁本地甄选黄金白银铂金彩金回收靠谱商家TOP榜单及联系号码汇总 - 余生黄金回收
  • Mermaid Live Editor终极指南:3分钟从代码小白到图表高手
  • 25+初老必备3款眼油,平细纹清爽不黏超安心 - 全网最美
  • 沁源县26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • 榆社县26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • AI音频分离技术深度解析:Ultimate Vocal Remover核心原理与实战应用
  • 告别I2C中断烦恼:手把手教你用I3C第二主机实现多主控与高效带内中断
  • 2026重庆名表回收甄选榜单,精准控损,守住腕表巅峰价值 - 奢侈品回收测评
  • Autosar NXP S32K3xx系列 基于EB Tresos 配置复杂驱动MCAL 工程导入关联 技术分享
  • 通达信数据接口终极指南:5步构建你的量化交易数据源
  • 山西大同经济开发区26年最新专业手表包包回收权威店铺推荐,TOP排行榜 - 莘州文化
  • MATLAB太阳角度计算工具包:输入经纬度和时间,直接输出天顶角、方位角与高度角
  • GD32E230 ADC注入通道实战:用定时器2触发,1ms精准采样电机相电流
  • 海盗船收购艾格特后推Nightsword V2无线鼠标:130美元,续航最长170小时