By instrumenting your functions, you shall enable detailed trace capabilities using Thundra. This will provide great insights into the behavior of your Lambda functions on each invocation made. Those capabilities can be enabled through instrumenting your function either manually or in the automated way. Thundra’s Python agent integrates with the OpenTracing API and by manual instrumentation, you can manually configure the functions you would like to trace. In contrast to manual instrumentation, automated instrumentation will allow you to enable monitoring detailed traces with little effort. You can also change automated instrumentation configuration with Lambda environment variable without requiring a new deployment.
Thundra uses the OpenTracing API to implement instrumentation. Thus, you can manually instrument your code by following OpenTracing API instructions as Thundra’s agents are compliant with the OpenTracing API.
To manually instrument your Python functions in order to see detailed spans, you will have to initialize
span instances from the
tracer object. When a function is called, the tracer will start an active span. If another function is called inside this function, another span is started for the callee and added as a child span to caller’s existing span. This generates a span tree.
The code snippet below is from the example provided in the Thundra blog that can be found here. As can be seen, a
ThundraTracer has been procured and a span has been started from it with the method
.start_active_span(). The logic to be monitored is defined within the scope, and the span details are set through the parameters passed into the method
def save_user(self, name, surname, id): tracer = ThundraTracer.get_instance() with tracer.start_active_span(operation_name='save_user', finish_on_close=True) as scope: user = self.get_user(self, id); if user is not None: raise exception('User with ' + id + ' already exists!') index = randint(10,20) try: validated = self._validate_id(id) except Exception as e: raise e if validated is True: return self.user_repository.save_user(index, name, surname,id)
Thundra’s Python agent trace plug-in can allow you to perform automated instrumentation of your Lambda code with a simple configuration using
Tracable from Thundra’s trace plugin.
The code snippet below is the same as the one presented in manual instrumentation, but depicting how to perform automated instrumentation.
from sklearn import neighbors from thundra.plugins.trace.traceable import Traceable (trace_error=True) def trainModel(): knn = neighbors.KNeighborsClassifier() return knn.fit()
As can be seen from the example, automated instrumentation simply involves adding the line
@Traceable(trace_args=True, trace_return_value=True) above the Python functions you wish to monitor via trace and spans. Also unlike manual instrumentation, here the span names are derived from the names of the functions over which instrumentation is done.
The parameters that are passed to
@Tracable are as below:
You can find more information on how to use these parameters in Trace Support.