Skip to content

Commit

Permalink
0.7.0: Apply tags from yabeda_tags to all metrics inside a job
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Aug 21, 2020
1 parent 302157f commit 328bb09
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.7.0 - 2020-07-15

### Changed

- Tags from `yabeda_tags` method are applied to all metrics collected inside a job, not only sidekiq-specific. See [#14](https://github.com/yabeda-rb/yabeda-sidekiq/issues/14). @Envek

## 0.6.0 - 2020-07-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module Sidekiq

class << self
def labelize(worker, job, queue)
{ queue: queue, worker: worker_class(worker, job), **custom_tags(worker, job).to_h }
{ queue: queue, worker: worker_class(worker, job) }
end

def worker_class(worker, job)
Expand Down
9 changes: 7 additions & 2 deletions lib/yabeda/sidekiq/server_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ module Yabeda
module Sidekiq
# Sidekiq worker middleware
class ServerMiddleware
# rubocop: disable Metrics/AbcSize, Metrics/MethodLength:
def call(worker, job, queue)
labels = Yabeda::Sidekiq.labelize(worker, job, queue)
custom_tags = Yabeda::Sidekiq.custom_tags(worker, job).to_h
labels = Yabeda::Sidekiq.labelize(worker, job, queue).merge(custom_tags)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
job_instance = ::Sidekiq::Job.new(job)
Yabeda.sidekiq_job_latency.measure(labels, job_instance.latency)
yield
Yabeda.with_tags(**custom_tags) do
yield
end
Yabeda.sidekiq_jobs_success_total.increment(labels)
rescue Exception # rubocop: disable Lint/RescueException
Yabeda.sidekiq_jobs_failed_total.increment(labels)
Expand All @@ -20,6 +24,7 @@ def call(worker, job, queue)
Yabeda.sidekiq_jobs_executed_total.increment(labels)
end
end
# rubocop: enable Metrics/AbcSize, Metrics/MethodLength:

private

Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/sidekiq/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Yabeda
module Sidekiq
VERSION = "0.6.0"
VERSION = "0.7.0"
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "active_job/queue_adapters/sidekiq_adapter"
require "pry"

require_relative "support/custom_metrics"
require_relative "support/jobs"
require_relative "support/sidekiq_inline_middlewares"

Expand Down
7 changes: 7 additions & 0 deletions spec/support/custom_metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

Yabeda.configure do
group :test do
counter :whatever
end
end
13 changes: 13 additions & 0 deletions spec/support/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def perform(*_args)
end
end

class SampleComplexJob
include Sidekiq::Worker

def perform(*_args)
Yabeda.test.whatever.increment({ explicit: true })
"My job is complex"
end

def yabeda_tags
{ implicit: true }
end
end

class FailingPlainJob
include Sidekiq::Worker

Expand Down
22 changes: 22 additions & 0 deletions spec/yabeda/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,26 @@
)
end
end

describe "#yabeda_tags worker method" do
it "uses custom labels for both sidekiq and application metrics" do
Yabeda.sidekiq.jobs_executed_total.values.clear # This is a hack
Yabeda.sidekiq.job_runtime.values.clear # This is a hack also
Yabeda.test.whatever.values.clear # And this

Sidekiq::Testing.inline! do
SampleComplexJob.perform_async
end

expect(Yabeda.sidekiq.jobs_executed_total.values).to eq(
{ queue: "default", worker: "SampleComplexJob", implicit: true } => 1,
)
expect(Yabeda.sidekiq.job_runtime.values).to include(
{ queue: "default", worker: "SampleComplexJob", implicit: true } => kind_of(Numeric),
)
expect(Yabeda.test.whatever.values).to include(
{ explicit: true, implicit: true } => 1,
)
end
end
end
2 changes: 1 addition & 1 deletion yabeda-sidekiq.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "sidekiq"
spec.add_dependency "yabeda", "~> 0.2"
spec.add_dependency "yabeda", "~> 0.6"

spec.add_development_dependency "activejob", ">= 6.0"
spec.add_development_dependency "bundler", "~> 2.0"
Expand Down

0 comments on commit 328bb09

Please sign in to comment.