class TopologicalInventory::Providers::Common::Metrics

Constants

ERROR_COUNTER_MESSAGE
ERROR_TYPES
OPERATIONS

Public Class Methods

new(port = 9394) click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 18
def initialize(port = 9394)
  return if port == 0

  configure_server(port)
  configure_metrics

  init_counters
end

Public Instance Methods

record_error(type = :general) click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 31
def record_error(type = :general)
  @error_counter&.observe(1, :type => type.to_s)
end
record_gauge(metric, opt, value: nil, labels: {}) click to toggle source

Common method for gauge

# File lib/topological_inventory/providers/common/metrics.rb, line 48
def record_gauge(metric, opt, value: nil, labels: {})
  case opt
  when :set then
    metric&.observe(value.to_i, labels)
  when :add then
    metric&.increment(labels)
  when :remove then
    metric&.decrement(labels)
  end
end
record_operation(name, labels = {}) click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 39
def record_operation(name, labels = {})
  @status_counter&.observe(1, (labels || {}).merge(:name => name))
end
record_operation_time(name, labels = {}, &block) click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 43
def record_operation_time(name, labels = {}, &block)
  record_time(@duration_seconds, (labels || {}).merge(:name => name), &block)
end
record_refresh_timing(labels = {}, &block) click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 35
def record_refresh_timing(labels = {}, &block)
  record_time(@refresh_timer, labels, &block)
end
record_time(metric, labels = {}) { || ... } click to toggle source

Common method for histogram

# File lib/topological_inventory/providers/common/metrics.rb, line 60
def record_time(metric, labels = {})
  result = nil
  time = Benchmark.realtime { result = yield }
  metric&.observe(time, labels)
  result
end
stop_server() click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 27
def stop_server
  @server&.stop
end

Private Instance Methods

configure_metrics() click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 89
def configure_metrics
  PrometheusExporter::Instrumentation::Process.start
  PrometheusExporter::Metric::Base.default_prefix = default_prefix

  @duration_seconds = PrometheusExporter::Metric::Histogram.new('duration_seconds', 'Duration of processed operation')
  @refresh_timer = PrometheusExporter::Metric::Histogram.new('refresh_time', 'Duration of full refresh')
  @error_counter = PrometheusExporter::Metric::Counter.new('errors_total', ERROR_COUNTER_MESSAGE)
  @status_counter = PrometheusExporter::Metric::Counter.new('status_counter', 'number of processed operations')

  [@duration_seconds, @refresh_timer, @error_counter, @status_counter].each do |metric|
    @server.collector.register_metric(metric)
  end
end
configure_server(port) click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 82
def configure_server(port)
  @server = PrometheusExporter::Server::WebServer.new(:port => port)
  @server.start

  PrometheusExporter::Client.default = PrometheusExporter::LocalClient.new(:collector => @server.collector)
end
default_prefix() click to toggle source
# File lib/topological_inventory/providers/common/metrics.rb, line 103
def default_prefix
  raise NotImplementedError, "#{__method__} must be implemented in a subclass"
end
init_counters() click to toggle source

Set all values to 0 (otherwise the counter is undefined)

# File lib/topological_inventory/providers/common/metrics.rb, line 70
def init_counters
  self.class::ERROR_TYPES.each do |err_type|
    @error_counter&.observe(0, :type => err_type)
  end

  self.class::OPERATIONS.each do |op|
    operation_status.each_key do |status|
      @status_counter&.observe(0, :name => op, :status => status.to_s)
    end
  end
end