Skip to content

Accept TraceContext from host #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion azure_functions_worker/bindings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .tracecontext import TraceContext
from .context import Context
from .meta import check_input_type_annotation
from .meta import check_output_type_annotation
Expand All @@ -10,5 +11,5 @@
'Out', 'Context',
'is_trigger_binding',
'check_input_type_annotation', 'check_output_type_annotation',
'from_incoming_proto', 'to_outgoing_proto',
'from_incoming_proto', 'to_outgoing_proto', 'TraceContext',
)
10 changes: 9 additions & 1 deletion azure_functions_worker/bindings/context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from . import TraceContext


class Context:

def __init__(self, func_name: str, func_dir: str,
invocation_id: str) -> None:
invocation_id: str, trace_context: TraceContext) -> None:
self.__func_name = func_name
self.__func_dir = func_dir
self.__invocation_id = invocation_id
self.__trace_context = trace_context

@property
def invocation_id(self) -> str:
Expand All @@ -17,3 +21,7 @@ def function_name(self) -> str:
@property
def function_directory(self) -> str:
return self.__func_dir

@property
def trace_context(self) -> TraceContext:
return self.__trace_context
19 changes: 19 additions & 0 deletions azure_functions_worker/bindings/tracecontext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class TraceContext:

def __init__(self, trace_parent: str,
trace_state: str, attributes: dict) -> None:
self.__trace_parent = trace_parent
self.__trace_state = trace_state
self.__attributes = attributes

@property
def Tracestate(self) -> str:
return self.__trace_state

@property
def Traceparent(self) -> str:
return self.__trace_parent

@property
def Attributes(self) -> str:
return self.__attributes
7 changes: 5 additions & 2 deletions azure_functions_worker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ async def _handle__invocation_request(self, req):

invocation_id = invoc_request.invocation_id
function_id = invoc_request.function_id

trace_context = bindings.TraceContext(
invoc_request.trace_context.trace_parent,
invoc_request.trace_context.trace_state,
invoc_request.trace_context.attributes)
# Set the current `invocation_id` to the current task so
# that our logging handler can find it.
current_task = asyncio.Task.current_task(self._loop)
Expand Down Expand Up @@ -298,7 +301,7 @@ async def _handle__invocation_request(self, req):

if fi.requires_context:
args['context'] = bindings.Context(
fi.name, fi.directory, invocation_id)
fi.name, fi.directory, invocation_id, trace_context)

if fi.output_types:
for name in fi.output_types:
Expand Down
2 changes: 2 additions & 0 deletions tests/unittests/http_functions/return_context/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ def main(req: azure.functions.HttpRequest, context: azure.functions.Context):
'ctx_func_name': context.function_name,
'ctx_func_dir': context.function_directory,
'ctx_invocation_id': context.invocation_id,
'ctx_trace_context_Traceparent': context.trace_context.Traceparent,
'ctx_trace_context_Tracestate': context.trace_context.Tracestate,
})
2 changes: 2 additions & 0 deletions tests/unittests/test_http_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def test_return_context(self):
self.assertEqual(data['ctx_func_name'], 'return_context')
self.assertIn('return_context', data['ctx_func_dir'])
self.assertIn('ctx_invocation_id', data)
self.assertIn('ctx_trace_context_Tracestate', data)
self.assertIn('ctx_trace_context_Traceparent', data)

def test_remapped_context(self):
r = self.webhost.request('GET', 'remapped_context')
Expand Down