Module: Rubino::Metrics::Renderer
- Defined in:
- lib/rubino/metrics.rb
Overview
Serializes metrics to Prometheus text exposition format:
# HELP name help text
# TYPE name counter|histogram
name{label="value",...} value
Label values are escaped for ‘“`, ``, and newline.
Class Method Summary collapse
- .call(metrics) ⇒ Object
- .escape(value) ⇒ Object
- .format_labels(labels) ⇒ Object
- .render_metric(metric) ⇒ Object
Class Method Details
.call(metrics) ⇒ Object
142 143 144 |
# File 'lib/rubino/metrics.rb', line 142 def self.call(metrics) metrics.flat_map { |m| render_metric(m) }.join("\n") + "\n" end |
.escape(value) ⇒ Object
170 171 172 |
# File 'lib/rubino/metrics.rb', line 170 def self.escape(value) value.to_s.gsub("\\", "\\\\").gsub('"', '\\"').gsub("\n", '\n') end |
.format_labels(labels) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/rubino/metrics.rb', line 163 def self.format_labels(labels) return "" if labels.empty? pairs = labels.map { |k, v| %(#{k}="#{escape(v)}") }.join(",") "{#{pairs}}" end |
.render_metric(metric) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rubino/metrics.rb', line 146 def self.render_metric(metric) lines = ["# HELP #{metric.name} #{metric.help}", "# TYPE #{metric.name} #{metric.type}"] case metric.type when :counter metric.each { |labels, value| lines << "#{metric.name}#{format_labels(labels)} #{value}" } when :histogram metric.each do |labels, data| data[:counts].each do |bucket, count| lines << "#{metric.name}_bucket#{format_labels(labels.merge(le: bucket.to_s))} #{count}" end lines << "#{metric.name}_sum#{format_labels(labels)} #{data[:sum]}" lines << "#{metric.name}_count#{format_labels(labels)} #{data[:count]}" end end lines end |