nginx中加入opentracing能力


nginx中加入opentracing能力

文章插图
 
Why Nginx tracingNginx是一个广泛使用的web原件,常用于反向代理、负载均衡服务器、API网关 。在日常的使用中,nginx常常要负责各种redirect、rewrite等服务,对于业务或者调用关系负责的情况,tracing能够帮助开发者直观分析请求链路,快速定位性能瓶颈,逐渐优化服务间依赖,也有助于开发者从更宏观的角度更好地理解整个系统 。
同时,如果开发者能够在代码内部也加入对tracing的支持,那么对于自己开发的服务整体从将可以以上帝视角直观地看到 。
环境要求nginx stable 1.2+
ngonx-opentracing module
jaeger环境
配置详情1. nginx.conf
load_module modules/ngx_http_opentracing_module.so;usernobody nobody;worker_processesauto;working_directory /search/odin/nginx/core;worker_rlimit_core 2G;error_loglogs/error.log error;pid/var/run/nginx.pid;events {use epoll;worker_connections51200;}http {include/etc/nginx/mime.types;default_typeApplication/octet-stream;opentracing_load_tracer /usr/local/lib/libjaegertracing_plugin.linux_amd64.so /etc/nginx/conf.d/jaeger-config.json;opentracing on;opentracing_propagate_context;opentracing_operation_name nginx-$host;opentracing_tag request.id $request_id;index index.html index.htm index.shtml;set_real_ip_from10.0.0.0/8;set_real_ip_from192.168.0.0/16;real_ip_headerX-Real-IP;log_format main '$host $remote_addr [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" "$http_user_agent" ''$cookie_SUID $request_time $cookie_SSUID ''$http_x_forwarded_for $request_length $cookie_YYID ''$connection_requests "$upstream_addr" $request_id';log_format mini '$time_local $status $body_bytes_sent $request_time $upstream_cache_status $server_name';access_log "logs/${server_name}_access_log" main;access_log logs/status_log mini;sendfileon;#tcp_nopushon;keepalive_timeout65;gzipon;gzip_min_length1024;gzip_comp_level6;gzip_typestext/plain text/xml application/x-JAVAscript text/css application/xml;include /etc/nginx/conf.d/*.conf;}首先,加载nginx 模块:
ngx_http_opentracing_module.so
load_module modules/ngx_http_opentracing_module.so;其次,加载并配置jaeger plugin
opentracing_load_tracer /usr/local/lib/libjaegertracing_plugin.linux_amd64.so /etc/nginx/conf.d/jaeger-config.json;opentracing on; # 启用opentracing,默认为offopentracing_propagate_context; # 启用传递contextopentracing_operation_name nginx-$host; # 为请求起个nameopentracing_tag request.id $request_id; # 设置span tag request.id,需要nginx 1.11以上其中专门设置了一个request.id的tag,这个request.id串起来从nginx到code的所有请求,方便检索请求
最后,附加一个jaeger的配置,localAgentHostPort需要配置成jaeger-agent所在host:port
{"service_name": "nginx","diabled": false,"reporter": {"logSpans": true,"localAgentHostPort": "jaeger:6831"},"sampler": {"type": "const","param": "1"}}2. server.conf
upstream api{server 127.0.0.1:8080;}server{listen *:80 default_server;server_name xx.test;opentracing_location_operation_name $request;opentracing_propagate_context;proxy_set_header X-Request-Id $request_id;proxy_set_headerHost $host;proxy_http_version 1.1;location ~ ^/api/ {proxy_pass http://api;}}以下三条配置分别设置了location name、启用传递context、设置header X-Request-Id为 $request_id
opentracing_location_operation_name $request;opentracing_propagate_context;proxy_set_header X-Request-Id $request_id;
nginx中加入opentracing能力

文章插图
 
 
3. 代码接入opentracing
import (.....opentracing "github.com/opentracing/opentracing-go""github.com/opentracing/opentracing-go/ext")func (c *AlertManagerController) Webhook() { spanCtx, _ := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Ctx.Request.Header)) span := opentracing.GlobalTracer().StartSpan("Webhook", ext.RPCServerOption(spanCtx)) // 关联为nginx的child uuid := c.Ctx.Input.Header("X-Request-Id") span.SetTag("request.id", uuid) // 设置request tag span.SetBaggageItem("request.id", uuid) // 向后续请求加入request.id span.LogKV("request.body", string(c.Ctx.Input.RequestBody))......}
nginx中加入opentracing能力

文章插图
 
?
nginx中加入opentracing能力

文章插图
 


推荐阅读