|
4 | 4 | from time import time |
5 | 5 | from asyncio import sleep |
6 | 6 | from urllib.parse import urlparse, quote |
7 | | -from opentelemetry import trace |
| 7 | +from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator |
8 | 8 |
|
9 | 9 | import azure.functions as func |
10 | 10 |
|
@@ -72,19 +72,7 @@ async def start_new(self, |
72 | 72 | request_url = self._get_start_new_url( |
73 | 73 | instance_id=instance_id, orchestration_function_name=orchestration_function_name) |
74 | 74 |
|
75 | | - # Get the current span |
76 | | - current_span = trace.get_current_span() |
77 | | - span_context = current_span.get_span_context() |
78 | | - |
79 | | - # Get the traceparent and tracestate from the span context |
80 | | - # Follows the W3C Trace Context specification for traceparent |
81 | | - # https://www.w3.org/TR/trace-context/#traceparent-header |
82 | | - trace_id = format(span_context.trace_id, '032x') |
83 | | - span_id = format(span_context.span_id, '016x') |
84 | | - trace_flags = format(span_context.trace_flags, '02x') |
85 | | - trace_parent = f"00-{trace_id}-{span_id}-{trace_flags}" |
86 | | - |
87 | | - trace_state = span_context.trace_state |
| 75 | + trace_parent, trace_state = DurableOrchestrationClient._get_current_activity_context() |
88 | 76 |
|
89 | 77 | response: List[Any] = await self._post_async_request( |
90 | 78 | request_url, |
@@ -563,9 +551,14 @@ async def signal_entity(self, entityId: EntityId, operation_name: str, |
563 | 551 | entity_Id=entityId) |
564 | 552 |
|
565 | 553 | request_url = options.to_url(self._orchestration_bindings.rpc_base_url) |
| 554 | + |
| 555 | + trace_parent, trace_state = DurableOrchestrationClient._get_current_activity_context() |
| 556 | + |
566 | 557 | response = await self._post_async_request( |
567 | 558 | request_url, |
568 | | - json.dumps(operation_input) if operation_input else None) |
| 559 | + json.dumps(operation_input) if operation_input else None, |
| 560 | + trace_parent, |
| 561 | + trace_state) |
569 | 562 |
|
570 | 563 | switch_statement = { |
571 | 564 | 202: lambda: None # signal accepted |
@@ -797,3 +790,23 @@ async def resume(self, instance_id: str, reason: str) -> None: |
797 | 790 | error_message = has_error_message() |
798 | 791 | if error_message: |
799 | 792 | raise Exception(error_message) |
| 793 | + |
| 794 | + """Gets the current trace activity traceparent and tracestate |
| 795 | +
|
| 796 | + Returns |
| 797 | + ------- |
| 798 | + tuple[str, str] |
| 799 | + A tuple containing the (traceparent, tracestate) |
| 800 | + """ |
| 801 | + @staticmethod |
| 802 | + def _get_current_activity_context() -> tuple[str, str]: |
| 803 | + carrier = {} |
| 804 | + |
| 805 | + # Inject the current trace context into the carrier |
| 806 | + TraceContextTextMapPropagator().inject(carrier) |
| 807 | + |
| 808 | + # Extract the traceparent and optionally the tracestate |
| 809 | + trace_parent = carrier.get("traceparent") |
| 810 | + trace_state = carrier.get("tracestate") |
| 811 | + |
| 812 | + return trace_parent, trace_state |
0 commit comments