Docker CLI 的 OpenTelemetry
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。它包括一個 Docker CLI 可以傳送指標的 OpenTelemetry 收集器,以及一個從收集器抓取指標的 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.yaml
並列存在:
- 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=https://:4317
執行
docker
命令以觸發 CLI 向 OpenTelemetry 收集器傳送指標訊號。$ docker version
要檢視 CLI 建立的遙測指標,請訪問 https://:9091/graph 開啟 Prometheus 表示式瀏覽器。
在查詢欄位中,輸入
command_time_milliseconds_total
,並執行查詢以檢視遙測資料。
可用指標
Docker CLI 當前僅匯出單個指標 command.time
,它測量命令的執行時長(以毫秒為單位)。此指標具有以下屬性:
command.name
: 命令的名稱command.status.code
: 命令的退出碼command.stderr.isatty
: 如果 stderr 連線到 TTY,則為 truecommand.stdin.isatty
: 如果 stdin 連線到 TTY,則為 truecommand.stdout.isatty
: 如果 stdout 連線到 TTY,則為 true