Module: JSONAPI::Instrumentation

Defined in:
lib/json_api/support/instrumentation.rb

Class Method Summary collapse

Class Method Details

.enabled?Boolean



5
6
7
# File 'lib/json_api/support/instrumentation.rb', line 5

def self.enabled?
  defined?(Rails) && Rails.respond_to?(:event) && Rails.event.respond_to?(:notify)
end

.relationship_event(action:, resource_type:, resource_id:, relationship_name:, related_ids: nil, related_type: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/json_api/support/instrumentation.rb', line 22

def self.relationship_event(action:, resource_type:, resource_id:, relationship_name:, related_ids: nil,
                            related_type: nil)
  return unless enabled?

  payload = {
    resource_type:,
    resource_id:,
    relationship_name:
  }
  payload[:related_type] = related_type if related_type
  payload[:related_ids] = Array(related_ids) if related_ids

  Rails.event.tagged("jsonapi", "relationship") do
    Rails.event.notify(
      "jsonapi.#{resource_type}.relationship.#{action}",
      payload
    )
  end
end

.resource_event(action:, resource_type:, resource_id:, changes: {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/json_api/support/instrumentation.rb', line 9

def self.resource_event(action:, resource_type:, resource_id:, changes: {})
  return unless enabled?

  Rails.event.tagged("jsonapi") do
    Rails.event.notify(
      "jsonapi.#{resource_type}.#{action}",
      resource_type:,
      resource_id:,
      changes: changes.compact
    )
  end
end