OpenResty
配置¶
配置 DDTrace¶
下载 Tracing 模块¶
访问 https://github.com/DataDog/nginx-datadog/releases, 在 Assets 中寻找并下载与当前处理器架构和版本匹配的模块 openresty-ngx_http_datadog_module-<处理器架构>-<版本>.so.tgz
安装 Tracing¶
- 创建模块路径
- 解压
root@root:/usr/local/openresty/nginx/modules$ tar xzvf openresty-ngx_http_datadog_module-<处理器架构>-<版本>.so.tgz
配置 Nginx¶
在 /usr/local/openresty/nginx/samples/nginx.conf 中添加以下内容:
# 加载模块
load_module modules/ngx_http_datadog_module.so;
worker_processes 1;
events {
worker_connections 1024;
}
http {
# 配置 Datakit URL
datadog_agent_url http://localhost:9529;
# 配置服务名称
datadog_service_name openresty-demo;
# 配置环境名称
datadog_environment test;
# 配置版本名称
datadog_version 1.0.0;
# 配置采样率 0-1.0
datadog_sample_rate 1.0;
# 配置标签
datadog_tag foo bar;
include mime.types;
default_type application/octet-stream;
# 通过 $datadog_trace_id 和 $datadog_span_id 为日志加入 trace_id 和 span_id,均为 16 进制
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$datadog_trace_id" "$datadog_span_id"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重启 OpenResty¶
略
开启 DataKit 采集¶
- 开启
ddtrace
- 重启 DataKit
配置 OpenTelemetry¶
构建 ngx_otel_module 模块¶
- 安装构建工具和依赖项
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
#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 ············
- 下载对应版本的 Nginx 源码
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 源码根目录执行以下命令
{NGINX_CONFIGURE_ARGUMENTS}为获取的 configure arguments 内容,并移除与 --add-module 相关的参数配置。
- 下载 ngx_otel_module 模块源码
- 编译 ngx_otel_module 模块
创建并进入 build 目录进行编译,编译完成后会在 build 目录生成 ngx_otel_module.so 文件
启用 ngx_otel_module 模块¶
- 将ngx_otel_module.so模块文件复制到 nginx modules 目录
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 模块的更多参数配置信息,请参考 ngx_otel_module 文档
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 配置是否正确
- 重载配置
# 方式1: 使用 openresty 命令重载配置
openresty -s reload
# 方式2: 使用 nginx 命令重载配置
/usr/local/openresty/nginx/sbin/nginx -s reload
开启 DataKit 采集¶
- 开启 OpenTelemetry 采集器
# 进入采集器配置文件目录
cd /usr/local/datakit/conf.d/samples
# 开启配置文件
cp opentelemetry.conf.sample opentelemetry.conf
- 重启 Datakit