Class: Startback::Audit::Prometheus

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/startback/audit/prometheus.rb

Overview

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

  • operation : class name of the operation executed

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared

#op_context, #op_data, #op_name

Constructor Details

#initialize(options = {}) ⇒ Prometheus

Returns a new instance of Prometheus.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/startback/audit/prometheus.rb', line 30

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

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



44
45
46
# File 'lib/startback/audit/prometheus.rb', line 44

def calls
  @calls
end

#errorsObject (readonly)

Returns the value of attribute errors.



44
45
46
# File 'lib/startback/audit/prometheus.rb', line 44

def errors
  @errors
end

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/startback/audit/prometheus.rb', line 44

def options
  @options
end

#prefixObject (readonly)

Returns the value of attribute prefix.



44
45
46
# File 'lib/startback/audit/prometheus.rb', line 44

def prefix
  @prefix
end

#registryObject (readonly)

Returns the value of attribute registry.



44
45
46
# File 'lib/startback/audit/prometheus.rb', line 44

def registry
  @registry
end

Instance Method Details

#call(runner, op) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/startback/audit/prometheus.rb', line 46

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