Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit af8a7c1

Browse files
authored
Merge pull request #37 from signalfx/add-trace-propagation-helper
Added tracing.inject helper function to help propagate tracing context
2 parents f366b3c + 8a98c11 commit af8a7c1

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

README.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,31 @@ Step 6: Add tracing to the Lambda function
199199
span.set_tag("example_tag", "example_value")
200200
201201
To review more examples and usage details, see [Jaeger Python Tracer](https://github.com/signalfx/jaeger-client-python>).
202-
202+
203+
Propagating trace context to outgoing requests or lambda response
204+
------------------------------------------------------------------
205+
206+
The library ships a helper function to inject tracing context headers into a dictionary like object.
207+
The function accepts two arguments. First argument must be a dictionary like object that the trace context is injected into.
208+
The second argument is optional and must be a OpenTracing span context. If one is not provided, the function uses the currently
209+
active span. Example:
210+
211+
.. code:: python
212+
213+
import signalfx_lambda
214+
215+
@signalfx_lambda.is_traced()
216+
def handler(event, context):
217+
headers = {}
218+
219+
# inject trace context into the headers dictionary
220+
signalfx_lambda.tracing.inject(headers)
221+
222+
request = urllib.request.Request('http://some-service', headers=headers)
223+
response = urllib.request.urlopen(request)
224+
225+
# your code
226+
203227
204228
Additional information
205229
------------------------

signalfx_lambda/tracing.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,25 @@ def init_jaeger_tracer(context):
6161
return tracer
6262

6363

64+
def inject(carrier, ctx=None):
65+
if not _tracer:
66+
raise RuntimeError((
67+
'tracing has not been initialized. Use signalfx_lambda.is_traced'
68+
' decorator to initialize tracing'))
69+
if not ctx:
70+
scope = _tracer.scope_manager.active
71+
if scope and scope.span:
72+
ctx = scope.span.context
73+
74+
if ctx:
75+
_tracer.inject(ctx, opentracing.Format.HTTP_HEADERS, carrier)
76+
77+
6478
class create_span(object):
6579
def __init__(self, event, context, auto_add_tags=True, operation_name=None):
6680
if not _tracer:
6781
raise RuntimeError((
68-
'tracing has not been initialized. Use signalfx_lambda.is_tracer'
82+
'tracing has not been initialized. Use signalfx_lambda.is_traced'
6983
' decorator to initialize tracing'))
7084
self.event = event
7185
self.context = context

0 commit comments

Comments
 (0)