From 50d0b3edff18a1a06971d08ba5143b3dcb84634d Mon Sep 17 00:00:00 2001 From: Janos Laszlo Vasik Date: Fri, 13 Mar 2026 09:35:26 +0100 Subject: [PATCH 1/3] Refactor handle_perform to use common_labels * Utilize #common_labels in EventHandler#handle_perform --- lib/yabeda/activejob/event_handler.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/yabeda/activejob/event_handler.rb b/lib/yabeda/activejob/event_handler.rb index 769c0f5..f79232c 100644 --- a/lib/yabeda/activejob/event_handler.rb +++ b/lib/yabeda/activejob/event_handler.rb @@ -8,11 +8,8 @@ def initialize(*event_args) end def handle_perform - labels = { - activejob: event.payload[:job].class.to_s, - queue: event.payload[:job].queue_name.to_s, - executions: event.payload[:job].executions.to_s, - } + labels = common_labels(event.payload[:job]) + if event.payload[:exception].present? Yabeda.activejob_failed_total.increment( labels.merge(failure_reason: event.payload[:exception].first.to_s), From 0a677334e2fd2fae6cbea9331ff8e51b0fda9d68 Mon Sep 17 00:00:00 2001 From: Janos Laszlo Vasik Date: Fri, 13 Mar 2026 10:00:48 +0100 Subject: [PATCH 2/3] Add support for custom tags * Add support for custom tags * Test custom tagging --- lib/yabeda/activejob.rb | 10 ++++++++++ lib/yabeda/activejob/event_handler.rb | 2 +- spec/support/rails_app.rb | 20 ++++++++++++++++++++ spec/yabeda/activejob_spec.rb | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/yabeda/activejob.rb b/lib/yabeda/activejob.rb index 51ff0d1..09ac1c9 100644 --- a/lib/yabeda/activejob.rb +++ b/lib/yabeda/activejob.rb @@ -15,6 +15,16 @@ module ActiveJob mattr_accessor :after_event_block, default: proc { |_event| } + def self.custom_tags(job) + return {} unless job.respond_to?(:yabeda_tags) + + if job.method(:yabeda_tags).arity.zero? + job.yabeda_tags + else + job.yabeda_tags(*job.arguments) + end + end + # rubocop: disable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize def self.install! Yabeda.configure do diff --git a/lib/yabeda/activejob/event_handler.rb b/lib/yabeda/activejob/event_handler.rb index f79232c..72afdaa 100644 --- a/lib/yabeda/activejob/event_handler.rb +++ b/lib/yabeda/activejob/event_handler.rb @@ -96,7 +96,7 @@ def common_labels(job) activejob: job.class.to_s, queue: job.queue_name, executions: job.executions.to_s, - ) + ).merge(Yabeda::ActiveJob.custom_tags(job)) end def call_after_event_block diff --git a/spec/support/rails_app.rb b/spec/support/rails_app.rb index d8e2ad0..4338863 100644 --- a/spec/support/rails_app.rb +++ b/spec/support/rails_app.rb @@ -38,6 +38,26 @@ def perform end end +class TaggedJob < ActiveJob::Base + def perform(tenant_id) + puts "Tagged job for #{tenant_id}" + end + + def yabeda_tags + { tenant: arguments.first } + end +end + +class TaggedArgsErrorJob < ActiveJob::Base + def perform(tenant_id) + raise StandardError + end + + def yabeda_tags(tenant_id) + { tenant: tenant_id } + end +end + Rails.application = TestApplication TestApplication.initialize! diff --git a/spec/yabeda/activejob_spec.rb b/spec/yabeda/activejob_spec.rb index 17233fc..d85f6db 100644 --- a/spec/yabeda/activejob_spec.rb +++ b/spec/yabeda/activejob_spec.rb @@ -244,4 +244,25 @@ expect(Yabeda.activejob.scheduled_total.values.values.sum).to eq(jobs_count) end end + + context "when job defines yabeda_tags" do + it "includes zero-arity custom tags in perform metrics" do + expect { TaggedJob.perform_later("tenant_1") }.to \ + increment_yabeda_counter(Yabeda.activejob.success_total) + .with_tags(queue: "default", activejob: "TaggedJob", executions: "1", tenant: "tenant_1") + .by(1) + end + + it "includes argument-forwarded custom tags in failed metrics" do + expect { TaggedArgsErrorJob.perform_later("tenant_1") }.to \ + increment_yabeda_counter(Yabeda.activejob.failed_total) + .with_tags( + queue: "default", + activejob: "TaggedArgsErrorJob", + executions: "1", + failure_reason: "StandardError", + tenant: "tenant_1", + ).by(1).and(raise_error(StandardError)) + end + end end From 1894d97a925dacdb930e9cfec1846a27234f4159 Mon Sep 17 00:00:00 2001 From: Janos Laszlo Vasik Date: Fri, 13 Mar 2026 10:33:42 +0100 Subject: [PATCH 3/3] Document custom tagging * Describe usage of custom per-job tags in README --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index ae69617..cb0e3a9 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,38 @@ end **Note**: Since the notifications are registered on install make sure to setup your after_event_block before calling install! +## Custom Tags + +You can add custom tags to all metrics emitted for a specific job by defining a `yabeda_tags` instance method on your job class. This works similarly to [yabeda-sidekiq's custom tags](https://github.com/yabeda-rb/yabeda-sidekiq#custom-tags). + +```ruby +class ImportJob < ActiveJob::Base + def perform(tenant_id, data) + # ... + end + + def yabeda_tags + { tenant: arguments.first } + end +end +``` + +The returned hash is merged into every metric's label set for that job (enqueued, executed, success, failed, runtime, latency). + +If your `yabeda_tags` method accepts arguments, the job's arguments will be forwarded to it: + +```ruby +class ImportJob < ActiveJob::Base + def perform(tenant_id, data) + # ... + end + + def yabeda_tags(tenant_id, _data) + { tenant: tenant_id } + end +end +``` + ## Metrics - Total enqueued jobs: `activejob.enqueued_total` segmented by: queue, activejob(job class name), executions(number of executions)