Module: RailsOrbit::Instrumentation

Defined in:
lib/rails_orbit/instrumentation.rb

Constant Summary collapse

SUBSCRIPTIONS =
[
  "enqueue.solid_queue",
  "perform.solid_queue",
  "failed_execution.solid_queue",
  "retry_execution.solid_queue",
  "discard_job.solid_queue",
  "cache_read.active_support",
  "cache_write.active_support",
  "cache_delete.active_support",
  "cache_fetch_hit.active_support",
  "error_recorded.solid_errors",
].freeze

Class Method Summary collapse

Class Method Details

.handle(event) ⇒ Object



37
38
39
40
41
# File 'lib/rails_orbit/instrumentation.rb', line 37

def handle(event)
  key, value, dimension = translate(event)
  return unless key
  MetricWriter.write(key: key, value: value, dimension: dimension)
end

.subscribe!Object



17
18
19
20
21
22
23
24
# File 'lib/rails_orbit/instrumentation.rb', line 17

def subscribe!
  unsubscribe! if @subscriptions
  @subscriptions = SUBSCRIPTIONS.map do |event_name|
    ActiveSupport::Notifications.subscribe(event_name) do |event|
      handle(event)
    end
  end
end

.subscribed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rails_orbit/instrumentation.rb', line 33

def subscribed?
  @subscriptions.present?
end

.translate(event) ⇒ Object



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
# File 'lib/rails_orbit/instrumentation.rb', line 43

def translate(event)
  case event.name
  when "enqueue.solid_queue"
    ["solid_queue.enqueued", 1, event.payload[:queue_name]]

  when "perform.solid_queue"
    duration_ms = event.duration.round(2)
    ["solid_queue.performed_ms", duration_ms, event.payload[:queue_name]]

  when "failed_execution.solid_queue"
    ["solid_queue.failed", 1, event.payload[:queue_name]]

  when "retry_execution.solid_queue"
    ["solid_queue.retried", 1, event.payload[:queue_name]]

  when "discard_job.solid_queue"
    ["solid_queue.discarded", 1, event.payload[:queue_name]]

  when "cache_read.active_support"
    hit = event.payload[:hit] ? "hit" : "miss"
    ["solid_cache.read_#{hit}", 1, event.payload[:store]]

  when "cache_write.active_support"
    ["solid_cache.write", 1, event.payload[:store]]

  when "cache_delete.active_support"
    ["solid_cache.delete", 1, event.payload[:store]]

  when "cache_fetch_hit.active_support"
    ["solid_cache.fetch_hit", 1, event.payload[:store]]

  when "error_recorded.solid_errors"
    ["solid_errors.recorded", 1, event.payload[:exception_class]]

  else
    nil
  end
end

.unsubscribe!Object



26
27
28
29
30
31
# File 'lib/rails_orbit/instrumentation.rb', line 26

def unsubscribe!
  Array(@subscriptions).each do |sub|
    ActiveSupport::Notifications.unsubscribe(sub)
  end
  @subscriptions = nil
end