Skip to content

Commit

Permalink
Rename middleware class according to default Sidekiq middleware namin…
Browse files Browse the repository at this point in the history
…g pattern
  • Loading branch information
vassilevsky committed Aug 8, 2018
1 parent 720a034 commit 9f17790
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/influxdb.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "sidekiq/influxdb/version"
require "sidekiq/influxdb/server_middleware"
require "sidekiq/middleware/server/influxdb"
78 changes: 0 additions & 78 deletions lib/sidekiq/influxdb/server_middleware.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/sidekiq/influxdb/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Sidekiq
module InfluxDB
VERSION = "0.4.0"
VERSION = "1.0.0"
end
end
82 changes: 82 additions & 0 deletions lib/sidekiq/middleware/server/influxdb.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/jobs.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "sidekiq/influxdb/server_middleware"
require "sidekiq/middleware/server/influxdb"

module LooksLikeWork
def perform
Expand All @@ -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
Expand Down

0 comments on commit 9f17790

Please sign in to comment.