Method: Sidekiq::Metrics::ExecutionTracker#flush

Defined in:
lib/sidekiq/metrics/tracking.rb

#flush(time = Time.now) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sidekiq/metrics/tracking.rb', line 57

def flush(time = Time.now)
  totals, jobs, grams = reset
  procd = totals["p"]
  fails = totals["f"]
  return if procd == 0 && fails == 0

  now = time.utc
  # nowdate = now.strftime("%Y%m%d")
  # "250214|8:4" is the 10 minute bucket for Feb 14 2025, 08:43
  nowmid = now.strftime("%y%m%d|%-H:%M")[0..-2]
  # "250214|8:43" is the 1 minute bucket for Feb 14 2025, 08:43
  nowshort = now.strftime("%y%m%d|%-H:%M")
  count = 0

  redis do |conn|
    # persist fine-grained histogram data
    if grams.size > 0
      conn.pipelined do |pipe|
        grams.each do |_, gram|
          gram.persist(pipe, now)
        end
      end
    end

    # persist coarse grained execution count + execution millis.
    # note as of today we don't use or do anything with the
    # daily or hourly rollups.
    [
      # ["j", jobs, nowdate, LONG_TERM],
      ["j", jobs, nowmid, MID_TERM],
      ["j", jobs, nowshort, SHORT_TERM]
    ].each do |prefix, data, bucket, ttl|
      conn.pipelined do |xa|
        stats = "#{prefix}|#{bucket}"
        data.each_pair do |key, value|
          xa.hincrby stats, key, value
          count += 1
        end
        xa.expire(stats, ttl)
      end
    end
    logger.debug "Flushed #{count} metrics"
    count
  end
end