With the growing trend of organizations migrating applications to incorporate serverless technologies, it’s more important than ever to have a full understanding of how various application components communicate with one another. Recently, AppDynamics received the AWS Lambda Ready designation (to go along with our other 3 AWS designations) validating our low-effort integration approach that provides usage-based, deep visibility into AWS Lambda.
Our integration with AWS Lambda is readily available in the AWS Marketplace for Java and Node.js, and today we will learn how we can monitor Lambda functions written in Python with our Python Tracer SDK.
Why an SDK? I thought AppDynamics used agents!
Traditional application deployments are written to maximize uptime on servers while running highly optimized code. AWS Lambda functions, on the other hand, only live as long as they are needed. The positive consequence is that this reduces costs normally associated with idle infrastructure. Conversely, the ephemeral nature of AWS Lambda functions makes it nigh-impossible to use a traditional agent-based approach that reports data on an interval basis.
Enter: the SDK. By leveraging an SDK-based approach, we remove the dependency on a specified period of time before performance data is sent to the AppDynamics controller.
But first, prerequisites
First time instrumenting a Lambda function with AppDynamics? There are a few prerequisites which can be found here along with information on how to subscribe to Serverless APM. Once you are subscribed to Serverless APM, we need to add a few environment variables on the Lambda function. The full list can be found in the AppDynamics documentation, but below are the environment variables that are required:
In addition to the environment variables, the following are also required:
- Python Lambda function must be using Python version 3.6 or greater.
- Python Package Installer (pip) must be installed and match the version of the Python Lambda function.
In our sample Python Lambda functions (which can be found here), we are using the Serverless framework to manage the deployment and environment variables. Your deployment mechanisms may and probably will vary.
Installing the tracer
The AppDynamics Python Lambda tracer resides in PyPI, and we can leverage pip to install the tracer. The tracer needs to be installed to the root directory of the Lambda function. To do this, we use the pip command as follows:
pip install appdynamics-lambda-tracer
In the sample code, we are using virtualenv to create our virtual development environment and install our dependencies. More information about this approach can be found in this blog from Serverless.com.
Instrument the function
After installing the tracer, we need to add the appropriate code to the Lambda function. In the first line of the function, we add an import statement for the appdynamics library before we import any other libraries.
After importing the library, we need to decorate any functions that are acting as Lambda handlers with the appdynamics.tracer decorator. By using the decorator design pattern, AppDynamics is able to monitor the performance of the Lambda function without making permanent modifications to it.
Adding exit calls
Need to show outbound HTTP calls? No problem – the Python Lambda tracer automatically discovers outbound exit calls to HTTP endpoints. Calls to another Lambda? We’ve got that covered. DynamoDB? Check!
Out of the box, the AppDynamics Python Lambda tracer automatically discovers outbound calls to HTTP endpoints, Amazon DynamoDB, and other Lambda functions. If you need to show an outbound call to another service (such as AWS Aurora), you can use the ExitCallContextManager in the context of a with statement to wrap the call out to the service. The method takes the following parameters (all required):
- exit_point_type: Exit call type.
- exit_point_sub_type: Exit call sub-type.
- identifying_properties: Python dictionary of properties defining the exit call.
A full list of the exit call types / subtypes / identifying properties can be found in the documentation.
Reporting errors
Out of the box, the AppDynamics Python Lambda tracer automatically detects and reports errors that occur during execution of a Lambda function. But there are times when something may be an error even though the function successfully executes. In cases like this, we can use the appdynamics.report_error function to report errors in a transaction.
In addition to overall errors, the AppDynamics Python Lambda tracer automatically detects and reports errors that occur during exit calls. If you have a custom situation where the result of an outbound call should be considered an error, you can use the report_exit_call_error method.
Integrate with end user monitoring
Taking advantage of AppDynamics End User Monitoring? We’ve got you covered! AppDynamics Serverless APM tracers are purpose-built to integrate with existing End User Monitoring applications, providing true end-to-end visibility on performance from your customer’s devices all the way through to your serverless functions. There are two steps to set this up:
- Enable the integration by setting the APPDYNAMICS_ENABLE_EUM environment variable on the Lambda function to true.
- Configure response header mappings. Info on setting up response header mappings can be found in the product documentation.
And… that’s a wrap!
Instrumenting your first Python Lambda function using AppDynamics is super simple! All of the sample Python code referenced can be found in Github (along with sample code for Java and NodeJS), and full documentation on the Python Lambda tracer is available here. If you have questions or feedback, you can find me on Twitter at @thewaynebrown. You can also connect with me on LinkedIn. If you’re interested in taking AppDynamics for a spin, sign up for a free trial here. You can also learn more about AppDynamics Serverless APM in the AWS Marketplace.