-
Notifications
You must be signed in to change notification settings - Fork 3
/
periodic_reporter.rb
90 lines (77 loc) Β· 2.32 KB
/
periodic_reporter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55
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
86
87
88
89
90
require "sidekiq/api"
module Honeykiq
class PeriodicReporter
def initialize(libhoney: nil, honey_client: nil)
@libhoney = libhoney || honey_client || Honeycomb.libhoney
end
def report(&extra)
send_instance_event(&extra)
Sidekiq::ProcessSet.new.each { |process| send_process_event(process, &extra) }
Sidekiq::Queue.all.each { |queue| send_queue_event(queue, &extra) }
end
private
attr_reader :libhoney
def send_instance_event(&extra)
libhoney.event.add(
type: :instance,
**instance_stats,
**redis_stats,
**(extra&.call(:instance) || {})
).send
end
def instance_stats
stats = Sidekiq::Stats.new
{
"instance.processes": stats.processes_size,
"instance.busy": stats.workers_size,
"instance.enqueued": stats.enqueued,
"instance.scheduled": stats.scheduled_size,
"instance.retries": stats.retry_size,
"instance.dead": stats.dead_size
}
end
def redis_stats
redis_info = fetch_redis_info
{
"redis.connections": redis_info["connected_clients"].to_i,
"redis.memory_used": redis_info["used_memory"].to_i
}
end
def fetch_redis_info
Sidekiq.redis do |redis|
redis.pipelined { |redis|
redis.call "INFO", "clients"
redis.call "INFO", "memory"
}
.map(&method(:hashify_info))
.reduce(&:merge)
end
end
def hashify_info(reply)
# based on: https://github.com/redis/redis-rb/blob/master/lib/redis/commands.rb
lines = reply.split("\r\n").grep_v(/^(#|$)/)
lines.map! { |line| line.split(":", 2) }
lines.compact!
lines.to_h
end
def send_process_event(process, &extra)
libhoney.event.add(
type: :process,
"meta.dyno": process["hostname"],
"meta.process_id": process["pid"],
"process.concurrency": process["concurrency"],
"process.busy": process["busy"],
**(extra&.call(:process, process) || {})
).send
end
def send_queue_event(queue, &extra)
libhoney.event.add(
type: :queue,
"queue.name": queue.name,
"queue.latency_sec": queue.latency.to_f,
"queue.size": queue.size,
**(extra&.call(:queue, queue) || {})
).send
end
end
end