背景
NGINX 是一個通用且流行的應用程序。也是最流行的 Web 服務器,它可用于提供靜態文件內容,但也通常與其他服務一起用作分布式系統中的組件,在其中它用作反向代理、負載均衡 或 API 網關。
分布式追蹤 distributed tracing 是一種可用于分析與監控應用程序的機制,將追蹤在從源到目的的整個過程中的單個請求,這與僅通過單個應用程序域來追蹤請求的形式不同。
換句話說,我們可以說分布式追蹤是對跨多個系統的多個請求的拼接。拼接通常由一個或多個相關 ID 完成,并且跟蹤通常是一組記錄的、跨所有系統的結構化日志事件,存儲在一個中心位置。
在這種背景的情況下, OpenTracing 應運而生。OpenTracing 是一個與應用供應商無關的 API,它可幫助開發人員輕松地跟蹤單一請求的域。目前有多種開源產品都支持 OpenTracing(例如,Jaeger, skywalking 等),并將其作為一種檢測分布式追蹤的標準化方法。
本文將圍繞,從 0 到 1 實現在 nginx 配置分布式追蹤的架構的簡單實例說明。本文實例使用的組件為
- nginx[1] v1.22
- jaeger-all-in-on[2] v1.38
- nginx-opentracing[3] v1.22
- jaeger-client-cpp[4] v0.9
源碼構建 nginx-opentracing
準備 nginx-opentracing
nginx-opentracing[5] 倉庫中可以看到,官方為每個 nginx 版本都提供了一個編譯好的動態庫(Nginx1.19.13+),我們可以直接拿來使用這個動態庫,如果你想將這個利用 Nginx 提供的編譯參數 --add-module=/path/to/module 構建為 nginx 的內置功能的話,可能會出現一些問題,例如下面的一些錯誤:
ngx_http_opentracing_module.so/configwasfound
/root/nginx-opentracing-0.25.0/opentracing//src/ngx_http_opentracing_module.cpp
Infileincludedfrom/root/nginx-opentracing-0.25.0/opentracing//src/ngx_http_opentracing_module.cpp0:
/root/nginx-opentracing-0.25.0/opentracing//src/load_tracer.h38:fatalerror:opentracing/dynamic_load.h:Nosuchfileordirectory
根據 issue[6] 中查詢得知 nginx-opentracing 需要嵌入到 nginx 中,是需要一些 opentracing-cpp[7] 因為對 c++不熟,嘗試調試很久還是上面的錯誤,故直接使用了官方提供的動態庫。
準備 jaeger-client-cpp
根據 nginx-opentracing 中提到的,還需要一個 jaeger-client-cpp[8] 的 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/[9]
cdgcc-5.4.0
./contrib/download_prerequisites
mkdirgcc-build-5.4.0
cdgcc-build-5.4.0
/usr/local/src/gcc-5.4.0/configure
--enable-checking=release
--enable-languages=c,c++
--disable-multilib
make&&makeinstall
參考這篇文章:升級 GCC[10]
cd/usr/bin/
mvgccgcc_back
mvg++g++_back
ln-s/usr/local/bin/gccgcc
ln-s/usr/local/bin/g++g++
編譯時遇到幾個問題
/lib64/libstdc++.so.6: version GLIBCXX_3.4.20' not found
gcc 編譯,libgcc動態庫有改動,恢復原狀即可
configure:error:C++compilermissingorinoperational
make[2]:***[configure-stage1-libcpp]Error1
make[2]:Leavingdirectory`/home/clay/programming/C++/gcc-4.8.1'
make[1]:***[stage1-bubble]Error2
make[1]:Leavingdirectory`/home/clay/programming/C++/gcc-4.8.1'
make:***[all]Error2
編譯 cmake
./configure--prefix=/path/to/app
make
makeinstall
這里遇到一個小問題 編譯過程中遇到 [libstdc++.so.6: version GLIBCXX_3.4.20 not found
因為這里使用了自己編譯的 gcc 版本,需要指定下動態庫的路徑[11]
LD_LIBRARY_PATH=/usr/local/lib64./configure--prefix=/usr/local/cmake
編譯 jaeger-client-cpp
這里根據官方提供的步驟操作即可
cdjaeger-client-cpp-0.9.0/
mkdirbuild
cdbuild
#這里建議使用下特色上網,編譯過程中會使用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"isnotbinarycompatiblein/root/nginx/conf/nginx.conf:1
遇到的問題,cc nou found,這里只需將 gcc 軟連接一份為 cc 即可
配置 nginx
準備 jaeger-client 的配置
jaeger.json,參數的說明可以參考configuration[12]
{
"service_name":"nginx",//服務名
"sampler":{
"type":"const",
"param":1
},
"reporter":{
"localAgentHostPort":"jaeger:6831"//jaegeragent的地址
},
"headers":{//jaeger的默認的jaegerBaggage頭設置
"jaegerDebugHeader":"jaeger-debug-id",
"jaegerBaggageHeader":"jaeger-baggage",
"traceBaggageHeaderPrefix":"uberctx-"
},
"baggage_restrictions":{
"denyBaggageOnInitializationFailure":false,
"hostPort":""
}
}
在 nginx 中開啟 opentracing
對于更多的 nginx-opentracing 的配置說明參數可以參考 Reference.md[13]
#加載 OpenTracing 動態模塊。
load_moduleconf/ngx_http_opentracing_module.so;
worker_processes1;
userrootroot;
events{
worker_connections1024;
}
http{
log_formatopentracing'{"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_tracerconf/libjaegertracing.soconf/jaeger.json;
#啟用tracing
opentracingon;
#設置tag,可選參數
opentracing_taghttp_user_agent$http_user_agent;
includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;
server{
listen80;
server_namelocalhost;
location/{
opentracing_operation_name$uri;
opentracing_propagate_context;
roothtml;
indexindex.htmlindex.htm;
}
access_loglogs/access.logopentracing;
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
}
}
注:這里使用的 opentracing-nginx 的動態庫為 ot16 ,linux-amd64-nginx-1.22.0-ot16-ngx_http_module.so.tgz ,另外一個版本不兼容,-t 檢查語法時會提示
配置說明
對于每一個 location 都可以對其設置別名,這個就是 opentracing_operation_name 與 opentracing_location_operation_name 的區別
http{
...
location=/upload/animal{
opentracing_location_operation_nameupload;
...
更多的配置說明可以參考 Tutorial.md[14]
此時我們可以在 jaeger 上查看,可以看到 NGINX 的 span(因為這里只配置了 NGINX,沒有配置更多的后端)。
審核編輯 :李倩
-
服務器
+關注
關注
12文章
9319瀏覽量
86087 -
架構
+關注
關注
1文章
519瀏覽量
25562 -
nginx
+關注
關注
0文章
154瀏覽量
12237
原文標題:讓你的 Nginx 支持分布式追蹤 OpenTracing
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
分布式日志追蹤ID實戰
分布式軟件系統
LED分布式恒流原理
分布式發電技術與微型電網
開放分布式追蹤(OpenTracing)入門與 Jaeger 實現
基于分布式調用鏈監控技術的全息排查功能
分布式系統的優勢是什么?
HarmonyOS應用開發-分布式設計
支持掉線自動報警Profinet遠程分布式IO模塊分享
各種分布式電源的電氣特性
如何高效完成HarmonyOS分布式應用測試?
【學習打卡】OpenHarmony的分布式任務調度
HarmonyOS應用開發-分布式語音攝像頭體驗
常見的分布式供電技術有哪些?
OpenHarmony技術論壇:分布式相機和分布式圖庫功能
![OpenHarmony技術論壇:<b class='flag-5'>分布式</b>相機和<b class='flag-5'>分布式</b>圖庫功能](https://file.elecfans.com/web2/M00/3F/1F/poYBAGJmSv-ASqk1AA8Dhi9OakI548.png)
評論