在 Compute Engine 上部署 Google 建構的 OpenTelemetry 收集器

如要從檢測應用程式收集 OTLP 記錄、指標和追蹤記錄,並將該遙測資料匯出至 Google Cloud,您可以在 Compute Engine 虛擬機器 (VM) 執行個體上部署 Google 建構的 OpenTelemetry Collector。

事前準備

如要執行 Google 建構的 OpenTelemetry 收集器,必須具備下列資源:

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  4. Compute Engine 執行個體。如果您沒有 Compute Engine 執行個體,請按照「建立及啟動 Compute Engine 執行個體」一文中的操作說明操作。
  5. 安裝 gcloud。如要瞭解如何安裝 gcloud,請參閱「安裝 gcloud CLI」。

設定收集器的權限

根據預設,Compute Engine 執行個體會使用 Compute Engine 預設服務帳戶 PROJECT_NUMBER-compute@。這個服務帳戶通常具備必要的 Identity and Access Management (IAM) 角色,可寫入本文所述的指標和記錄:

如果您選擇使用使用者自行管理的服務帳戶,而非預設帳戶,則必須為該帳戶指派這些角色。詳情請參閱「服務帳戶」。

安裝收集器

如要將 Google 建構的 OpenTelemetry Collector 安裝為 Compute Engine 執行個體的套件,請完成下列步驟。

Debian 與 Ubuntu

  1. 使用 SSH 或類似工具開啟 VM 執行個體的終端機連線,並確認您具備 sudo 存取權。

  2. 執行下列指令,設定 Package Repository:

    sudo mkdir -p /etc/apt/keyrings
    sudo curl -o /etc/apt/keyrings/otelcol-google.asc https://packages.cloud.google.com/apt/doc/apt-key.gpg
    cat <<EOF | sudo tee /etc/apt/sources.list.d/otelcol-google.list
    deb [signed-by=/etc/apt/keyrings/otelcol-google.asc] https://us-apt.pkg.dev/projects/cloud-ops-agents-artifacts google-cloud-opentelemetry-collector-apt main
    EOF
    
  3. 重新整理套件管理工具並安裝套件:

    sudo apt update
    sudo apt install otelcol-google
    

    安裝完成後,Google 建構的 OpenTelemetry Collector 會自動啟動。

CentOS、RHEL 和 Rocky Linux

  1. 使用 SSH 或類似工具開啟 VM 執行個體的終端機連線,並確認您具備 sudo 存取權。

  2. 執行下列指令,設定 Package Repository:

    cat <<EOF | sudo tee /etc/yum.repos.d/otelcol-google.repo
    [otelcol-google]
    name=Google Built OpenTelemetry Collector
    baseurl=https://us-yum.pkg.dev/projects/cloud-ops-agents-artifacts/google-cloud-opentelemetry-collector-yum
    autorefresh=0
    enabled=1
    type=rpm-md
    gpgcheck=1
    repo_gpgcheck=0
    gpgkey=http://dl.google.com/linux/linux_signing_key.pub
    EOF
    
  3. 安裝套件:

    sudo yum install otelcol-google
    

    安裝完成後,Google 建構的 OpenTelemetry Collector 會自動啟動。

SLES

  1. 使用 SSH 或類似工具開啟 VM 執行個體的終端機連線,並確認您具備 sudo 存取權。

  2. 執行下列指令,設定 Package Repository:

    cat <<EOF | sudo tee /etc/zypp/repos.d/otelcol-google.repo
    [otelcol-google]
    name=Google Built OpenTelemetry Collector
    baseurl=https://us-yum.pkg.dev/projects/cloud-ops-agents-artifacts/google-cloud-opentelemetry-collector-yum
    autorefresh=0
    enabled=1
    type=rpm-md
    gpgkey=http://dl.google.com/linux/linux_signing_key.pub
    EOF
    
  3. 安裝套件:

    sudo zypper install otelcol-google
    

    安裝完成後,Google 建構的 OpenTelemetry Collector 會自動啟動。

Windows

  1. 使用 RDP 或類似工具連線至執行個體,然後登入 Windows。

  2. 在 PowerShell 圖示上按一下滑鼠右鍵,然後選取「Run as Administrator」(以管理員身分執行),開啟具備管理員權限的 PowerShell 終端機。

  3. 執行下列 PowerShell 指令,設定 Package Repository:

    googet addrepo otelcol-google `
        https://us-googet.pkg.dev/projects/cloud-ops-agents-artifacts/repos/google-cloud-opentelemetry-collector-googet
    
  4. 安裝套件:

    googet install otelcol-google
    

    安裝完成後,Google 建構的 OpenTelemetry Collector 會自動啟動。

部署收集器

Google 建構的 OpenTelemetry Collector 預設包含最低限度的設定,不會匯出遙測資料。如要匯出遙測資料,請在安裝收集器後,覆寫預設設定來設定收集器。請將收集器的設定放在下列檔案中:

  • Linux:/etc/otelcol-google/config.yaml
  • Windows 系統:C:\Program Files\Google\OpenTelemetry Collector\config.yaml

設定收集器

我們提供 OpenTelemetry Collector 設定,可與 Google 建構的 Collector 搭配使用。這項設定旨在提供大量 OTLP 指標、記錄和追蹤記錄。這項設定也能避免常見的攝入問題。您可以新增設定,但強烈建議不要移除元素。

本節說明提供的設定、匯出工具、處理器、接收器等重要元件,以及其他可用元件。

提供的收集器設定

您可以在 opentelemetry-operations-collector 存放區的 google-built-opentelemetry-collector 目錄中找到收集器設定:

receivers:
  # Open two OTLP servers:
  # - On port 4317, open an OTLP GRPC server
  # - On port 4318, open an OTLP HTTP server
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver
  otlp:
    protocols:
      grpc:
        endpoint: localhost:4317
      http:
        cors:
          # This effectively allows any origin
          # to make requests to the HTTP server.
          allowed_origins:
          - http://*
          - https://*
        endpoint: localhost:4318

processors:
  # The memorylimiter will check the memory usage of the collector process.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/memorylimiterprocessor
  memory_limiter:
    check_interval: 1s
    limit_percentage: 65
    spike_limit_percentage: 20

  # The resourcedetection processor is configured to detect GCP resources.
  # Resource attributes that represent the GCP resource the collector is
  # running on will be attached to all telemetry that goes through this
  # processor.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourcedetectionprocessor
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourcedetectionprocessor#gcp-metadata
  resourcedetection:
    detectors: [gcp]
    timeout: 10s

  # The resource/gcp_project_id processor sets the GCP project ID attribute on telemetry data exported via OTLP.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourceprocessor
  resource/gcp_project_id:
    attributes:
    - action: insert
      key: gcp.project_id
      value: ${GOOGLE_CLOUD_PROJECT}

  # The metricstarttime processor sets start time on delta metrics for OTLP export.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/metricstarttimeprocessor
  metricstarttime:
    strategy: subtract_initial_point

  transform/collision:
    metric_statements:
    - context: datapoint
      statements:
      - set(attributes["exported_location"], attributes["location"])
      - delete_key(attributes, "location")
      - set(attributes["exported_cluster"], attributes["cluster"])
      - delete_key(attributes, "cluster")
      - set(attributes["exported_namespace"], attributes["namespace"])
      - delete_key(attributes, "namespace")
      - set(attributes["exported_job"], attributes["job"])
      - delete_key(attributes, "job")
      - set(attributes["exported_instance"], attributes["instance"])
      - delete_key(attributes, "instance")
      - set(attributes["exported_project_id"], attributes["project_id"])
      - delete_key(attributes, "project_id")

  # For each Prometheus unknown-typed metric, which is a gauge, create a counter that is an exact copy of this metric.
  # The GCP OTLP endpoint will add appropriate suffixes for the counter and gauge.
  transform/prometheus_unknown_types:
    error_mode: ignore
    metric_statements:
    - context: metric
      statements:
      - copy_metric(Concat([metric.name, "unknowncounter"], ":")) where metric.metadata["prometheus.type"] == "unknown" and not HasSuffix(metric.name, ":unknowncounter")
      - convert_gauge_to_sum("cumulative", true) where HasSuffix(metric.name, ":unknowncounter")
      - set(metric.name, Substring(metric.name, 0, Len(metric.name)-Len(":unknowncounter"))) where HasSuffix(metric.name, ":unknowncounter")

  transform/otlp_grpc/preserve_instrumentation_source_version:
    error_mode: ignore
    log_statements:
    - context: log
      statements:
      - set(attributes["instrumentation_source"], instrumentation_scope.name) where instrumentation_scope.name != ""
      - set(attributes["instrumentation_version"], instrumentation_scope.version) where instrumentation_scope.version != ""

  transform/otlp_grpc/copy_service_resource_labels:
    error_mode: ignore
    log_statements:
    - context: log
      statements:
      - set(attributes["service.name"], resource.attributes["service.name"]) where resource.attributes["service.name"] != nil
      - set(attributes["service.namespace"], resource.attributes["service.namespace"]) where resource.attributes["service.namespace"] != nil
      - set(attributes["service.instance.id"], resource.attributes["service.instance.id"]) where resource.attributes["service.instance.id"] != nil

exporters:
  # Dedicated OTLP gRPC exporters are configured per signal (logs, metrics, traces)
  # so each sending queue and batch size matches the Cloud Telemetry API quotas:
  # https://docs.cloud.google.com/stackdriver/quotas
  #
  # Note: In addition to per-request item limits (8,192 for logs/traces, 200 for
  # metrics), the API enforces a 5 MiB request size limit. An 8,192-item batch
  # stays under 5 MiB only when the average log or span is under ~640 bytes. If
  # your payloads are larger, reduce `max_size` or set `sending_queue.batch.sizer: bytes`
  # and update `min_size` and `max_size` with byte limits that make sense for your deployment.
  #
  # For `telemetry.googleapis.com:443`, `sending_queue.num_consumers` can be increased up to 100
  # per exporter if you have the CPU and network bandwidth to increase request concurrency.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlpexporter
  otlp_grpc/logs:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth
    sending_queue:
      enabled: true
      sizer: bytes
      queue_size: 20971520 # 20 MiB queue bound
      block_on_overflow: true
      num_consumers: 25
      batch:
        sizer: items
        min_size: 8192
        max_size: 8192
        flush_timeout: 1s

  otlp_grpc/metrics:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth
    sending_queue:
      enabled: true
      sizer: bytes
      queue_size: 20971520 # 20 MiB queue bound
      block_on_overflow: true
      num_consumers: 25
      batch:
        sizer: items
        min_size: 200
        max_size: 200
        flush_timeout: 1s

  otlp_grpc/traces:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth
    sending_queue:
      enabled: true
      sizer: bytes
      queue_size: 20971520 # 20 MiB queue bound
      block_on_overflow: true
      num_consumers: 25
      batch:
        sizer: items
        min_size: 8192
        max_size: 8192
        flush_timeout: 1s

extensions:
  # Opens an endpoint on 13133 that can be used to check the
  # status of the collector. Since this does not configure the
  # `path` config value, the endpoint will default to `/`.
  #
  # When running on Cloud Run, this extension is required and not optional.
  # In other environments it is recommended but may not be required for operation
  # (i.e. in Container-Optimized OS or other GCE environments).
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/healthcheckextension
  health_check:
    endpoint: 0.0.0.0:13133
  googleclientauth:

service:
  extensions:
  - health_check
  - googleclientauth
  pipelines:
    logs:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - transform/otlp_grpc/preserve_instrumentation_source_version
      - transform/otlp_grpc/copy_service_resource_labels
      - memory_limiter
      exporters:
      - otlp_grpc/logs
    metrics/otlp:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - transform/collision
      - transform/prometheus_unknown_types
      - metricstarttime
      - memory_limiter
      exporters:
      - otlp_grpc/metrics
    traces:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - memory_limiter
      exporters:
      - otlp_grpc/traces
  # Internal telemetry for the collector supports both push and pull-based telemetry data transmission.
  # Leveraging the pre-configured OTLP receiver eliminates the need for an additional port.
  #
  # Docs:
  # https://opentelemetry.io/docs/collector/internal-telemetry/
  telemetry:
    metrics:
      readers:
        - periodic:
            exporter:
              otlp:
                protocol: grpc
                endpoint: http://localhost:4317
                insecure: true

出口商

收集器設定會使用下列項目:

處理器

收集器設定包含下列處理器:

  • batch:設定為批次處理遙測要求,每項要求最多 Google Cloud 個項目,或每 5 秒 Google Cloud 最短間隔 (以先到者為準)。

  • memory_limiter:限制收集器的記憶體用量,在超過限制時捨棄資料點,避免記憶體不足而當機。

  • resourcedetection:自動偵測 Google Cloud 資源標籤,例如 project_id。

接收器

收集器設定僅包含 otlp 接收器。如要瞭解如何檢測應用程式,將 OTLP 追蹤記錄和指標傳送至收集器的 OTLP 端點,請參閱「選擇檢測方法」。

可用元件

Google 建構的 OpenTelemetry Collector 包含大多數使用者所需的元件,可在 Google Cloud Observability 中提供豐富體驗。如需可用元件的完整清單,請參閱 opentelemetry-operations-collector 存放區中的元件。

如要要求變更或新增可用元件,請在 opentelemetry-operations-collector 存放區中提出功能要求。

生成遙測資料

您可以使用開放原始碼 telemetrygen 工具測試設定。連結中包含下載及執行工具的操作說明。如要執行工具,必須安裝 Go 或 Docker。

幾分鐘後,應用程式產生的遙測資料會開始透過收集器,流向每個信號的 Google Cloud 控制台。

查看遙測資料

Google 建構的 OpenTelemetry Collector 會將檢測應用程式的指標、記錄和追蹤記錄傳送至 Google Cloud Observability。收集器也會傳送自我觀測能力指標。以下各節說明如何查看這項遙測資料。

查看指標

Google 建構的 OpenTelemetry Collector 會收集 Prometheus 指標,您可以使用 Metrics Explorer 查看這些指標。收集的指標取決於應用程式的檢測,但 Google 建構的收集器也會寫入一些自我指標。

如要查看 Google 建構的 OpenTelemetry Collector 收集的指標,請按照下列步驟操作:
  1. 前往 Google Cloud 控制台的「Metrics Explorer」頁面:

    前往「Metrics Explorer」

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 在 Google Cloud 控制台的工具列中,選取 Google Cloud 專案。如果是 App Hub 設定,請選取 App Hub 主專案或已啟用應用程式管理功能的資料夾管理專案。
  3. 在「Metric」(指標) 元素中,展開「Select a metric」(選取指標) 選單,在篩選列中輸入 Prometheus Target,然後使用子選單選取特定資源類型和指標:
    1. 在「Active resources」(有效資源) 選單中,選取「Prometheus Target」(Prometheus 目標)。
    2. 如要選取指標,請使用「使用中的指標類別」和「使用中的指標」選單。 Google 建構的 OpenTelemetry Collector 收集的指標,前置字串為 prometheus.googleapis.com。
    3. 按一下「套用」。
  4. 如要新增篩選器,從查詢結果中移除時間序列,請使用「Filter」(篩選器) 元素。

  5. 設定資料的查看方式。

    如果指標的測量結果是累計值,Metrics Explorer 會自動依據校正週期正規化測量資料,圖表則會顯示比率。詳情請參閱「種類、類型和轉換」。

    測量整數或雙精度值時 (例如使用 counter 指標),Metrics Explorer 會自動加總所有時間序列。如要變更這項行為,請將「Aggregation」(匯總) 項目中的第一個選單設為「None」(無)。

    如要進一步瞭解如何設定圖表,請參閱「在使用 Metrics Explorer 時選取指標」。

查看追蹤記錄

如要查看追蹤記錄資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的 「Trace Explorer」頁面:

    前往「Trace explorer」(Trace 探索工具)

    您也可以透過搜尋列找到這個頁面。

  2. 在 Google Cloud 控制台的工具列中,選取 Google Cloud 專案。如要設定 App Hub,請選取 App Hub 主專案或管理專案。
  3. 在頁面的表格部分選取一列。
  4. 在「Trace details」(追蹤記錄詳細資料) 面板的甘特圖中,選取所需時距。

    畫面上會開啟一個面板,顯示追蹤要求的相關資訊。這些詳細資料包括方法、狀態碼、位元組數,以及呼叫端的使用者代理程式。

  5. 如要查看與這個追蹤記錄相關聯的記錄檔,請選取「Logs & Events」(記錄檔和事件) 分頁標籤。

    這個分頁會顯示個別記錄。如要查看記錄項目的詳細資料,請展開記錄項目。您也可以點選「View Logs」(查看記錄),並使用 Logs Explorer 查看記錄。

如要進一步瞭解如何使用 Cloud Trace Explorer,請參閱「尋找及探索追蹤記錄」。

查看記錄檔

您可以在 Logs Explorer 中檢查記錄,也可以查看相關聯的追蹤記錄 (如有)。

  1. 前往 Google Cloud 控制台的 「Logs Explorer」頁面:

    前往「Logs Explorer」(記錄檔探索工具)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果。

  2. 找出檢測應用程式的記錄項目,然後展開該項目查看詳細資料。

  3. 在含有追蹤訊息的記錄項目上,按一下 「Traces」(追蹤記錄),然後選取「View trace details」(查看追蹤記錄詳細資料)。

    「Trace details」(追蹤記錄詳細資料) 面板隨即開啟,並顯示所選追蹤記錄。

如要進一步瞭解如何使用 Logs Explorer,請參閱「使用 Logs Explorer 查看記錄檔」。

觀察收集器並偵錯

Google 建構的 OpenTelemetry Collector 會自動提供自我觀測能力指標,協助您監控其效能,並確保 OTLP 擷取管道持續正常運作。

如要監控收集器,請安裝收集器的範例資訊主頁。這個資訊主頁可讓您一目瞭然地查看 Collector 的多項指標,包括運作時間、記憶體用量,以及對 Google Cloud Observability 的 API 呼叫。

如要安裝資訊主頁,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Dashboards」(資訊主頁) 頁面:

    前往「Dashboards」(資訊主頁)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 按一下「資訊主頁範本」。
  3. 搜尋「OpenTelemetry Collector」資訊主頁。
  4. 選用:如要預覽資訊主頁,請選取該資訊主頁。
  5. 按一下「 Add dashboard to your list」(將資訊主頁新增至清單),然後完成對話方塊。

    您可以在對話方塊中選取資訊主頁名稱,並為資訊主頁新增標籤。

如要進一步瞭解如何安裝資訊主頁,請參閱「安裝資訊主頁範本」。