IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

Show your support

More detail in NGINX error logs · Dec 5, 21:38 by Shannon Wynter

Where I’m working now we have a rather complicated internal structure to our rather large web application and it’s sometime super hard to track down the problem – especially if a request triggers an API call.

I’ve modified our frontend configuration to pass a X-Trace-ID and all our logging/servers to include that trace id in any given request

	set $tid $pid-$msec-$remote_addr-$connection-$request_id;
	if ($http_x_trace_id) {
		set $tid $http_x_trace_id;
	}
 	proxy_set_header X-Trace-ID $tid;
	fastcgi_param HTTP_X_TRACE_ID $tid;

Except when it comes to error logs, if anything goes wrong with a proxied request, we’re out of luck – well not any more :D

  1. diff -dur nginx-1.11.6.org/src/http/ngx_http_request.c nginx-1.11.6/src/http/ngx_http_request.c
  2. --- nginx-1.11.6.org/src/http/ngx_http_request.c2016-11-16 01:11:47.000000000 +1000
  3. +++ nginx-1.11.6/src/http/ngx_http_request.c2016-12-05 21:18:16.000000000 +1000
  4. @@ -118,6 +118,10 @@
  5.                   offsetof(ngx_http_headers_in_t, content_type),
  6.                   ngx_http_process_header_line },
  7.   
  8. +    { ngx_string("X-Trace-ID"),
  9. +                 offsetof(ngx_http_headers_in_t, x_trace_id),
  10. +                 ngx_http_process_header_line },
  11. +
  12.      { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
  13.                   ngx_http_process_header_line },
  14.   
  15. @@ -3656,5 +3660,11 @@
  16.          buf = p;
  17.      }
  18.   
  19. +    if (r->headers_in.x_trace_id) {
  20. +        p = ngx_snprintf(buf, len, ", trace_id: \"%V\"",
  21. +                         &r->headers_in.x_trace_id->value);
  22. +        buf = p;
  23. +    }
  24. +
  25.      return buf;
  26. }
  27. diff -dur nginx-1.11.6.org/src/http/ngx_http_request.h nginx-1.11.6/src/http/ngx_http_request.h
  28. --- nginx-1.11.6.org/src/http/ngx_http_request.h2016-11-16 01:11:47.000000000 +1000
  29. +++ nginx-1.11.6/src/http/ngx_http_request.h2016-12-05 21:33:07.000000000 +1000
  30. @@ -185,6 +185,7 @@
  31.      ngx_table_elt_t                  *content_length;
  32.      ngx_table_elt_t                  *content_range;
  33.      ngx_table_elt_t                  *content_type;
  34. +    ngx_table_elt_t                  *x_trace_id;
  35.   
  36.      ngx_table_elt_t                  *range;
  37.      ngx_table_elt_t                  *if_range;
  38.  
  39. Download this code: nginx_trace_id.1.11.6.patch (Downloaded 13 time(s))

Apply this patch to nginx-1.11.6 and X-Trace-ID will appear in your logs if it’s available

Comments

Spam no more - rel=nofollow is active here, spamming my comments will not help your page rank.

  Textile help
---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==---