Class: Startback::Audit::Prometheus

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ Prometheus

Returns a new instance of Prometheus.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



41
42
43
# File 'lib/startback/audit/prometheus.rb', line 41

def calls
  @calls
end

#errorsObject (readonly)

Returns the value of attribute errors.



41
42
43
# File 'lib/startback/audit/prometheus.rb', line 41

def errors
  @errors
end

#optionsObject (readonly)

Returns the value of attribute options.



41
42
43
# File 'lib/startback/audit/prometheus.rb', line 41

def options
  @options
end

#prefixObject (readonly)

Returns the value of attribute prefix.



41
42
43
# File 'lib/startback/audit/prometheus.rb', line 41

def prefix
  @prefix
end

#registryObject (readonly)

Returns the value of attribute registry.



41
42
43
# File 'lib/startback/audit/prometheus.rb', line 41

def registry
  @registry
end

Instance Method Details

#call(runner, op) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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