From 9f17790d5245b81abfde9ab7f816308fd6613a43 Mon Sep 17 00:00:00 2001 From: "i.vasilevskiy" Date: Wed, 8 Aug 2018 16:46:16 +0400 Subject: [PATCH] Rename middleware class according to default Sidekiq middleware naming pattern --- README.md | 2 +- lib/sidekiq/influxdb.rb | 2 +- lib/sidekiq/influxdb/server_middleware.rb | 78 --------------------- lib/sidekiq/influxdb/version.rb | 2 +- lib/sidekiq/middleware/server/influxdb.rb | 82 +++++++++++++++++++++++ test/jobs.rb | 4 +- 6 files changed, 87 insertions(+), 83 deletions(-) delete mode 100644 lib/sidekiq/influxdb/server_middleware.rb create mode 100644 lib/sidekiq/middleware/server/influxdb.rb diff --git a/README.md b/README.md index 10f0fc3..45b39d3 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ require "sidekiq/influxdb/server_middleware" Sidekiq.configure_server do |config| config.server_middleware do |chain| - chain.add Sidekiq::InfluxDB::ServerMiddleware, + chain.add Sidekiq::Middleware::Server::InfluxDB, influxdb_client: InfluxDB::Client.new(options), # REQUIRED series_name: 'sidekiq_jobs', # optional, default shown retention_policy: nil, # optional, default nil diff --git a/lib/sidekiq/influxdb.rb b/lib/sidekiq/influxdb.rb index 0e9e35c..3ecc6b6 100644 --- a/lib/sidekiq/influxdb.rb +++ b/lib/sidekiq/influxdb.rb @@ -1,2 +1,2 @@ require "sidekiq/influxdb/version" -require "sidekiq/influxdb/server_middleware" +require "sidekiq/middleware/server/influxdb" diff --git a/lib/sidekiq/influxdb/server_middleware.rb b/lib/sidekiq/influxdb/server_middleware.rb deleted file mode 100644 index 7de9b4b..0000000 --- a/lib/sidekiq/influxdb/server_middleware.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'set' -require 'influxdb' - -module Sidekiq - module InfluxDB - class ServerMiddleware - def initialize( - influxdb_client:, - series_name: 'sidekiq_jobs', - retention_policy: nil, - start_events: true, - tags: {}, - except: [] - ) - @influxdb = influxdb_client - @series = series_name - @retention = retention_policy - @start_events = start_events - @tags = tags - @secret_agents = Set.new(except) - end - - def call(worker, msg, queue) - if @secret_agents.include?(worker.class) - yield - return - end - t = Time.now.to_f - data = { - tags: { - class: worker.class.name, - queue: queue, - event: 'start', - }.merge(@tags), - values: { - jid: msg['jid'], - creation_time: msg['created_at'], - waited: t - msg['created_at'], - }, - timestamp: in_correct_precision(t) - } - save(data) if @start_events - begin - yield - data[:tags][:event] = 'finish' - rescue => e - data[:tags][:event] = 'error' - data[:tags][:error] = e.class.name - end - tt = Time.now.to_f - data[:values][:worked] = tt - t - data[:values][:total] = tt - msg['created_at'] - data[:timestamp] = in_correct_precision(tt) - save(data) - raise e if e - end - - private - - def save(data) - @influxdb.write_point(@series, data, precision, @retention) - end - - def precision - @influxdb.config.time_precision - end - - def in_correct_precision(t) - case precision - # In order of probability in real-world setups - when 'ms' then (t * 1000).to_i - when 's' then t.to_i - when 'u' then (t * 1000000).to_i - end - end - end - end -end diff --git a/lib/sidekiq/influxdb/version.rb b/lib/sidekiq/influxdb/version.rb index e54b77e..9299645 100644 --- a/lib/sidekiq/influxdb/version.rb +++ b/lib/sidekiq/influxdb/version.rb @@ -1,5 +1,5 @@ module Sidekiq module InfluxDB - VERSION = "0.4.0" + VERSION = "1.0.0" end end diff --git a/lib/sidekiq/middleware/server/influxdb.rb b/lib/sidekiq/middleware/server/influxdb.rb new file mode 100644 index 0000000..8e6af8a --- /dev/null +++ b/lib/sidekiq/middleware/server/influxdb.rb @@ -0,0 +1,82 @@ +require 'set' +require 'influxdb' + +module Sidekiq + module Middleware + module Server + class InfluxDB + + def initialize( + influxdb_client:, + series_name: 'sidekiq_jobs', + retention_policy: nil, + start_events: true, + tags: {}, + except: [] + ) + @influxdb = influxdb_client + @series = series_name + @retention = retention_policy + @start_events = start_events + @tags = tags + @secret_agents = Set.new(except) + end + + def call(worker, msg, queue) + if @secret_agents.include?(worker.class) + yield + return + end + t = Time.now.to_f + data = { + tags: { + class: worker.class.name, + queue: queue, + event: 'start', + }.merge(@tags), + values: { + jid: msg['jid'], + creation_time: msg['created_at'], + waited: t - msg['created_at'], + }, + timestamp: in_correct_precision(t) + } + save(data) if @start_events + begin + yield + data[:tags][:event] = 'finish' + rescue => e + data[:tags][:event] = 'error' + data[:tags][:error] = e.class.name + end + tt = Time.now.to_f + data[:values][:worked] = tt - t + data[:values][:total] = tt - msg['created_at'] + data[:timestamp] = in_correct_precision(tt) + save(data) + raise e if e + end + + private + + def save(data) + @influxdb.write_point(@series, data, precision, @retention) + end + + def precision + @influxdb.config.time_precision + end + + def in_correct_precision(t) + case precision + # In order of probability in real-world setups + when 'ms' then (t * 1000).to_i + when 's' then t.to_i + when 'u' then (t * 1000000).to_i + end + end + + end + end + end +end diff --git a/test/jobs.rb b/test/jobs.rb index e030be5..b44bb1d 100644 --- a/test/jobs.rb +++ b/test/jobs.rb @@ -1,4 +1,4 @@ -require "sidekiq/influxdb/server_middleware" +require "sidekiq/middleware/server/influxdb" module LooksLikeWork def perform @@ -24,7 +24,7 @@ class IgnoredJob Sidekiq.configure_server do |config| config.server_middleware do |chain| - chain.add Sidekiq::InfluxDB::ServerMiddleware, + chain.add Sidekiq::Middleware::Server::InfluxDB, influxdb_client: InfluxDB::Client.new('test', time_precision: 'ms'), except: [IgnoredJob] end