Class: GitLab::Exporter::PrometheusMetrics
- Inherits:
-
Object
- Object
- GitLab::Exporter::PrometheusMetrics
- Defined in:
- lib/gitlab_exporter/prometheus.rb
Overview
Prometheus metrics container
Provides a simple API to ‘add` metrics and then turn them `to_s` which will just dump all the metrics in prometheus format
The add method also can take any arbitrary amount of labels in a ‘key: value` format.
Class Method Summary collapse
- .clear_descriptions ⇒ Object
- .describe(name, description, metric_type = nil) ⇒ Object
- .description(name) ⇒ Object
- .metric_type(name) ⇒ Object
Instance Method Summary collapse
- #add(name, value, quantile = false, **labels) ⇒ Object
-
#initialize(include_timestamp: true) ⇒ PrometheusMetrics
constructor
A new instance of PrometheusMetrics.
- #to_s ⇒ Object
Constructor Details
#initialize(include_timestamp: true) ⇒ PrometheusMetrics
Returns a new instance of PrometheusMetrics.
12 13 14 15 16 |
# File 'lib/gitlab_exporter/prometheus.rb', line 12 def initialize(include_timestamp: true) @metrics = Hash.new { |h, k| h[k] = [] } @quantiles = Hash.new { |h, k| h[k] = [] } = end |
Class Method Details
.clear_descriptions ⇒ Object
34 35 36 37 |
# File 'lib/gitlab_exporter/prometheus.rb', line 34 def clear_descriptions @metric_descriptions = {} @metric_types = {} end |
.describe(name, description, metric_type = nil) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/gitlab_exporter/prometheus.rb', line 19 def describe(name, description, metric_type = nil) @metric_descriptions ||= {} @metric_descriptions[name] = description @metric_types ||= {} @metric_types[name] = metric_type end |
.description(name) ⇒ Object
26 27 28 |
# File 'lib/gitlab_exporter/prometheus.rb', line 26 def description(name) @metric_descriptions && @metric_descriptions[name] end |
.metric_type(name) ⇒ Object
30 31 32 |
# File 'lib/gitlab_exporter/prometheus.rb', line 30 def metric_type(name) @metric_types && @metric_types[name] end |
Instance Method Details
#add(name, value, quantile = false, **labels) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gitlab_exporter/prometheus.rb', line 40 def add(name, value, quantile = false, **labels) fail "value '#{value}' must be a number" unless value.is_a?(Numeric) if quantile @quantiles[{ name: name, labels: labels }] << value else @metrics[name] << { value: value, labels: labels, timestamp: (Time.now.to_f * 1000).to_i } end self end |
#to_s ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/gitlab_exporter/prometheus.rb', line 52 def to_s add_quantiles_to_metrics buffer = "" @metrics.each do |name, measurements| buffer << "# HELP #{name} #{self.class.description(name)}\n" if self.class.description(name) buffer << "# TYPE #{name} #{self.class.metric_type(name)}\n" if self.class.metric_type(name) measurements.each do |measurement| buffer << name.to_s labels = labels(measurement[:labels]) buffer << "{#{labels}}" unless labels.empty? buffer << " #{measurement[:value]}" buffer << " #{measurement[:timestamp]}" if buffer << "\n" end end buffer end |