sidekiq-influxdb 1.0.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,4 @@
1
1
  require "sidekiq/influxdb/version"
2
2
  require "sidekiq/middleware/server/influxdb"
3
+ require "sidekiq/metrics/queues"
4
+ require "sidekiq/metrics/stats"
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module InfluxDB
3
- VERSION = "1.0.0"
3
+ VERSION = "1.4.1"
4
4
  end
5
5
  end
@@ -0,0 +1,41 @@
1
+ require 'sidekiq/api'
2
+
3
+ module Sidekiq
4
+ module Metrics
5
+ class Queues
6
+
7
+ def initialize(
8
+ influxdb_client:,
9
+ series_name: 'sidekiq_queues',
10
+ retention_policy: nil,
11
+ tags: {}
12
+ )
13
+ @influxdb = influxdb_client
14
+ @series = series_name
15
+ @retention = retention_policy
16
+ @tags = tags
17
+ end
18
+
19
+ def publish
20
+ queues = Sidekiq::Stats::Queues.new.lengths
21
+
22
+ queues.each do |queue, size|
23
+ save(
24
+ tags: {queue: queue}.merge(@tags),
25
+ values: {size: size}
26
+ )
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def save(data)
33
+ @influxdb.write_point(@series, data, precision, @retention)
34
+ end
35
+
36
+ def precision
37
+ @influxdb.config.time_precision
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ require 'sidekiq/api'
2
+
3
+ module Sidekiq
4
+ module Metrics
5
+ class Stats
6
+
7
+ def initialize(
8
+ influxdb_client:,
9
+ series_name: 'sidekiq_stats',
10
+ retention_policy: nil,
11
+ tags: {}
12
+ )
13
+ @influxdb = influxdb_client
14
+ @series = series_name
15
+ @retention = retention_policy
16
+ @tags = tags
17
+ end
18
+
19
+ def publish
20
+ stats = Sidekiq::Stats.new.instance_variable_get(:@stats)
21
+ stats.delete(:default_queue_latency)
22
+
23
+ stats.each do |stat, size|
24
+ save(
25
+ tags: {stat: stat}.merge(@tags),
26
+ values: {size: size}
27
+ )
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def save(data)
34
+ @influxdb.write_point(@series, data, precision, @retention)
35
+ end
36
+
37
+ def precision
38
+ @influxdb.config.time_precision
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,64 +1,74 @@
1
1
  require 'set'
2
- require 'influxdb'
3
2
 
4
3
  module Sidekiq
5
4
  module Middleware
6
5
  module Server
7
6
  class InfluxDB
8
7
 
9
- def initialize(
10
- influxdb_client:,
11
- series_name: 'sidekiq_jobs',
12
- retention_policy: nil,
13
- start_events: true,
14
- tags: {},
15
- except: []
16
- )
17
- @influxdb = influxdb_client
18
- @series = series_name
19
- @retention = retention_policy
20
- @start_events = start_events
21
- @tags = tags
22
- @secret_agents = Set.new(except)
8
+ def initialize(options = {})
9
+ @influxdb = options.fetch(:influxdb_client)
10
+ @series = options.fetch(:series_name, 'sidekiq_jobs')
11
+ @retention = options.fetch(:retention_policy, nil)
12
+ @start_events = options.fetch(:start_events, true)
13
+ @tags = options.fetch(:tags, {})
14
+ @secret_agents = class_names(options.fetch(:except, []))
15
+ @clock = options.fetch(:clock, -> { Time.now.to_f })
23
16
  end
24
17
 
25
- def call(worker, msg, queue)
26
- if @secret_agents.include?(worker.class)
18
+ def call(_worker, msg, _queue)
19
+ if @secret_agents.include?(job_class_name(msg))
27
20
  yield
28
21
  return
29
22
  end
30
- t = Time.now.to_f
31
- data = {
32
- tags: {
33
- class: worker.class.name,
34
- queue: queue,
35
- event: 'start',
36
- }.merge(@tags),
37
- values: {
38
- jid: msg['jid'],
39
- creation_time: msg['created_at'],
40
- waited: t - msg['created_at'],
41
- },
42
- timestamp: in_correct_precision(t)
43
- }
44
- save(data) if @start_events
23
+
24
+ started_at = @clock.call
25
+ waited = started_at - msg['created_at']
26
+ record(started_at, msg, {event: 'start'}, {waited: waited}) if @start_events
27
+
45
28
  begin
46
29
  yield
47
- data[:tags][:event] = 'finish'
30
+ tags = {event: 'finish'}
48
31
  rescue => e
49
- data[:tags][:event] = 'error'
50
- data[:tags][:error] = e.class.name
32
+ tags = {event: 'error', error: e.class.name}
51
33
  end
52
- tt = Time.now.to_f
53
- data[:values][:worked] = tt - t
54
- data[:values][:total] = tt - msg['created_at']
55
- data[:timestamp] = in_correct_precision(tt)
56
- save(data)
34
+
35
+ finished_at = @clock.call
36
+ worked = finished_at - started_at
37
+ record(finished_at, msg, tags, {waited: waited, worked: worked, total: waited + worked})
38
+
57
39
  raise e if e
58
40
  end
59
41
 
60
42
  private
61
43
 
44
+ def class_names(except)
45
+ Set.new([except].flatten.map{|e| class_name(e) })
46
+ end
47
+
48
+ def class_name(class_or_name)
49
+ class_or_name.name
50
+ rescue NoMethodError
51
+ class_or_name
52
+ end
53
+
54
+ def job_class_name(msg)
55
+ msg['wrapped'] || msg['class']
56
+ end
57
+
58
+ def record(t, msg, tags, values = {})
59
+ save(
60
+ tags: {
61
+ class: job_class_name(msg),
62
+ queue: msg['queue']
63
+ }.merge(tags).merge(@tags),
64
+ values: {
65
+ jid: msg['jid'],
66
+ creation_time: msg['created_at']
67
+ }.merge(values),
68
+ timestamp: in_correct_precision(t)
69
+ )
70
+ end
71
+
62
72
  def save(data)
63
73
  @influxdb.write_point(@series, data, precision, @retention)
64
74
  end
@@ -70,9 +80,12 @@ module Sidekiq
70
80
  def in_correct_precision(t)
71
81
  case precision
72
82
  # In order of probability in real-world setups
73
- when 'ms' then (t * 1000).to_i
83
+ when 'ms' then (t * 1_000).to_i
74
84
  when 's' then t.to_i
75
- when 'u' then (t * 1000000).to_i
85
+ when 'u' then (t * 1_000_000).to_i
86
+ when 'ns' then (t * 1_000_000_000).to_i
87
+ when 'm' then (t / 60).to_i * 60
88
+ when 'h' then (t / 60 / 60).to_i * 60 * 60
76
89
  end
77
90
  end
78
91
 
@@ -21,6 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_runtime_dependency "influxdb"
22
22
  spec.add_runtime_dependency "sidekiq"
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.16"
25
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "coveralls"
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-influxdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Vassilevsky
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-08 00:00:00.000000000 Z
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: influxdb
@@ -39,55 +39,64 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bundler
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.16'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.16'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rake
56
+ name: coveralls
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
69
- description:
68
+ version: '0'
69
+ description:
70
70
  email:
71
71
  - vassilevsky@gmail.com
72
72
  executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".coveralls.yml"
76
77
  - ".gitignore"
78
+ - ".rspec"
77
79
  - ".ruby-version"
80
+ - ".travis.yml"
81
+ - CHANGELOG.md
78
82
  - CODE_OF_CONDUCT.md
83
+ - CONTRIBUTING.md
79
84
  - Gemfile
85
+ - LICENSE
80
86
  - Makefile
81
87
  - README.md
82
88
  - Rakefile
89
+ - grafana_dashboard.json
83
90
  - lib/sidekiq/influxdb.rb
84
91
  - lib/sidekiq/influxdb/version.rb
92
+ - lib/sidekiq/metrics/queues.rb
93
+ - lib/sidekiq/metrics/stats.rb
85
94
  - lib/sidekiq/middleware/server/influxdb.rb
86
95
  - sidekiq-influxdb.gemspec
87
96
  homepage: https://github.com/vassilevsky/sidekiq-influxdb
88
97
  licenses: []
89
98
  metadata: {}
90
- post_install_message:
99
+ post_install_message:
91
100
  rdoc_options: []
92
101
  require_paths:
93
102
  - lib
@@ -102,9 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
111
  - !ruby/object:Gem::Version
103
112
  version: '0'
104
113
  requirements: []
105
- rubyforge_project:
106
- rubygems_version: 2.6.13
107
- signing_key:
114
+ rubygems_version: 3.2.3
115
+ signing_key:
108
116
  specification_version: 4
109
117
  summary: Writes Sidekiq job metrics to InfluxDB
110
118
  test_files: []