如何讓你的Nginx支持分布式追蹤詳解
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-cpp 的 tracer
才可以正常運行(這也是作為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,沒有配置更多的后端)。
Reference
2 libstdc++.so.6: version GLIBCXX_3.4.20 not found
總結
到此這篇關于如何讓你的Nginx支持分布式追蹤的文章就介紹到這了,更多相關Nginx支持分布式追蹤內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx stream 配置代理(Nginx TCP/UDP 負載均衡)
本文主要介紹了Nginx stream 配置代理(Nginx TCP/UDP 負載均衡),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11教你利用Nginx 服務搭建子域環境提升二維地圖加載性能的步驟
這篇文章主要介紹了利用 Nginx 服務搭建子域環境提升二維地圖加載性能,本文分步驟通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-09-09
最新評論