Class: Yabeda::Prometheus::Adapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/yabeda/prometheus/adapter.rb

Defined Under Namespace

Classes: UndeclaredMetricTags

Instance Method Summary collapse

Instance Method Details

#build_name(metric) ⇒ Object



92
93
94
# File 'lib/yabeda/prometheus/adapter.rb', line 92

def build_name(metric)
  [metric.group, metric.name, metric.unit].compact.join('_').to_sym
end

#debug!Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/yabeda/prometheus/adapter.rb', line 102

def debug!
  Yabeda.configure do
    group :prometheus_exporter

    histogram :render_duration,
              tags: %i[], unit: :seconds,
              buckets: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
              comment: "Time required to render all metrics in Prometheus format"
  end
end

#perform_counter_increment!(metric, tags, value) ⇒ Object



36
37
38
39
40
# File 'lib/yabeda/prometheus/adapter.rb', line 36

def perform_counter_increment!(metric, tags, value)
  registry.get(build_name(metric)).increment(by: value, labels: tags)
rescue ::Prometheus::Client::LabelSetValidator::InvalidLabelSetError => e
  raise UndeclaredMetricTags.new(build_name(metric), e)
end

#perform_gauge_set!(metric, tags, value) ⇒ Object



52
53
54
55
56
# File 'lib/yabeda/prometheus/adapter.rb', line 52

def perform_gauge_set!(metric, tags, value)
  registry.get(build_name(metric)).set(value, labels: tags)
rescue ::Prometheus::Client::LabelSetValidator::InvalidLabelSetError => e
  raise UndeclaredMetricTags.new(build_name(metric), e)
end

#perform_histogram_measure!(metric, tags, value) ⇒ Object



70
71
72
73
74
# File 'lib/yabeda/prometheus/adapter.rb', line 70

def perform_histogram_measure!(metric, tags, value)
  registry.get(build_name(metric)).observe(value, labels: tags)
rescue ::Prometheus::Client::LabelSetValidator::InvalidLabelSetError => e
  raise UndeclaredMetricTags.new(build_name(metric), e)
end

#perform_summary_observe!(metric, tags, value) ⇒ Object



86
87
88
89
90
# File 'lib/yabeda/prometheus/adapter.rb', line 86

def perform_summary_observe!(metric, tags, value)
  registry.get(build_name(metric)).observe(value, labels: tags)
rescue ::Prometheus::Client::LabelSetValidator::InvalidLabelSetError => e
  raise UndeclaredMetricTags.new(build_name(metric), e)
end

#register_counter!(metric) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/yabeda/prometheus/adapter.rb', line 26

def register_counter!(metric)
  validate_metric!(metric)
  registry.counter(
    build_name(metric),
    docstring: metric.comment,
    labels: Array(metric.tags),
    store_settings: store_settings(metric),
  )
end

#register_gauge!(metric) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/yabeda/prometheus/adapter.rb', line 42

def register_gauge!(metric)
  validate_metric!(metric)
  registry.gauge(
    build_name(metric),
    docstring: metric.comment,
    labels: Array(metric.tags),
    store_settings: store_settings(metric),
  )
end

#register_histogram!(metric) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yabeda/prometheus/adapter.rb', line 58

def register_histogram!(metric)
  validate_metric!(metric)
  buckets = metric.buckets || ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
  registry.histogram(
    build_name(metric),
    docstring: metric.comment,
    buckets: buckets,
    labels: Array(metric.tags),
    store_settings: store_settings(metric),
  )
end

#register_summary!(metric) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/yabeda/prometheus/adapter.rb', line 76

def register_summary!(metric)
  validate_metric!(metric)
  registry.summary(
    build_name(metric),
    docstring: metric.comment,
    labels: Array(metric.tags),
    store_settings: store_settings(metric),
  )
end

#registryObject



22
23
24
# File 'lib/yabeda/prometheus/adapter.rb', line 22

def registry
  @registry ||= ::Prometheus::Client.registry
end

#validate_metric!(metric) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
# File 'lib/yabeda/prometheus/adapter.rb', line 96

def validate_metric!(metric)
  return if metric.comment

  raise ArgumentError, 'Prometheus require metrics to have comments'
end