如何让你的Nginx支持分布式追踪详解


Posted in Servers onJuly 07, 2022

Background

NGINX 是一个通用且流行的应用程序。也是最流行的 Web 服务器,它可用于提供静态文件内容,但也通常与其他服务一起用作分布式系统中的组件,在其中它用作反向代理、负载均衡 或 API 网关。

分布式追踪 distributed tracing 是一种可用于分析与监控应用程序的机制,将追踪在从源到目的的整个过程中的单个请求,这与仅通过单个应用程序域来追踪请求的形式不同。

换句话说,我们可以说分布式追踪是对跨多个系统的多个请求的拼接。拼接通常由一个或多个相关 ID 完成,并且跟踪通常是一组记录的、跨所有系统的结构化日志事件,存储在一个中心位置。

在这种背景的情况下, OpenTracing 应运而生。OpenTracing 是一个与应用供应商无关的 API,它可帮助开发人员轻松地跟踪单一请求的域。目前有多种开源产品都支持 OpenTracing(例如,Jaeger, skywalking 等),并将其作为一种检测分布式追踪的标准化方法。

本文将围绕,从0到1实现在nginx配置分布式追踪的架构的简单实例说明。本文实例使用的组件为

  • nginx-1.22
  • jaeger-all-in-on v1.38
  • nginx-opentracing v1.22
  • jaeger-client-cpp v0.9

源码构建nginx-opentracing

准备nginx-opentracing

nginx-opentracing 仓库中可以看到,官方为每个nginx版本都提供了一个编译好的动态库(Nginx1.19.13+),我们可以直接拿来使用这个动态库,如果你想将这个利用Nginx 提供的编译参数 --add-module=/path/to/module 构建为nginx的内置功能的话,可能会出现一些问题,例如下面的一些错误:

ngx_http_opentracing_module.so/config was found
/root/nginx-opentracing-0.25.0/opentracing//src/ngx_http_opentracing_module.cpp
In file included from /root/nginx-opentracing-0.25.0/opentracing//src/ngx_http_opentracing_module.cpp:1:0:
/root/nginx-opentracing-0.25.0/opentracing//src/load_tracer.h:3:38: fatal error: opentracing/dynamic_load.h: No such file or directory

根据 issue 中查询得知 nginx-opentracing 需要嵌入到nginx中,是需要一些 opentracing-cpp 因为对c++不熟,尝试调试很久还是上面的错误,故直接使用了官方提供的动态库。

准备jaeger-client-cpp

根据 nginx-opentracing 中提到的,还需要一个 jaeger-client-cpptracer 才可以正常运行(这也是作为jaeger架构中的角色)

来到 jaeger-client-cpp 看到Release提供的编译好的动态库已经很久了,而最新版都没有提供相应编译的版本,需要我们自己编译

说明: 编译依赖CMake 3.3+,gcc 4.9.2+

我们的编译环境使用CentOS 7 默认gcc与CMake都符合要求需要自行编译两个的版本。

编译gcc

gcc下载地址:https://ftp.gnu.org/gnu/gcc/

cd gcc-5.4.0
./contrib/download_prerequisites

mkdir gcc-build-5.4.0
cd gcc-build-5.4.0

/usr/local/src/gcc-5.4.0/configure \
	--enable-checking=release \
	--enable-languages=c,c++ \
	--disable-multilib
	
make && make install

引用处理 refer 1

cd /usr/bin/
mv gcc gcc_back
mv g++ g++_back
ln -s /usr/local/bin/gcc gcc
ln -s /usr/local/bin/g++ g++

编译时遇到几个问题

/lib64/libstdc++.so.6: version GLIBCXX_3.4.20' not found

gcc 编译,libgcc动态库有改动,恢复原状即可

configure: error: C++ compiler missing or inoperational

make[2]: \*** [configure-stage1-libcpp] Error 1    
make[2]: Leaving directory `/home/clay/programming/C++/gcc-4.8.1'    
make[1]: \*** [stage1-bubble] Error 2    
make[1]: Leaving directory `/home/clay/programming/C++/gcc-4.8.1'    
make: \*** [all] Error 2

编译cmake

./configure --prefix=/path/to/app

make
make install

