diff --git a/python/src/etos_api/routers/v1alpha/router.py b/python/src/etos_api/routers/v1alpha/router.py index 2570f5e..2a4d0ee 100644 --- a/python/src/etos_api/routers/v1alpha/router.py +++ b/python/src/etos_api/routers/v1alpha/router.py @@ -32,7 +32,8 @@ from etos_lib.kubernetes import TestRun, Environment, Kubernetes from fastapi import FastAPI, HTTPException from starlette.responses import Response -from opentelemetry import trace +from opentelemetry import trace, context +from opentelemetry.propagate import inject from opentelemetry.trace import Span @@ -105,6 +106,18 @@ async def health_check(): return Response(status_code=204) +def get_current_context() -> str: + """Get the current OpenTelemetry context.""" + ctx = context.get_current() + LOGGER.info("Current OpenTelemetry context: %s", ctx) + carrier = {} + # inject() creates a dict with context reference, + # e. g. {'traceparent': '00-0be6c260d9cbe9772298eaf19cb90a5b-371353ee8fbd3ced-01'} + inject(carrier) + env = ",".join(f"{k}={v}" for k, v in carrier.items()) + return env + + async def _create_testrun(etos: StartTestrunRequest, span: Span) -> dict: """Create a testrun for ETOS to execute. @@ -197,6 +210,9 @@ async def _create_testrun(etos: StartTestrunRequest, span: Span) -> dict: "etos.eiffel-community.github.io/id": testrun_id, "etos.eiffel-community.github.io/cluster": os.getenv("ETOS_CLUSTER", "Unknown"), }, + annotations={ + "etos.eiffel-community.github.io/traceparent": get_current_context(), + }, ), spec=TestRunSpec( cluster=os.getenv("ETOS_CLUSTER", "Unknown"), @@ -204,13 +220,15 @@ async def _create_testrun(etos: StartTestrunRequest, span: Span) -> dict: retention=retention, suiteRunner=Image( image=os.getenv( - "SUITE_RUNNER_IMAGE", "registry.nordix.org/eiffel/etos-suite-runner:latest" + "SUITE_RUNNER_IMAGE", + "registry.nordix.org/eiffel/etos-suite-runner:latest", ), imagePullPolicy=os.getenv("SUITE_RUNNER_IMAGE_PULL_POLICY", "IfNotPresent"), ), logListener=Image( image=os.getenv( - "LOG_LISTENER_IMAGE", "registry.nordix.org/eiffel/etos-log-listener:latest" + "LOG_LISTENER_IMAGE", + "registry.nordix.org/eiffel/etos-log-listener:latest", ), imagePullPolicy=os.getenv("LOG_LISTENER_IMAGE_PULL_POLICY", "IfNotPresent"), ),