Skip to content

Commit

Permalink
Add custom notifications (handlers)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillshevch committed Dec 21, 2019
1 parent ed00fc7 commit be295c0
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 8 deletions.
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
PATH
remote: .
specs:
query_track (0.0.8)
query_track (0.0.9)
activesupport
dry-configurable
slack_hook

GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.0)
activesupport (6.0.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.1, >= 2.1.8)
zeitwerk (~> 2.2)
ast (2.4.0)
byebug (11.0.1)
concurrent-ruby (1.1.5)
diff-lcs (1.3)
dry-configurable (0.8.3)
dry-configurable (0.9.0)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.7)
dry-core (0.4.9)
concurrent-ruby (~> 1.0)
i18n (1.6.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.3)
minitest (5.11.3)
minitest (5.13.0)
parallel (1.18.0)
parser (2.6.5.0)
ast (~> 2.4.0)
Expand Down Expand Up @@ -59,7 +59,7 @@ GEM
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.6.0)
zeitwerk (2.1.9)
zeitwerk (2.2.2)

PLATFORMS
ruby
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ end

# <img src='https://github.com/kirillshevch/query_track/blob/master/examples/slack.jpg' alt='Incoming Hook Example' />

## Custom Notifications (Handlers)

You can write your own handler for slow queries. Send data to any source(for e.g. to logs storage) or make notification for another source(Email, Messengers, etc.)

```ruby
QueryTrack::Settings.configure do |config|
config.duration = 0.5
config.notifications.custom_handler = -> (sql, duration, trace) {
# data processing...
}
end
```

## Production Usage Notes

When [QueryTrack](https://github.com/kirillshevch/query_track/blob/master/lib/query_track/notifications/slack.rb#L21) send slack hooks, request is executed in separate thread. So there should be no synchronous delays.
Expand Down
1 change: 1 addition & 0 deletions lib/query_track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'query_track/settings'
require 'query_track/trace'
require 'query_track/filters'
require 'query_track/notifications/custom'
require 'query_track/notifications/slack'
require 'query_track/notifications/log'
require 'query_track/event_processor'
Expand Down
1 change: 1 addition & 0 deletions lib/query_track/event_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def call
if duration_seconds > QueryTrack::Settings.config.duration
QueryTrack::Notifications::Slack.new(event.payload[:sql], duration_seconds).call
QueryTrack::Notifications::Log.new(event.payload[:sql], duration_seconds).call
QueryTrack::Notifications::Custom.new(event.payload[:sql], duration_seconds).call
end
end

Expand Down
20 changes: 20 additions & 0 deletions lib/query_track/notifications/custom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module QueryTrack
module Notifications
class Custom
attr_reader :code, :duration

def initialize(code, duration)
@code = code.strip
@duration = duration
end

def call
return unless QueryTrack::Settings.config.notifications.custom_handler

trace = QueryTrack::Trace.new(caller).call

QueryTrack::Settings.config.notifications.custom_handler.call(code, duration, trace)
end
end
end
end
1 change: 1 addition & 0 deletions lib/query_track/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Settings

setting :notifications do
setting :slack, ''
setting :custom_handler
end

setting :logs, false
Expand Down
2 changes: 1 addition & 1 deletion lib/query_track/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module QueryTrack
VERSION = '0.0.8'
VERSION = '0.0.9'
end
32 changes: 32 additions & 0 deletions spec/query_track/notifications/custom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
RSpec.describe QueryTrack::Notifications::Custom do
let(:code) { 'COMMIT' }
let(:duration) { 5 }

subject { described_class.new(code, duration) }

context 'custom handler not specified' do
before do
QueryTrack::Settings.configure do |config|
config.duration = 1.0
end
end

it 'should not return anything' do
expect(subject.call).to be_nil
end
end

context 'custom handler specified' do
before do
QueryTrack::Settings.configure do |config|
config.duration = 1.0
config.notifications.custom_handler = -> (sql, duration, trace) {}
end
end

it 'should call custom handler' do
expect(QueryTrack::Settings.config.notifications.custom_handler).to receive(:call)
subject.call
end
end
end

0 comments on commit be295c0

Please sign in to comment.