全部產品
Search
文件中心

Managed Service for Prometheus:基於 OpenTelemetry Collector 寫入指標到阿里雲 Prometheus

更新時間:Jun 20, 2025

該方案將部署在 ACK 容器叢集,且已經進行 OpenTelemetry SDK 埋點的應用,通過阿里雲可觀測監控 Prometheus 版實現指標全面監控。統一採集 OTel 原生指標、業務自訂埋點指標和 Trace Span 轉化的指標,實現應用全面監控。將 Trace 資料轉換為指標,可以有效降低資料攝取量。

OpenTelemetry 概述

OpenTelemetry(簡稱 OTel)是一個開源的可觀測性架構,提供統一的 API、SDK 和工具,用於產生、收集和匯出分布式系統的遙測資料(包括指標(Metrics)追蹤(Traces)和日誌(Logs))。其核心目標是解決不同工具和系統之間的可觀測性資料片段化問題。

OpenTelemetry 的指標(Metrics)是量化系統行為的結構化資料,用於監控系統效能和健康狀態。指標來源取決於不同語言的 SDK 或 Agent 埋點。以 Java 應用為例,通常指標包括:JVM 標準指標、使用者自埋點指標和 Trace 資料轉換的指標。

OpenTelemetry 的指標設計與 Prometheus 相容,通過 OpenTelemetry Collector 可以將指標轉換為 Prometheus 格式,從而無縫對接阿里雲可觀測監控 Prometheus 版

OpenTelemetry Collector 的作用

OpenTelemetry Collector 是一個可擴充的資料處理管道,負責從資料來源(如應用、服務)收集遙測資料,並將其轉換為目標系統(如 Prometheus)所需的格式。

資料收集

應用通過 OpenTelemetry SDK 產生指標資料,通過 OTLP(OpenTelemetry Protocol) 或其他協議(如 Prometheus 原生協議)發送到 Collector。

資料轉換

在社區 Collector 擴充中,您可以使用以下兩種 Exporter 將 OTel 指標轉換為 Prometheus 格式。

  • Prometheus Exporter 將 OpenTelemetry 指標轉換為 Prometheus 格式,並提供端點供 Prometheus Agent 來抓取資料。

    1. 指標名稱映射:將 OpenTelemetry 指標名稱轉換為 Prometheus 相容的格式。

    2. 標籤處理:保留或重新命名標籤以符合 Prometheus 的命名規則。

    3. 資料類型轉換

      • gauge → Prometheus gauge

      • sum → Prometheus countergauge(根據 Monotonic 屬性)

      • histogram → Prometheus histogram(通過 bucketsum 子指標)

      參考配置如下,此配置意味著在 1234連接埠上提供指標抓取 Endpoint。

      exporters:
        prometheus:
          endpoint: "0.0.0.0:1234"
          namespace: "acs"
          const_labels:
            label1: value1
          send_timestamps: true
          metric_expiration: 5m
          enable_open_metrics: true
          add_metric_suffixes: false
          resource_to_telemetry_conversion:
            enabled: true
  • Prometheus Remote Write Exporter 將 OpenTelemetry 指標轉換為 Prometheus 格式,並直接通過 RemoteWrite 協議寫入目標 Prometheus 服務。

    與 Prometheus Exporter 一樣,該 Exporter 同樣會進行資料格式的轉換。參考配置如下:

    exporters:
      prometheusremotewrite:
        endpoint: http://<Prometheus Endpoint>/api/v1/write
        namespace: "acs"
        resource_to_telemetry_conversion:
          enabled: true
        timeout: 10s   
        headers:
          Prometheus-Remote-Write-Version: "0.1.0"
        external_labels:
          data-mode: metrics

在 ACK 叢集中部署的應用最佳實務

一、阿里雲 Prometheus 服務準備

  • 叢集已開啟 Prometheus 監控

    如果您的 ACK 叢集已經開啟 Prometheus 監控,那麼 Prometheus 執行個體已經存在。您可以登入CloudMonitor控制台,在Prometheus 監控 > 執行個體列表頁面,查詢與 ACK 容器叢集名稱一致的 Prometheus 執行個體。image

  • 叢集未開啟 Prometheus 監控

    您可以登入Container Service管理主控台,單擊目的地組群,然後在組件管理頁面安裝 ack-arms-prometheus 組件,安裝後會自動開啟叢集的 Prometheus 監控。58

二、Collector 部署(SideCar 模式)

