Skip to content

Commit cfbb2e6

Browse files
authored
Merge pull request #29 from kirillshevch/feature/custom-handlers
Add custom notifications (handlers)
2 parents 7431b2d + be295c0 commit cfbb2e6

8 files changed

Lines changed: 76 additions & 8 deletions

File tree

Gemfile.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
PATH
22
remote: .
33
specs:
4-
query_track (0.0.8)
4+
query_track (0.0.9)
55
activesupport
66
dry-configurable
77
slack_hook
88

99
GEM
1010
remote: https://rubygems.org/
1111
specs:
12-
activesupport (6.0.0)
12+
activesupport (6.0.2.1)
1313
concurrent-ruby (~> 1.0, >= 1.0.2)
1414
i18n (>= 0.7, < 2)
1515
minitest (~> 5.1)
1616
tzinfo (~> 1.1)
17-
zeitwerk (~> 2.1, >= 2.1.8)
17+
zeitwerk (~> 2.2)
1818
ast (2.4.0)
1919
byebug (11.0.1)
2020
concurrent-ruby (1.1.5)
2121
diff-lcs (1.3)
22-
dry-configurable (0.8.3)
22+
dry-configurable (0.9.0)
2323
concurrent-ruby (~> 1.0)
2424
dry-core (~> 0.4, >= 0.4.7)
2525
dry-core (0.4.9)
2626
concurrent-ruby (~> 1.0)
27-
i18n (1.6.0)
27+
i18n (1.7.0)
2828
concurrent-ruby (~> 1.0)
2929
jaro_winkler (1.5.3)
30-
minitest (5.11.3)
30+
minitest (5.13.0)
3131
parallel (1.18.0)
3232
parser (2.6.5.0)
3333
ast (~> 2.4.0)
@@ -59,7 +59,7 @@ GEM
5959
tzinfo (1.2.5)
6060
thread_safe (~> 0.1)
6161
unicode-display_width (1.6.0)
62-
zeitwerk (2.1.9)
62+
zeitwerk (2.2.2)
6363

6464
PLATFORMS
6565
ruby

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ end
7575

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

78+
## Custom Notifications (Handlers)
79+
80+
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.)
81+
82+
```ruby
83+
QueryTrack::Settings.configure do |config|
84+
config.duration = 0.5
85+
config.notifications.custom_handler = -> (sql, duration, trace) {
86+
# data processing...
87+
}
88+
end
89+
```
90+
7891
## Production Usage Notes
7992

8093
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.

lib/query_track.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'query_track/settings'
66
require 'query_track/trace'
77
require 'query_track/filters'
8+
require 'query_track/notifications/custom'
89
require 'query_track/notifications/slack'
910
require 'query_track/notifications/log'
1011
require 'query_track/event_processor'

lib/query_track/event_processor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def call
1616
if duration_seconds > QueryTrack::Settings.config.duration
1717
QueryTrack::Notifications::Slack.new(event.payload[:sql], duration_seconds).call
1818
QueryTrack::Notifications::Log.new(event.payload[:sql], duration_seconds).call
19+
QueryTrack::Notifications::Custom.new(event.payload[:sql], duration_seconds).call
1920
end
2021
end
2122

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module QueryTrack
2+
module Notifications
3+
class Custom
4+
attr_reader :code, :duration
5+
6+
def initialize(code, duration)
7+
@code = code.strip
8+
@duration = duration
9+
end
10+
11+
def call
12+
return unless QueryTrack::Settings.config.notifications.custom_handler
13+
14+
trace = QueryTrack::Trace.new(caller).call
15+
16+
QueryTrack::Settings.config.notifications.custom_handler.call(code, duration, trace)
17+
end
18+
end
19+
end
20+
end

lib/query_track/settings.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Settings
66

77
setting :notifications do
88
setting :slack, ''
9+
setting :custom_handler
910
end
1011

1112
setting :logs, false

lib/query_track/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module QueryTrack
2-
VERSION = '0.0.8'
2+
VERSION = '0.0.9'
33
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
RSpec.describe QueryTrack::Notifications::Custom do
2+
let(:code) { 'COMMIT' }
3+
let(:duration) { 5 }
4+
5+
subject { described_class.new(code, duration) }
6+
7+
context 'custom handler not specified' do
8+
before do
9+
QueryTrack::Settings.configure do |config|
10+
config.duration = 1.0
11+
end
12+
end
13+
14+
it 'should not return anything' do
15+
expect(subject.call).to be_nil
16+
end
17+
end
18+
19+
context 'custom handler specified' do
20+
before do
21+
QueryTrack::Settings.configure do |config|
22+
config.duration = 1.0
23+
config.notifications.custom_handler = -> (sql, duration, trace) {}
24+
end
25+
end
26+
27+
it 'should call custom handler' do
28+
expect(QueryTrack::Settings.config.notifications.custom_handler).to receive(:call)
29+
subject.call
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)