From be295c0b4000735e04ef84f434080925ed3e3e06 Mon Sep 17 00:00:00 2001 From: Kirill Shevchenko Date: Thu, 19 Dec 2019 22:54:54 +0200 Subject: [PATCH] Add custom notifications (handlers) --- Gemfile.lock | 14 ++++---- README.md | 13 ++++++++ lib/query_track.rb | 1 + lib/query_track/event_processor.rb | 1 + lib/query_track/notifications/custom.rb | 20 ++++++++++++ lib/query_track/settings.rb | 1 + lib/query_track/version.rb | 2 +- spec/query_track/notifications/custom_spec.rb | 32 +++++++++++++++++++ 8 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 lib/query_track/notifications/custom.rb create mode 100644 spec/query_track/notifications/custom_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 4253406..b5054c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - query_track (0.0.8) + query_track (0.0.9) activesupport dry-configurable slack_hook @@ -9,25 +9,25 @@ PATH 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) @@ -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 diff --git a/README.md b/README.md index f4b13cf..10514c8 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,19 @@ end # 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. diff --git a/lib/query_track.rb b/lib/query_track.rb index ad7836a..349caa2 100644 --- a/lib/query_track.rb +++ b/lib/query_track.rb @@ -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' diff --git a/lib/query_track/event_processor.rb b/lib/query_track/event_processor.rb index 944b1cd..c942eb9 100644 --- a/lib/query_track/event_processor.rb +++ b/lib/query_track/event_processor.rb @@ -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 diff --git a/lib/query_track/notifications/custom.rb b/lib/query_track/notifications/custom.rb new file mode 100644 index 0000000..0c5b9f3 --- /dev/null +++ b/lib/query_track/notifications/custom.rb @@ -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 diff --git a/lib/query_track/settings.rb b/lib/query_track/settings.rb index af96595..691efa2 100644 --- a/lib/query_track/settings.rb +++ b/lib/query_track/settings.rb @@ -6,6 +6,7 @@ class Settings setting :notifications do setting :slack, '' + setting :custom_handler end setting :logs, false diff --git a/lib/query_track/version.rb b/lib/query_track/version.rb index e4e9751..a3c6581 100644 --- a/lib/query_track/version.rb +++ b/lib/query_track/version.rb @@ -1,3 +1,3 @@ module QueryTrack - VERSION = '0.0.8' + VERSION = '0.0.9' end diff --git a/spec/query_track/notifications/custom_spec.rb b/spec/query_track/notifications/custom_spec.rb new file mode 100644 index 0000000..49850f6 --- /dev/null +++ b/spec/query_track/notifications/custom_spec.rb @@ -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