Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusv committed Feb 18, 2017
1 parent 5c5a153 commit ce75a0a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
8 changes: 1 addition & 7 deletions example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
counter = Prometheus::Client::Counter.new(:mycounter, 'Example counter')
gauge = Prometheus::Client::Gauge.new(:mygauge, 'Example gauge', {}, :livesum)
histogram = Prometheus::Client::Histogram.new(:myhistogram, 'Example histogram', {}, [0, 1, 2])
summary = Prometheus::Client::Histogram.new(:mysummary, 'Example summary', {})
prometheus.register(counter)
prometheus.register(gauge)
prometheus.register(histogram)
prometheus.register(summary)

counter.increment({'foo': 'bar'}, 2)
counter.increment({'foo': 'biz'}, 4)
Expand All @@ -23,11 +21,7 @@
histogram.observe({'foo': 'biz'}, 0.5)
histogram.observe({'foo': 'bar'}, 1.5)
histogram.observe({'foo': 'biz'}, 2)
summary.observe({'foo': 'bar'}, 0.5)
summary.observe({'foo': 'biz'}, 0.5)
summary.observe({'foo': 'bar'}, 1.5)
summary.observe({'foo': 'biz'}, 2)

#puts Prometheus::Client::Formats::Text.marshal(prometheus)

pp(Prometheus::Client::Formats::Text.marshal_multiprocess)
puts Prometheus::Client::Formats::Text.marshal_multiprocess
27 changes: 23 additions & 4 deletions lib/prometheus/client/formats/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def self.marshal_multiprocess(path=ENV['prometheus_multiproc_dir'])
parts = File.basename(f).split("_")
type = parts[0].to_sym
d = MmapedDict.new(f)
puts "================"
puts d.all_values
puts "================"
d.all_values.each do |key, value|
metric_name, name, labelnames, labelvalues = JSON.parse(key)
metric = metrics.fetch(metric_name, {
Expand Down Expand Up @@ -86,15 +89,17 @@ def self.marshal_multiprocess(path=ENV['prometheus_multiproc_dir'])
samples[[name, labels]] = value
end
when :histogram
bucket = labels.select{|l| l[0] == 'le' }
puts "------", labels
bucket = labels.select{|l| l[0] == 'le' }.map {|k, v| v.to_f}.first
if bucket
without_le = labels.select{ |l| l[0] != 'le' }
b = buckets.fetch(without_le, {})
v = b.fetch(bucket[0].to_f, 0.0) + value
v = b.fetch(bucket, 0.0) + value
if !buckets.has_key?(without_le)
buckets[without_le] = {}
end
buckets[without_le][bucket[0].to_f] = v
puts "INSERTING BUCKET #{bucket}"
buckets[without_le][bucket] = v
else
s = samples.fetch([name, labels], 0.0)
samples[[name, labels]] = s + value
Expand Down Expand Up @@ -122,8 +127,22 @@ def self.marshal_multiprocess(path=ENV['prometheus_multiproc_dir'])
[name, labels.to_h, value]
end
end
metrics.values
end

output = ''
metrics.each do |name, metric|
output += "# HELP #{name} #{metric[:help]}\n"
output += "# TYPE #{name} #{metric[:type].to_s}\n"
metric[:samples].each do |metric_name, labels, value|
if !labels.empty?
labelstr = '{' + labels.sort.map { |k, v| "#{k}='#{v}'" }.join(',') + '}'
else
labelstr = ''
end
output += "#{metric_name}#{labelstr} #{value}\n"
end
end
output
end

class << self
Expand Down
3 changes: 2 additions & 1 deletion lib/prometheus/client/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def initialize(type, name, labels, buckets)
@total = ValueClass.new(type, name, name.to_s + '_count', labels)

buckets.each do |bucket|
self[bucket] = ValueClass.new(type, name, name.to_s + '_count', labels)
# TODO: check that there are no user-defined "le" labels.
self[bucket] = ValueClass.new(type, name, name.to_s + '_bucket', labels.merge({'le' => bucket.to_s}))
end
end

Expand Down
11 changes: 9 additions & 2 deletions lib/prometheus/client/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ def initialize(name, labels, estimator)
@total = ValueClass.new(name, name + '_count', labels, estimator.observations)

estimator.invariants.each do |invariant|
self[invariant.quantile] = ValueClass.new(name, labels, estimator.query(invariant.quantile), nil)
self[invariant.quantile] = ValueClass.new(type, name, labels, estimator.query(invariant.quantile), nil)
end
end
end

def initialize(name, docstring, base_labels = {}, multiprocess_mode)
if ENV['prometheus_multiproc_dir']
raise ArgumentError, "Summary metric type does not have multiprocess support"
end
super(name, docstring, base_labels)
end

def type
:summary
end
Expand Down Expand Up @@ -56,7 +63,7 @@ def values

private

def default
def default(labels)
Quantile::Estimator.new
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/prometheus/client/valuetype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MmapedValue
@@pid = Process.pid

def initialize(type, metric_name, name, labels, multiprocess_mode='')
puts "/////////////", type.to_s
file_prefix = type.to_s
if type == :gauge
file_prefix += '_' + multiprocess_mode.to_s
Expand Down

0 comments on commit ce75a0a

Please sign in to comment.