Dynatrace OneAgent SDK for Python
The Dynatrace OneAgent SDK for Python allows developers to extend Dynatrace's monitoring capabilities for custom components and frameworks. It integrates with the Dynatrace OneAgent to provide deep visibility into custom services, databases, and messaging systems, enabling end-to-end tracing and performance analysis. The current version is 1.5.2.20260107.153442. Releases are generally tied to new versions of the Dynatrace OneAgent SDK for C/C++, with bugfix releases in between.
Common errors
-
Application crashes or unexpected behavior on Python 3.12+
cause Older `oneagent-sdk` versions (prior to 1.5.1) did not have full compatibility with Python 3.12 and newer.fixUpgrade to `oneagent-sdk>=1.5.1` to ensure support for Python 3.12 and later: `pip install --upgrade oneagent-sdk`. -
ModuleNotFoundError: No module named 'pkg_resources' (when installing with setuptools>=81) or installation failures with uv>=0.9.16 on musl/Alpine.
cause Versions of `oneagent-sdk` prior to 1.5.2 had a hard dependency on `pkg_resources` which was removed in recent `setuptools` versions, or packaging issues affecting `uv` on specific platforms.fixUpgrade to `oneagent-sdk>=1.5.2` to resolve these installation and dependency issues: `pip install --upgrade oneagent-sdk`. -
Dynatrace OneAgent SDK is not active. Is OneAgent running?
cause The Python SDK could not connect to a running Dynatrace OneAgent. This can happen if the OneAgent is not installed, not running, or an incompatible version is present.fixVerify that the Dynatrace OneAgent is installed and running on the host. Ensure the OneAgent version meets the minimum requirements specified by your `oneagent-sdk` version (e.g., v1.251+ for SDK v1.5.x).
Warnings
- deprecated Dynatrace OneAgent SDK for Python versions 1.4 and older were officially deprecated as of November 2023 and are no longer supported since June 1, 2024. Running unsupported versions may lead to compatibility issues or lack of new features.
- breaking The OneAgent SDK requires a compatible Dynatrace OneAgent to be installed and running on the host. SDK versions are typically bundled with a specific C/C++ SDK version, which in turn dictates the minimum required OneAgent version. For example, SDK v1.5.x requires OneAgent v1.251+.
- gotcha Installing `oneagent-sdk` with newer versions of `uv` (0.9.16+) or `setuptools` (v81+) could fail on musl/Alpine platforms or due to a hard dependency on `pkg_resources`.
- gotcha Versions prior to 1.5.1 had issues with full compatibility for Python 3.12 and newer, potentially leading to crashes or unexpected behavior.
Install
-
pip install oneagent-sdk
Imports
- SDKConfig
from oneagent_sdk import SDKConfig
- OneAgentSDK
from oneagent_sdk import OneAgentSDK
- SDKState
from oneagent_sdk.sdk_state import SDKState
from oneagent_sdk import SDKState
Quickstart
import os
from oneagent_sdk import SDKConfig, OneAgentSDK, SDKState, ChannelType, DatabaseVendor, MessageBusVendor
def initialize_sdk():
"""Initializes the Dynatrace OneAgent SDK."""
# SDKConfig can be customized, e.g., to enable/disable specific features.
# For most cases, the default configuration is sufficient as it connects
# to the local OneAgent automatically.
config = SDKConfig()
sdk = OneAgentSDK(config)
if sdk.get_current_state() != SDKState.ACTIVE:
print("Dynatrace OneAgent SDK is not active. Is OneAgent running and configured?")
print(f"Current state: {sdk.get_current_state().name}")
# In a production environment, you might log this as a warning or error
# and ensure your application can run gracefully without SDK full functionality.
else:
print("Dynatrace OneAgent SDK is active.")
return sdk
def example_tracing_and_database_call(sdk: OneAgentSDK):
"""Demonstrates basic tracing for a custom service, database, and message bus."""
print("\n--- Starting example tracing ---")
# Trace a custom service method
with sdk.trace_custom_service("MyPythonApplication", "process_order"):
print("Processing an order...")
# Simulate a database call
db_info = sdk.create_database_info(
name="CustomerDB",
vendor=DatabaseVendor.POSTGRESQL,
channel_type=ChannelType.TCP_IP,
channel_endpoint="db.example.com:5432",
# You can also specify user, port, etc., but avoid sensitive data
)
with sdk.trace_database_request(db_info, "SELECT * FROM orders WHERE status = 'pending';"):
print("Executing database query...")
import time
time.sleep(0.1) # Simulate network/DB latency
print("Database operation complete.")
# Simulate a message bus interaction (e.g., receiving a message)
msg_bus_info = sdk.create_message_bus_info(
vendor=MessageBusVendor.RABBITMQ,
destination_name="order_queue",
# You might specify topic, broker, etc., depending on the vendor
)
with sdk.trace_message_bus_receive_activity(msg_bus_info, "order_msg_001"):
print("Receiving message from message bus...")
time.sleep(0.05) # Simulate message processing
print("Message bus interaction complete.")
print("Order processing finished. OneAgent should have captured this trace.")
print("--- Example tracing finished ---\n")
if __name__ == "__main__":
sdk_instance = initialize_sdk()
if sdk_instance.get_current_state() == SDKState.ACTIVE:
example_tracing_and_database_call(sdk_instance)
else:
print("SDK not active, skipping tracing examples. Please ensure Dynatrace OneAgent is running.")