由於指標統計計算要求同一個 Pod 執行個體的 Metric 或 Trace 到達同一個 Collector,採用 Gateway 模式部署需要處理負載平衡,相對複雜。因此建議您直接採用 SideCar 模式部署 Collector。

使用 Prometheus Exporter 模式

說明

該方式的好處是使用者不需要處理 Prometheus 的寫入鏈路鑒權相關事項,同時可以調整採集配置來調整指標採集時間間隔。

部署架構圖
部署配置參考
# Kubernetes Deployment 樣本
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    metadata:
      labels:
        # Pod 上打入特定標籤,通常以應用資訊來命名,方便後續配置指標採集
        observability: opentelemetry-collector
    spec:
      volumes:
      - name: otel-config-volume
        configMap:
          # 該配置通過下述的 Collector 配置參考建立
          name: otel-config
      containers:
        - name: app
          image: your-app:latest
          env:
            - name: OTEL_EXPORTER_OTLP_ENDPOINT
              value: http://localhost:4317
        - name: otel-collector
          # 可直接使用我們提供的 Collector 鏡像(包含了Prometheus相關擴充外掛程式)
          # 根據地區資訊,替換鏡像名稱中的regionId
          image: registry-<regionId>.ack.aliyuncs.com/acs/otel-collector:v0.128.0-7436f91	
          args: ["--config=/etc/otel/config/otel-config.yaml"]
          ports:
            - containerPort: 1234  # Prometheus 端點
              name: metrics
          volumeMounts:
          - name: otel-config-volume
            mountPath: /etc/otel/config
Collector 配置參考
說明

請根據應用請求量配置 Collector 的資源限制(CPU/Memory),使其可以正常處理所有資料。

apiVersion: v1
kind: ConfigMap
metadata:
  name: otel-config
  namespace: <app-namespace>
data:
  otel-config.yaml: |
    extensions:
      zpages:
        endpoint: localhost:55679

    receivers:
      otlp:
        protocols:
          grpc:
            endpoint: 0.0.0.0:4317
          http:
            endpoint: 0.0.0.0:4318
    
    processors:
      batch:
      memory_limiter:
        # 75% of maximum memory up to 2G
        limit_mib: 1536
        # 25% of limit up to 2G
        spike_limit_mib: 512
        check_interval: 5s
      resource:
        attributes:
          - key: process.runtime.description
            action: delete
          - key: process.command_args
            action: delete
          - key: telemetry.distro.version
            action: delete
          - key: telemetry.sdk.name
            action: delete
          - key: telemetry.sdk.version
            action: delete
          - key: service.instance.id
            action: delete
          - key: process.runtime.name
            action: delete
          - key: process.runtime.description
            action: delete
          - key: process.pid
            action: delete
          - key: process.executable.path
            action: delete
          - key: process.command.args
            action: delete
          - key: os.description
            action: delete
          - key: instance
            action: delete
          - key: container.id
            action: delete
    
    connectors:
      spanmetrics:
        histogram:
          explicit:
            buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10]
        dimensions: 
          - name: http.method
            default: "GET"
          - name: http.response.status_code
          - name: http.route
            # 自訂 Attribute
          - name: user.id
        metrics_flush_interval: 15s
        exclude_dimensions:
        metrics_expiration: 3m
        events:
          enabled: true
          dimensions: 
          - name: default
            default: "GET"
    
    
    exporters:
      debug:
        verbosity: detailed
      prometheus:
        endpoint: "0.0.0.0:1234"
        namespace: "acs"
        const_labels:
          label1: value1
        send_timestamps: true
        metric_expiration: 5m
        enable_open_metrics: true
        add_metric_suffixes: false
        resource_to_telemetry_conversion:
          enabled: true
            
    
    service:
      pipelines:
        logs:
          receivers: [otlp]
          exporters: [debug]
        traces:
          receivers: [otlp]
          processors: [resource]
          exporters: [spanmetrics]
        metrics:
          receivers: [otlp]
          processors: [memory_limiter, batch]
          exporters: [prometheus]
        metrics/2:
          receivers: [spanmetrics]
          exporters: [prometheus]
    
      extensions: [zpages]

該配置的含義是處理寫入的 Metrics 和 Traces 資料,通過 resource類型的 processors丟棄通常不關心的環境 Attributes,防止指標資料量過大。通過 spanmetrics將 Span 關鍵統計資料轉換為指標。

配置採集任務
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: opentelemetry-collector-podmonitor
  namespace: default
  annotations:
    arms.prometheus.io/discovery: "true"
