This page describes how to use the Python Serverless Tracer API to modify the behavior of the tracer within your AWS Lambda function.
Create Exit Calls
Exit calls are automatically instrumented for HTTP, Amazon DynamoDB, and inter-AWS Lambda calls. All other protocols need to manually create an exit call.
To create an exit call, use the ExitCallContextManager
method in a with
statement to wrap the exit call. The method takes the following required parameters:
The ExitCallContextManager
method automatically reports any runtime exceptions that occur within the method's scope as exit call errors.
The code sample below demonstrates how to use the exit call method for a MySQL database:
import appdynamics # Add AppDynamics libraries. Must be the first line of code
@appdynamics.tracer
def handler(event, context):
with appdynamics.ExitCallContextManager(exit_point_type="DB", exit_point_sub_type="DB", identifying_properties={"VENDOR": "MYSQL"}) as ec:
data = fetch_from_db()
PY
Exit Call Types and Identifying Properties
In the Controller, each exit call has a distinct type, determined by a set of identifying properties. Include at least one identifying property for each exit call type.
The exitCall
types, subtypes, and identifying properties are listed below. For exit call subtypes, you can use the examples listed in the table or use an empty string.
Exit Call Type | Exit Call Subtype | Identifying Properties |
---|
HTTP | HTTP | HOST
PORT
URL
QUERY STRING
|
DB | DB
| URL
HOST
PORT
DATABASE
VERSION
VENDOR
|
CACHE | CACHE
| VENDOR
SERVER POOL
|
CUSTOM | Cassandra CQL
Couchbase
Amazon Web Services
Mongo DB
| Any user-defined set of properties |
Create Exit Call Error Reports
The ExitCallContextManager
method automatically reports errors. If you want to add custom error reports, use the report_exit_call_error
method. The method takes the following parameters:
error_name:
Name of the errorerror_message
(Optional): Descriptive error messagehttp_status_code
(Optional): The HTTP status code if available.
The code sample below demonstrates how you can use the exit call error report method:
import appdynamics
@appdynamics.tracer
def handler(event, context):
#Create an exit call
with appdynamics.ExitCallContextManager("DB", "DB", {"HOST": "ec2-12-123-123-12.us-west-2.compute.amazonaws.com", "PORT": "3306", "DATABASE": "movies", "VENDOR": "MYSQL"}) as ec:
movies = fetch_movies_from_mysql_db()
if movies == None:
#Create an exit call error report
ec.report_exit_call_error(error_name="DBError", error_message="Item not found") #ec is the object returned by ExitCallContextManager above
PY
Create Transaction Error Reports
By default, the Python Tracer automatically detects transaction error reports. You can customize these error reports using the appdynamics.report_error(error_name: str, error_message: str)
method to report transaction errors. The method takes the following parameters:
The code sample below demonstrates how you can use the transaction error report method:
import appdynamics # Add AppDynamics libraries. Must be the first line of code
import requests #Add request library
@appdynamics.tracer # Must come before the handler function
def my_handler(event, context):
try:
r = requests.get('https://api.github.com/events')
except Exception as e:
appdynamics.report_error(error_name=type(e).__name__, error_message=str(e)) # Reports a transaction error
PY