Skip to content

Commit

Permalink
start wrapping trace context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgaard-itis committed Nov 27, 2024
1 parent 1720a3d commit 49a5ee2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/service-library/src/servicelib/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import wraps
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Deque

from opentelemetry import context as tracing_context
from opentelemetry import context as otcontext

from .utils_profiling_middleware import dont_profile, is_profiling, profile_context

Expand Down Expand Up @@ -36,7 +36,7 @@ class Context:

@dataclass
class QueueElement:
tracing_context: tracing_context.Context
tracing_context: otcontext.Context
do_profile: bool = False
input: Awaitable | None = None
output: Any | None = None
Expand Down Expand Up @@ -165,7 +165,7 @@ async def worker(in_q: Queue[QueueElement], out_q: Queue) -> None:
while True:
element = await in_q.get()
in_q.task_done()
tracing_context.attach(element.tracing_context)
otcontext.attach(element.tracing_context)
# check if requested to shutdown
try:
do_profile = element.do_profile
Expand All @@ -177,7 +177,7 @@ async def worker(in_q: Queue[QueueElement], out_q: Queue) -> None:
except Exception as e: # pylint: disable=broad-except
result = e
finally:
tracing_context.detach(element.tracing_context)
otcontext.detach(element.tracing_context)
await out_q.put(result)

logging.info(
Expand All @@ -195,7 +195,7 @@ async def worker(in_q: Queue[QueueElement], out_q: Queue) -> None:
queue_input = QueueElement(
input=decorated_function(*args, **kwargs),
do_profile=is_profiling(),
tracing_context=tracing_context.get_current(),
tracing_context=otcontext.get_current(),
)
await context.in_queue.put(queue_input)
wrapped_result = await context.out_queue.get()
Expand Down
28 changes: 28 additions & 0 deletions packages/service-library/src/servicelib/tracing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
from typing import Final

from opentelemetry import context as otcontext
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from settings_library.tracing import TracingSettings

_tracing_settings: Final[TracingSettings] = TracingSettings.create_from_envs()


LoggingInstrumentor().instrument(set_logging_format=False)


def _is_tracing() -> bool:
# TODO get this value by inspecting settings
return True


def get_current() -> otcontext.Context | None:
if not _is_tracing():
return None
return otcontext.get_current()


def attach(context: otcontext.Context | None) -> None:
if context is not None:
otcontext.attach(context)


def detach(context: otcontext.Context | None) -> None:
if context is not None:
otcontext.detach(context)

0 comments on commit 49a5ee2

Please sign in to comment.