Class: Sidekiq::Metrics::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/metrics/query.rb

Overview

Allows caller to query for Sidekiq execution metrics within Redis. Caller sets a set of attributes to act as filters. #fetch will call Redis and return a Hash of results.

NB: all metrics and times/dates are UTC only. We specifically do not support timezones.

Defined Under Namespace

Classes: JobResult, MarkResult, Result

Instance Method Summary collapse

Constructor Details

#initialize(pool: nil, now: Time.now) ⇒ Query

Returns a new instance of Query.



18
19
20
21
22
# File 'lib/sidekiq/metrics/query.rb', line 18

def initialize(pool: nil, now: Time.now)
  @time = now.utc
  @pool = pool || Sidekiq.default_configuration.redis_pool
  @klass = nil
end

Instance Method Details

#for_job(klass, minutes: 60) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sidekiq/metrics/query.rb', line 56

def for_job(klass, minutes: 60)
  result = Result.new

  time = @time
  redis_results = @pool.with do |conn|
    conn.pipelined do |pipe|
      minutes.times do |idx|
        key = "j|#{time.strftime("%Y%m%d")}|#{time.hour}:#{time.min}"
        pipe.hmget key, "#{klass}|ms", "#{klass}|p", "#{klass}|f"
        result.prepend_bucket time
        time -= 60
      end
    end
  end

  time = @time
  @pool.with do |conn|
    redis_results.each do |(ms, p, f)|
      result.job_results[klass].add_metric "ms", time, ms.to_i if ms
      result.job_results[klass].add_metric "p", time, p.to_i if p
      result.job_results[klass].add_metric "f", time, f.to_i if f
      result.job_results[klass].add_hist time, Histogram.new(klass).fetch(conn, time).reverse
      time -= 60
    end
  end

  result.marks = fetch_marks(result.starts_at..result.ends_at)

  result
end

#top_jobs(class_filter: nil, minutes: 60) ⇒ Object

Get metric data for all jobs from the last hour

+class_filter+: return only results for classes matching filter


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sidekiq/metrics/query.rb', line 26

def top_jobs(class_filter: nil, minutes: 60)
  result = Result.new

  time = @time
  redis_results = @pool.with do |conn|
    conn.pipelined do |pipe|
      minutes.times do |idx|
        key = "j|#{time.strftime("%Y%m%d")}|#{time.hour}:#{time.min}"
        pipe.hgetall key
        result.prepend_bucket time
        time -= 60
      end
    end
  end

  time = @time
  redis_results.each do |hash|
    hash.each do |k, v|
      kls, metric = k.split("|")
      next if class_filter && !class_filter.match?(kls)
      result.job_results[kls].add_metric metric, time, v.to_i
    end
    time -= 60
  end

  result.marks = fetch_marks(result.starts_at..result.ends_at)

  result
end