Ruby Features

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?

  1. Update the scout_apm gem: bundle update scout_apm
  2. Set the configuration option:
  1. Deploy

How does Deploy Tracking work?

Scout tracks deploys to correlate code changes with performance. It detects deploys via:

  1. Capistrano : Reads the REVISION file automatically
  2. Heroku : Enable Dyno Metadata to set HEROKU_SLUG_COMMIT
  3. Custom deployment : Set SCOUT_REVISION_SHA environment variable
  4. 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:

classUserincludeScoutApm::Tracerdef export_activity# Do workend  instrument_method :export_activityend

For code blocks:

self.class.instrument("User", "generate_profile_pic") do# Do workend

How 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_scoutdef sample_requests_for_scout  sample_rate =0.9# Capture 90% of requestsScoutApm::Transaction.ignore!ifrand> sample_rateend