Docker CLI 的 OpenTelemetry

Introduced in Docker Engine version 26.1.0

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。 它包括一个CLI可以发送指标到的OpenTelemetry收集器, 以及一个从收集器抓取指标的Prometheus后端。

compose.yml
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
    # 通过gRPC和HTTP接收信号
    receivers:
      otlp:
        protocols:
          grpc:
          http:
    
    # 为Prometheus建立一个抓取端点
    exporters:
      prometheus:
        endpoint: "0.0.0.0:8889"
    
    service:
      pipelines:
        metrics:
          receivers: [otlp]
          exporters: [prometheus]
  • prom.yml
    # 配置 Prometheus 以抓取 OpenTelemetry 收集器端点
    scrape_configs:
      - job_name: "otel-collector"
        scrape_interval: 1s
        static_configs:
          - targets: ["otelcol:8889"]

有了这些文件后:

  1. 启动 Docker Compose 服务:

    $ docker compose up
    
  2. 配置Docker CLI以将遥测数据导出到OpenTelemetry收集器。

    $ export DOCKER_CLI_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
    
  3. 运行一个docker命令来触发CLI向OpenTelemetry收集器发送一个度量信号。

    $ docker version
    
  4. 要查看由CLI创建的遥测指标,请通过访问http://localhost:9091/graph打开Prometheus表达式浏览器。

  5. 查询字段中,输入command_time_milliseconds_total,并执行查询以查看遥测数据。

可用指标

Docker CLI 目前导出了一个单一的指标,command.time,它测量命令的执行时间,单位为毫秒。该指标具有以下属性:

  • command.name: 命令的名称
  • command.status.code: 命令的退出代码
  • command.stderr.isatty: 如果stderr连接到TTY,则为true
  • command.stdin.isatty: 如果 stdin 连接到 TTY,则为 true
  • command.stdout.isatty: 如果标准输出连接到TTY,则为true