这里遇到一个小问题 编译过程中遇到 [libstdc++.so.6: version GLIBCXX_3.4.20 not found

因为这里使用了自己编译的gcc版本,需要指定下动态库的路径 refer 2

LD_LIBRARY_PATH=/usr/local/lib64 ./configure --prefix=/usr/local/cmake

编译jaeger-client-cpp

这里根据官方提供的步骤操作即可

cd jaeger-client-cpp-0.9.0/
mkdir build
cd build
# 这里建议使用下强国特色上网,编译过程中会使用Hunter自动下载所需的依赖项
ALL_PROXY=http://x.0.0.x:10811 /usr/local/cmake/bin/cmake .. 
make

注:依赖项挺大的,下载时间可能很长,会hang主,只需等待结束即可

​编译完成后 libjaegertracing.so.0.9.0 则是我们需要的

编译nginx

./configure \
	--user=web_www \
	--group=web_www \
	--with-pcre \
	--with-compat \
	--with-http_ssl_module  \
	--with-http_gzip_static_module \
	--prefix=/root/nginx  \
	--with-http_stub_status_module

--with-compat 必须加上,表面允许使用动态库,否则编译完在启动时会报下面的错误

nginx: [emerg] module "/root/nginx/conf/ngx_http_opentracing_module.so" is not binary compatible in /root/nginx/conf/nginx.conf:1

遇到的问题,cc nou found,这里只需将 gcc 软连接一份为 cc 即可

配置nginx

准备jaeger-client的配置

jaeger.json

{
  "service_name": "nginx", // 服务名
  "sampler": {
    "type": "const",
    "param": 1
  },
  "reporter": {
    "localAgentHostPort": "jaeger:6831" // jaeger agent的地址
  },
  "headers": { // jaeger的默认的jaeger Baggage头设置
    "jaegerDebugHeader": "jaeger-debug-id",
    "jaegerBaggageHeader": "jaeger-baggage",
    "traceBaggageHeaderPrefix": "uberctx-"
  },
  "baggage_restrictions": {
    "denyBaggageOnInitializationFailure": false,
    "hostPort": ""
  }
}

在nginx中开启opentracing

# 加载 OpenTracing 动态模块。
load_module conf/ngx_http_opentracing_module.so;
worker_processes  1;
user root root;

events {
    worker_connections  1024;
}
http {
    log_format opentracing '{"timestamp":"$time_iso8601",'
                       '"source":"$server_addr",'
                       '"hostname":"$hostname",'
                       '"ip":"$http_x_forwarded_for",'
                       '"traceID":"$opentracing_context_uber_trace_id",'
                       '"client":"$remote_addr",'
                       '"request_method":"$request_method",'
                       '"scheme":"$scheme",'
                       '"domain":"$server_name",'
                       '"referer":"$http_referer",'
                       '"request":"$request_uri",'
                       '"args":"$args",'
                       '"size":$body_bytes_sent,'
                       '"status": $status,'
                       '"responsetime":$request_time,'
                       '"upstreamtime":"$upstream_response_time",'
                       '"upstreamaddr":"$upstream_addr",'
                       '"http_user_agent":"$http_user_agent",'
                       '"https":"$https"'
                       '}';
	# 加载 tracer,这里使用的jaeger,需要传递配置文件
    opentracing_load_tracer conf/libjaegertracing.so conf/jaeger.json;
    # 启用 tracing 
    opentracing on; 
    # 设置tag,可选参数
    opentracing_tag http_user_agent $http_user_agent;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            opentracing_operation_name $uri;
	    opentracing_propagate_context;
            root   html;
            index  index.html index.htm;
        }
	access_log logs/access.log opentracing;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

注:这里使用的 opentracing-nginx 的动态库为 ot16 ,linux-amd64-nginx-1.22.0-ot16-ngx_http_module.so.tgz ,另外一个版本不兼容,-t 检查语法时会提示

此时我们可以在jaeger上查看,可以看到 NGINX 的 span(因为这里只配置了NGINX,没有配置更多的后端)。

如何让你的Nginx支持分布式追踪详解

如何让你的Nginx支持分布式追踪详解

Reference

1 CentOS7 升级 GCC 到 5.4.0 版本

2 libstdc++.so.6: version GLIBCXX_3.4.20 not found

3 nginx load_module

总结

到此这篇关于如何让你的Nginx支持分布式追踪的文章就介绍到这了,更多相关Nginx支持分布式追踪内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!


Tags in this post...

Servers 相关文章推荐
nginx配置proxy_pass中url末尾带/与不带/的区别详解
Mar 31 Servers
Nginx 过滤静态资源文件的访问日志的实现
Mar 31 Servers
nginx+lua单机上万并发的实现
May 31 Servers
解决使用了nginx获取IP地址都是127.0.0.1 的问题
Sep 25 Servers
解决xampp安装后Apache无法启动
Mar 21 Servers
Nginx设置HTTPS的方法步骤 443证书配置方法
Mar 21 Servers
从零开始在Centos7上部署SpringBoot项目
Apr 07 Servers
阿里云 Windows server 2019 配置FTP
Apr 28 Servers
Nginx限流和黑名单配置
May 20 Servers
阿里云服务器Ubuntu 20.04上安装Odoo 15
May 20 Servers
keepalived + nginx 实现高可用方案
Dec 24 Servers
解决ubuntu安装软件时,status-code=409报错的问题
Dec 24 Servers
Zabbix对Kafka topic积压数据监控的解决方案
Zabbix对Kafka topic积压数据监控的问题(bug优化)
nginx静态资源的服务器配置方法
Jul 07 #Servers
Nginx配置使用详解
nginx代理实现静态资源访问的示例代码
Jul 07 #Servers
使用 DataAnt 监控 Apache APISIX的原理解析
设置IIS Express并发数
You might like
细谈php中SQL注入攻击与XSS攻击
2012/06/10 PHP
在windows服务器开启php的gd库phpinfo中未发现
2013/01/13 PHP
深入分析php中接口与抽象类的区别
2013/06/08 PHP
SAE实时日志接口SDK用法示例
2016/10/09 PHP
PHP运用foreach神奇的转换数组(实例讲解)
2018/02/01 PHP
PHP html_entity_decode()函数讲解
2019/02/25 PHP
理解Javascript_09_Function与Object
2010/10/16 Javascript
JavaScript NaN和Infinity特殊值 [译]
2012/09/20 Javascript
js实现的奥运倒计时时钟效果代码
2015/12/09 Javascript
酷! 不同风格页面布局幻灯片特效js实现
2021/02/19 Javascript
基于jQuery实现表格内容的筛选功能
2016/08/21 Javascript
jQuery根据ID、CLASS、等获取对象的实例
2016/12/04 Javascript
详解Jquery的事件操作和文档操作
2016/12/19 Javascript
微信小程序返回多级页面的实现方法
2017/10/27 Javascript
bootstrap tooltips在 angularJS中的使用方法
2019/04/10 Javascript
js实现动态时钟
2020/03/12 Javascript
[04:09]2018年度DOTA2社区贡献奖-完美盛典
2018/12/16 DOTA
python模拟登录百度代码分享(获取百度贴吧等级)
2013/12/27 Python
Python3操作MongoDB增册改查等方法详解
2020/02/10 Python
python实现最短路径的实例方法
2020/07/19 Python
Python操作PostgreSql数据库的方法(基本的增删改查)
2020/12/29 Python
HTML5实现WebSocket协议原理浅析
2014/07/07 HTML / CSS
html5给汉字加拼音加进度条的实现代码
2020/04/07 HTML / CSS
Html5 webview元素定位工具的实现
2020/08/07 HTML / CSS
空指针到底是什么
2012/08/07 面试题
什么是"引用"?申明和使用"引用"要注意哪些问题?
2016/03/03 面试题
xxx同志考察材料
2014/02/07 职场文书
护理专业学生职业生涯规划范文
2014/03/11 职场文书
护理专业毕业生自荐书
2014/05/24 职场文书
法人委托书的范本格式
2014/09/11 职场文书
公司财务会计主管应聘求职信
2014/09/26 职场文书
违反单位工作制度检讨书
2014/10/25 职场文书
python - asyncio异步编程
2021/04/06 Python
mysql分组后合并显示一个字段的多条数据方式
2022/01/22 MySQL
vue route新窗口跳转页面并且携带与接收参数
2022/04/10 Vue.js
使用Cargo工具高效创建Rust项目
2022/08/14 Javascript