aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2020-06-25 13:16:56 +0100
committerNat Lasseter <nat.lasseter@york.ac.uk>2020-06-25 13:16:56 +0100
commit5020bfd6cabb588dd34ff4171acac42c9b0e1945 (patch)
tree80e3d59e0aeb896d7fb45bb83200251793f97b10
parent8872aaaea63ba2e58c8cd3ee739972e7f8903288 (diff)
Added transactions to the collector so we can support exception handling on the raiders
-rw-r--r--lib/longboat/collector.rb46
-rw-r--r--lib/longboat/raiders.rb33
2 files changed, 63 insertions, 16 deletions
diff --git a/lib/longboat/collector.rb b/lib/longboat/collector.rb
index 10e2f84..01bb7f0 100644
--- a/lib/longboat/collector.rb
+++ b/lib/longboat/collector.rb
@@ -1,27 +1,47 @@
module Longboat
+ class CollectorTransactionError < Exception; end
+
class Collector
def initialize(config)
@metrics = {}
@config = config
+ @transaction = nil
end
def report!(name, value, help: nil, type: nil, labels: {}, timestamp: Time.now)
+ raise CollectorTransactionError if @tranaction.nil?
+
name = prefix(name)
- @metrics[name] ||= {help: help, type: type}
- @metrics[name][labels] = {value: value, timestamp: timestamp}
+ @transaction[name] ||= {help: help, type: type}
+ @transaction[name][labels] = {value: value, timestamp: timestamp}
end
def redact!(name, labels: nil)
+ raise CollectorTransactionError if @tranaction.nil?
+
name = prefix(name)
if labels.nil?
- @metrics.delete(name)
+ @transaction.delete(name)
else
- @metrics[name].delete(labels)
+ @transaction[name].delete(labels)
end
end
+ def begin!
+ @transaction = clone_metrics
+ end
+
+ def abort!
+ @transaction = nil
+ end
+
+ def commit!
+ @metrics = @transaction
+ @transaction = nil
+ end
+
def prometheus_metrics
res = ""
@metrics.each do |name, metric|
@@ -50,5 +70,23 @@ module Longboat
def prefix(name)
"#{@config[:metric_prefix]}#{name}"
end
+
+ def clone_metrics
+ clone = {}
+
+ @metrics.each do |metric, attributes|
+ clone[metric] = {}
+ clone[metric][:help] = attributes[:help]
+ clone[metric][:type] = attributes[:type]
+
+ attributes.each do |labels, value|
+ next if [:help, :type].include?(labels)
+
+ clone[metric][labels.clone] = value.clone
+ end
+ end
+
+ clone
+ end
end
end
diff --git a/lib/longboat/raiders.rb b/lib/longboat/raiders.rb
index 3e6f246..2740570 100644
--- a/lib/longboat/raiders.rb
+++ b/lib/longboat/raiders.rb
@@ -21,19 +21,28 @@ module Longboat
end
def raid!
+ puts "RAID"
@raiders.each do |name, raider|
- start_time = Time.now
- raider.raid
- end_time = Time.now
- time_taken = end_time - start_time
-
- @collector.report!(
- "longboat_meta_raider_runtime",
- (time_taken.to_f * 1000).to_i,
- help: "Time taken by a raider whilst raiding in ms",
- type: "guage",
- labels: {raider: name}
- )
+ @collector.begin!
+
+ begin
+ start_time = Time.now
+ raider.raid
+ end_time = Time.now
+ time_taken = end_time - start_time
+
+ @collector.report!(
+ "longboat_meta_raider_runtime",
+ (time_taken.to_f * 1000).to_i,
+ help: "Time taken by a raider whilst raiding in ms",
+ type: "guage",
+ labels: {raider: name}
+ )
+
+ @collector.commit!
+ rescue Exception => e
+ @collector.abort!
+ end
end
end