Skip to content

Commit

Permalink
WIP: Strategies for dealing with label errors, other than just raising
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Magliola committed Sep 14, 2018
1 parent 3880328 commit 8f036af
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/prometheus/client/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module Prometheus
module Client
class Config
attr_accessor :data_store
attr_accessor :label_error_strategy

def initialize
@data_store = Prometheus::Client::DataStores::SimpleHash.new
@label_error_strategy = ErrorStrategies::Raise # we'll have a custom one, off-gem for Raven (or we can provide that one by default), and set it to that in production
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus/client/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def increment(by: 1, labels: {})
raise ArgumentError, 'increment must be a non-negative number' if by < 0

label_set = label_set_for(labels)
@store.increment(labels: label_set, by: by)
@store.increment(labels: label_set, by: by) if label_set
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/prometheus/client/error_strategies/raise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ErrorStrategies
class Raise
def self.label_error(exception_klass, message)
raise exception_klass, message
end
end
end
7 changes: 4 additions & 3 deletions lib/prometheus/client/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ def set(value, labels: {})
raise ArgumentError, 'value must be a number'
end

@store.set(labels: label_set_for(labels), val: value)
label_set = label_set_for(labels)
@store.set(labels: label_set, val: value) if label_set
end

# Increments Gauge value by 1 or adds the given value to the Gauge.
# (The value can be negative, resulting in a decrease of the Gauge.)
def increment(by: 1, labels: {})
label_set = label_set_for(labels)
@store.increment(labels: label_set, by: by)
@store.increment(labels: label_set, by: by) if label_set
end

# Decrements Gauge value by 1 or subtracts the given value from the Gauge.
# (The value can be negative, resulting in a increase of the Gauge.)
def decrement(by: 1, labels: {})
label_set = label_set_for(labels)
@store.increment(labels: label_set, by: -by)
@store.increment(labels: label_set, by: -by) if label_set
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/prometheus/client/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def type

def observe(value, labels: {})
base_label_set = label_set_for(labels)
return unless base_label_set

@store.synchronize do
buckets.each do |upper_limit|
Expand Down
11 changes: 7 additions & 4 deletions lib/prometheus/client/label_set_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class ReservedLabelError < LabelSetError; end
def initialize(expected_labels:, reserved_labels: [])
@expected_labels = expected_labels.sort
@reserved_labels = BASE_RESERVED_LABELS + reserved_labels
@validated = {}
end

def valid?(labels)
Expand All @@ -34,17 +33,17 @@ def valid?(labels)
end

def validate(labels)
return labels if @validated.key?(labels.hash)

valid?(labels)

unless keys_match?(labels)
raise InvalidLabelSetError, "labels must have the same signature " \
"(keys given: #{labels.keys.sort} vs." \
" keys expected: #{expected_labels}"

return false
end

@validated[labels.hash] = labels
labels
end

private
Expand Down Expand Up @@ -73,3 +72,7 @@ def validate_reserved_key(key)
end
end
end


# Replace all of these Raises for
# `Prometheus::Client.config.label_error_strategy.label_error(ExceptionClass, "errMessage")`
1 change: 1 addition & 0 deletions lib/prometheus/client/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def type
# Records a given value.
def observe(value, labels: {})
base_label_set = label_set_for(labels)
return unless base_label_set

@store.synchronize do
@store.increment(labels: base_label_set.merge(quantile: "count"), by: 1)
Expand Down

0 comments on commit 8f036af

Please sign in to comment.