Enrich Tracing

Manual Instrumentation with Open Tracing

Configuring Thundra’s Trace Support in the Python agent involves declaring a trace object at the start of your Lambda function. It must be noted that Trace Support is by default enabled when you wrap your function. However, if you would like to configure your Trace Support, along with performing manual instrumentation, then you must get a tracer instance with the command ThundraTracer.getInstance()

Tracing Code Blocks Using Context Manager

To trace a code block you can use the context manager returned calling

tracer.start_active_span(operation_name="write_user_to_db")

where operation_name indicates the name of the span relevant to the traced block of code.

from thundra.opentracing.tracer import ThundraTracer
def save_user(name, surname, id):
# Get tracer instance
tracer = ThundraTracer.get_instance()
# Trace code block with context manager
with tracer.start_active_span(operation_name="write_user_to_db") as scope:
# Perform operations to be traced
# ...

If you already use opentracing to instrument your code, you may overwrite the opentracing.tracer with a thundra tracer instance:

# Get tracer instance
tracer = ThundraTracer.get_instance()
# Set the tracer
opentracing.tracer = tracer

An example usage

from thundra.opentracing.tracer import ThundraTracer
def save_user(name, surname, id):
# Get tracer instance
tracer = ThundraTracer.get_instance()
opentracing.tracer = tracer
span = tracer.start_span(operation_name="operation_name")
span.set_tag('key', 'value')
# Do some operations ...
# ...
span.finish()

Tracing Methods Using @Traceable Decorator

To trace a method using automated instrumentation, import Traceable from Thundra’s trace plugin as below:

from thundra.plugins.trace.traceable import Traceable

Then you can use @Traceable decorator to trace your functions as below:

@Traceable(trace_args=True, trace_return_value=True, trace_error=True, trace_err)
def method_to_be_traced():
""" Traced method """
# ...

Configuring Line-by-Line Tracing

Line-by-Line tracing is supported by Thundra's Python agent and it allows you to monitor your Lambda functions literally by each line. By default, line by line tracing behavior is disabled but can be enabled by passing arguments to @Traceable decorator.

Line-by-Line tracing can be enabled by setting trace_line_by_line to True.

@Traceable(trace_line_by_line=True)
def method_to_be_traced():
""" Traced method """
# ...

Local variables in the method can be traced by setting trace_local_variables to True when line-by-line tracing is enabled.

@Traceable(trace_line_by_line=True, trace_local_variables=True)
def method_to_be_traced():
""" Traced method """
# ...

Source code in line can be traced by setting trace_lines_with_source toTrue when line-by-line tracing is enabled.

@Traceable(trace_line_by_line=True, trace_lines_with_source=True)
def method_to_be_traced():
""" Traced method """
# ...

Configuring Trace Arguments

Configure trace arguments to keep track of the arguments within the scope of your span.

Trace arguments are disabled by default. However, they can easily be enabled by adding the trace_args = True in the @Traceable decorator.

Configuring Trace Return Values

Configure trace return values to monitor all values within the scope of your span.

Trace return values are disabled by default. However, they can easily be enabled by adding the trace_return_value = True in the @Traceable decorator.

Configuring Trace Errors

Configure trace errors to monitor any errors, if any, that is created under the scope of your span.

Trace errors are disabled by default. However, they can easily be enabled by adding the trace_error = True in the @Traceable decorator.