Class: Talkbird::Instrumentation::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/talkbird/instrumentation/event.rb

Overview

General instrumentation event for an ActiveSupport notification.

Constant Summary collapse

NAMESPACE =
'sendbird'
START_EVENT =
'start_request.sendbird'
END_EVENT =
'request.sendbird'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Event

Array of expected params:

  • name

  • start_time

  • finish_time

  • id of the event

  • payload (simple hash with either :request or :response)



73
74
75
76
77
78
79
# File 'lib/talkbird/instrumentation/event.rb', line 73

def initialize(params)
  @name = params[0]
  @start_time = params[1]
  @finish_time = params[2]
  @id = params[3]
  @payload = params[4]
end

Class Method Details

.anonymize_app_id_from_url(url) ⇒ Object



59
60
61
62
63
# File 'lib/talkbird/instrumentation/event.rb', line 59

def anonymize_app_id_from_url(url)
  app_id = Talkbird::Client.application_id.downcase

  url.to_s.gsub(app_id, '...')
end

.debug?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/talkbird/instrumentation/event.rb', line 29

def debug?
  ENV.key?('SENDBIRD_DEBUG')
end

.normalized_request_data(payload) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/talkbird/instrumentation/event.rb', line 33

def normalized_request_data(payload)
  data = payload[:request]

  hsh = {
    type: 'request',
    method: data.verb.upcase,
    url: anonymize_app_id_from_url(data.uri)
  }

  hsh[:body] = data.body if Event.debug?
  hsh
end

.normalized_response_data(payload) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/talkbird/instrumentation/event.rb', line 46

def normalized_response_data(payload)
  data = payload[:response]

  hsh = {
    type: 'response',
    code: data.status,
    url: anonymize_app_id_from_url(data.uri)
  }

  hsh[:body] = data.body if Event.debug?
  hsh
end

.register_instrumentation_for_requestObject



15
16
17
18
19
20
# File 'lib/talkbird/instrumentation/event.rb', line 15

def register_instrumentation_for_request
  ActiveSupport::Notifications.subscribe(START_EVENT) do |*params|
    data = Talkbird::Instrumentation::Event.new(params)
    $stdout.puts data
  end
end

.register_instrumentation_for_responseObject



22
23
24
25
26
27
# File 'lib/talkbird/instrumentation/event.rb', line 22

def register_instrumentation_for_response
  ActiveSupport::Notifications.subscribe(END_EVENT) do |*params|
    data = Talkbird::Instrumentation::Event.new(params)
    $stdout.puts data
  end
end

Instance Method Details

#normalized_payloadObject



81
82
83
84
85
86
87
88
89
# File 'lib/talkbird/instrumentation/event.rb', line 81

def normalized_payload
  if @name == START_EVENT
    Event.normalized_request_data(@payload)
  elsif @name == END_EVENT
    Event.normalized_response_data(@payload)
  else
    {}
  end
end

#to_hObject



91
92
93
94
95
96
97
# File 'lib/talkbird/instrumentation/event.rb', line 91

def to_h
  data = normalized_payload

  {
    id: @id
  }.merge(data)
end

#to_sObject



99
100
101
# File 'lib/talkbird/instrumentation/event.rb', line 99

def to_s
  to_h.map { |name, val| "#{name}=#{val}" }.join(' ')
end