如何通过OpenTelemetry Collector将链路追踪数据上报到APM
OpenTelemetry Collector作为数据收集和转发的代理,支持从多种来源接收数据,经过处理后导出到不同的后端。使用Collector可以实现数据的统一收集、过滤、增强和转发,降低应用端的开销。本文介绍如何通过OpenTelemetry Collector将应用的链路追踪数据上报至华为云应用性能管理(APM)服务。
前提条件
示例demo
使用springboot实现一个简单掷骰子应用。
- 编写业务代码,初始化一个springboot项目。
- 创建RollController.java文件,内容如下:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; @RestController public class RollDice { @GetMapping("/rolldice") public String rollDiceAndSave(@RequestParam("player") Optional<String> player) { int result = 0; //掷骰子 try { Thread.sleep(100); result = ThreadLocalRandom.current().nextInt(1, 7); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } saveResult(player.orElse("Anonymous player"), result); return Integer.toString(result); } private void saveResult(String player, int dicePoint) { //模拟数据库操作耗时 try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } - application.properties 内容如下:
server.port=8080
- 创建RollController.java文件,内容如下:
- 启动Demo,命令如下:
java -jar otel-demo-0.0.1-SNAPSHOT.jar & curl http://localhost:8080/rolldice
操作步骤
- 部署Collector。
- 访问GitHub Releases 页面确认版本,以ECS安装为例,示例如下。其他安装方式可参考Collector安装。
mkdir otel_collector && cd otel_collector wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.140.0/otelcol_0.140.0_linux_amd64.tar.gz tar -xvf otelcol_0.140.0_linux_amd64.tar.gz
- 创建config.yaml配置文件,用于定义collector的数据接收、处理和导出规则。内容参考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: pprof: endpoint: 0.0.0.0:1777 zpages: endpoint: 0.0.0.0:55679 receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 processors: # Data sources: traces resource: attributes: - key: service.name #可以在探针启动参数中定义 value: 应用名称.组件名称.环境名称 action: upsert exporters: # Data sources: traces, metrics, logs otlp: endpoint: 上报地址 headers: Authentication: "鉴权信息" tls: insecure: true service: pipelines: traces: #链路追踪数据启用对应的配置 receivers: [otlp] processors: [resource] exporters: [otlp] extensions: [health_check, pprof, zpages] - 配置完成后,启动 opentelemetry-collector。
./otelcol --config=./config.yaml &
- 访问GitHub Releases 页面确认版本,以ECS安装为例,示例如下。其他安装方式可参考Collector安装。
- 启动opentelemetry探针,命令如下:
java -javaagent:探针安装路径 \ -Dotel.exporter.otlp.protocol=grpc \ -Dotel.exporter.otlp.traces.endpoint=http://127.0.0.1:4317 \ #collector receivers.otlp.protocols.grpc.endpoint地址 -Dotel.metrics.exporter=none \ -Dotel.logs.exporter=none \ -Dotel.service.name=otel-czf.collector.dev \ #也可在collector的config.yaml中配置 -jar <yourApp>.jar
- 根据业务实际触发的业务请求情况,在APM控制台链路追踪中查看应用的监控数据。详细操作参见接口调用。
例如:调用demo接口“http://localhost:8080/rolldice”重复9次,可以在“接口调用”页面查看到“GET /rolldice”的调用次数为9。
