yabeda-activejob 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08158815f9482115a22e494dd4f3b65fd296adb566f1b641f3fa39b15d232bcd'
4
- data.tar.gz: 5d57d4ee2d12c75453f852422d044728cff9d7631928f04a76886d321b425bd0
3
+ metadata.gz: 3da58bbe370af6fa52cd9021520491f692d4310ea5dcd93d27b0f78eaf5c940b
4
+ data.tar.gz: b3a955f99cfe4482a9cd1da53720967ee508e3b62bbe430a16eaf46e583e00d1
5
5
  SHA512:
6
- metadata.gz: ea6312266b05349917950bae645a413a257e3ed970ec4b9a5b82d0a177487a6dea3529a144849e489bd37c4aff4ea21a53058d4857226b3f154816c7e551f069
7
- data.tar.gz: 7e60c91f4613c747e8572ddbebbad53c7c42613845e3dadb7d3df3b89486ebd332fa9f90bf993d2da4a72b87f917f4ffa4ca98397cdb03f3dac0eb65e4eb8ea2
6
+ metadata.gz: 9264ea6fc38f7c9aa81543e17dca965a4a68e141bddfaa578e51859a8177d70e1cee195f088f0f9964a70077411f1717478489581a1f2b3b4124d16a893d1b40
7
+ data.tar.gz: c7b404f2a8a05c024df6c81d6720dccfaa6a55ba64fcc9e73ada1035720a29fa2903d1e39ff6377f5a73e89b469781c8a5d6a54fed5d91b5b54be7c9a280b82f
data/README.md CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Yabeda metrics around rails activejobs. The motivation came from wanting something similar to [yabeda-sidekiq](https://github.com/yabeda-rb/yabeda-sidekiq) for
7
7
  resque but decided to generalize even more with just doing it on the activejob level since that is likely more in use
8
- than just resque. and could implement a lot of the general metrics needed without having to leverage your used adapters
9
- implementation and oh the redis calls.
10
- The intent is to have this plugin with an exporter such as [prometheus](https://github.com/yabeda-rb/yabeda-prometheus).
8
+ than just resque. and could implement a lot of the general metrics needed without having to leverage the adapter
9
+ implementation and, oh the redis calls.
11
10
 
12
- ## Usage
13
- How to use my plugin.
11
+ Sample [Grafana dashboard](https://grafana.com/grafana/dashboards/17303) ID: [17303](https://grafana.com/grafana/dashboards/17303)
12
+
13
+ The intent is to have this plugin with an exporter such as [prometheus](https://github.com/yabeda-rb/yabeda-prometheus).
14
14
 
15
15
  ## Installation
16
16
  Add this line to your application's Gemfile:
@@ -21,22 +21,99 @@ gem 'yabeda-activejob'
21
21
  # gem 'yabeda-prometheus'
22
22
  ```
23
23
 
24
+ And then execute:
25
+
26
+ $ bundle
24
27
  ### Registering metrics on server process start
28
+ Depending on your activejob adapter the installation process may be different for you. If using sidekiq:
29
+ ```ruby
30
+ # config/initializers/sidekiq or elsewhere
31
+ Sidekiq.configure_server do |_config|
32
+ Yabeda::ActiveJob.install!
33
+ end
34
+ ```
35
+
36
+ If using with resque:
37
+ ```ruby
38
+ # config/initializers/yabeda.rb or elsewhere
39
+ Yabeda::ActiveJob.install!
40
+ ```
41
+ If using resque you may need to use [yabeda-prometheus-mmap](https://github.com/yabeda-rb/yabeda-prometheus-mmap) or set your storage type to direct file store so that the metrics are available
42
+ to your collector.
43
+
44
+ To set your storage type to direct file store you can do the following in your yabeda initializer:
25
45
 
26
- Currently, yabeda-activejob does not automatically install on your rails server (this will be added in the future). For now to install
27
- you can do the following:
28
46
  ```ruby
29
- # config/initializers/yabeda.rb
30
- Yabeda::ActiveJob.install!
47
+ # config/initializers/yabeda.rb or elsewhere
48
+ Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(dir: "/tmp")
31
49
  ```
32
50
 
51
+ **Note** if using direct file datastore it must be called before registering any metrics.
52
+
53
+ If using `resque` with prometheus and scraping your resque process via the `/metrics` endpoint is unfeasible consider setting up a
54
+ push gateway. Once set up, you can use the `after_event_block` to push metrics to your push gateway after every event is
55
+ complete.
56
+
57
+ ````ruby
58
+ Yabeda.configure do
59
+ Yabeda::ActiveJob.after_event_block = Proc.new do |event|
60
+ # do your pushing or any custom code here
61
+ end
62
+ Yabeda::ActiveJob.install!
63
+ end
64
+ ````
65
+
66
+ **Note**: Since the notifications are registered on install make sure to setup your after_event_block before calling install!
67
+
33
68
  ## Metrics
34
69
 
35
- - Total jobs processed: `activejob.executed_total`
36
- - Total successful jobs processed: `activejob.success_total`
37
- - Total failed jobs processed: `activejob.failed_total`
70
+ - Total enqueued jobs: `activejob.enqueued_total` segmented by: queue, activejob(job class name), executions(number of executions)
71
+ - Total jobs processed: `activejob.executed_total` segmented by: queue, activejob(job class name), executions(number of executions)
72
+ - Total successful jobs processed: `activejob.success_total` segmented by: queue, activejob(job class name), executions(number of executions)
73
+ - Total failed jobs processed: `activejob.failed_total` segmented by: queue, activejob(job class name), executions(number of executions), failure_reason(error class)
38
74
  - Job runtime: `activejob.runtime` (in seconds)
39
75
  - Job latency: `activejob.latency` (in seconds)
40
76
 
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Fullscript/yabeda-activejob.
80
+
81
+ ### Releasing
82
+
83
+ 1. Bump version number in `lib/yabeda/activejob/version.rb`
84
+
85
+ In case of pre-releases keep in mind [rubygems/rubygems#3086](https://github.com/rubygems/rubygems/issues/3086) and check version with command like `Gem::Version.new(Yabeda::ActiveJob::VERSION).to_s`
86
+
87
+ 2. Fill `CHANGELOG.md` with missing changes, add header with version and date.
88
+
89
+ 3. Make a commit:
90
+
91
+ ```sh
92
+ git add lib/yabeda/activejob/version.rb CHANGELOG.md
93
+ version=$(ruby -r ./lib/yabeda/activejob/version.rb -e "puts Gem::Version.new(Yabeda::ActiveJob::VERSION)")
94
+ git commit --message="${version}: " --edit
95
+ ```
96
+
97
+ 4. Create annotated tag:
98
+
99
+ ```sh
100
+ git tag v${version} --annotate --message="${version}: " --edit --sign
101
+ ```
102
+
103
+ 5. Fill version name into subject line and (optionally) some description (list of changes will be taken from changelog and appended automatically)
104
+
105
+ 6. Push it:
106
+
107
+ ```sh
108
+ git push --follow-tags
109
+ ```
110
+
111
+ 7. GitHub Actions will create a new release, build and push gem into RubyGems! You're done!
112
+
41
113
  ## License
114
+
42
115
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
116
+
117
+ [Yabeda-sidekiq]: https://github.com/yabeda-rb/yabeda-sidekiq "Inspiration for this gem"
118
+ [yabeda]: https://github.com/yabeda-rb/yabeda
119
+ [yabeda-prometheus]: https://github.com/yabeda-rb/yabeda-prometheus
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yabeda
4
4
  module ActiveJob
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "yabeda"
4
4
  require "yabeda/activejob/version"
5
+ require "active_support"
5
6
 
6
7
  module Yabeda
7
8
  # Small set of metrics on activejob jobs
@@ -11,6 +12,8 @@ module Yabeda
11
12
  30, 60, 120, 300, 1800, 3600, 21_600, # In cases jobs are very long-running
12
13
  ].freeze
13
14
 
15
+ mattr_accessor :after_event_block, default: proc { |_event| }
16
+
14
17
  # rubocop: disable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
15
18
  def self.install!
16
19
  Yabeda.configure do
@@ -40,8 +43,8 @@ module Yabeda
40
43
  event = ActiveSupport::Notifications::Event.new(*args)
41
44
  labels = {
42
45
  activejob: event.payload[:job].class.to_s,
43
- queue: event.payload[:job].instance_variable_get(:@queue_name).to_s,
44
- executions: event.payload[:job].instance_variable_get(:@executions).to_s,
46
+ queue: event.payload[:job].queue_name.to_s,
47
+ executions: event.payload[:job].executions.to_s,
45
48
  }
46
49
  if event.payload[:exception].present?
47
50
  activejob_failed_total.increment(
@@ -53,6 +56,7 @@ module Yabeda
53
56
 
54
57
  activejob_executed_total.increment(labels)
55
58
  activejob_runtime.measure(labels, Yabeda::ActiveJob.ms2s(event.duration))
59
+ Yabeda::ActiveJob.after_event_block.call(event) if Yabeda::ActiveJob.after_event_block.respond_to?(:call)
56
60
  end
57
61
 
58
62
  # start job event
@@ -61,12 +65,14 @@ module Yabeda
61
65
 
62
66
  labels = {
63
67
  activejob: event.payload[:job].class.to_s,
64
- queue: event.payload[:job].instance_variable_get(:@queue_name),
65
- executions: event.payload[:job].instance_variable_get(:@executions).to_s,
68
+ queue: event.payload[:job].queue_name,
69
+ executions: event.payload[:job].executions.to_s,
66
70
  }
67
71
 
68
72
  labels.merge!(event.payload.slice(*Yabeda.default_tags.keys - labels.keys))
69
- activejob_latency.measure(labels, Yabeda::ActiveJob.job_latency(event))
73
+ job_latency = Yabeda::ActiveJob.job_latency(event)
74
+ activejob_latency.measure(labels, job_latency) if job_latency.present?
75
+ Yabeda::ActiveJob.after_event_block.call(event) if Yabeda::ActiveJob.after_event_block.respond_to?(:call)
70
76
  end
71
77
 
72
78
  ActiveSupport::Notifications.subscribe "enqueue.active_job" do |*args|
@@ -74,19 +80,22 @@ module Yabeda
74
80
 
75
81
  labels = {
76
82
  activejob: event.payload[:job].class.to_s,
77
- queue: event.payload[:job].instance_variable_get(:@queue_name),
78
- executions: event.payload[:job].instance_variable_get(:@executions).to_s,
83
+ queue: event.payload[:job].queue_name,
84
+ executions: event.payload[:job].executions.to_s,
79
85
  }
80
86
 
81
87
  labels.merge!(event.payload.slice(*Yabeda.default_tags.keys - labels.keys))
82
88
  activejob_enqueued_total.increment(labels)
89
+ Yabeda::ActiveJob.after_event_block.call(event) if Yabeda::ActiveJob.after_event_block.respond_to?(:call)
83
90
  end
84
91
  end
85
92
  end
86
93
  # rubocop: enable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
87
94
 
88
95
  def self.job_latency(event)
89
- enqueue_time = event.payload[:job].instance_variable_get(:@enqueued_at)
96
+ enqueue_time = event.payload[:job].enqueued_at
97
+ return nil unless enqueue_time.present?
98
+
90
99
  enqueue_time = Time.parse(enqueue_time).utc
91
100
  perform_at_time = Time.parse(event.end.to_s).utc
92
101
  (perform_at_time - enqueue_time)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yabeda-activejob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fullscript
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-29 00:00:00.000000000 Z
11
+ date: 2022-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails