导出器
In order to visualize and analyze your telemetry, you will need to export your data to an OpenTelemetry Collector or a backend such as Jaeger, Zipkin, Prometheus or a vendor-specific one.
As part of OpenTelemetry Ruby you will find many exporters being available. Among them, the OpenTelemetry Protocol (OTLP) exporters provide the best experience for you as an end-user, since it is a general-purpose telemetry data delivery protocol designed in the scope of the OpenTelemetry project.
To learn more about the OTLP protocol, you can read the OTLP Specification.
Below you will find some introductions on how to set up exporters for OTLP and other common protocols in your code.
OTLP 端点
要将跟踪数据发送到 OTLP 端点(比如收集器或 Jaeger),您需要使用一个导出器包,比如 opentelemetry-exporter-otlp
:
bundle add opentelemetry-exporter-otlp
gem install opentelemetry-exporter-otlp
接下来,配置导出器以指向一个 OTLP 端点。例如,您可以通过在代码中添加 require 'opentelemetry-exporter-otlp'
来更新 config/initializers/opentelemetry.rb
文件:
# config/initializers/opentelemetry.rb
require 'opentelemetry/sdk'
require 'opentelemetry/instrumentation/all'
require 'opentelemetry-exporter-otlp'
OpenTelemetry::SDK.configure do |c|
c.service_name = 'dice-ruby'
c.use_all() # 启用所有仪表化!
end
现在,如果您运行应用程序,它将使用 OTLP 导出跟踪数据:
rails server -p 8080
默认情况下,跟踪数据会发送到在本地主机的端口 4318 上监听的 OTLP 端点。您可以通过设置 OTEL_EXPORTER_OTLP_ENDPOINT
来更改端点:
env OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318/v1/traces" rails server -p 8080
为了快速尝试 OTLP 导出器并在接收端可视化跟踪数据,您可以在 Docker 容器中运行 Jaeger:
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-e COLLECTOR_OTLP_ENABLED=true \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:latest
Zipkin
要尽快设置 Zipkin,可以在 Docker 容器中运行它:
docker run --rm -d -p 9411:9411 --name zipkin openzipkin/zipkin
将导出器包作为应用程序的依赖项安装:
bundle add opentelemetry-exporter-zipkin
gem install opentelemetry-exporter-zipkin
更新您的 OpenTelemetry 配置以使用导出器并将数据发送到您的 Zipkin 后端:
# config/initializers/opentelemetry.rb
require 'opentelemetry/sdk'
require 'opentelemetry/instrumentation/all'
require 'opentelemetry-exporter-zipkin'
OpenTelemetry::SDK.configure do |c|
c.service_name = 'dice-ruby'
c.use_all() # 启用所有仪表化!
end
现在,如果您运行应用程序,设置环境变量 OTEL_TRACES_EXPORTER
为 zipkin:
env OTEL_TRACES_EXPORTER=zipkin rails server
默认情况下,跟踪数据会发送到在本地主机的端口 9411 上监听的 Zipkin 端点。您可以通过设置 OTEL_EXPORTER_ZIPKIN_ENDPOINT
来更改端点:
env OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:9411" rails server