Skip to content

Commit

Permalink
Add upstream address and name tags (#104)
Browse files Browse the repository at this point in the history
When a request is redirected to an upstream, then it is not visible
where it went. The ngx_http_upstream module maintains a variable
"upstream_addr" for the resolved IP address + port information. It
also sets the name of the used upstream config entry to the request
as request->upstream->upstream->host.
So add that information to the new tags "upstream.address" and
"upstream.name".
  • Loading branch information
sriemer authored and rnburn committed Aug 20, 2019
1 parent 9895e97 commit 327cc25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions opentracing/src/ngx_http_opentracing_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const std::pair<ngx_str_t, ngx_str_t> default_opentracing_tags[] = {
{ngx_string("component"), ngx_string("nginx")},
{ngx_string("nginx.worker_pid"), ngx_string("$pid")},
{ngx_string("peer.address"), ngx_string("$remote_addr:$remote_port")},
{ngx_string("upstream.address"), ngx_string("$upstream_addr")},
{ngx_string("http.method"), ngx_string("$request_method")},
{ngx_string("http.url"), ngx_string("$scheme://$http_host$request_uri")},
{ngx_string("http.host"), ngx_string("$http_host")}};
Expand Down
15 changes: 15 additions & 0 deletions opentracing/src/request_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ static void add_status_tags(const ngx_http_request_t *request,
}
}

//------------------------------------------------------------------------------
// add_upstream_name
//------------------------------------------------------------------------------
static void add_upstream_name(const ngx_http_request_t *request,
opentracing::Span &span) {
if (!request->upstream || !request->upstream->upstream ||
!request->upstream->upstream->host.data)
return;
auto host = request->upstream->upstream->host;
auto host_str = to_string(host);
span.SetTag("upstream.name", host_str);
}

//------------------------------------------------------------------------------
// RequestTracing
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -157,6 +170,7 @@ void RequestTracing::on_exit_block(
add_script_tags(main_conf_->tags, request_, *span_);
add_script_tags(loc_conf_->tags, request_, *span_);
add_status_tags(request_, *span_);
add_upstream_name(request_, *span_);

// If the location operation name is dependent upon a variable, it may not
// have been available when the span was first created, so set the operation
Expand Down Expand Up @@ -184,6 +198,7 @@ void RequestTracing::on_log_request() {
"finishing opentracing request span for %p", request_);
add_status_tags(request_, *request_span_);
add_script_tags(main_conf_->tags, request_, *request_span_);
add_upstream_name(request_, *request_span_);

// When opentracing_operation_name points to a variable and it can be
// initialized or modified at any phase of the request, so set the
Expand Down

0 comments on commit 327cc25

Please sign in to comment.