Python Features
Python Features
How does Deploy Tracking work in Python?
Scout tracks deploys to correlate code changes with performance. Set the revision SHA via:
- Config API :
import osfrom scout_apm.api import ConfigConfig.set(revision_sha=os.popen("git rev-parse HEAD").read().strip())
- Environment variable:
SCOUT_REVISION_SHA=your_sha - Heroku: Enable Dyno Metadata to auto-set
HEROKU_SLUG_COMMIT
How do I add Custom Context?
Add business metadata to traces for filtering:
import scout_apm.apiscout_apm.api.Context.add("user_id", request.user.id)scout_apm.api.Context.add("plan_type", "premium")
Keys must be alphanumeric with dashes/underscores (no spaces). Values can be any JSON-serializable type.
How do I rename a transaction?
Use rename_transaction() for custom naming:
import scout_apm.apiscout_apm.api.rename_transaction("Controller/custom_name")
For GraphQL, rename based on operation:
scout_apm.api.rename_transaction("Controller/"+ derive_graphql_name())
Avoid high-cardinality names (like including user IDs).
How do I add custom instrumentation?
Scout provides two types of instrumentation:
Transactions - wrap flows of work (web requests, cron jobs):
with scout_apm.api.WebTransaction("Foo"):# work here# Or for background tasks:with scout_apm.api.BackgroundTransaction("Bar"):# work here
Timing - measure individual operations:
with scout_apm.api.instrument("Computation") as inst: inst.tag("record_count", 100)# work here# Or as a decorator:@scout_apm.api.instrument("Computation")def my_function():# work here
How do I instrument async code?
Use the async_ decorator for async functions:
@scout_apm.api.WebTransaction.async_("Foo")asyncdef foo():# async work@scout_apm.api.instrument.async_("Bar")asyncdef bar():# async work
How do I ignore a transaction?
Call ignore_transaction() within the request:
import scout_apm.apiif is_health_check(): scout_apm.api.ignore_transaction()
For probability sampling:
import randomif random.uniform(0, 1) >0.9: scout_apm.api.ignore_transaction()
Does Scout support ARM/Graviton?
Yes. The Python agent automatically detects ARM support. If needed, explicitly set the core_agent_triple configuration (e.g., x86_64-unknown-linux-musl for MUSL-based Linux).