Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ The sampling is done per-thread using a round-robin counter, ensuring even distr
This is useful for high-traffic applications where you want to reduce the volume of
APM data while still maintaining representative performance insights.

### Test Environment

**SolidAPM is automatically disabled in the test
environment** to prevent test pollution and improve test performance.

You can disable SolidAPM in other environments if needed:

```ruby
# config/environments/staging.rb
SolidApm.enabled = false
```

### Transaction Name Filtering

Filter specific transactions by name using exact string matches or regular expressions:
Expand Down
2 changes: 2 additions & 0 deletions lib/solid_apm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'apexcharts'

require 'solid_apm/version'
require 'solid_apm/railtie'
require 'solid_apm/engine'
require 'solid_apm/sampler'
require 'solid_apm/cleanup_service'
Expand All @@ -13,6 +14,7 @@ module SolidApm
mattr_accessor :mcp_server_config, default: {}
mattr_accessor :silence_active_record_logger, default: true
mattr_accessor :transaction_sampling, default: 1
mattr_accessor :enabled, default: true
mattr_accessor(
:transaction_filters, default: [
/^SolidApm::/,
Expand Down
6 changes: 4 additions & 2 deletions lib/solid_apm/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module SolidApm
class Engine < ::Rails::Engine
isolate_namespace SolidApm

config.app_middleware.use Middleware
initializer 'solid_apm.middleware', before: :build_middleware_stack do |app|
app.middleware.use SolidApm::Middleware if SolidApm.enabled
end

initializer 'solid_apm.assets' do |app|
# Add engine's assets to the load path for both Propshaft and Sprockets
Expand Down Expand Up @@ -49,7 +51,7 @@ class Engine < ::Rails::Engine
end

config.after_initialize do
SpanSubscriber::Base.subscribe!
SpanSubscriber::Base.subscribe! if SolidApm.enabled
end
end
end
8 changes: 5 additions & 3 deletions lib/solid_apm/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ def initialize(app)
end

def call(env)
return @app.call(env) unless SolidApm.enabled

self.class.init_transaction
status, headers, body = @app.call(env)

env['rack.after_reply'] ||= []
env['rack.after_reply'] ||= []
env['rack.after_reply'] << ->() do
self.class.call
rescue StandardError => e
Expand All @@ -25,8 +27,8 @@ def self.call
SpanSubscriber::Base.transaction = nil

if transaction.nil? ||
transaction_filtered?(transaction.name) ||
!Sampler.should_sample?
transaction_filtered?(transaction.name) ||
!Sampler.should_sample?

SpanSubscriber::Base.spans = nil
return
Expand Down
10 changes: 10 additions & 0 deletions lib/solid_apm/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module SolidApm
class Railtie < Rails::Railtie
config.before_initialize do
# Always disable in test environment to prevent test pollution
SolidApm.enabled = false if Rails.env.test?
end
end
end
19 changes: 19 additions & 0 deletions spec/lib/solid_apm/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'SolidApm configuration' do
describe '.enabled' do
it 'is disabled in test environment' do
# SolidAPM is always disabled in test to prevent pollution
expect(SolidApm.enabled).to be false
end

it 'can be set to false' do
original = SolidApm.enabled
SolidApm.enabled = false
expect(SolidApm.enabled).to be false
SolidApm.enabled = original
end
end
end
13 changes: 13 additions & 0 deletions spec/lib/solid_apm/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe SolidApm::Railtie do
describe 'test environment auto-disable' do
it 'is disabled in test environment' do
# Railtie always disables in test environment
expect(Rails.env.test?).to be true
expect(SolidApm.enabled).to be false
end
end
end
22 changes: 21 additions & 1 deletion spec/services/solid_apm/middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# frozen_string_literal: true

RSpec.describe SolidApm::Middleware do
describe 'when disabled' do
around do |example|
original = SolidApm.enabled
SolidApm.enabled = false
example.run
ensure
SolidApm.enabled = original
end

it 'passes through without tracking' do
app = ->(_env) { [200, {}, ['OK']] }
middleware = described_class.new(app)

status, _headers, _body = middleware.call({})

expect(status).to eq(200)
expect(SolidApm::SpanSubscriber::Base.transaction).to be_nil
end
end

describe '.transaction_filtered?' do
before do
SolidApm.transaction_filters = []
Expand Down Expand Up @@ -74,4 +94,4 @@
end
end
end
end
end