APISIX OpenTelemetry プラグインは、APISIX のトレースデータを収集し、OpenTelemetry Collector にレポートします。その後、OpenTelemetry Collector はトレースデータを Managed Service for OpenTelemetry に転送します。APISIX OpenTelemetry プラグインは、HTTP 経由でのみ OpenTelemetry Collector にトレースデータをレポートします。 Google 遠隔手続き呼出し (gRPC) 経由でのトレースデータのレポートはサポートされていません。
前提条件
APISIX 2.13.0 以降を使用していること。
エンドポイントを取得していること。
概要
OpenTelemetry を使用して APISIX のトレースデータをレポートするには、次の手順を実行します。
OpenTelemetry Collector をデプロイします。OpenTelemetry Collector は、トレースなどの可観測性データをさまざまなデータソースから受信、処理、エクスポートするオープンソースのコレクターです。
APISIX で OpenTelemetry プラグインを有効にします。APISIX の構成ファイルを修正して OpenTelemetry プラグインを有効にすることで、OpenTelemetry Collector が APISIX の可観測性データを収集できるようにすることができます。
OpenTelemetry プラグインの有効範囲を指定します。APISIX Admin API を使用して、OpenTelemetry プラグインの有効範囲を指定できます。
APISIX トレースを表示します。 Managed Service for OpenTelemetry コンソールで、OpenTelemetry プラグインによって生成された APISIX トレースを表示できます。
プロセス
1. OpenTelemetry Collector をデプロイする
方法 1:ACK コンソールでのインストール
ACK コンソール にログインします。左側のナビゲーションウィンドウで、 を選択します。
表示されるページで、
opentelemetry-collector
を見つけてクリックします。表示されるパネルで、右上隅にある [デプロイ] をクリックします。[デプロイ] パネルで、ターゲットクラスタを選択し、[次へ] をクリックします。
[パラメーター] ステップで、次のパラメーターを追加し、[OK] をクリックします。
説明${HTTP Endpoint}
を、前提条件 で取得した HTTP エンドポイント(http://tracing-analysis-dc-hz.aliyuncs.com/adapt_xxxxx/api/otlp/traces
など)に置き換えます。receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 http: cors: allowed_origins: - http://* - https://* endpoint: 0.0.0.0:4318 # OTLP HTTP Receiver processors: batch: exporters: otlphttp: traces_endpoint: '${HTTP Endpoint}' tls: insecure: true service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [otlphttp]
例:
方法 2:手動インストール
次の例は、Docker を使用して OpenTelemetry Collector をデプロイする方法を示しています。 詳細については、「Install the Collector」をご参照ください。
opentelemetry-config.yaml
という名前のファイルを作成し、次の内容をファイルにコピーします。このファイルは、OpenTelemetry Collector の動作と機能(データの受信、処理、エクスポート方法など)を定義および構成するために使用されます。
説明ファイル内の
${HTTP Endpoint}
を、このトピックの 前提条件 セクションで取得した HTTP エンドポイントに置き換えます。例:http://tracing-analysis-dc-hz.aliyuncs.com/adapt_xxxxx/api/otlp/traces
。receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 http: cors: allowed_origins: - http://* - https://* endpoint: 0.0.0.0:4318 # OTLP HTTP Receiver processors: batch: exporters: otlphttp: traces_endpoint: '${HTTP Endpoint}' tls: insecure: true service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [otlphttp]
OpenTelemetry Collector を起動します。
docker run -v $(pwd)/opentelemetry-config.yaml:/etc/otelcol-contrib/config.yaml otel/opentelemetry-collector-contrib:0.105.0
2. APISIX で OpenTelemetry プラグインを有効にする
APISIX V3.12 以降
APISIX の
config.yaml
ファイルで、OpenTelemetry
プラグインを有効にします。... plugins: ... # その他の有効なプラグイン - opentelemetry # OpenTelemetry プラグインを有効にする
プラグインのパラメーターを設定します。
説明構成内の次の変数を置き換えます。
${Service Name}
をアプリケーション名(APISIX
など)に置き換えます。これは、Managed Service for OpenTelemetry コンソールの [アプリケーション] ページに表示されます。${Host IP}
をホスト IP アドレスに置き換えます。これは、手順 4 の [トレースの詳細] ページの右側にある [スパンの詳細] セクションに表示されます。${OpenTelemetry Collector Address}
を OpenTelemetry Collector の IP アドレス(127.0.0.1
など)に置き換えます。${admin_key}
を APISIX Admin API の認証キーに置き換えます。
OpenTelemetry プラグインの構成方法の詳細については、opentelemetry トピックの「コレクターの構成」セクションをご参照ください。
curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/opentelemetry -H "X-API-KEY: ${admin_key}" -X PUT -d ' { "trace_id_source": "x-request-id", "resource": { "service.name": "${Service Name}", "host.ip":"${Host IP}" }, "collector": { "address": "${OpenTelemetry Collector Address}:4318", "request_timeout": 3, "batch_span_processor": { "drop_on_queue_full": false, "max_queue_size": 1024, "batch_timeout": 2, "inactive_timeout": 1, "max_export_batch_size": 16 }, "set_ngx_var": false } }'
APISIX V3.12 より前
OpenTelemetry
プラグインを有効にし、APISIX の config.yaml
ファイルで OpenTelemetry Collector の構成を変更します。
構成内の次の変数を置き換えます。
${Service Name}
をアプリケーション名(APISIX
など)に置き換えます。これは、Managed Service for OpenTelemetry コンソールの [アプリケーション] ページに表示されます。${Host IP}
をホスト IP アドレスに置き換えます。これは、手順 4 の [トレースの詳細] ページの右側にある [スパンの詳細] セクションに表示されます。${OpenTelemetry Collector Address}
を OpenTelemetry Collector の IP アドレス(127.0.0.1
など)に置き換えます。
OpenTelemetry プラグインの構成方法の詳細については、opentelemetry トピックの「コレクターの構成」セクションをご参照ください。
...
plugins:
... # その他の有効なプラグイン
- opentelemetry # OpenTelemetry プラグインを有効にする
plugin_attr:
...
opentelemetry: # OpenTelemetry プラグインの構成
resource:
service.name: ${Service Name} # アプリケーション名
host.ip: ${Host IP} # ホスト IP アドレス
collector:
address: ${OpenTelemetry Collector Address}:4318 # OpenTelemetry Collector の OpenTelemetry Protocol (OTLP) HTTP レシーバーのエンドポイント
request_timeout: 3
batch_span_processor: # バッチでデータを処理するための構成
drop_on_queue_full: false
max_queue_size: 6
batch_timeout: 2
inactive_timeout: 1
max_export_batch_size: 2
3. APISIX OpenTelemetry プラグインの有効範囲を指定する
OpenTelemetry プラグインをグローバルに有効にするように構成します。
説明sampler
パラメーターがalways_on
に設定されている場合、各リクエストが追跡され、トレースが生成されます。OpenTelemetry の属性の構成方法の詳細については、opentelemetry トピックの「属性」セクションをご参照ください。
curl 'http://127.0.0.1:9080/apisix/admin/global_rules/1' \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \ -X PUT -d '{ "plugins": { "opentelemetry": { "sampler": { "name": "always_on" } } } }'
特定のルートに対してのみ OpenTelemetry プラグインが有効になるように構成します。
curl http://127.0.0.1:9080/apisix/admin/routes/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \ -X PUT -d ' { "uri": "/get", "plugins": { "opentelemetry": { "sampler": { "name": "always_on" } } }, "upstream": { "type": "roundrobin", "nodes": { "httpbin.org:80": 1 } } }'
4. APISIX トレースを表示する
上記の構成を完了したら、APISIX を使用してルートを作成し、アクセスできます。 その後、Managed Service for OpenTelemetry コンソール にログインして、OpenTelemetry によって生成された APISIX トレースを表示できます。
[アプリケーション] ページで、APISIX アプリケーションの名前をクリックします。
[トレースの詳細] タブで、APISIX トレース情報を表示します。
例
準備
Git、Docker、Docker Compose をインストールします。
APISIX 2.13.0 以降を使用していることを確認します。
手順
APISIX の公式 Web サイトから Docker Compose のデモをダウンロードします。
git clone https://github.com/apache/apisix-docker.git cd apisix-docker/example
OpenTelemetry Collector を Docker Compose のデモに追加します。
apisix-docker/example
フォルダーにot_conf
という名前のフォルダーを作成し、config.yaml
という名前のファイルを作成します。説明${HTTP Endpoint}
を、このトピックの「前提条件」セクションで取得した HTTP エンドポイントに置き換えます。例:http://tracing-analysis-dc-hz.aliyuncs.com/adapt_xxxxx/api/otlp/traces
。
receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 http: cors: allowed_origins: - http://* - https://* endpoint: 0.0.0.0:4318 processors: batch: exporters: otlphttp: traces_endpoint: '${HTTP Endpoint}' tls: insecure: true service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [otlphttp]
apisix-docker/example/docker-compose.yml
ファイルを変更して、OpenTelemetry Collector を Docker Compose のデモに追加します。次のサンプルコードは、変更後の
docker-compose.yml
ファイルの内容全体を示しています。# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # version: "3" services: apisix: image: apache/apisix:${APISIX_IMAGE_TAG:-3.9.0-debian} restart: always volumes: - ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro depends_on: - etcd ##network_mode: host ports: - "9180:9180/tcp" - "9080:9080/tcp" - "9091:9091/tcp" - "9443:9443/tcp" - "9092:9092/tcp" networks: apisix: etcd: image: bitnami/etcd:3.5.11 restart: always volumes: - etcd_data:/bitnami/etcd environment: ETCD_ENABLE_V2: "true" ALLOW_NONE_AUTHENTICATION: "yes" ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379" ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379" ports: - "2379:2379/tcp" networks: apisix: web1: image: nginx:1.19.0-alpine restart: always volumes: - ./upstream/web1.conf:/etc/nginx/nginx.conf ports: - "9081:80/tcp" environment: - NGINX_PORT=80 networks: apisix: web2: image: nginx:1.19.0-alpine restart: always volumes: - ./upstream/web2.conf:/etc/nginx/nginx.conf ports: - "9082:80/tcp" environment: - NGINX_PORT=80 networks: apisix: prometheus: image: prom/prometheus:v2.25.0 restart: always volumes: - ./prometheus_conf/prometheus.yml:/etc/prometheus/prometheus.yml ports: - "9090:9090" networks: apisix: grafana: image: grafana/grafana:7.3.7 restart: always ports: - "3000:3000" volumes: - "./grafana_conf/provisioning:/etc/grafana/provisioning" - "./grafana_conf/dashboards:/var/lib/grafana/dashboards" - "./grafana_conf/config/grafana.ini:/etc/grafana/grafana.ini" networks: apisix: otel-collector: image: otel/opentelemetry-collector-contrib:0.105.0 volumes: - ./ot_conf/config.yaml:/etc/otelcol-contrib/config.yaml # OpenTelemetry Collector の構成ファイルをマウントする ports: - 4317:4317 # OTLP gRPC レシーバー - 4318:4318 # OTLP HTTP レシーバー networks: apisix: networks: apisix: driver: bridge volumes: etcd_data: driver: local
APISIX で OpenTelemetry プラグインを有効にします。
ファイルの末尾に次の内容を追加して、APISIX の
apisix-docker/example/apisix_conf/config.yaml
構成ファイルを変更します。plugins: - opentelemetry plugin_attr: prometheus: export_addr: ip: "0.0.0.0" port: 9091 opentelemetry: resource: service.name: APISIX host.ip: 127.0.0.1 collector: address: docker-apisix-otel-collector-1:4318 # OTLP HTTP Receiver アドレス request_timeout: 3 batch_span_processor: drop_on_queue_full: false max_queue_size: 6 batch_timeout: 2 inactive_timeout: 1 max_export_batch_size: 2
APISIX Docker Compose のデモを起動します。
apisix-docker/example
ディレクトリで次のコマンドを実行します。docker compose -p docker-apisix up -d
OpenTelemetry プラグインをグローバルに有効にするように構成します。
APISIX Admin API を使用して、OpenTelemetry プラグインをグローバルに有効にするように構成します。
curl 'http://127.0.0.1:9180/apisix/admin/global_rules/1' \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \ -X PUT -d '{ "plugins": { "opentelemetry": { "sampler": { "name": "always_on" } } } }'
APISIX ルートを作成し、トレースデータのレポートテストを実行します。
APISIX Admin API を使用してルートを作成します。
curl "http://127.0.0.1:9180/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d ' { "methods": ["GET"], "host": "example.com", "uri": "/anything/*", "upstream": { "type": "roundrobin", "nodes": { "httpbin.org:80": 1 } } }'
次の curl コマンドを実行します。APISIX OpenTelemetry プラグインは、このリクエストのトレースを生成し、Managed Service for OpenTelemetry にトレースデータをレポートします。
curl -i -X GET "http://127.0.0.1:9080/anything/foo?arg=10" -H "Host: example.com"
期待される出力:
# curl -i -X GET "http://127.0.0.1:9080/anything/foo?arg=10" -H "Host: example.com" HTTP/1.1 200 OK Content-Type: application/json Content-Length: 501 Connection: keep-alive Date: Wed, 24 Jul 2024 03:26:11 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Server: APISIX/3.9.0 { "args": { "arg": "10" }, "data": "", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Host": "example.com", "Traceparent": "00-xxxxxx-xxxx-01", "User-Agent": "curl/7.61.1", "X-Amzn-Trace-Id": "Root=1-xxx-xxxx", "X-Forwarded-Host": "example.com" }, "json": null, "method": "GET", "origin": "x.x.x.x, x.x.x.x", "url": "http://example.com/anything/foo?arg=10" }
Managed Service for OpenTelemetry コンソール にログインし、OpenTelemetry によって生成された APISIX トレースを表示します。
[アプリケーション] ページで、APISIX アプリケーションの名前をクリックします。
[トレースの詳細] タブで、APISIX トレース情報を表示します。
参考資料
APISIX は、Apache APISIX コミュニティによってメンテナンスされているクラウドネイティブ API ゲートウェイです。APISIX は、動的構成、リアルタイム応答、高パフォーマンスをサポートし、負荷分散やカナリアリリースなどのさまざまなトラフィック管理機能を提供します。詳細については、Apache APISIX 公式 Web サイトをご覧ください。