Module: Binnacle::HttpLogger

Defined in:
lib/binnacle/http_logging/http_logger.rb

Class Method Summary collapse

Class Method Details

.allow?(url) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/binnacle/http_logging/http_logger.rb', line 34

def self.allow?(url)
  unless Binnacle.configuration.url_blacklist_pattern.nil?
    return false if url.to_s.match(Binnacle.configuration.url_blacklist_pattern)
  end

  !url.to_s.match(Binnacle.configuration.url_whitelist_pattern).nil?
end

.extract_body_data(body, encoding = nil, content_type = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/binnacle/http_logging/http_logger.rb', line 42

def self.extract_body_data(body, encoding = nil, content_type=nil)
  return unless text_based?(content_type)

  # open-uri wraps the response in a Net::ReadAdapter that defers reading
  # the content, so the reponse body is not available here.
  return if body.is_a?(Net::ReadAdapter)

  if encoding =~ /gzip/
    begin
      sio = StringIO.new( body.to_s )
      gz = Zlib::GzipReader.new( sio )
      body = gz.read
    rescue
      # nothing to see here, move along!
    end
  end

  utf_encoded(body.to_s, content_type)
end

.extract_data(data) ⇒ Object



62
63
64
# File 'lib/binnacle/http_logging/http_logger.rb', line 62

def self.extract_data(data)
  utf_encoded(data.to_s)
end

.signal(url, method, host, port, path, query, status, duration, headers = {}, body = nil, encoding = nil, content_type = nil, data = nil) ⇒ Object



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/binnacle/http_logging/http_logger.rb', line 8

def self.signal(url, method, host, port, path, query, status, duration, headers = {}, body = nil, encoding = nil, content_type = nil, data = nil)
  return if !self.allow?(url)

  bm = duration ? (duration * 1000) : 0.0

  signal_data = {
    direction: :out,
    url: url,
    method: method.to_s.upcase,
    host: host,
    port: port,
    path: path,
    query: query,
    format: content_type,
    time: Time.now,
    status: status,
    duration: bm,
    headers: headers,
    body: extract_body_data(body, encoding, content_type),
    data: extract_data(data),
    message: "#{method.to_s.upcase} #{url} AS #{content_type} (duration: #{bm}ms)"
  }

  Binnacle.client.log_http_event(signal_data) if Binnacle.client
end