Class: HttpLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/metrics/net.rb

Overview

Usage:

require 'http_logger'

Setup logger

HttpLogger.logger = Logger.new('/tmp/all.log')
HttpLogger.log_headers = true

Do request

res = Net::HTTP.start(url.host, url.port) { |http|
  http.request(req)
}
...

View the log

cat /tmp/all.log

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.collapse_body_limitObject

Returns the value of attribute collapse_body_limit.



26
27
28
# File 'lib/metrics/net.rb', line 26

def collapse_body_limit
  @collapse_body_limit
end

.colorizeObject

Returns the value of attribute colorize.



31
32
33
# File 'lib/metrics/net.rb', line 31

def colorize
  @colorize
end

.ignoreObject

Returns the value of attribute ignore.



32
33
34
# File 'lib/metrics/net.rb', line 32

def ignore
  @ignore
end

.levelObject

Returns the value of attribute level.



33
34
35
# File 'lib/metrics/net.rb', line 33

def level
  @level
end

.log_headersObject

Returns the value of attribute log_headers.



27
28
29
# File 'lib/metrics/net.rb', line 27

def log_headers
  @log_headers
end

.log_request_bodyObject

Returns the value of attribute log_request_body.



28
29
30
# File 'lib/metrics/net.rb', line 28

def log_request_body
  @log_request_body
end

.log_response_bodyObject

Returns the value of attribute log_response_body.



29
30
31
# File 'lib/metrics/net.rb', line 29

def log_response_body
  @log_response_body
end

.loggerObject

Returns the value of attribute logger.



30
31
32
# File 'lib/metrics/net.rb', line 30

def logger
  @logger
end

Class Method Details

.deprecate_config(option) ⇒ Object



52
53
54
# File 'lib/metrics/net.rb', line 52

def self.deprecate_config(option)
  warn "Net::HTTP.#{option} is deprecated. Use HttpLogger.#{option} instead."
end

.instanceObject



48
49
50
# File 'lib/metrics/net.rb', line 48

def self.instance
  @instance ||= HttpLogger.new
end

.perform(*args, &block) ⇒ Object



44
45
46
# File 'lib/metrics/net.rb', line 44

def self.perform(*args, &block)
  instance.perform(*args, &block)
end

Instance Method Details

#perform(http, request, request_body) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metrics/net.rb', line 56

def perform(http, request, request_body)
  start_time = Time.now
  tags = {}
  response = yield
rescue => ex
  tags = tags.merge({error_type: ex.class})
  raise
ensure
  values = {response_time: ((Time.now - start_time)*1000).round(2)}
  if require_logging?(http, request)
    tags = tags.merge({endpoint: http.address, method: request.method})
    log_request_url(http, request, start_time)
    log_request_body(request)
    log_request_headers(request)
    if defined?(response) && response
      tags = tags.merge({status: response.code.to_i}) 
      log_response_code(response)
      log_response_headers(response)
      log_response_body(response.body)
    end
    ZuoraConnect::AppInstance.write_to_telegraf(direction: :outbound, tags: tags, values: values)
  end
end