Container-Optimized OS に OpenTelemetry Collector をデプロイする

このドキュメントでは、Container-Optimized OS で Google が構築した OpenTelemetry Collector を実行し、計測対象のアプリケーションから OTLP のログ、指標、トレースを収集して、そのデータを Google Cloudにエクスポートする方法について説明します。

始める前に

OpenTelemetry Collector を実行するには、次のリソースが必要です。

  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 (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.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  4. Container-Optimized OS 仮想マシン(VM)。Container-Optimized OS VM が存在しない場合は、インスタンスの作成と構成の手順に沿って操作します。
  5. gcloud のインストール。gcloud のインストールについては、Google Cloud CLI をインストールするをご覧ください。
  6. Collector の権限を構成する

    デフォルトでは、Container-Optimized OS VM は Compute Engine のデフォルトのサービス アカウント PROJECT_NUMBER-compute@ を使用します。このサービス アカウントには通常、このドキュメントで説明する指標とログの書き込みに必要な Identity and Access Management(IAM)のロールが付与されています。

    インスタンスにカスタム サービス アカウントを構成する場合は、サービス アカウントへのアクセスを管理するをご覧ください。

    Collector をデプロイする

    Google がビルドした OpenTelemetry Collector を実行するには、Container-Optimized OS VM の構成ファイルを指定する必要があります。cloud-init ツールを使用して構成ファイルを作成できます。Google が構築した Collector を使用する場合の推奨される cloud-init ファイルは次のとおりです。

    write_files:
    - path: /etc/config/config.yaml
      permissions: 0644
      owner: root
      content: |
        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 batch processor is in place to regulate both the number of requests
          # being made and the size of those requests.
          #
          # Docs:
          # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/batchprocessor
          batch:
            send_batch_max_size: 200
            send_batch_size: 200
            timeout: 5s
    
          # 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
    
          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")
    
        exporters:
          # The googlecloud exporter will export telemetry to different
          # Google Cloud services:
          # Logs -> Cloud Logging
          # Metrics -> Cloud Monitoring
          # Traces -> Cloud Trace
          #
          # Docs:
          # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/googlecloudexporter
          googlecloud:
            log:
              default_log_name: opentelemetry-collector
    
          # The googlemanagedprometheus exporter will send metrics to
          # Google Managed Service for Prometheus.
          #
          # Docs:
          # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/googlemanagedprometheusexporter
          googlemanagedprometheus:
    
        service:
          pipelines:
            logs:
              receivers:
              - otlp
              processors:
              - resourcedetection
              - memory_limiter
              - batch
              exporters:
              - googlecloud
            metrics/otlp:
              receivers:
              - otlp
              processors:
              - resourcedetection
              - transform/collision
              - memory_limiter
              - batch
              exporters:
              - googlemanagedprometheus
            traces:
              receivers:
              - otlp
              processors:
              - resourcedetection
              - memory_limiter
              - batch
              exporters:
              - googlecloud
          # 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: localhost:4317
    

    テレメトリーを送信する Collector コンテナとシステム上の他のコンテナ間の通信を容易にするために、Docker bridge ネットワークを作成することをおすすめします。ネットワークを作成するには、次のコマンドを実行します。

    docker network create -d bridge otel
    

    次のコマンドを使用して Collector コンテナを実行します。

    docker run -d \
        --network otel \
        --name opentelemetry-collector \
        -v /etc/config:/etc/config \
        us-docker.pkg.dev/cloud-ops-agents-artifacts/google-cloud-opentelemetry-collector/otelcol-google:0.137.0 \
        --config=/etc/config/config.yaml

    上記のコマンドは、次の処理を行います。

    • Collector コンテナをバックグラウンドで実行します。
    • Collector コンテナを、先ほど作成した otel ブリッジ ネットワークに接続します。他のコンテナをブリッジに接続してテレメトリーを送信できます。
    • 構成ファイルをコンテナにマウントして、ファイルにアクセスし Collector を構成できるようにします。

    Collector を構成する

    Google が構築した Collector で使用できる OpenTelemetry Collector 構成が用意されています。この構成は、大量の OTLP の指標、ログ、トレースを送信するように設計されています。この構成は、一般的な取り込みの問題を回避するようにも設計されています。構成に追加することはできますが、要素を削除しないことを強くおすすめします。

    このセクションでは、用意されている構成、エクスポータ、プロセッサ、レシーバーなどの主要コンポーネント、および利用可能なその他のコンポーネントについて説明します。

    用意されている Collector の構成

    Collector の構成は、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 batch processor is in place to regulate both the number of requests
      # being made and the size of those requests.
      #
      # Docs:
      # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/batchprocessor
      batch:
        send_batch_max_size: 200
        send_batch_size: 200
        timeout: 5s
    
      # 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
    
      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")
    
    exporters:
      # The googlecloud exporter will export telemetry to different
      # Google Cloud services:
      # Logs -> Cloud Logging
      # Metrics -> Cloud Monitoring
      # Traces -> Cloud Trace
      #
      # Docs:
      # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/googlecloudexporter
      googlecloud:
        log:
          default_log_name: opentelemetry-collector
    
      # The googlemanagedprometheus exporter will send metrics to
      # Google Managed Service for Prometheus.
      #
      # Docs:
      # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/googlemanagedprometheusexporter
      googlemanagedprometheus:
    
    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
    
    service:
      extensions:
      - health_check
      pipelines:
        logs:
          receivers:
          - otlp
          processors:
          - resourcedetection
          - memory_limiter
          - batch
          exporters:
          - googlecloud
        metrics/otlp:
          receivers:
          - otlp
          processors:
          - resourcedetection
          - transform/collision
          - memory_limiter
          - batch
          exporters:
          - googlemanagedprometheus
        traces:
          receivers:
          - otlp
          processors:
          - resourcedetection
          - memory_limiter
          - batch
          exporters:
          - googlecloud
      # 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: localhost:4317
    

    エクスポータ

    Collector の構成には、次のエクスポータが含まれています。

    • googlecloud エクスポータ(ログとトレース用)。このエクスポータは、デフォルトのログ名で構成されています。

    • googlemanagedprometheus エクスポータ(指標用)。このエクスポータには構成は必要ありませんが、構成オプションがあります。googlemanagedprometheus エクスポータの構成オプションについては、Google Cloud Managed Service for Prometheus ドキュメントの OpenTelemetry Collector を使ってみるをご覧ください。

    プロセッサ

    Collector の構成には、次のプロセッサが含まれています。

    • batch: テレメトリー リクエストを、 Google Cloud のリクエストあたりの最大エントリ数に達した時点か、 Google Cloud の 5 秒ごとの最小間隔のいずれか早いほうでバッチ処理するように構成します。

    • memory_limiter: Collector のメモリ使用量を制限し、上限を超えたときにデータポイントを破棄することで、メモリ不足によるクラッシュを防止します。

    • resourcedetection: project_id などの Google Cloud リソースラベルを自動的に検出します。

    レシーバー

    Collector の構成には、otlp レシーバーのみが含まれています。アプリケーションを計測して OTLP のトレースと指標を Collector の OTLP エンドポイントに送信することについては、計測方法を選択するをご覧ください。

    使用可能なコンポーネント

    Google が構築した OpenTelemetry Collector には、Google Cloud Observability 内でリッチなエクスペリエンスを実現するためにほとんどのユーザーが必要とするコンポーネントが含まれています。使用可能なコンポーネントの全一覧については、opentelemetry-operations-collector リポジトリのコンポーネントをご覧ください。

    使用可能なコンポーネントの変更または追加をリクエストするには、opentelemetry-operations-collector リポジトリで機能リクエストを開きます

    テレメトリーを生成する

    オープンソースの telemetrygen ツールを使用して構成をテストできます。OpenTelemetry プロジェクトは、GitHub Container Registry にコンテナを提供します。

    次のコマンドを実行する前に、Collector をデプロイするで Docker コマンドに使用されているデフォルトを変更した場合は、次のプレースホルダを置き換えます。

    • otel: Docker bridge ネットワークの作成時に指定した名前。
    • opentelemetry-collector: コンテナの実行時に指定した名前。

    ログを生成する

    telemetrygen ツールを使用してログを生成するには、次のコマンドを実行します。

    docker run \
      --net=otel \
      ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:v0.105.0 \
      logs --otlp-insecure --rate=3 --duration=5m \
      --otlp-endpoint=opentelemetry-collector:4317
    

    指標を生成

    telemetrygen ツールを使用して指標を生成するには、次のコマンドを実行します。

    docker run \
      --net=otel \
      ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:v0.105.0 \
      metrics --otlp-insecure --rate=0.1 --duration=5m \
      --otlp-endpoint=opentelemetry-collector:4317
    

    指標を生成

    telemetrygen ツールを使用してトレースを生成するには、次のコマンドを実行します。

    docker run \
      --net=otel \
      ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:v0.105.0 \
      traces --otlp-insecure --rate=3 --duration=5m \
      --otlp-endpoint=opentelemetry-collector:4317
    

    数分後、アプリケーションによって生成されたテレメトリーが、シグナルごとに Collector を介して Google Cloud コンソールに転送され始めます。

    テレメトリーを表示する

    Google が構築した OpenTelemetry Collector は、計測対象のアプリケーションから Google Cloud Observability に指標、ログ、トレースを送信します。Collector は自己オブザーバビリティ指標も送信します。以下の各セクションでは、このテレメトリーを表示する方法について説明します。

    指標を表示する

    Google が構築した OpenTelemetry Collector は、Metrics Explorer を使用して表示できる Prometheus 指標を収集します。収集される指標はアプリの計測によって異なりますが、Google が構築した Collector も一部のセルフ指標を書き込みます。

    Google が構築した OpenTelemetry Collector によって収集された指標を表示する手順は次のとおりです。
    1. Google Cloud コンソールで [Metrics explorer] のページに移動します。

      [Metrics Explorer] に移動

      検索バーを使用してこのページを検索する場合は、小見出しが [Monitoring] の結果を選択します。

    2. Google Cloud コンソールのツールバーで、 Google Cloud プロジェクトを選択します。App Hub の構成には、App Hub ホスト プロジェクトまたはアプリ管理用フォルダの管理プロジェクトを選択します。
    3. [指標] 要素の [指標を選択] メニューを開いてフィルタバーに「Prometheus Target」と入力し、サブメニューを使用して特定のリソースタイプと指標を選択します。
      1. [有効なリソース] メニューで、[Prometheus Target] を選択します。
      2. 指標を選択するには、[有効な指標カテゴリ] メニューと [有効な指標] メニューを使用します。Google が構築した OpenTelemetry Collector によって収集された指標には、接頭辞 prometheus.googleapis.com が設定されています。
      3. [適用] をクリックします。
    4. クエリ結果から時系列を削除するフィルタを追加するには、[フィルタ] 要素を使用します。

    5. データの表示方法を構成します。

      指標の測定値が累積されている場合、Metrics Explorer によりアライメント期間ごとに測定データが自動的に正規化され、その結果、グラフに率が表示されます。詳細については、種類、タイプ、変換をご覧ください。

      counter 指標など、integer 値または double 値が測定されると、Metrics Explorer によりすべての時系列が自動的に合計されます。この動作を変更するには、[集計] エントリの最初のメニューを [なし] に設定します。

      グラフの構成の詳細については、Metrics Explorer 使用時の指標の選択をご覧ください。

    トレースを表示する

    トレースデータを表示するには、次の操作を行います。

    1. Google Cloud コンソールで、[Trace エクスプローラ] ページに移動します。

      [Trace エクスプローラ] に移動

      このページは、検索バーを使用して見つけることもできます。

    2. Google Cloud コンソールのツールバーで、 Google Cloud プロジェクトを選択します。App Hub 構成の場合は、App Hub ホスト プロジェクトまたは管理プロジェクトを選択します。
    3. ページの表セクションで、行を選択します。
    4. [トレースの詳細] パネルのガントチャートで、スパンを選択します。

      パネルが開き、トレースされたリクエストに関する情報が表示されます。詳細には、メソッド、ステータス コード、バイト数、呼び出し元のユーザー エージェントが含まれます。

    5. このトレースに関連付けられているログを表示するには、[ログとイベント] タブを選択します。

      このタブには、個々のログが表示されます。ログエントリの詳細を表示するには、ログエントリを開きます。[ログを表示] をクリックし、ログ エクスプローラを使用してログを表示することもできます。

    Cloud Trace エクスプローラの使用方法について詳しくは、トレースを検索して調査するをご覧ください。

    ログを表示する

    ログ エクスプローラではログを調査できます。また、関連するトレース(存在する場合)を確認することもできます。

    1. Google Cloud コンソールで [ログ エクスプローラ] ページに移動します。

      [ログ エクスプローラ] に移動

      検索バーを使用してこのページを検索する場合は、小見出しが [Logging] の結果を選択します。

    2. 計測対象のアプリのログエントリを見つけます。詳細を表示するには、ログエントリを開きます。

    3. トレース メッセージを含むログエントリの [トレース] をクリックし、[トレースの詳細を表示] を選択します。

      [トレースの詳細] パネルが開き、選択したトレースが表示されます。

    ログ エクスプローラの使用方法については、ログ エクスプローラを使用してログを表示するをご覧ください。

    Collector のモニタリングとデバッグ

    Google が構築した OpenTelemetry Collector は、パフォーマンスのモニタリングと、OTLP 取り込みパイプラインの継続的な稼働時間の確保に役立つセルフ オブザーバビリティ指標を自動的に提示します。

    Collector をモニタリングするには、Collector のサンプル ダッシュボードをインストールします。このダッシュボードでは、稼働時間、メモリ使用量、Google Cloud Observability に対する API 呼び出しなど、Collector のさまざまな指標に関する分析情報を一目で確認できます。

    ダッシュボードのインストール手順は次のとおりです。

    1. Google Cloud コンソールで [ ダッシュボード] ページに移動します。

      [ダッシュボード] に移動

      検索バーを使用してこのページを検索する場合は、小見出しが [Monitoring] の結果を選択します。

    2. [ダッシュボード テンプレート] をクリックします。
    3. OpenTelemetry Collector ダッシュボードを検索します。
    4. 省略可: ダッシュボードをプレビューするには、ダッシュボードを選択します。
    5. [ ダッシュボードをリストに追加] をクリックし、ダイアログを完了します。

      このダイアログでは、ダッシュボードの名前を選択し、ダッシュボードにラベルを追加できます。

    ダッシュボードのインストールの詳細については、ダッシュボード テンプレートをインストールするをご覧ください。