Class: PriceHubble::Instrumentation::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/price_hubble/instrumentation/log_subscriber.rb

Overview

Produce logs for requests.

Instance Method Summary collapse

Instance Method Details

#color_method(method) ⇒ String

Decide which color to use for the given HTTP/client method.

Parameters:

  • method (String)

    the method to inspect

Returns:

  • (String)

    the ANSI color code



131
132
133
134
135
136
137
138
139
140
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 131

def color_method(method)
  case method
  when /delete/i then RED
  when /get|search|reload|find/i then BLUE
  when /post|create/i then GREEN
  when /put|patch|update/i then YELLOW
  when /login|logout|download|query/i then CYAN
  else MAGENTA
  end
end

#color_status(status) ⇒ String

Decide which color to use for the given HTTP status code.

Parameters:

  • status (Integer)

    the HTTP status code

Returns:

  • (String)

    the ANSI color code



117
118
119
120
121
122
123
124
125
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 117

def color_status(status)
  case status
  when 0..199 then MAGENTA
  when 200..299 then GREEN
  when 300..399 then YELLOW
  when 400..599 then RED
  else WHITE
  end
end

#log_action_summary(event) ⇒ Object

Print some top-level request/action details.

Parameters:

  • event (ActiveSupport::Notifications::Event)

    the subscribed event



29
30
31
32
33
34
35
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 29

def log_action_summary(event)
  env = event.payload
  info do
    "[#{req_id(env)}] #{req_origin(env)} -> #{res_result(env)} " \
      "(#{event.duration.round(1)}ms)"
  end
end

#log_request_details(event) ⇒ Object

Print details about the request.

Parameters:

  • event (ActiveSupport::Notifications::Event)

    the subscribed event



40
41
42
43
44
45
46
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 40

def log_request_details(event)
  env = event.payload
  debug do
    "[#{req_id(env)}] #{req_dest(env)} > " \
      "#{env.request_headers.sort.to_h.to_json}"
  end
end

#log_response_details(event) ⇒ Object

When no response is available (due to timeout, DNS resolve issues, etc) we just can log an error without details. Otherwise print details about the response.

Parameters:

  • event (ActiveSupport::Notifications::Event)

    the subscribed event



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 53

def log_response_details(event)
  env = event.payload

  if env.response.nil?
    return error do
      "[#{req_id(env)}] #{req_dest(env)} < #{res_result(env)}"
    end
  end

  debug do
    "[#{req_id(env)}] #{req_dest(env)} < " \
      "#{env.response_headers.sort.to_h.to_json}"
  end
end

#loggerLogger?

Return the PriceHubble SDK configured logger when logging is enabled. Otherwise nil is returned and the subscriber is never started.

Returns:

  • (Logger, nil)

    the logger to use



11
12
13
14
15
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 11

def logger
  return unless PriceHubble.configuration.request_logging

  PriceHubble.configuration.logger
end

#req_dest(env) ⇒ String

Format the request destination.

Parameters:

  • env (Faraday::Env)

    the request/response environment

Returns:

  • (String)

    the request identifier



92
93
94
95
96
97
98
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 92

def req_dest(env)
  method = env[:method].to_s.upcase
  method = color(method, color_method(method), bold: true)
  url = env[:url].to_s.gsub(/access_token=[^&]+/,
                            'access_token=[FILTERED]')
  "#{method} #{url}"
end

#req_id(env) ⇒ String

Format the request identifier.

Parameters:

  • env (Faraday::Env)

    the request/response environment

Returns:

  • (String)

    the request identifier



72
73
74
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 72

def req_id(env)
  env.request.context[:request_id].to_s
end

#req_origin(env) ⇒ String

Format the request/action origin.

Parameters:

  • env (Faraday::Env)

    the request/response environment

Returns:

  • (String)

    the request identifier



80
81
82
83
84
85
86
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 80

def req_origin(env)
  req = env.request.context
  action = req[:action]
  action = color(action, color_method(action), bold: true)
  client = req[:client].to_s.gsub('PriceHubble::Client', 'PriceHubble')
  "#{client.underscore}##{action}"
end

#request(event) ⇒ Object

Log request statistics and debugging details.

Parameters:

  • event (ActiveSupport::Notifications::Event)

    the subscribed event



20
21
22
23
24
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 20

def request(event)
  log_action_summary(event)
  log_request_details(event)
  log_response_details(event)
end

#res_result(env) ⇒ String

Format the request result.

Parameters:

  • env (Faraday::Env)

    the request/response environment

Returns:

  • (String)

    the request identifier



104
105
106
107
108
109
110
111
# File 'lib/price_hubble/instrumentation/log_subscriber.rb', line 104

def res_result(env)
  return color('no response', RED, bold: true).to_s if env.response.nil?

  status = env[:status]
  color("#{status}/#{env[:reason_phrase]}",
        color_status(status),
        bold: true)
end