Skip to content

Add monotonic_active_support_logger #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- Add `monotonic_active_support_logger` [#1531][https://github.com/getsentry/sentry-ruby/pull/1531]
- Support after-retry reporting to `sentry-sidekiq` [#1532](https://github.com/getsentry/sentry-ruby/pull/1532)
- Generate Security Header Endpoint with `Sentry.csp_report_uri` from dsn [#1507](https://github.com/getsentry/sentry-ruby/pull/1507)

Expand Down Expand Up @@ -103,7 +104,7 @@ This is to prevent the background job tracing consumes too much of your transact

- Implement sentry-trace propagation [#1446](https://github.com/getsentry/sentry-ruby/pull/1446)

The SDK will insert the `sentry-trace` to outgoing requests made with `Net::HTTP`. Its value would look like `d827317d25d5420aa3aa97a0257db998-57757614642bdff5-1`.
The SDK will insert the `sentry-trace` to outgoing requests made with `Net::HTTP`. Its value would look like `d827317d25d5420aa3aa97a0257db998-57757614642bdff5-1`.

If the receiver service also uses Sentry and the SDK supports performance monitoring, its tracing event will be connected with the sender application's.

Expand All @@ -119,7 +120,6 @@ With this new option, users can skip exceptions reported from rake tasks by sett

### Bug Fixes

- Allow toggling background sending on the fly [#1447](https://github.com/getsentry/sentry-ruby/pull/1447)
- Allow toggling background sending on the fly [#1447](https://github.com/getsentry/sentry-ruby/pull/1447)
- Disable background worker for runner mode [#1448](https://github.com/getsentry/sentry-ruby/pull/1448)
- Fixes [#1324](https://github.com/getsentry/sentry-ruby/issues/1324)

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "sentry/rails/instrument_payload_cleanup_helper"

module Sentry
module Rails
module Breadcrumb
module MonotonicActiveSupportLogger
class << self
include InstrumentPayloadCleanupHelper

def add(name, started, _finished, _unique_id, data)
# skip Rails' internal events
return if name.start_with?("!")

if data.is_a?(Hash)
# we should only mutate the copy of the data
data = data.dup
cleanup_data(data)
end

crumb = Sentry::Breadcrumb.new(
data: data,
category: name,
timestamp: started.to_i
)
Sentry.add_breadcrumb(crumb)
end

def inject
@subscriber = ::ActiveSupport::Notifications.monotonic_subscribe(/.*/) do |name, started, finished, unique_id, data|
# we only record events that has a float as started timestamp
if started.is_a?(Float)
add(name, started, finished, unique_id, data)
end
end
end

def detach
::ActiveSupport::Notifications.unsubscribe(@subscriber)
end
end
end
end
end
end
7 changes: 7 additions & 0 deletions sentry-rails/lib/sentry/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def inject_breadcrumbs_logger
require 'sentry/rails/breadcrumb/active_support_logger'
Sentry::Rails::Breadcrumb::ActiveSupportLogger.inject
end

if Sentry.configuration.breadcrumbs_logger.include?(:monotonic_active_support_logger)
return warn "Usage of `monotonic_active_support_logger` require a version of Rails >= 6.1, please upgrade your Rails version or use another logger" if ::Rails.version.to_f < 6.1

require 'sentry/rails/breadcrumb/monotonic_active_support_logger'
Sentry::Rails::Breadcrumb::MonotonicActiveSupportLogger.inject
end
end

def setup_backtrace_cleanup_callback
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require "spec_helper"


RSpec.describe "Sentry::Breadcrumbs::MonotonicActiveSupportLogger", type: :request do
before do
make_basic_app do |sentry_config|
sentry_config.breadcrumbs_logger = [:monotonic_active_support_logger]
sentry_config.traces_sample_rate = 1.0
end
end

after do
require 'sentry/rails/breadcrumb/monotonic_active_support_logger'
Sentry::Rails::Breadcrumb::MonotonicActiveSupportLogger.detach
# even though we cleanup breadcrumbs in the rack middleware
# Breadcrumbs::MonotonicActiveSupportLogger subscribes to "every" instrumentation
# so it'll create other instrumentations "after" the request is finished
# and we should clear those as well
Sentry.get_current_scope.clear_breadcrumbs
end

let(:transport) do
Sentry.get_current_client.transport
end

let(:breadcrumb_buffer) do
Sentry.get_current_scope.breadcrumbs
end

let(:event) do
transport.events.first.to_json_compatible
end

after do
transport.events = []
end

context "given a Rails version < 6.1", skip: Rails.version.to_f >= 6.1 do
it "does not run instrumentation" do
get "/exception"

breadcrumbs = event.dig("breadcrumbs", "values")
expect(breadcrumbs.count).to be_zero
end
end

context "given a Rails version >= 6.1", skip: Rails.version.to_f <= 6.1 do
it "captures correct data of exception requests" do
get "/exception"

expect(response.status).to eq(500)
breadcrumbs = event.dig("breadcrumbs", "values")
expect(breadcrumbs.count).to eq(2)

breadcrumb = breadcrumbs.detect { |b| b["category"] == "process_action.action_controller" }
expect(breadcrumb["data"]).to include(
{
"controller" => "HelloController",
"action" => "exception",
"params" => { "controller" => "hello", "action" => "exception" },
"format" => "html",
"method" => "GET", "path" => "/exception",
}
)
expect(breadcrumb["data"].keys).not_to include("headers")
expect(breadcrumb["data"].keys).not_to include("request")
expect(breadcrumb["data"].keys).not_to include("response")
end

it "captures correct request data of normal requests" do
p = Post.create!

get "/posts/#{p.id}"

breadcrumbs = event.dig("breadcrumbs", "values")

breadcrumb = breadcrumbs.detect { |b| b["category"] == "process_action.action_controller" }
expect(breadcrumb["data"]).to include(
{
"controller" => "PostsController",
"action" => "show",
"params" => { "controller" => "posts", "action" => "show", "id" => p.id.to_s },
"format" => "html",
"method" => "GET", "path" => "/posts/#{p.id}",
}
)
expect(breadcrumb["data"].keys).not_to include("headers")
expect(breadcrumb["data"].keys).not_to include("request")
expect(breadcrumb["data"].keys).not_to include("response")
end

it "ignores exception data" do
get "/view_exception"

expect(event.dig("breadcrumbs", "values", -1, "data").keys).not_to include("exception")
expect(event.dig("breadcrumbs", "values", -1, "data").keys).not_to include("exception_object")
end

it "ignores events that doesn't have a float as started attributes" do
expect do
ActiveSupport::Notifications.publish "foo", Time.now
end.not_to raise_error

expect(breadcrumb_buffer.count).to be_zero
end
end
end