Docker CLI 的 OpenTelemetry

在Docker Engine 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收集器端点
  • 运行一个接收来自CLI命令调用的信号的OpenTelemetry收集器
  • 运行一个后端来存储从收集器接收到的数据

以下 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
    # 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"]

准备好这些文件后

  1. 启动 Docker Compose 服务。

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

    $ export DOCKER_CLI_OTEL_EXPORTER_OTLP_ENDPOINT=https://#:4317
    
  3. 运行一个docker命令,触发 CLI 向 OpenTelemetry 收集器发送指标信号。

    $ docker version
    
  4. 要查看 CLI 创建的遥测指标,请访问https://#: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:如果 stdout 附加到 TTY,则为 true