Scout APM Python Agent

3.5.3 · active · verified Thu Apr 16

Scout APM is an Application Performance Monitoring (APM) agent for Python that provides detailed performance metrics and transaction traces for web applications (Django, Flask, FastAPI, etc.) and background jobs (Celery, RQ). It helps identify performance bottlenecks, monitor errors, manage logs, and track external services. The current stable version is 3.5.3, with regular updates to support new Python versions and frameworks.

Common errors

Warnings

Install

Imports

Quickstart

The quickstart demonstrates global configuration via `scout_apm.api.Config.set()` and agent installation via `scout_apm.api.install()`. It also includes an example of custom instrumentation using `WebTransaction` and `instrument` for non-framework specific code. In production, `SCOUT_KEY`, `SCOUT_NAME`, and `SCOUT_MONITOR` are commonly set via environment variables.

import os
from scout_apm.api import Config, install

# Configure Scout APM (often done via environment variables in production)
# For local testing, ensure these are set in your environment or replace os.environ.get with actual values.
Config.set(key=os.environ.get('SCOUT_KEY', 'YOUR_SCOUT_KEY'),
           name=os.environ.get('SCOUT_NAME', 'My Python App'),
           monitor=os.environ.get('SCOUT_MONITOR', 'True').lower() == 'true')

# Install the agent (should be called early in your application startup)
install()

# Example of custom instrumentation (e.g., in a non-web script or job)
from scout_apm.api import WebTransaction, instrument

def my_complex_function():
    with instrument('MyCustomOperation'):
        # Simulate some work
        sum(range(1000000))
    return 'Operation complete'

if __name__ == '__main__':
    # In a web framework, transactions are typically auto-instrumented.
    # For custom scripts or background jobs:
    with WebTransaction('MyScriptExecution'):
        print(my_complex_function())

view raw JSON →