Module: SourceMonitor::Metrics
- Defined in:
- lib/source_monitor/metrics.rb
Class Method Summary collapse
- .counter(name) ⇒ Object
- .counters ⇒ Object
- .gauge(name, value) ⇒ Object
- .gauge_value(name) ⇒ Object
- .gauges ⇒ Object
- .increment(name, value = 1) ⇒ Object
- .reset! ⇒ Object
- .setup_subscribers! ⇒ Object
- .snapshot ⇒ Object
Class Method Details
.counter(name) ⇒ Object
17 18 19 |
# File 'lib/source_monitor/metrics.rb', line 17 def counter(name) counters[name.to_s] end |
.counters ⇒ Object
69 70 71 |
# File 'lib/source_monitor/metrics.rb', line 69 def counters @counters ||= Hash.new(0) end |
.gauge(name, value) ⇒ Object
13 14 15 |
# File 'lib/source_monitor/metrics.rb', line 13 def gauge(name, value) gauges[name.to_s] = value end |
.gauge_value(name) ⇒ Object
21 22 23 |
# File 'lib/source_monitor/metrics.rb', line 21 def gauge_value(name) gauges[name.to_s] end |
.gauges ⇒ Object
73 74 75 |
# File 'lib/source_monitor/metrics.rb', line 73 def gauges @gauges ||= {} end |
.increment(name, value = 1) ⇒ Object
9 10 11 |
# File 'lib/source_monitor/metrics.rb', line 9 def increment(name, value = 1) counters[name.to_s] += value end |
.reset! ⇒ Object
29 30 31 32 |
# File 'lib/source_monitor/metrics.rb', line 29 def reset! @counters = Hash.new(0) @gauges = {} end |
.setup_subscribers! ⇒ Object
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 |
# File 'lib/source_monitor/metrics.rb', line 34 def setup_subscribers! return if defined?(@subscribed) && @subscribed ActiveSupport::Notifications.subscribe("source_monitor.fetch.start") do |_name, _start, _finish, _id, payload| increment(:fetch_started_total) gauge(:last_fetch_source_id, payload[:source_id]) if payload.key?(:source_id) end ActiveSupport::Notifications.subscribe("source_monitor.fetch.finish") do |_name, start, finish, _id, payload| increment(:fetch_finished_total) success = payload.fetch(:success, true) if success increment(:fetch_success_total) else increment(:fetch_failure_total) end duration_ms = payload[:duration_ms] || ((finish - start) * 1000.0).round(2) gauge(:last_fetch_duration_ms, duration_ms) end ActiveSupport::Notifications.subscribe("source_monitor.scheduler.run") do |_name, start, finish, _id, payload| enqueued = payload[:enqueued_count].to_i duration_ms = payload[:duration_ms] || ((finish - start) * 1000.0).round(2) increment(:scheduler_runs_total) increment(:scheduler_sources_enqueued_total, enqueued) gauge(:scheduler_last_enqueued_count, enqueued) gauge(:scheduler_last_duration_ms, duration_ms) gauge(:scheduler_last_run_at_epoch, finish.to_f) end @subscribed = true end |
.snapshot ⇒ Object
25 26 27 |
# File 'lib/source_monitor/metrics.rb', line 25 def snapshot { counters: counters.dup, gauges: gauges.dup } end |