Class: ZuoraConnect::PageRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/middleware/metrics_middleware.rb

Overview

Object of this class is passed to the ActiveSupport::Notification hook

Instance Method Summary collapse

Instance Method Details

#call(name, started, finished, unique_id, payload) ⇒ Object

This method is triggered when a non error page is loaded (not 404)



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
# File 'lib/middleware/metrics_middleware.rb', line 8

def call(name, started, finished, unique_id, payload)
  # If the url contains any css or JavaScript files then do not collect metrics for them
  return nil if ["css", "assets", "jpg", "png", "jpeg", "ico"].any? { |word| payload[:path].include?(word) }

  # Getting the endpoint and the content_type
  content_hash = {:html => "text/html", :js => "application/javascript", :json => "application/json", :csv => "text/csv"}
  content_type = content_hash.key?(payload[:format]) ? content_hash[payload[:format]] : payload[:format]
  content_type = content_type.to_s.gsub('text/javascript', 'application/javascript')

  # payloads with 500 requests do not have status as it is not set by the controller
  # https://github.com/rails/rails/issues/33335
  #status_code = payload[:status] ? payload[:status] : payload[:exception_object].present? ? 500 : ""
  if payload[:exception].present?
    status_code, exception = [500, payload[:exception].first]
  else
    status_code, exception = [payload[:status], nil]
  end

  tags = {method: payload[:method], status: status_code, error_type: exception, content_type: content_type, controller: payload[:controller], action: payload[:action]}.compact

  values = {view_time: payload[:view_runtime], db_time: payload[:db_runtime], response_time: ((finished-started)*1000)}.compact
  values = values.map{ |k,v| [k,v.round(2)]}.to_h

  ZuoraConnect::AppInstanceBase.write_to_telegraf(direction: :inbound, tags: tags, values: values)
end