使用 OpenTelemetry 对 OpenResty 进行链路追踪最佳实践

简介

本文介绍如何使用 OpenTelemetry 对 OpenResty 进行链路追踪。OpenResty 是一个基于 Nginx 与 LuaJIT 的高性能 Web 平台,通过集成 Lua 脚本语言,提供了强大的动态处理能力。OpenResty 支持通过 ngx_otel_module 模块采集调用链数据,并直接上报至观测云平台。

观测云

观测云采集器 DataKit 支持 OpenTelemetry 采集插件,能够接收 ngx_otel_module 模块链路数据并在平台统一分析。

部署 DataKit

登录观测云控制台,点击「集成」-「DataKit」-「Linux」,复制安装命令在服务器执行即可。

开启 OpenTelemetry 插件

bash 复制代码
# 进入采集器配置文件目录
cd /usr/local/datakit/conf.d/samples
# 开启配置文件
cp opentelemetry.conf.sample opentelemetry.conf
# 重启 Datakit
datakit service -R

接入步骤

构建 ngx_otel_module 模块

  • 安装构建工具和依赖项
arduino 复制代码
sudo apt update

sudo apt install cmake build-essential libssl-dev zlib1g-dev libpcre3-dev

sudo apt install pkg-config libc-ares-dev libre2-dev # for gRPC
  • 确定 OpenResty 使用的 Nginx 版本和编译配置

    openresty -V

以下为输出示例:

shell 复制代码
openresty -V
#nginx version: openresty/1.25.3.2
#built with OpenSSL 1.1.1w  11 Sep 2023
#TLS SNI support enabled
#configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.3 --add-module=../echo-nginx-module-0.63 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.33 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.09 --add-module=../srcache-nginx-module-0.33 --add-module=../ngx_lua-0.10.26 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.37 --add-module=../array-var-nginx-module-0.06 --add-module=../memc-nginx-module-0.20 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.9 --add-module=../ngx_stream_lua-0.0.14 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --with-http_v3_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_slice_module --with-http_gunzip_module --with-threads --with-stream --without-pcre2 --with-http_ssl_module
  • 下载对应版本的 Nginx 源码
bash 复制代码
git clone https://github.com/nginx/nginx.git
cd nginx
git checkout {OPENRESTY_NGINX_VERSION}  # 切换到对应版本Tag

{OPENRESTY_NGINX_VERSION} 替换为对应版本 Tag,以 1.25.3.2 版本为例,对应的版本 Tag 为 release-1.25.3

  • 配置 Nginx 编译参数

在 nginx 源码根目录执行以下命令

scss 复制代码
./auto/configure {NGINX_CONFIGURE_ARGUMENTS} --with-compat 

{NGINX_CONFIGURE_ARGUMENTS} 为获取的 configure arguments 内容,并移除与 --add-module 相关的参数配置。

  • 下载 ngx_otel_module 模块源码
bash 复制代码
cd ..
git clone https://github.com/nginxinc/nginx-otel.git
cd /nginx-otel
  • 编译 ngx_otel_module 模块

创建并进入 build 目录进行编译,编译完成后会在 build 目录生成 ngx_otel_module.so 文件。

bash 复制代码
mkdir build
cd build
cmake -DNGX_OTEL_NGINX_BUILD_DIR=/nginx/objs ..
make

启用 ngx_otel_module 模块

  • ngx_otel_module.so 模块文件复制到 nginx modules 目录
bash 复制代码
mkdir -p /usr/local/openresty/nginx/modules/
cp ngx_otel_module.so /usr/local/openresty/nginx/modules/
  • 配置 nginx.conf

为 OpenResty 启用链路追踪,您需要在 Nginx 主配置文件 /usr/local/openresty/nginx/conf/nginx.conf 中加载 ngx_otel_module 模块并添加配置项。注意 ngx_otel_module 模块目前仅支持 gRPC 方式上报,不支持 HTTP 方式上报。关于 ngx_otel_module 模块的更多参数配置信息,请参考 nginx.org/en/docs/ngx...

bash 复制代码
load_module modules/ngx_otel_module.so; # 加载 ngx_otel_module
...
http {
    ...

    otel_exporter {
        endpoint "127.0.0.1:4317"; #  gRPC 接入点
        #header Authentication "${GRPC_TOKEN}"; # 前提条件中获取的鉴权 Token
    }

    otel_trace on;                     # 开启链路追踪
    otel_service_name openresty-otel;  # 应用名
    otel_trace_context propagate;      # 向下游服务注入Trace上下文
    ...
}
  • 检查 nginx.conf 配置是否正确
bash 复制代码
/usr/local/openresty/nginx/sbin/nginx -t

如下,表明配置无误

vbnet 复制代码
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
  • 重载配置
bash 复制代码
# 方式1: 使用 openresty 命令重载配置
openresty -s reload

# 方式2: 使用 nginx 命令重载配置
/usr/local/openresty/nginx/sbin/nginx -s reload

注意事项

如果执行编译,出现如图所示卡住的情况,可以通过以下方案解决:

  • 确保使用完全相同的 nginx 版本。
  • 使用nginx -V 查看当前 nginx 的编译参数。
  • 执行 openresty -V 检查 configure arguments 参数是否正确。

然后重新编译即可

查看链路

总结

OpenResty(Nginx+LuaJIT 高性能 Web 平台)通过 ngx_otel_module 模块采集调用链数据,经观测云 DataKit 采集器的 OpenTelemetry 采集器接收,实现链路追踪数据在观测云平台的统一分析上报。

相关推荐
苏渡苇3 天前
Spring Insight 里如何把 Span 画成瀑布时间线
后端·spring·spring cloud·springboot·监控·apm
行百里er3 天前
轻量级 Spring 监测工具——Spring Insight 发布了
spring boot·后端·监控
宋均浩4 天前
告警规则 243 砍到 27:Prometheus 降噪实战,日均打扰 47 次 → 3 次
云原生·监控·devops
小小ken5 天前
水星摄像头(MIPC552W)双摄版和乔安摄像头(JA-A12,JA-w1)接入easynvr具体步骤
监控·摄像头·nvr·easynvr·水星·乔安
行百里er5 天前
Spring Insight 里如何把 Span 画成瀑布时间线
spring boot·后端·监控
行百里er6 天前
Spring Insight 里如何把 Span 收成一条链路
spring boot·spring cloud·监控
北京盛世宏博7 天前
老旧档案库房环境恒温恒湿自动化改造:不破坏原有库房结构的轻量化集成方案
监控·档案温湿度
行百里er8 天前
Spring Insight 里如何画出服务拓扑
spring boot·spring cloud·监控
行百里er9 天前
Spring Insight 里如何对 Span 进行清洗
spring boot·spring cloud·监控
盛世宏博智慧档案10 天前
户外配电柜为什么要装POE供电以太网温湿度传感器?
服务器·网络·人工智能·监控·温湿度·配电柜