Skip to content

Commit

Permalink
Add sequel-rails event formatter for SQL queries (#801)
Browse files Browse the repository at this point in the history
The sequel-rails gem has its own ActiveSupport::Notifications
instrumentor, tracking events about Sequel queries that are being made.
https://github.com/TalentBox/sequel-rails/blob/db6778f1fbdc15a4a53f9f406dc6354debdd17fb/lib/sequel_rails/sequel/database/active_support_notification.rb#L11-L16

This conflicts with our own sequel instrumentation, recording a
duplicate event. This duplicate event can be worked around by setting
`instrument_sequel` to `false`, disabling the AppSignal integration.
This issue is not addressed in this commit. It requires the manual
config change.

The problem then is that the sequel-rails ActiveSupport::Notifications
event was not handled in any special way, so the event was stored
without an event body, not showing the query that was executed.

To track the SQL query that was made, add an AppSignal event formatter
(used by the ActiveSupport::Notifications integration) to extract the
SQL query from the event and store it on the AppSignal event.

Fixes #799
  • Loading branch information
tombruijn authored Jan 7, 2022
1 parent 2587eae commit 243c1ed
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changesets/track-sequel-rails-sql-query-in-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "fix"
---

Improve compatibility with the sequel-rails gem by tracking the performed SQL query in instrumentation events.
24 changes: 24 additions & 0 deletions lib/appsignal/event_formatter/sequel/sql_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Appsignal
class EventFormatter
# @api private
module Sequel
# Compatability with the sequel-rails gem.
# The sequel-rails gem adds its own ActiveSupport::Notifications events
# that conflict with our own sequel instrumentor. Without this event
# formatter the sequel-rails events are recorded without the SQL query
# that's being executed.
class SqlFormatter
def format(payload)
[payload[:name].to_s, payload[:sql], SQL_BODY_FORMAT]
end
end
end
end
end

Appsignal::EventFormatter.register(
"sql.sequel",
Appsignal::EventFormatter::Sequel::SqlFormatter
)
30 changes: 30 additions & 0 deletions spec/lib/appsignal/event_formatter/sequel/sql_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe Appsignal::EventFormatter::Sequel::SqlFormatter do
let(:klass) { described_class }
let(:formatter) { klass.new }

it "registers the sql.sequel event formatter" do
expect(Appsignal::EventFormatter.registered?("sql.sequel", klass)).to be_truthy
end

describe "#format" do
before do
stub_const(
"SequelDatabaseTypeClass",
Class.new do
def self.to_s
"SequelDatabaseTypeClassToString"
end
end
)
end
let(:payload) do
{
:name => SequelDatabaseTypeClass,
:sql => "SELECT * FROM users"
}
end
subject { formatter.format(payload) }

it { is_expected.to eq ["SequelDatabaseTypeClassToString", "SELECT * FROM users", 1] }
end
end

0 comments on commit 243c1ed

Please sign in to comment.