Posted on 

APISIX Integrates with OpenTelemetry to Collect Tracing Data

环境介绍

  1. APISIX 运行在本地
  2. opentelemetry 相关的服务包含 client, server, jaeger, prometheus, zipkin

安装基础环境

下载代码, 这个代码包含一个 docker compose 的一套文件

1
2
3
git clone https://github.com/open-telemetry/opentelemetry-collector-contrib.git
cd opentelemetry-collector-contrib/examples/demo
docker compose up -d

这个环境里面包含上面说的全部服务, 除了 APISIX 本身.

将 APISIX 集成进去

修改 APISIX 的配置

修改 conf/config-default.yaml ,因为 opentelemetry 的插件默认是没有启用的, 所以需要启用一下.

1
2
3
4
5
6
7
8
9
10
11
12
13
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index 359e2c2d..c99be803 100755
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -437,7 +437,7 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 12015
- zipkin # priority: 12011
#- skywalking # priority: 12010
- #- opentelemetry # priority: 12009
+ - opentelemetry # priority: 12009
- ext-plugin-pre-req # priority: 12000
- fault-injection # priority: 11000
- mocking # priority: 10900

启动 APISIX

1
2
3
4
5
6
$ make run
[ info ] run -> [ Start ]
/Users/guohao/workspace/apisix/bin/apisix start
/usr/local/openresty//luajit/bin/luajit ./apisix/cli/apisix.lua start
APISIX is running...
[ info ] run -> [ Done ]

修改 opentelemetry-collector-contrib demo 的配置

运行 otel/opentelemetry-collector 接受 http 上报

之前已经下载安装了代码, 现在修改一下里面的配置.

1
2
3
4
5
6
7
8
9
10
11
12
diff --git a/examples/demo/otel-collector-config.yaml b/examples/demo/otel-collector-config.yaml
index 38f9415203..858647ec94 100644
--- a/examples/demo/otel-collector-config.yaml
+++ b/examples/demo/otel-collector-config.yaml
@@ -2,6 +2,7 @@ receivers:
otlp:
protocols:
grpc:
+ http:

exporters:
prometheus:

添加这个配置文件目的是运行 APISIX 上报, 具体参考一下 conf/config-default.yaml 里面的 attr .

修改 docker-compose.yaml

因为 APISIX 是在宿主机器, 而这些服务是在容器里面. 但是默认的配置文件不允许和宿主机器通讯.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
diff --git a/examples/demo/docker-compose.yaml b/examples/demo/docker-compose.yaml
index ea94ffff28..822b26c23e 100644
--- a/examples/demo/docker-compose.yaml
+++ b/examples/demo/docker-compose.yaml
@@ -9,6 +9,8 @@ services:
- "16686:16686"
- "14268"
- "14250"
+ networks:
+ - demo_default

# Zipkin
zipkin-all-in-one:
@@ -16,6 +18,8 @@ services:
restart: always
ports:
- "9411:9411"
+ networks:
+ - demo_default

# Collector
otel-collector:
@@ -30,10 +34,13 @@ services:
- "8889:8889" # Prometheus exporter metrics
- "13133:13133" # health_check extension
- "4317:4317" # OTLP gRPC receiver
+ - '4318:4318'
- "55679:55679" # zpages extension
depends_on:
- jaeger-all-in-one
- zipkin-all-in-one
+ networks:
+ - demo_default

demo-client:
build:
@@ -42,9 +49,11 @@ services:
restart: always
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=otel-collector:4317
- - DEMO_SERVER_ENDPOINT=http://demo-server:7080/hello
+ - DEMO_SERVER_ENDPOINT=http://192.168.31.166:9080/hello
depends_on:
- demo-server
+ networks:
+ - demo_default

demo-server:
build:
@@ -66,3 +75,9 @@ services:
- ./prometheus.yaml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
+ networks:
+ - demo_default
+
+networks:
+ demo_default:
+ external: true

这个网络 external 是关键的, 运行访问外网.

第二个修改点, DEMO_SERVER_ENDPOINT 这个配置是指向 APISIX 网关的.192.168.31.166 这个 IP 地址是开发机器的地址.

配置 router

查看一下 container 信息了.

1
2
3
4
5
6
7
8
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
396943977b8a demo-demo-client "/app/main" 46 minutes ago Up 43 minutes demo-demo-client-1
1443892b209b demo-demo-server "/app/main" 46 minutes ago Up 46 minutes 0.0.0.0:53827->7080/tcp demo-demo-server-1
31bda5db3190 otel/opentelemetry-collector:0.67.0 "/otelcol --config=/…" 46 minutes ago Up 43 minutes 0.0.0.0:1888->1888/tcp, 0.0.0.0:4317-4318->4317-4318/tcp, 0.0.0.0:8888-8889->8888-8889/tcp, 0.0.0.0:13133->13133/tcp, 0.0.0.0:55679->55679/tcp, 55678/tcp demo-otel-collector-1
f469be73c494 prom/prometheus:latest "/bin/prometheus --c…" 56 minutes ago Up 56 minutes 0.0.0.0:9090->9090/tcp prometheus
298d6ba05bb1 jaegertracing/all-in-one:latest "/go/bin/all-in-one-…" 56 minutes ago Up 56 minutes 5775/udp, 6831-6832/udp, 5778/tcp, 0.0.0.0:16686->16686/tcp, 0.0.0.0:53028->14250/tcp, 0.0.0.0:53029->14268/tcp demo-jaeger-all-in-one-1
09d9d219e559 openzipkin/zipkin:latest "start-zipkin" 56 minutes ago Up 56 minutes (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp demo-zipkin-all-in-one-1

这个时候测试一下 demo-demo-server 验证一下端口是不是可以工作的.

1
2
$ curl http://192.168.31.166:53827/hello
Hello World⏎

配置 router

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"opentelemetry": {
"sampler": {
"name": "always_on"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:53827": 1
}
}
}'

验证 router 的是不是正常工作

1
2
curl http://192.168.31.166:9080/hello
Hello World

这个时候APISIX已经工作了,再检查一下 demo-demo-client 的日志

1
2
3
4
5
6
7
8
9
10
docker logs -f 396943977b8a
...
Latency: 253.957ms
Latency: 19.111ms
#0: LineLength: 866By
Latency: 236.504ms
#0: LineLength: 792By
#1: LineLength: 380By
#2: LineLength: 806By
Latency: 48.093ms

检查 jaeger

访问 http://localhost:16686/search service 的状态是 demo-client .

jaeger.png
jaeger.png