-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# encoding: UTF-8 | ||
|
||
module Prometheus | ||
module Client | ||
module Formats | ||
# Text format is human readable mainly used for manual inspection. | ||
module Text | ||
VERSION = '0.0.4' | ||
TYPE = 'text/plain; version=' + VERSION | ||
|
||
METRIC_LINE = '%s%s %s' | ||
TYPE_LINE = '# TYPE %s %s' | ||
HELP_LINE = '# HELP %s %s' | ||
|
||
LABEL = '%s="%s"' | ||
SEPARATOR = ',' | ||
DELIMITER = "\n" | ||
|
||
REGEX = { doc: /[\n\\]/, label: /[\n\\"]/ } | ||
REPLACE = { "\n" => '\n', '\\' => '\\\\', '"' => '\"' } | ||
|
||
def self.marshal(registry) | ||
lines = [] | ||
|
||
registry.metrics.each do |metric| | ||
lines << format(TYPE_LINE, metric.name, metric.type) | ||
lines << format(HELP_LINE, metric.name, escape(metric.docstring)) | ||
|
||
metric.values.each do |label_set, value| | ||
set = metric.base_labels.merge(label_set) | ||
lines << format(METRIC_LINE, metric.name, labels(set), value) | ||
end | ||
end | ||
|
||
# there must be a trailing delimiter | ||
(lines << nil).join(DELIMITER) | ||
end | ||
|
||
private | ||
|
||
def self.labels(set) | ||
return if set.empty? | ||
|
||
strings = set.reduce([]) do |memo, (key, value)| | ||
memo << format(LABEL, key, escape(value, :label)) | ||
end | ||
|
||
"{#{strings.join(SEPARATOR)}}" | ||
end | ||
|
||
def self.escape(string, format = :doc) | ||
string.to_s.gsub(REGEX[format], REPLACE) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# encoding: UTF-8 | ||
|
||
require 'prometheus/client/formats/text' | ||
|
||
describe Prometheus::Client::Formats::Text do | ||
let(:registry) do | ||
double(metrics: [ | ||
double( | ||
name: :foo, | ||
docstring: 'foo description', | ||
base_labels: {}, | ||
type: :counter, | ||
values: { { code: 'red' } => 42 }, | ||
), | ||
double( | ||
name: :bar, | ||
docstring: "bar description\nwith newline", | ||
base_labels: { status: 'success' }, | ||
type: :gauge, | ||
values: { { code: 'pink' } => 15 }, | ||
), | ||
double( | ||
name: :baz, | ||
docstring: 'baz "description" \\escaping', | ||
base_labels: {}, | ||
type: :counter, | ||
values: { { text: %Q(with "quotes", \\escape \n and newline) } => 15 }, | ||
), | ||
],) | ||
end | ||
|
||
describe '.marshal' do | ||
it 'returns a Text format version 0.0.4 compatible representation' do | ||
expect(subject.marshal(registry)).to eql <<-'TEXT' | ||
# TYPE foo counter | ||
# HELP foo foo description | ||
foo{code="red"} 42 | ||
# TYPE bar gauge | ||
# HELP bar bar description\nwith newline | ||
bar{status="success",code="pink"} 15 | ||
# TYPE baz counter | ||
# HELP baz baz "description" \\escaping | ||
baz{text="with \"quotes\", \\escape \n and newline"} 15 | ||
TEXT | ||
end | ||
end | ||
end |