Skip to content

Commit

Permalink
Avoid logging errors when expanding a variable with a null context (r…
Browse files Browse the repository at this point in the history
…esolves #124) (#151)

When opentracing is set to off in the nginx configuration and if an
opentracing variable was used, get_opentracing_context would return
null during its expansion and an error was logged. Instead I think
a NGX_ERROR should be returned so the variable is expanded to '-'.

Also I made sure the full variable name is logged instead of only
the prefix (`opentracing_context_x_datadog_trace_id` instead of
`opentracing_context_`).
  • Loading branch information
verdie-g authored Feb 2, 2021
1 parent 080a87f commit a0533c6
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions opentracing/src/opentracing_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ static ngx_int_t expand_opentracing_context_variable(
variable_name.size() - prefix_length};

auto context = get_opentracing_context(request);
// Context can be null if opentracing is set to off.
if (context == nullptr) {
throw std::runtime_error{"no OpenTracingContext attached to request"};
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, request->connection->log, 0,
"failed to expand %V: no OpenTracingContext attached to request %p",
data, request);
return NGX_ERROR;
}

auto span_context_value = context->lookup_span_context_value(request, key);
Expand All @@ -61,9 +65,9 @@ static ngx_int_t expand_opentracing_context_variable(
return NGX_OK;
} catch (const std::exception& e) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"failed to expand %s"
"failed to expand %V"
" for request %p: %s",
opentracing_context_variable_name.data(), request, e.what());
data, request, e.what());
return NGX_ERROR;
}

Expand Down

0 comments on commit a0533c6

Please sign in to comment.