Skip to content

Commit f655a1a

Browse files
authored
Add support to send trace information to the WebJobs extension (#540)
1 parent 10ceb4d commit f655a1a

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

azure/durable_functions/models/DurableOrchestrationClient.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import time
55
from asyncio import sleep
66
from urllib.parse import urlparse, quote
7+
from opentelemetry import trace
78

89
import azure.functions as func
910

@@ -71,8 +72,25 @@ async def start_new(self,
7172
request_url = self._get_start_new_url(
7273
instance_id=instance_id, orchestration_function_name=orchestration_function_name)
7374

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
88+
7489
response: List[Any] = await self._post_async_request(
75-
request_url, self._get_json_input(client_input))
90+
request_url,
91+
self._get_json_input(client_input),
92+
trace_parent,
93+
trace_state)
7694

7795
status_code: int = response[0]
7896
if status_code <= 202 and response[1]:

azure/durable_functions/models/utils/http_utils.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import aiohttp
44

55

6-
async def post_async_request(url: str, data: Any = None) -> List[Union[int, Any]]:
6+
async def post_async_request(url: str,
7+
data: Any = None,
8+
trace_parent: str = None,
9+
trace_state: str = None) -> List[Union[int, Any]]:
710
"""Post request with the data provided to the url provided.
811
912
Parameters
@@ -12,15 +15,23 @@ async def post_async_request(url: str, data: Any = None) -> List[Union[int, Any]
1215
url to make the post to
1316
data: Any
1417
object to post
18+
trace_parent: str
19+
traceparent header to send with the request
20+
trace_state: str
21+
tracestate header to send with the request
1522
1623
Returns
1724
-------
1825
[int, Any]
1926
Tuple with the Response status code and the data returned from the request
2027
"""
2128
async with aiohttp.ClientSession() as session:
22-
async with session.post(url,
23-
json=data) as response:
29+
headers = {}
30+
if trace_parent:
31+
headers["traceparent"] = trace_parent
32+
if trace_state:
33+
headers["tracestate"] = trace_state
34+
async with session.post(url, json=data, headers=headers) as response:
2435
# We disable aiohttp's input type validation
2536
# as the server may respond with alternative
2637
# data encodings. This is potentially unsafe.

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ furl==2.1.0
1111
pytest-asyncio==0.20.2
1212
autopep8
1313
types-python-dateutil
14+
opentelemetry-api

tests/models/test_DurableOrchestrationClient.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def delete(self, url: str):
6767
assert url == self._expected_url
6868
return self._response
6969

70-
async def post(self, url: str, data: Any = None):
70+
async def post(self, url: str, data: Any = None, trace_parent: str = None, trace_state: str = None):
7171
assert url == self._expected_url
7272
return self._response
7373

0 commit comments

Comments
 (0)