From aac886927734e097095bac3a2c1ed3bd92a8b156 Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Mon, 6 Jan 2025 18:47:47 -0800 Subject: [PATCH 1/2] otel: fix segfaults when otel not configured This commit adds NULL checks for the request->otel object that were missed in the Traceparent and Tracestate routines. Closes: https://github.com/nginx/unit/issues/1523 Closes: https://github.com/nginx/unit/issues/1526 Fixes: 9d3dcb800 ("otel: add build tooling to include otel code") Signed-off-by: Ava Hahn --- src/nxt_otel.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/nxt_otel.c b/src/nxt_otel.c index eea12e24f..ef796a7ce 100644 --- a/src/nxt_otel.c +++ b/src/nxt_otel.c @@ -311,17 +311,13 @@ nxt_otel_test_and_call_state(nxt_task_t *task, nxt_http_request_t *r) void nxt_otel_request_error_path(nxt_task_t *task, nxt_http_request_t *r) { - if (r->otel->trace == NULL) { + if (r->otel == NULL || r->otel->trace == NULL) { return; } // response headers have been cleared nxt_otel_propagate_header(task, r); - - // collect span immediately - if (r->otel) { - nxt_otel_state_transition(r->otel, NXT_OTEL_COLLECT_STATE); - } + nxt_otel_state_transition(r->otel, NXT_OTEL_COLLECT_STATE); nxt_otel_test_and_call_state(task, r); } @@ -344,6 +340,9 @@ nxt_otel_parse_traceparent(void *ctx, nxt_http_field_t *field, uintptr_t data) */ r = ctx; + if (r->otel == NULL) { + return NXT_OK; + } if (field->value_length != NXT_OTEL_TRACEPARENT_LEN) { goto error_state; @@ -391,6 +390,10 @@ nxt_otel_parse_tracestate(void *ctx, nxt_http_field_t *field, uintptr_t data) s.start = field->value; r = ctx; + if (r->otel == NULL) { + return NXT_OK; + } + r->otel->trace_state = s; /* From bf03b35ce186bef715a76117da4fc5a42c37fb96 Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Tue, 7 Jan 2025 15:27:26 -0800 Subject: [PATCH 2/2] otel: remove deadcode The superfluous else condition in nxt_otel_propagate_header was dead code. This commit removes it. Signed-off-by: Ava Hahn --- src/nxt_otel.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/nxt_otel.c b/src/nxt_otel.c index ef796a7ce..38ef02eaa 100644 --- a/src/nxt_otel.c +++ b/src/nxt_otel.c @@ -67,7 +67,7 @@ nxt_otel_propagate_header(nxt_task_t *task, nxt_http_request_t *r) * if we didn't inherit a trace id then we need to add the * traceparent header to the request */ - } else if (r->otel->trace_id == NULL) { + } else { nxt_otel_rs_copy_traceparent(traceval, r->otel->trace); @@ -92,15 +92,6 @@ nxt_otel_propagate_header(nxt_task_t *task, nxt_http_request_t *r) nxt_otel_rs_add_event_to_trace(r->otel->trace, &traceparent_name, &traceparent); - - /* - * potentially nxt_http_request_error called before headers - * finished parsing - */ - } else { - nxt_log(task, NXT_LOG_DEBUG, - "not propagating tracing headers for missing trace"); - return; } f = nxt_list_add(r->resp.fields);