Skip to content

Commit f7cf950

Browse files
yojagadHazhzeng
authored andcommitted
Accept TraceContext from host (#554)
* Set correlation id in context * Modified context being passed from host. * Updated parameters * Testing build. (This will be reverted). * Unit test failure. * code quality unit test failure. * Code quality unit test. * Indentation fix * Updated DOTNET_VERSION and runtime version. * Modified test and addressed code review comments * Updated test * Testing * Modified test * removed space * New line at end of file
1 parent e507a98 commit f7cf950

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

azure_functions_worker/bindings/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .tracecontext import TraceContext
12
from .context import Context
23
from .meta import check_input_type_annotation
34
from .meta import check_output_type_annotation
@@ -10,5 +11,5 @@
1011
'Out', 'Context',
1112
'is_trigger_binding',
1213
'check_input_type_annotation', 'check_output_type_annotation',
13-
'from_incoming_proto', 'to_outgoing_proto',
14+
'from_incoming_proto', 'to_outgoing_proto', 'TraceContext',
1415
)

azure_functions_worker/bindings/context.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from . import TraceContext
2+
3+
14
class Context:
25

36
def __init__(self, func_name: str, func_dir: str,
4-
invocation_id: str) -> None:
7+
invocation_id: str, trace_context: TraceContext) -> None:
58
self.__func_name = func_name
69
self.__func_dir = func_dir
710
self.__invocation_id = invocation_id
11+
self.__trace_context = trace_context
812

913
@property
1014
def invocation_id(self) -> str:
@@ -17,3 +21,7 @@ def function_name(self) -> str:
1721
@property
1822
def function_directory(self) -> str:
1923
return self.__func_dir
24+
25+
@property
26+
def trace_context(self) -> TraceContext:
27+
return self.__trace_context
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class TraceContext:
2+
3+
def __init__(self, trace_parent: str,
4+
trace_state: str, attributes: dict) -> None:
5+
self.__trace_parent = trace_parent
6+
self.__trace_state = trace_state
7+
self.__attributes = attributes
8+
9+
@property
10+
def Tracestate(self) -> str:
11+
return self.__trace_state
12+
13+
@property
14+
def Traceparent(self) -> str:
15+
return self.__trace_parent
16+
17+
@property
18+
def Attributes(self) -> str:
19+
return self.__attributes

azure_functions_worker/dispatcher.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ async def _handle__invocation_request(self, req):
269269

270270
invocation_id = invoc_request.invocation_id
271271
function_id = invoc_request.function_id
272-
272+
trace_context = bindings.TraceContext(
273+
invoc_request.trace_context.trace_parent,
274+
invoc_request.trace_context.trace_state,
275+
invoc_request.trace_context.attributes)
273276
# Set the current `invocation_id` to the current task so
274277
# that our logging handler can find it.
275278
current_task = asyncio.Task.current_task(self._loop)
@@ -298,7 +301,7 @@ async def _handle__invocation_request(self, req):
298301

299302
if fi.requires_context:
300303
args['context'] = bindings.Context(
301-
fi.name, fi.directory, invocation_id)
304+
fi.name, fi.directory, invocation_id, trace_context)
302305

303306
if fi.output_types:
304307
for name in fi.output_types:

tests/unittests/http_functions/return_context/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ def main(req: azure.functions.HttpRequest, context: azure.functions.Context):
99
'ctx_func_name': context.function_name,
1010
'ctx_func_dir': context.function_directory,
1111
'ctx_invocation_id': context.invocation_id,
12+
'ctx_trace_context_Traceparent': context.trace_context.Traceparent,
13+
'ctx_trace_context_Tracestate': context.trace_context.Tracestate,
1214
})

tests/unittests/test_http_functions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def test_return_context(self):
104104
self.assertEqual(data['ctx_func_name'], 'return_context')
105105
self.assertIn('return_context', data['ctx_func_dir'])
106106
self.assertIn('ctx_invocation_id', data)
107+
self.assertIn('ctx_trace_context_Tracestate', data)
108+
self.assertIn('ctx_trace_context_Traceparent', data)
107109

108110
def test_remapped_context(self):
109111
r = self.webhost.request('GET', 'remapped_context')

0 commit comments

Comments
 (0)