Ruby Features
What is Auto Instruments?
Auto Instruments automatically breaks down time spent in your custom Ruby on Rails controller code without requiring manual instrumentation. It instruments Ruby’s Abstract Syntax Tree (AST) as code loads, making method calls visible in traces.
Without Auto Instruments, custom code time appears as “Controller”. With it enabled, you see the specific method calls consuming time.
Auto Instruments requires Ruby 2.3.1+ and is available in scout_apm version 2.6.0+.
How do I enable Auto Instruments?
- Update the scout_apm gem:
bundle update scout_apm - Set the configuration option:
- In config file:
auto_instruments: true - Via environment variable:
SCOUT_AUTO_INSTRUMENTS=true
- In config file:
- Deploy
How does Deploy Tracking work?
Scout tracks deploys to correlate code changes with performance. It detects deploys via:
- Capistrano: Reads the
REVISIONfile automatically - Heroku: Enable Dyno Metadata to set
HEROKU_SLUG_COMMIT - Custom deployment: Set
SCOUT_REVISION_SHAenvironment variable - Git repo: Parses
git rev-parse --short HEAD
What is Custom Context?
Custom Context lets you add business metadata to traces for filtering in Trace Explorer. Examples include user IDs, account types, or spending tiers.
Add user context:
ScoutApm::Context.add_user(id: @user.id)Add general context:
ScoutApm::Context.add(monthly_spend: @account.monthly_spend)Context values can be numeric, string, boolean, time, or date. Field names must use alphanumeric characters, dashes, and underscores (no spaces).
How do I add custom instrumentation?
For method calls, include the tracer and use instrument_method:
class User
include ScoutApm::Tracer
def export_activity
# Do work
end
instrument_method :export_activity
endFor code blocks:
self.class.instrument("User", "generate_profile_pic") do
# Do work
endHow do I rename a transaction?
Use ScoutApm::Transaction.rename for cases where the default naming isn’t descriptive enough:
ScoutApm::Transaction.rename("posts/json_format")For GraphQL endpoints, rename based on the operation:
ScoutApm::Transaction.rename("GraphQL/" + operation_name)Avoid high-cardinality names (like including user IDs) as Scout limits the number of tracked transactions.
How do I ignore a transaction?
In your code:
ScoutApm::Transaction.ignore!Or use probability sampling:
before_action :sample_requests_for_scout
def sample_requests_for_scout
sample_rate = 0.9 # Capture 90% of requests
ScoutApm::Transaction.ignore! if rand > sample_rate
end