diff options
Diffstat (limited to 'lib/longboat/collector.rb')
-rw-r--r-- | lib/longboat/collector.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/longboat/collector.rb b/lib/longboat/collector.rb new file mode 100644 index 0000000..ccf103b --- /dev/null +++ b/lib/longboat/collector.rb @@ -0,0 +1,44 @@ +module Longboat + class Collector + def initialize + @metrics = {} + @jobs = [] + end + + def report!(name, value, help: nil, type: nil, labels: {}) + @metrics[name] ||= {help: help, type: type} + @metrics[name][labels] = value + end + + def metrics + timestamp = (Time.now.to_f * 1000).to_i + res = "" + @metrics.each do |name, metric| + res << "#HELP #{name} #{metric[:help]}\n" unless metric[:help].nil? + res << "#TYPE #{name} #{metric[:type]}\n" unless metric[:type].nil? + + metric.each do |labels, value| + next if labels == :help + next if labels == :type + labellist = [] + labels.each do |k, v| + labellist << "#{k}=\"#{v}\"" + end + labellist = labellist.join(",") + res << "#{name}{#{labellist}} #{value} #{timestamp}\n" + end + end + res + end + + def register!(job) + @jobs << job + end + + def collect! + @jobs.each do |job| + job.run + end + end + end +end |