適用於 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。<p>

設定

要開始擷取 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
    # 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=http://localhost:4317
    
  3. 執行 docker 命令以觸發 CLI 將指標訊號發送到 OpenTelemetry 收集器。

    $ docker version
    
  4. 要檢視 CLI 建立的遙測指標,請前往 http://localhost:9091/graph可用的指標

    Docker CLI 目前匯出單一指標 `command.time`,它以毫秒為單位測量命令的執行持續時間。此指標具有以下屬性