Module: PgqPrometheus::Config

Defined in:
lib/pgq_prometheus/config.rb

Constant Summary collapse

ALLOWED_FROM =
[:queue, :consumer, nil]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

._metricsObject

Returns the value of attribute _metrics.



10
11
12
# File 'lib/pgq_prometheus/config.rb', line 10

def _metrics
  @_metrics
end

.typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/pgq_prometheus/config.rb', line 10

def type
  @type
end

Class Method Details

.register_counter(name, help, options = {}) ⇒ Object

Parameters:

  • name (Symbol, String)
  • help (String)
  • options (String) (defaults to: {})

    keys :from, :column, :apply

Raises:

  • ArgumentError



62
63
64
# File 'lib/pgq_prometheus/config.rb', line 62

def self.register_counter(name, help, options = {})
  register_metric 'PrometheusExporter::Metric::Counter', name, help, options
end

.register_gauge(name, help, options = {}) ⇒ Object

Parameters:

  • name (Symbol, String)
  • help (String)
  • options (String) (defaults to: {})

    keys :from, :column, :apply

Raises:

  • ArgumentError



70
71
72
# File 'lib/pgq_prometheus/config.rb', line 70

def self.register_gauge(name, help, options = {})
  register_metric 'PrometheusExporter::Metric::Gauge', name, help, options
end

.register_histogram(name, help, options = {}) ⇒ Object

Parameters:

  • name (Symbol, String)
  • help (String)
  • options (String) (defaults to: {})

    keys :from, :column, :apply

Raises:

  • ArgumentError



78
79
80
81
82
# File 'lib/pgq_prometheus/config.rb', line 78

def self.register_histogram(name, help, options = {})
  buckets = options.delete(:buckets)
  options[:metric_args] ||= [buckets: buckets]
  register_metric 'PrometheusExporter::Metric::Histogram', name, help, options
end

.register_metric(metric_class, name, help, options = {}) ⇒ Object

Parameters:

  • metric_class (Class<Object>, String)
  • name (Symbol, String)
  • help (String)
  • options (String) (defaults to: {})

    keys :from, :column, :apply

Raises:

  • ArgumentError



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pgq_prometheus/config.rb', line 18

def self.register_metric(metric_class, name, help, options = {})
  raise ArgumentError, 'metric_class must be present' if metric_class.nil?

  name = name.to_sym
  raise ArgumentError, "metric #{name} already defined - unregister it" if _metrics.key?(name)

  from = options[:from]
  column = options[:column] || name
  apply = options[:apply]

  unless ALLOWED_FROM.include?(from)
    raise ArgumentError, "invalid :from, allowed: #{ALLOWED_FROM.map(&:inspect).join(', ')}"
  end

  if apply.nil?
    case from
    when :queue
      apply = proc { |queue_info| queue_info[column.to_sym] }
    when :consumer
      apply = proc { |consumer_info, _queue_info| consumer_info[column.to_sym] }
    else
      raise ArgumentError, 'require :apply block for metric without :from'
    end
  end

  _metrics[name] = {
      metric_class: metric_class,
      help: help,
      metric_args: options[:metric_args] || [],
      labels: options[:labels] || {},
      from: from,
      apply: apply
  }
end

.register_summary(name, help, options = {}) ⇒ Object

Parameters:

  • name (Symbol, String)
  • help (String)
  • options (String) (defaults to: {})

    keys :from, :column, :apply

Raises:

  • ArgumentError



88
89
90
91
92
# File 'lib/pgq_prometheus/config.rb', line 88

def self.register_summary(name, help, options = {})
  buckets = options.delete(:quantiles)
  options[:metric_args] ||= [quantiles: buckets]
  register_metric 'PrometheusExporter::Metric::Summary', name, help, options
end

.unregister_metric(name) ⇒ Object

Parameters:

  • name (Symbol, String)


54
55
56
# File 'lib/pgq_prometheus/config.rb', line 54

def self.unregister_metric(name)
  _metrics.delete(name.to_sym)
end