-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instrumentation events with Pulsar (#441)
- Loading branch information
1 parent
4178573
commit 5dd2676
Showing
19 changed files
with
292 additions
and
123 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
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,52 @@ | ||
require "./spec_helper" | ||
|
||
describe "Instrumentation" do | ||
it "publishes the query and args" do | ||
TestDatabase.query "SELECT * FROM users" | ||
|
||
event = Avram::Events::QueryEvent.logged_events.last | ||
event.query.should eq("SELECT * FROM users") | ||
event.queryable.should be_nil | ||
end | ||
|
||
it "labels the query if coming from a Queryable" do | ||
UserQuery.new.name("Bob").first? | ||
|
||
event = Avram::Events::QueryEvent.logged_events.last | ||
event.query.should contain("WHERE users.name = $1") | ||
event.args.to_s.should contain("Bob") | ||
event.queryable.should eq("User") | ||
end | ||
|
||
it "labels the scalar if coming from a Queryable" do | ||
UserQuery.new.name("Bob").select_count | ||
|
||
event = Avram::Events::QueryEvent.logged_events.last | ||
event.query.should contain("WHERE users.name = $1") | ||
event.args.to_s.should contain("Bob") | ||
event.queryable.should eq("User") | ||
end | ||
|
||
it "publishes failed queries" do | ||
expect_raises PQ::PQError do | ||
TestDatabase.scalar "NOT VALID SORRY" | ||
end | ||
|
||
event = Avram::Events::FailedQueryEvent.logged_events.last | ||
event.query.should contain("NOT VALID SORRY") | ||
end | ||
|
||
it "publishes failed operations" do | ||
Task::SaveOperation.create do |_op, _task| | ||
event = Avram::Events::SaveFailedEvent.logged_events.last | ||
event.operation_class.should eq("Task::SaveOperation") | ||
end | ||
end | ||
|
||
it "publishes successful operations" do | ||
Employee::SaveOperation.create!(name: "Someone Special") | ||
|
||
event = Avram::Events::SaveSuccessEvent.logged_events.last | ||
event.operation_class.should eq("Employee::SaveOperation") | ||
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
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
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 @@ | ||
class Avram::Events::FailedQueryEvent < Pulsar::Event | ||
getter :error_message, :query, :args, :queryable | ||
|
||
def initialize(@error_message : String, @query : String, @args : String?, @queryable : String? = nil) | ||
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,6 @@ | ||
class Avram::Events::QueryEvent < Pulsar::TimedEvent | ||
getter :query, :args, :queryable | ||
|
||
def initialize(@query : String, @args : String?, @queryable : String? = nil) | ||
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,19 @@ | ||
class Avram::Events::SaveFailedEvent < Pulsar::Event | ||
getter :operation_class, :attributes | ||
|
||
def initialize( | ||
@operation_class : String, | ||
@attributes : Array(Avram::GenericAttribute) | ||
) | ||
end | ||
|
||
def invalid_attributes | ||
attributes.reject(&.valid?) | ||
end | ||
|
||
def error_messages_as_string | ||
invalid_attributes.map do |attribute| | ||
"#{attribute.name} #{attribute.errors.join(", ")}" | ||
end.join(". ") | ||
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,9 @@ | ||
class Avram::Events::SaveSuccessEvent < Pulsar::Event | ||
getter :operation_class, :attributes | ||
|
||
def initialize( | ||
@operation_class : String, | ||
@attributes : Array(Avram::GenericAttribute) | ||
) | ||
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,23 @@ | ||
# A generic version of `Avram::Attribute` that is used for reporting and metrics. | ||
# | ||
# This is a data only version of an `Avram::Atribute`. It is purely for | ||
# retrieving and reporting on data. For example, `Avram::GenericAttribute` is | ||
# used by `Avram::Events::SaveFailedEvent` so that subscribers can | ||
# get information about attributes that failed to save. | ||
class Avram::GenericAttribute | ||
getter :name, :param, :original_value, :value, :param_key, :errors | ||
|
||
def initialize( | ||
@name : Symbol, | ||
@param : String?, | ||
@original_value : String?, | ||
@value : String?, | ||
@param_key : String, | ||
@errors : Array(String) | ||
) | ||
end | ||
|
||
def valid? | ||
errors.empty? | ||
end | ||
end |
Oops, something went wrong.