yabeda-activejob 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75adb58471b09079b2c7137a9e6d245ca34a87f5943bddbcca8488aa8a7fed21
4
+ data.tar.gz: 4eaff38047f48d812c9ec216520d1d180e04ee0a1c28fed56a74af3dff1cb31d
5
+ SHA512:
6
+ metadata.gz: 722e179fb2e42ea0724f83fa58e4f078419ea6b213570ba651abbde64f79516999fd8de3b10c8ff6adb79191bacaf5d4a7b17c1aef360ce400be5ecae8ba5703
7
+ data.tar.gz: 1b53949b9e40c84df03d36065be01efa4b321b9f9e261f9dd3b4b774a0cfd1cdd53ede740fb2a1b2b06537991f1cb9c1350cee8bf2723ecc9b2ff3ada67774bd
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Josh Etsenake
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Yabeda::ActiveJob
2
+ Yabeda metrics around rails activejobs. The motivation came from wanting something similar to [yabeda-sidekiq](https://github.com/yabeda-rb/yabeda-sidekiq) for
3
+ resque but decided to generalize even more with just doing it on the activejob level since that is likely more in use
4
+ than just resque. and could implement a lot of the general metrics needed without having to leverage your used adapters
5
+ implementation and oh the redis calls.
6
+ The intent is to have this plugin with an exporter such as [prometheus](https://github.com/yabeda-rb/yabeda-prometheus).
7
+
8
+ ## Usage
9
+ How to use my plugin.
10
+
11
+ ## Installation
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'yabeda-activejob'
16
+ # Then add monitoring system adapter, e.g.:
17
+ # gem 'yabeda-prometheus'
18
+ ```
19
+
20
+ ### Registering metrics on server process start
21
+
22
+ Currently, yabeda-activejob does not automatically install on your rails server (this will be added in the future). For now to install
23
+ you can do the following:
24
+ ```ruby
25
+ # config/initializers/yabeda.rb
26
+ Yabeda::ActiveJob.install!
27
+ ```
28
+
29
+ ## Metrics
30
+
31
+ - Total jobs processed: `activejob.job_executed_total`
32
+ - Total successful jobs processed: `activejob.job_success_total`
33
+ - Total failed jobs processed: `activejob.job_failed_total`
34
+ - Job runtime: `activejob.job_runtime` (in seconds)
35
+ - Job latency: `activejob.job_latency` (in seconds)
36
+
37
+ ## License
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+
5
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
6
+ load "rails/tasks/engine.rake"
7
+
8
+ load "rails/tasks/statistics.rake"
9
+
10
+ require "bundler/gem_tasks"
11
+
12
+ require "rake/testtask"
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << "test"
16
+ t.pattern = "test/**/*_test.rb"
17
+ t.verbose = false
18
+ end
19
+
20
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :yabeda_activejob do
4
+ # # Task goes here
5
+ # end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module ActiveJob
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yabeda"
4
+ require "yabeda/activejob/version"
5
+
6
+ module Yabeda
7
+ # Small set of metrics on activejob jobs
8
+ module ActiveJob
9
+ LONG_RUNNING_JOB_RUNTIME_BUCKETS = [
10
+ 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, # standard (from Prometheus)
11
+ 30, 60, 120, 300, 1800, 3600, 21_600, # In cases jobs are very long-running
12
+ ].freeze
13
+
14
+ # rubocop: disable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
15
+ def self.install!
16
+ Yabeda.configure do
17
+ group :activejob
18
+
19
+ counter :job_executed_total, tags: %i[queue activejob executions],
20
+ comment: "A counter of the total number of activejobs executed."
21
+ counter :job_success_total, tags: %i[queue activejob executions],
22
+ comment: "A counter of the total number of activejobs successfully processed."
23
+ counter :job_failed_total, tags: %i[queue activejob executions failure_reason],
24
+ comment: "A counter of the total number of jobs failed for an activejob."
25
+
26
+ histogram :job_runtime, comment: "A histogram of the activejob execution time.",
27
+ unit: :seconds, per: :activejob,
28
+ tags: %i[queue activejob executions],
29
+ buckets: LONG_RUNNING_JOB_RUNTIME_BUCKETS
30
+
31
+ histogram :job_latency, comment: "The job latency, the difference in seconds between enqueued and running time",
32
+ unit: :seconds, per: :activejob,
33
+ tags: %i[queue activejob executions],
34
+ buckets: LONG_RUNNING_JOB_RUNTIME_BUCKETS
35
+
36
+ # job complete event
37
+ ActiveSupport::Notifications.subscribe "perform.active_job" do |*args|
38
+ ::Rails.logger.debug("JOB COMPLETE")
39
+
40
+ event = ActiveSupport::Notifications::Event.new(*args)
41
+ labels = {
42
+ 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,
45
+ }
46
+ if event.payload[:exception].present?
47
+ activejob_job_failed_total.increment(
48
+ labels.merge(failure_reason: event.payload[:exception].join(",")),
49
+ )
50
+ else
51
+ activejob_job_success_total.increment(labels)
52
+ end
53
+
54
+ activejob_job_executed_total.increment(labels)
55
+ activejob_job_runtime.measure(labels, Yabeda::ActiveJob.ms2s(event.duration))
56
+ end
57
+
58
+ # start job event
59
+ ActiveSupport::Notifications.subscribe "perform_start.active_job" do |*args|
60
+ event = ActiveSupport::Notifications::Event.new(*args)
61
+ ::Rails.logger.debug("JOB START")
62
+ puts "JOB START"
63
+
64
+ labels = {
65
+ activejob: event.payload[:job].class.to_s,
66
+ queue: event.payload[:job].instance_variable_get(:@queue_name),
67
+ executions: event.payload[:job].instance_variable_get(:@executions).to_s,
68
+ }
69
+ ::Rails.logger.info(labels.inspect)
70
+
71
+ labels.merge!(event.payload.slice(*Yabeda.default_tags.keys - labels.keys))
72
+ activejob_job_latency.measure(labels, Yabeda::ActiveJob.job_latency(event))
73
+ end
74
+ end
75
+ end
76
+ # rubocop: enable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
77
+
78
+ def self.job_latency(event)
79
+ enqueue_time = event.payload[:job].instance_variable_get(:@enqueued_at)
80
+ enqueue_time = Time.parse(enqueue_time).utc
81
+ perform_at_time = Time.parse(event.end.to_s).utc
82
+ (perform_at_time - enqueue_time)
83
+ end
84
+
85
+ def self.ms2s(milliseconds)
86
+ (milliseconds.to_f / 1000).round(3)
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda-activejob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fullscript
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yabeda
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '6.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '6.0'
97
+ description: Prometheus exporter for collecting metrics around your activejobs
98
+ email:
99
+ - josh.etsenake@fullscript.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - MIT-LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - lib/tasks/yabeda/activejob_tasks.rake
108
+ - lib/yabeda/activejob.rb
109
+ - lib/yabeda/activejob/version.rb
110
+ homepage: https://github.com/Fullscript/yabeda-activejob
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ homepage_uri: https://github.com/Fullscript/yabeda-activejob
115
+ source_code_uri: https://github.com/Fullscript/yabeda-activejob
116
+ changelog_uri: https://github.com/Fullscript/yabeda-activejob/CHANGELOG.md
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '2.5'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.1.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Yabeda Prometheus exporter for monitoring your activejobs
136
+ test_files: []