From a0533c62d54debb88f753c0fe509e3fece1ee77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire?= Date: Tue, 2 Feb 2021 04:29:37 -0800 Subject: [PATCH] Avoid logging errors when expanding a variable with a null context (resolves #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_`). --- opentracing/src/opentracing_variable.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/opentracing/src/opentracing_variable.cpp b/opentracing/src/opentracing_variable.cpp index e4d3157d..a30d3972 100644 --- a/opentracing/src/opentracing_variable.cpp +++ b/opentracing/src/opentracing_variable.cpp @@ -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); @@ -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; }