OpenTelemetry for Docker CLI
Docker CLI 支持
OpenTelemetry 检测
以发出有关命令调用的指标。默认情况下此功能是禁用的。
您可以配置 CLI 开始向指定的端点发送指标。
这使您能够捕获有关 docker 命令
调用的信息,以便更深入地了解您的 Docker 使用情况。
导出指标是可选的,您可以通过指定指标收集器的目标地址来控制数据的发送位置。
什么是 OpenTelemetry?
OpenTelemetry,简称 OTel,是一个用于创建和管理遥测数据(如链路追踪、指标和日志)的开源可观测性框架。 OpenTelemetry 是供应商和工具无关的,这意味着它可以与各种可观测性后端配合使用。
Docker CLI 对 OpenTelemetry 仪表化工具的支持意味着 CLI 可以按照 Open Telemetry 规范中定义的协议和约定,发出关于发生事件的信息。
工作原理
Docker CLI 默认不发送遥测数据。只有在您的系统上设置了环境变量时,Docker CLI 才会尝试向您指定的端点发送 OpenTelemetry 指标。
DOCKER_CLI_OTEL_EXPORTER_OTLP_ENDPOINT=<endpoint>该变量指定了 OpenTelemetry 收集器的端点,用于发送关于 docker CLI 调用的遥测数据。要捕获这些数据,您需要在那个端点上运行一个 OpenTelemetry 收集器。
采集器的目的是接收遥测数据,对其进行处理,并将其导出到后端。后端是存储遥测数据的地方。 您可以从多种不同的后端中选择,例如 Prometheus 或 InfluxDB。
某些后端提供了直接可视化指标的工具。 或者,您也可以运行一个专用的前端,支持生成更有用的图表,例如 Grafana。
设置
要开始捕获 Docker CLI 的遥测数据,您需要:
- 将
DOCKER_CLI_OTEL_EXPORTER_OTLP_ENDPOINT环境变量设置为指向 OpenTelemetry 收集器端点 - 运行一个 OpenTelemetry 收集器,用于接收来自 CLI 命令调用的信号
- 运行一个后端,用于存储从采集器接收到的数据
以下 Docker Compose 文件引导启动一组服务,以便开始使用 OpenTelemetry。 它包含一个 OpenTelemetry 收集器,CLI 可以将指标发送到该收集器, 以及一个从收集器抓取指标的 Prometheus 后端。
name: cli-otel
services:
prometheus:
image: prom/prometheus
command:
- "--config.file=/etc/prometheus/prom.yml"
ports:
# Publish the Prometheus frontend on localhost:9091
- 9091:9090
restart: always
volumes:
# Store Prometheus data in a volume:
- prom_data:/prometheus
# Mount the prom.yml config file
- ./prom.yml:/etc/prometheus/prom.yml
otelcol:
image: otel/opentelemetry-collector
restart: always
depends_on:
- prometheus
ports:
- 4317:4317
volumes:
# Mount the otelcol.yml config file
- ./otelcol.yml:/etc/otelcol/config.yaml
volumes:
prom_data:此服务假设以下两个配置文件与
compose.yml 并存:
- otelcol.yml
# Receive signals over gRPC and HTTP receivers: otlp: protocols: grpc: http: # Establish an endpoint for Prometheus to scrape from exporters: prometheus: endpoint: "0.0.0.0:8889" service: pipelines: metrics: receivers: [otlp] exporters: [prometheus] - prom.yml
# Configure Prometheus to scrape the OpenTelemetry collector endpoint scrape_configs: - job_name: "otel-collector" scrape_interval: 1s static_configs: - targets: ["otelcol:8889"]
有了这些文件:
启动 Docker Compose 服务:
$ docker compose up配置 Docker CLI 以将遥测数据导出到 OpenTelemetry 收集器。
$ export DOCKER_CLI_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317运行一个
docker命令以触发 CLI 向 OpenTelemetry 收集器发送度量信号。$ docker version要查看由 CLI 创建的遥测指标,请通过访问 http://localhost:9091/graph 打开 Prometheus 表达式浏览器。
在查询字段中,输入
command_time_milliseconds_total,然后执行 查询以查看遥测数据。
可用的指标
Docker CLI 当前导出一个单一指标 command.time,该指标以毫秒为单位测量命令的执行持续时间。该指标具有以下属性:
command.name: 命令的名称command.status.code: 命令的退出代码command.stderr.isatty: 如果 stderr 连接到 TTY,则为 truecommand.stdin.isatty: 如果标准输入连接到TTY,则为truecommand.stdout.isatty: 如果标准输出连接到TTY,则为true