The Python Agent APIs:
- Let you programmatically define custom business transactions that would not automatically be detected by the agent.
- Let you create custom exit calls to discover and monitor backends and that are not automatically detected by the agent.
If your instrumented application starts up and you do not see the all business transactions or backends that you expect to see, first check Python Supported Environments to see if your framework or exit point is supported. If not, you can use the APIs to instrument your code manually.
Custom Business Transactions
You can use the start_bt() and end_bt() methods to surround the code that you want to monitor as a custom business transaction.
Or you can use the "bt" context manager. Consider using the "bt" context manager where you start and end the business transaction in the same code; i.e., where you can wrap the whole business transaction in a with statement.
For example, given the code:
you want to report do_work() as a business transaction.
Use start_bt() and end_bt()
This example uses start_bt() and end_bt() to create a business transaction named 'do_work'.
Use "bt" context manager
If the business transaction starts and ends in the same context, you can use the "bt" context manager instead. This is simpler:
Custom Exit Calls
You can use the start_exit_call() and end_exit_call() methods to create a custom exit call from a specific business transaction to a backend that the Python Agent does not automatically detect.
The business transaction must be a custom business transaction.
If you want to make a custom exit call from a business transaction that is normally automatically detected, you can exclude that business transaction to prevent it from being automatically detected and then create it as a custom business transaction. This enables you to get the BtHandle that you need to create the custom exit call. See Configure Python Web Custom Match and Exclude Rules for information on excluding a business transaction.
Given the code:
you want to send the query via an exit call to a proprietary database.
You want the database to be labeled "Financials Database" in the controller UI.
You want the backend properties that appear in the backend dashboard to appear as:
The following examples assume you are wrapping the exit call in a custom business transaction named 'department rollup', created in another part of your code.
Use start_exit_call() and end_exit_call()
This example uses start_exit_call() and end_exit_call().
Use "exit_call" context manager
If the business transaction starts and ends in the same context, you can use the simpler "exit_call" context manager instead.
The next example starts a custom exit call to a Cassandra backend from a business transaction that was auto-detected by the Python Agent's default Flask instrumentation. It uses Flask's import feature to get the request object which it passes to appd_get_active_bt_handle().
Other supported frameworks have different mechanisms for getting the request object.