Skip to content

Commit

Permalink
Merge 6bc415d into 733408e
Browse files Browse the repository at this point in the history
  • Loading branch information
grobie committed May 16, 2014
2 parents 733408e + 6bc415d commit 15b3823
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
4 changes: 0 additions & 4 deletions lib/prometheus/client/formats/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ def self.marshal(registry)
}
end.to_json
end

def self.unmarshal(*)
fail NotImplementedError
end
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions lib/prometheus/client/formats/text.rb
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
47 changes: 47 additions & 0 deletions spec/prometheus/client/formats/text_spec.rb
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

0 comments on commit 15b3823

Please sign in to comment.