Custom Context
What is Custom Context?
Custom Context adds business metadata to traces, enabling filtering in Trace Explorer. It helps answer questions like: - How many users are impacted by slow requests? - How are slow requests affecting high-paying customers? - What’s the performance for trial vs. paid customers?
How do I add context in Ruby?
User context:
ScoutApm::Context.add_user(id: @user.id)General context:
ScoutApm::Context.add(monthly_spend: @account.monthly_spend)
ScoutApm::Context.add(plan_type: "enterprise")How do I add context in Python?
import scout_apm.api
scout_apm.api.Context.add("user_id", request.user.id)
scout_apm.api.Context.add("plan_type", "premium")What types of values can context contain?
- Strings
- Numbers
- Booleans
- Time/Date (Ruby)
- Any JSON-serializable type (Python)
What are the naming restrictions?
Context keys must: - Use alphanumeric characters, dashes, and underscores - NOT contain spaces
Invalid context is ignored without error.
What context does Scout collect by default?
- Request URI
- Client IP address (can be disabled via
collect_remote_ip: false)
Should I include personally identifiable information (PII)?
We recommend using IDs instead of explicit names or emails. For example, use user_id: 12345 rather than user_email: "user@example.com".
How do I use context in Trace Explorer?
Context fields appear as filterable dimensions in Trace Explorer. You can: - Filter traces by any context field - See distribution of traces across context values - Correlate performance issues with specific user segments