class Startback::Audit::Prometheus

Prometheus exporter abstraction, that can be registered as an around hook on OperationRunner and as a prometheus client on Context instances.

The exporter uses the ruby client for prometheus to expose metrics regarding Operation runs.

The following metrics are exported:

A counter 'operation_errors' (failed runs) A histogram 'operation_calls'

All these metrics use the following labels

Given that this Exporter is intended to be used as around hook on an `OperationRunner`, operations that fail at construction time will not be exported at all, since they can't be ran in the first place. This may lead to metrics not containing important errors cases if operations check their input at construction time.

Attributes

calls[R]
errors[R]
options[R]
prefix[R]
registry[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/startback/audit/prometheus.rb, line 27
def initialize(options = {})
  @prefix = options[:prefix] || "startback"
  @options = options
  @registry = ::Prometheus::Client.registry
  all_labels = [:operation, :startback_version] + option_labels.keys
  @errors = @registry.counter(
    :"#{prefix}_operation_errors",
    docstring: 'A counter of operation errors',
    labels: all_labels)
  @calls = @registry.histogram(
    :"#{prefix}_operation_calls",
    docstring: 'A histogram of operation latency',
    labels: all_labels)
end

Public Instance Methods

call(runner, op) { || ... } click to toggle source
# File lib/startback/audit/prometheus.rb, line 43
def call(runner, op)
  name = op_name(op)
  result = nil
  time = Benchmark.realtime{
    result = yield
  }
  ignore_safely {
    @calls.observe(time, labels: get_labels(name))
  }
  result
rescue => ex
  ignore_safely {
    @errors.increment(labels: get_labels(name))
  }
  raise
end

Protected Instance Methods

get_labels(op_name) click to toggle source
# File lib/startback/audit/prometheus.rb, line 69
def get_labels(op_name)
  option_labels.merge({
    operation: op_name,
    startback_version: version
  })
end
ignore_safely() { || ... } click to toggle source
# File lib/startback/audit/prometheus.rb, line 62
def ignore_safely
  yield
rescue => ex
  puts ex.class.to_s + "\n" + ex.message + "\n" + ex.backtrace.join("\n")
  nil
end
op_name(op) click to toggle source
# File lib/startback/audit/prometheus.rb, line 84
def op_name(op)
  case op
  when String then op
  when Class  then op.name
  else op.class.name
  end
end
option_labels() click to toggle source
# File lib/startback/audit/prometheus.rb, line 76
def option_labels
  @options[:labels] || {}
end
version() click to toggle source
# File lib/startback/audit/prometheus.rb, line 80
def version
  Startback::VERSION
end