使用OpenTelemetry對APISIX進行鏈路追蹤
APISIX OpenTelemetry插件支持收集APISIX的調(diào)用鏈數(shù)據(jù)并上報至OpenTelemetry Collector,再由OpenTelemetry Collector轉(zhuǎn)發(fā)上報至可觀測鏈路 OpenTelemetry 版。APISIX OpenTelemetry插件目前僅支持通過HTTP方式上報調(diào)用鏈數(shù)據(jù)至OpenTelemetry Collector,不支持gRPC方式上報。
前提條件
APISIX版本 ≥ v2.13.0。
新版控制臺
登錄可觀測鏈路 OpenTelemetry 版控制臺,在左側(cè)導(dǎo)航欄單擊接入中心。
在開源框架區(qū)域單擊OpenTelemetry卡片。
在彈出的OpenTelemetry面板中選擇數(shù)據(jù)需要上報的地域。
說明初次接入的地域?qū)詣舆M行資源初始化。
選擇連接方式和上報方式,然后復(fù)制接入點信息。
連接方式:若您的服務(wù)部署在阿里云上,且所屬地域與選擇的接入地域一致,推薦使用阿里云內(nèi)網(wǎng)方式,否則選擇公網(wǎng)方式。
上報方式:根據(jù)客戶端支持的協(xié)議類型選擇HTTP或gRPC協(xié)議上報數(shù)據(jù)。
舊版控制臺
在左側(cè)導(dǎo)航欄單擊集群配置,然后在右側(cè)頁面單擊接入點信息頁簽。
在頁面頂部選擇需要接入的地域,然后在集群信息區(qū)域打開顯示Token開關(guān)。
在客戶端采集工具區(qū)域單擊OpenTelemetry。
在相關(guān)信息列中,獲取接入點信息。
說明如果應(yīng)用部署于阿里云生產(chǎn)環(huán)境,則選擇阿里云VPC網(wǎng)絡(luò)接入點,否則選擇公網(wǎng)接入點。
方案概覽
使用OpenTelemetry對APISIX進行鏈路追蹤,主要分為以下幾步:
部署OpenTelemetry Collector:OpenTelemetry Collector是一個開源可觀測數(shù)據(jù)采集器,負責(zé)接收、處理和導(dǎo)出來自各種數(shù)據(jù)源的可觀測數(shù)據(jù)(例如調(diào)用鏈)。
在APISIX中啟用OpenTelemetry插件:通過修改APISIX的配置文件啟用OpenTelemetry插件,允許OpenTelemetry Collector收集APISIX的可觀測數(shù)據(jù)。
設(shè)置APISIX OpenTelemetry插件生效范圍:通過APISIX Admin API可以設(shè)置OpenTelemetry插件的生效范圍,可以全局啟用,也可以為指定的路由啟用。
查看APISIX調(diào)用鏈:您可以在控制臺查看由OpenTelemetry生成的APISIX調(diào)用鏈。
接入步驟
一. 部署OpenTelemetry Collector
下文以Docker方式為例,介紹如何部署OpenTelemetry Collector。更多部署方式,請參見下載并部署OpenTelemetry Collector。
創(chuàng)建
opentelemetry-config.yaml
文件,并將下面的內(nèi)容拷貝至文件。該文件用于定義和配置OpenTelemetry Collector的行為和功能,包括如何接收、處理和導(dǎo)出數(shù)據(jù)。
說明請將
${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
二. 在APISIX中啟用OpenTelemetry插件
您需要在APISIX的config.yaml
配置文件中啟用opentelemetry
插件并修改Collector配置。
請將
${Service Name}
替換為應(yīng)用名稱,例如APISIX,該應(yīng)用名稱會顯示在可觀測鏈路 OpenTelemetry 版的應(yīng)用列表中。請將
${OpenTelemetry Collector Address}
替換為Collector的訪問地址,如127.0.0.1
。關(guān)于OpenTelemetry插件的更多上報配置,請參見APISIX Opentelemetry 插件上報配置。
...
plugins:
... # 其他已經(jīng)啟用的插件
- opentelemetry # 啟用OpenTelemetry插件
plugin_attr:
...
opentelemetry: # OpenTelemetry相關(guān)配置
resource:
service.name: ${Service Name} # 應(yīng)用名稱
collector:
address: ${OpenTelemetry Collector Address}:4318 # OpenTelemetry Collector 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
三. 設(shè)置APISIX OpenTelemetry插件生效范圍
全局啟用OpenTelemetry插件。
說明sampler
設(shè)置為always_on
代表全采樣,每次請求都會被追蹤并產(chǎn)生調(diào)用鏈。關(guān)于OpenTelemetry插件的更多屬性配置,請參見APISIX 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 } } }'
四. 查看APISIX調(diào)用鏈
完成以上配置后,您可通過APISIX創(chuàng)建路由并進行訪問。然后可以登錄可觀測鏈路 OpenTelemetry 版控制臺,查看由OpenTelemetry生成的APISIX調(diào)用鏈。
在應(yīng)用列表頁查看APISIX應(yīng)用。
在調(diào)用鏈分析頁查看APISIX的調(diào)用鏈。
操作示例
準備工作
確認已安裝Git、Docker和Docker Compose。
確認APISIX版本 ≥ v2.13.0。
操作步驟
下載APISIX官方Docker Compose Demo。
git clone https://github.com/apache/apisix-docker.git cd apisix-docker/example
將OpenTelemetry Collector添加到APISIX Docker Compose Demo中。
在
apisix-docker/example
文件夾下創(chuàng)建ot_conf
文件夾,并創(chuàng)建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服務(wù)。修改后的完整
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 receiver - 4318:4318 # OTLP http receiver networks: apisix: networks: apisix: driver: bridge volumes: etcd_data: driver: local
在APISIX中啟用OpenTelemetry插件。
修改APISIX配置文件
apisix-docker/example/apisix_conf/config.yaml
,在文件末尾追加以下內(nèi)容。plugins: - opentelemetry plugin_attr: prometheus: export_addr: ip: "0.0.0.0" port: 9091 opentelemetry: resource: service.name: APISIX collector: address: docker-apisix-otel-collector-1:4318 # OTLP HTTP Receiver address 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 Demo。
請在
apisix-docker/example
目錄下執(zhí)行。docker compose -p docker-apisix up -d
全局啟用OpenTelemetry插件。
通過APISIX Admin API設(shè)置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" } } } }'
創(chuàng)建APISIX路由并測試調(diào)用鏈上報。
使用APISIX Admin API創(chuàng)建路由。
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 } } }'
訪問以下地址,APISIX OpenTelemetry插件會為本次請求生成調(diào)用鏈并上報至可觀測鏈路 OpenTelemetry 版。
curl -i -X GET "http://127.0.0.1:9080/anything/foo?arg=10" -H "Host: example.com"
預(yù)期輸出:
# 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" }
登錄可觀測鏈路 OpenTelemetry 版控制臺,查看由OpenTelemetry生成的APISIX調(diào)用鏈。
在應(yīng)用列表頁查看APISIX應(yīng)用。
在調(diào)用鏈分析頁查看APISIX的調(diào)用鏈。
相關(guān)信息
APISIX是一款云原生API網(wǎng)關(guān),由Apache APISIX社區(qū)維護。它具有動態(tài)、實時、高性能等特點,提供了負載均衡、灰度發(fā)布(金絲雀發(fā)布)等豐富的流量管理功能。更多信息,請參見Apache APISIX。