OpenTelemetry Distro
为了尽可能快速地使用 OpenTelemetry 和自动插装,同时又不牺牲灵活性,OpenTelemetry Distro 提供了一种机制来自动配置一些最常见的用户选项。通过利用其强大功能,OpenTelemetry 用户可以按需配置组件。opentelemetry-distro 包为希望入门的用户提供了一些默认设置,它配置了
- SDK TracerProvider
- BatchSpanProcessor
- OTLP
SpanExporter以将数据发送到 OpenTelemetry Collector
该包还为有兴趣创建替代 Distro 的任何人提供了起点。该包实现的接口由自动插装通过 opentelemetry_distro 和 opentelemetry_configurator 入口点加载,以在任何其他代码执行之前配置应用程序。
为了自动将数据从 OpenTelemetry 导出到 OpenTelemetry Collector,安装该包将设置所有必需的入口点。
pip install opentelemetry-distro[otlp] opentelemetry-instrumentation
在本地启动 Collector 以查看正在导出的数据。编写以下文件
# /tmp/otel-collector-config.yaml
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
exporters:
# NOTE: Prior to v0.86.0 use `logging` instead of `debug`.
debug:
verbosity: detailed
service:
pipelines:
traces:
receivers: [otlp]
exporters: [debug]
然后启动 Docker 容器
docker run -p 4317:4317 \
-v /tmp/otel-collector-config.yaml:/etc/otel-collector-config.yaml \
otel/opentelemetry-collector:latest \
--config=/etc/otel-collector-config.yaml
以下代码将创建一个没有任何配置的 span。
# no_configuration.py
from opentelemetry import trace
with trace.get_tracer("my.tracer").start_as_current_span("foo"):
with trace.get_tracer("my.tracer").start_as_current_span("bar"):
print("baz")
最后,使用自动插装运行 no_configuration.py
opentelemetry-instrument python no_configuration.py
生成的 span 将出现在 Collector 的输出中,看起来与此类似
Resource labels:
-> telemetry.sdk.language: STRING(python)
-> telemetry.sdk.name: STRING(opentelemetry)
-> telemetry.sdk.version: STRING(1.1.0)
-> service.name: STRING(unknown_service)
InstrumentationLibrarySpans #0
InstrumentationLibrary __main__
Span #0
Trace ID : db3c99e5bfc50ef8be1773c3765e8845
Parent ID : 0677126a4d110cb8
ID : 3163b3022808ed1b
Name : bar
Kind : SPAN_KIND_INTERNAL
Start time : 2021-05-06 22:54:51.23063 +0000 UTC
End time : 2021-05-06 22:54:51.230684 +0000 UTC
Status code : STATUS_CODE_UNSET
Status message :
Span #1
Trace ID : db3c99e5bfc50ef8be1773c3765e8845
Parent ID :
ID : 0677126a4d110cb8
Name : foo
Kind : SPAN_KIND_INTERNAL
Start time : 2021-05-06 22:54:51.230549 +0000 UTC
End time : 2021-05-06 22:54:51.230706 +0000 UTC
Status code : STATUS_CODE_UNSET
Status message :