spec:
  selector:
    matchLabels:
      observability: opentelemetry-collector
  podMetricsEndpoints:
  - port: metrics
    interval: 15s
    scheme: http
    path: /metrics

使用 Prometheus RemoteWrite Exporter 模式

說明
  • 該方式適用於資料量較大,採集不穩定情境,由 Collector 直接寫入到 Prometheus 執行個體。

  • 配置相對複雜,需要自行處理資料寫入鏈路配置。

部署架構圖
寫入資料鏈路準備

由於該方式是由 Collector 直接寫入資料到 Prometheus 執行個體,因此首先需要擷取 Prometheus 的 Endpoint 和認證資訊。

  • Endpoint 擷取

    您可以登入通過CloudMonitor控制台,在Prometheus 監控 > 執行個體列表頁面,尋找叢集對應的 Prometheus 執行個體(通常Prometheus 執行個體 ID 與 ACK 叢集 ID 一致,執行個體名稱與容器叢集名稱一致)。單擊目標執行個體名稱,在設定頁面,尋找 Remote Write 地址,擷取其中的內網地址待用。57

  • 認證資訊擷取

    您有以下兩種方案可以選擇:

    • V2 版本 Prometheus 執行個體支援配置免密策略,針對叢集當前 VPC 內網支援免密寫入。

    • 分配指標資料寫入子帳號,並分配 AliyunPrometheusMetricWriteAccess 系統角色許可權,擷取該子帳號的 AK/SK 分別作為寫入的帳號和密碼。

部署配置參考

Collector 部署配置與使用 Prometheus Exporter 模式一致,請直接參考上述Prometheus Exporter 部署配置

Collector 配置參考
apiVersion: v1
kind: ConfigMap
metadata:
  name: otel-config
  namespace: <app-namespace>
data:
  otel-config.yaml: |
    extensions:
    zpages:
      endpoint: localhost:55679

    receivers:
      otlp:
        protocols:
          grpc:
            endpoint: 0.0.0.0:4317
          http:
            endpoint: 0.0.0.0:4318
    
    processors:
      batch:
      memory_limiter:
        # 75% of maximum memory up to 2G
        limit_mib: 1536
        # 25% of limit up to 2G
        spike_limit_mib: 512
        check_interval: 5s
      resource:
        attributes:
          - key: process.runtime.description
            action: delete
          - key: process.command_args
            action: delete
          - key: telemetry.distro.version
            action: delete
          - key: telemetry.sdk.name
            action: delete
          - key: telemetry.sdk.version
            action: delete
          - key: service.instance.id
            action: delete
          - key: process.runtime.name
            action: delete
          - key: process.runtime.description
            action: delete
          - key: process.pid
            action: delete
          - key: process.executable.path
            action: delete
          - key: process.command.args
            action: delete
          - key: os.description
            action: delete
          - key: instance
            action: delete
          - key: container.id
            action: delete
    
    connectors:
      spanmetrics:
        histogram:
          explicit:
            buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10]
        dimensions: 
          - name: http.method
            default: "GET"
          - name: http.response.status_code
          - name: http.route
            # 自訂 Attribute
          - name: user.id
        metrics_flush_interval: 15s
        exclude_dimensions:
        metrics_expiration: 3m
        events:
          enabled: true
          dimensions: 
          - name: default
            default: "GET"
    
    
    exporters:
      debug:
        verbosity: detailed
      prometheusremotewrite:
        # 這裡替換為 Prometheus RemoteWrite 內網地址
        endpoint: http://<Endpoint>/api/v3/write
        namespace: "acs"
        resource_to_telemetry_conversion:
          enabled: true
        timeout: 10s   
        headers:
          Prometheus-Remote-Write-Version: "0.1.0"
          # 如果未開啟免密,該配置是必需的
          Authorization: Basic <base64-encoded-username-password>
        external_labels:
          data-mode: metrics
            
    
    service:
      pipelines:
        logs:
          receivers: [otlp]
          exporters: [debug]
        traces:
          receivers: [otlp]
          processors: [resource]
          exporters: [spanmetrics]
        metrics:
          receivers: [otlp]
          processors: [memory_limiter, batch]
          exporters: [prometheusremotewrite]
        metrics/2:
          receivers: [spanmetrics]
          exporters: [prometheusremotewrite]
    
      extensions: [zpages]
  • 使用上文中擷取到的地址替換配置中的 Prometheus RemoteWrite Endpoint 地址。

  • base64-encoded-username-password可以通過以下方式擷取:

    echo -n 'AK:SK' | base64

三、驗證用例