autodynatrace: Auto Instrumentation for OneAgent SDK
autodynatrace is a Python library that provides automatic instrumentation for applications using the Dynatrace OneAgent SDK. It enables monitoring and tracing for various popular frameworks and libraries like Flask, Django, FastAPI, SQLAlchemy, and aiohttp, typically requiring only a single import at the start of an application. The current version is 2.1.1, with releases occurring as new framework versions or features are supported, often focusing on stability and broader compatibility.
Common errors
-
ModuleNotFoundError: No module named 'oneagent'
cause The Dynatrace OneAgent SDK for Python (`oneagent-sdk` PyPI package), which autodynatrace depends on at runtime, is not installed in the environment.fixInstall the SDK using pip: `pip install oneagent-sdk`. -
My Flask/Django/FastAPI application is running, but no traces or metrics are appearing in Dynatrace.
cause This typically happens if `autodynatrace` was not imported as the very first statement in your application's entry point, or if the OneAgent itself isn't properly configured/running.fixEnsure `import autodynatrace` is the absolute first line of code in your main application file. Also, verify that the Dynatrace OneAgent is installed and configured correctly for your environment (e.g., by ensuring the `DT_ONEAGENT_PYTHON_SDK_PATH` environment variable is set or by running your application within a OneAgent-monitored process). -
Asynchronous code (e.g., in FastAPI, aiohttp) is not being traced by Dynatrace when using autodynatrace.
cause For `autodynatrace` versions 2.1.0 and higher, asyncio instrumentation requires an explicit environment variable to be set.fixSet the environment variable `AUTODYNATRACE_INSTRUMENT_CONCURRENT=True` before starting your application. For example: `export AUTODYNATRACE_INSTRUMENT_CONCURRENT=True`.
Warnings
- gotcha Autodynatrace relies on the Dynatrace OneAgent SDK for Python (`oneagent-sdk` PyPI package) being installed and properly configured in the environment. Without `oneagent-sdk` present, instrumentation will fail with a `ModuleNotFoundError` or silently not capture data.
- gotcha For complete and effective auto-instrumentation, `import autodynatrace` MUST be the very first import statement in your application's main entry point file. Importing it later may result in incomplete or failed instrumentation of certain frameworks or libraries.
- breaking Version 2.1.0 introduced initial support for asyncio. For asyncio applications (e.g., FastAPI, aiohttp), you *must* explicitly set the environment variable `AUTODYNATRACE_INSTRUMENT_CONCURRENT=True` for instrumentation to be active. Asyncio code will not be traced without this setting.
- gotcha Autodynatrace supports a specific set of frameworks and database drivers (e.g., Flask, Django, FastAPI, SQLAlchemy, Psycopg2). If your application uses an unsupported framework or an unsupported version, instrumentation may not work or may be incomplete.
Install
-
pip install autodynatrace -
pip install autodynatrace oneagent-sdk
Imports
- autodynatrace
import autodynatrace
Quickstart
# app.py
import os
# Must be the first import to ensure all frameworks are patched correctly
import autodynatrace
from flask import Flask
# For asyncio applications (from v2.1.0+), uncomment the line below:
# os.environ['AUTODYNATRACE_INSTRUMENT_CONCURRENT'] = 'True'
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Dynatrace Instrumented World!'
if __name__ == '__main__':
# To run:
# 1. pip install autodynatrace flask oneagent-sdk
# 2. python app.py
# 3. Access http://localhost:5000
# 4. Check Dynatrace monitoring for traces in your Dynatrace tenant
# (Requires OneAgent and SDK configuration)
app.run(debug=True, port=5000)