From b37a611fc6768d982d4234282b034a166a0a995a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de>
Date: Sat, 7 Dec 2024 01:45:03 +0100
Subject: [PATCH] Actually extract/inject trace from/into HTTP headers

---
 src/api/routes/route.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/api/routes/route.py b/src/api/routes/route.py
index aca1758..4c9e8c1 100644
--- a/src/api/routes/route.py
+++ b/src/api/routes/route.py
@@ -240,13 +240,21 @@ def _handle_api_request(
     start_time = time.time_ns()
     
     if do_tracing:
+        from opentelemetry.instrumentation.utils import propagator
         from opentelemetry.sdk.trace import Tracer
         assert isinstance(open_telemetry_tracer, Tracer)
-        with open_telemetry_tracer.start_as_current_span("api-request", attributes={
+        
+        context = propagator.extract(request.headers)
+        
+        with open_telemetry_tracer.start_as_current_span("api-request", context=context, attributes={
             "url": request.url,
             "server": _SERVER_NAME,
         }):
             resp = _execute_api_route(call_counter, allow_while_readonly, allow_while_disabled, rate_limiters, func, args, kwargs)
+        
+        # Note that the context still contains the client's span, and we inject that. Not our span (the HTTP header is
+        # called "traceparent")
+        propagator.inject(resp.headers, context=context)
     else:
         resp = _execute_api_route(call_counter, allow_while_readonly, allow_while_disabled, rate_limiters, func, args, kwargs)
     
-- 
GitLab