-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for dry-monitor events (#996)
New hook that adds transaction events every time a dry-monitor notifications instrumentation is called. There's also specific support for `rom-sql` events emitted through `dry-monitor`. Co-authored-by: Luismi Ramírez <[email protected]>
- Loading branch information
1 parent
6932bb3
commit 29970d9
Showing
12 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
bump: "patch" | ||
type: "add" | ||
--- | ||
|
||
Events from `dry-monitor` are now supported. There's also native support for `rom-sql` instrumentation events if they're configured. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem "dry-monitor", "~> 1.0.1" | ||
|
||
gemspec :path => '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
class EventFormatter | ||
module Rom | ||
class SqlFormatter | ||
def format(payload) | ||
["query.#{payload[:name]}", payload[:query], SQL_BODY_FORMAT] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Appsignal::EventFormatter.register( | ||
"sql.dry", | ||
Appsignal::EventFormatter::Rom::SqlFormatter | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
class Hooks | ||
# @api private | ||
class DryMonitorHook < Appsignal::Hooks::Hook | ||
register :dry_monitor | ||
|
||
def dependencies_present? | ||
defined?(::Dry::Monitor::Notifications) | ||
end | ||
|
||
def install | ||
require "appsignal/integrations/dry_monitor" | ||
|
||
::Dry::Monitor::Notifications.prepend(Appsignal::Integrations::DryMonitorIntegration) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module Integrations | ||
module DryMonitorIntegration | ||
def instrument(event_id, payload = {}, &block) | ||
Appsignal::Transaction.current.start_event | ||
|
||
super | ||
ensure | ||
title, body, body_format = Appsignal::EventFormatter.format("#{event_id}.dry", payload) | ||
|
||
Appsignal::Transaction.current.finish_event( | ||
title || event_id.to_s, | ||
title, | ||
body, | ||
body_format | ||
) | ||
end | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
spec/lib/appsignal/event_formatter/rom/sql_formatter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Appsignal::EventFormatter::Rom::SqlFormatter do | ||
let(:klass) { described_class } | ||
let(:formatter) { klass.new } | ||
|
||
it "registers the sql event formatter" do | ||
expect(Appsignal::EventFormatter.registered?("sql.dry", klass)).to be_truthy | ||
end | ||
|
||
describe "#format" do | ||
let(:payload) do | ||
{ | ||
:name => "postgres", | ||
:query => "SELECT * FROM users" | ||
} | ||
end | ||
subject { formatter.format(payload) } | ||
|
||
it { is_expected.to eq ["query.postgres", "SELECT * FROM users", 1] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
if DependencyHelper.dry_monitor_present? | ||
require "dry-monitor" | ||
|
||
describe Appsignal::Hooks::DryMonitorHook do | ||
describe "#dependencies_present?" do | ||
subject { described_class.new.dependencies_present? } | ||
|
||
context "when Dry::Monitor::Notifications constant is found" do | ||
before { stub_const "Dry::Monitor::Notifications", Class.new } | ||
|
||
it { is_expected.to be_truthy } | ||
end | ||
|
||
context "when Dry::Monitor::Notifications constant is not found" do | ||
before { hide_const "Dry::Monitor::Notifications" } | ||
|
||
it { is_expected.to be_falsy } | ||
end | ||
end | ||
end | ||
|
||
describe "#install" do | ||
it "installs the dry-monitor hook" do | ||
start_agent | ||
|
||
expect(Dry::Monitor::Notifications.included_modules).to include( | ||
Appsignal::Integrations::DryMonitorIntegration | ||
) | ||
end | ||
end | ||
|
||
describe "Dry Monitor Integration" do | ||
before :context do | ||
start_agent | ||
end | ||
|
||
let!(:transaction) do | ||
Appsignal::Transaction.create("uuid", Appsignal::Transaction::HTTP_REQUEST, "test") | ||
end | ||
|
||
let(:notifications) { Dry::Monitor::Notifications.new(:test) } | ||
|
||
context "when is a dry-sql event" do | ||
let(:event_id) { :sql } | ||
let(:payload) do | ||
{ | ||
:name => "postgres", | ||
:query => "SELECT * FROM users" | ||
} | ||
end | ||
|
||
it "creates an sql event" do | ||
notifications.instrument(event_id, payload) | ||
expect(transaction.to_h["events"]).to match([ | ||
{ | ||
"allocation_count" => kind_of(Integer), | ||
"body" => "SELECT * FROM users", | ||
"body_format" => Appsignal::EventFormatter::SQL_BODY_FORMAT, | ||
"child_allocation_count" => kind_of(Integer), | ||
"child_duration" => kind_of(Float), | ||
"child_gc_duration" => kind_of(Float), | ||
"count" => 1, | ||
"duration" => kind_of(Float), | ||
"gc_duration" => kind_of(Float), | ||
"name" => "query.postgres", | ||
"start" => kind_of(Float), | ||
"title" => "query.postgres" | ||
} | ||
]) | ||
end | ||
end | ||
|
||
context "when is an unregistered formatter event" do | ||
let(:event_id) { :foo } | ||
let(:payload) do | ||
{ | ||
:name => "foo" | ||
} | ||
end | ||
|
||
it "creates a generic event" do | ||
notifications.instrument(event_id, payload) | ||
expect(transaction.to_h["events"]).to match([ | ||
{ | ||
"allocation_count" => kind_of(Integer), | ||
"body" => "", | ||
"body_format" => Appsignal::EventFormatter::DEFAULT, | ||
"child_allocation_count" => kind_of(Integer), | ||
"child_duration" => kind_of(Float), | ||
"child_gc_duration" => kind_of(Float), | ||
"count" => 1, | ||
"duration" => kind_of(Float), | ||
"gc_duration" => kind_of(Float), | ||
"name" => "foo", | ||
"start" => kind_of(Float), | ||
"title" => "" | ||
} | ||
]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters