Module: Vault::Log

Defined in:
lib/vault-tools/log.rb

Class Method Summary collapse

Class Method Details

.count(name, value = 1, extra_data = {}) ⇒ Object

Log a count metric.

Parameters:

  • name (String)

    The name of the metric.

  • value (Integer) (defaults to: 1)

    The number of items counted. Can be suffixed with a unit.

  • extra_data (Hash) (defaults to: {})

    Optional extra data to log.



8
9
10
# File 'lib/vault-tools/log.rb', line 8

def self.count(name, value = 1, extra_data = {})
  log(extra_data.merge("count##{Config.app_name}.#{name}" => value))
end

.count_status(status) ⇒ Object

Log an HTTP status code. Two log metrics are written each time this method is invoked:

  • The first one emits a metric called http_200 for an HTTP 200 response.
  • The second one emits a metric called http_2xx for the same code.

This makes it possible to easily measure individual HTTP status codes as well as classes of HTTP status codes.

Parameters:

  • status (Fixnum)

    The HTTP status code to record.



23
24
25
26
27
28
# File 'lib/vault-tools/log.rb', line 23

def self.count_status(status)
  count("http.#{status}")
  if status_prefix = status.to_s.match(/\d/)[0]
    count("http.#{status_prefix}xx")
  end
end

.log(data, &block) ⇒ Object

Write a message with key/value pairs to the log stream.

Parameters:

  • data (Hash)

    A mapping of key/value pairs to include in the output.

  • block (Proc)

    Optionally, a block of code to run before writing the log message.



59
60
61
62
63
# File 'lib/vault-tools/log.rb', line 59

def self.log(data, &block)
  data['source'] ||= Config.app_deploy if Config.app_deploy
  data['app'] ||= Config.app_name if Config.app_name
  Scrolls.log(data, &block)
end

.measure(name, value, extra_data = {}) ⇒ Object

Log a measurement.

Parameters:

  • name (String)

    The name of the metric.

  • value (Float)

    Value for the metric. A unit may be appended.

  • extra_data (Hash) (defaults to: {})

    Optional extra data to log.



35
36
37
# File 'lib/vault-tools/log.rb', line 35

def self.measure(name, value, extra_data = {})
  log(extra_data.merge("measure##{Config.app_name}.#{name}" => value))
end

.time(name, duration) ⇒ Object

Log a timing metric.

Parameters:

  • name (String)

    A Sinatra-formatted route URL.

  • duration (Fixnum)

    The duration to record, in milliseconds.



43
44
45
46
47
48
49
50
51
# File 'lib/vault-tools/log.rb', line 43

def self.time(name, duration)
  if name
    name.gsub(/\/:\w+/, '').            # Remove param names from path.
         gsub(/[\/_]+/, "-").           # Replace slash with dash.
         gsub(/[^A-Za-z0-9\-\_]/, '').  # Only keep subset of chars.
         sub(/^-+/, "").                # Strip the leading dashes.
         tap { |name| measure(name, "#{duration}ms") }
  end
end