如何讓你的Nginx支持分布式追蹤詳解
目錄
- Background
- 源碼構(gòu)建nginx-opentracing
- 準(zhǔn)備nginx-opentracing
- 準(zhǔn)備jaeger-client-cpp
- 編譯gcc
- 編譯cmake
- 編譯jaeger-client-cpp
- 編譯nginx
- 配置nginx
- 準(zhǔn)備jaeger-client的配置
- 在nginx中開啟opentracing
- 總結(jié)
Background
NGINX 是一個通用且流行的應(yīng)用程序。也是最流行的 Web 服務(wù)器,它可用于提供靜態(tài)文件內(nèi)容,但也通常與其他服務(wù)一起用作分布式系統(tǒng)中的組件,在其中它用作反向代理、負(fù)載均衡 或 API 網(wǎng)關(guān)。
分布式追蹤 distributed tracing
是一種可用于分析與監(jiān)控應(yīng)用程序的機制,將追蹤在從源到目的的整個過程中的單個請求,這與僅通過單個應(yīng)用程序域來追蹤請求的形式不同。
換句話說,我們可以說分布式追蹤是對跨多個系統(tǒng)的多個請求的拼接。拼接通常由一個或多個相關(guān) ID 完成,并且跟蹤通常是一組記錄的、跨所有系統(tǒng)的結(jié)構(gòu)化日志事件,存儲在一個中心位置。
在這種背景的情況下, OpenTracing
應(yīng)運而生。OpenTracing
是一個與應(yīng)用供應(yīng)商無關(guān)的 API,它可幫助開發(fā)人員輕松地跟蹤單一請求的域。目前有多種開源產(chǎn)品都支持 OpenTracing(例如,Jaeger
, skywalking
等),并將其作為一種檢測分布式追蹤的標(biāo)準(zhǔn)化方法。
本文將圍繞,從0到1實現(xiàn)在nginx配置分布式追蹤的架構(gòu)的簡單實例說明。本文實例使用的組件為
nginx-1.22
jaeger-all-in-on v1.38
nginx-opentracing v1.22
jaeger-client-cpp v0.9
源碼構(gòu)建nginx-opentracing
準(zhǔn)備nginx-opentracing
準(zhǔn)備jaeger-client-cpp
根據(jù) nginx-opentracing
中提到的,還需要一個 jaeger-client-cpp 的 tracer
才可以正常運行(這也是作為jaeger架構(gòu)中的角色)
來到 jaeger-client-cpp
看到Release提供的編譯好的動態(tài)庫已經(jīng)很久了,而最新版都沒有提供相應(yīng)編譯的版本,需要我們自己編譯
說明: 編譯依賴CMake 3.3+,gcc 4.9.2+
我們的編譯環(huán)境使用CentOS 7 默認(rèn)gcc與CMake都符合要求需要自行編譯兩個的版本。
編譯gcc
gcc下載地址:https://ftp.gnu.org/gnu/gcc/
cd gcc-5.4.0./contrib/download_prerequisitesmkdir gcc-build-5.4.0cd 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_backmv g++ g++_backln -s /usr/local/bin/gcc gccln -s /usr/local/bin/g++ g++
編譯時遇到幾個問題
/lib64/libstdc++.so.6: version GLIBCXX_3.4.20' not found
gcc 編譯,libgcc
動態(tài)庫有改動,恢復(fù)原狀即可
configure: error: C++ compiler missing or inoperationalmake[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/appmakemake install
這里遇到一個小問題 編譯過程中遇到 [libstdc++.so.6: version GLIBCXX_3.4.20 not found
因為這里使用了自己編譯的gcc版本,需要指定下動態(tài)庫的路徑 refer 2
LD_LIBRARY_PATH=/usr/local/lib64 ./configure --prefix=/usr/local/cmake
編譯jaeger-client-cpp
這里根據(jù)官方提供的步驟操作即可
cd jaeger-client-cpp-0.9.0/mkdir buildcd build# 這里建議使用下強國特色上網(wǎng),編譯過程中會使用Hunter自動下載所需的依賴項ALL_PROXY=http://x.0.0.x:10811 /usr/local/cmake/bin/cmake .. make
注:依賴項挺大的,下載時間可能很長,會hang主,只需等待結(jié)束即可
?編譯完成后 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
必須加上,表面允許使用動態(tài)庫,否則編譯完在啟動時會報下面的錯誤
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
準(zhǔn)備jaeger-client的配置
jaeger.json
{ "service_name": "nginx", // 服務(wù)名 "sampler": { "type": "const", "param": 1 }, "reporter": { "localAgentHostPort": "jaeger:6831" // jaeger agent的地址 }, "headers": { // jaeger的默認(rèn)的jaeger Baggage頭設(shè)置 "jaegerDebugHeader": "jaeger-debug-id", "jaegerBaggageHeader": "jaeger-baggage", "traceBaggageHeaderPrefix": "uberctx-" }, "baggage_restrictions": { "denyBaggageOnInitializationFailure": false, "hostPort": "" }}
在nginx中開啟opentracing
# 加載 OpenTracing 動態(tài)模塊。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; # 設(shè)置tag,可選參數(shù) opentracing_tag http_user_agent $http_user_agent; include mime.types; default_type application/octet-stream; sendfileon; 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
的動態(tài)庫為 ot16 ,linux-amd64-nginx-1.22.0-ot16-ngx_http_module.so.tgz
,另外一個版本不兼容,-t
檢查語法時會提示
此時我們可以在jaeger上查看,可以看到 NGINX 的 span(因為這里只配置了NGINX,沒有配置更多的后端)。
Reference
1 CentOS7 升級 GCC 到 5.4.0 版本
2 libstdc++.so.6: version GLIBCXX_3.4.20 not found
3 nginx load_module
總結(jié)
到此這篇關(guān)于如何讓你的Nginx支持分布式追蹤的文章就介紹到這了,更多相關(guān)Nginx支持分布式追蹤內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
