Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Validating Queue (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance Feick authored Mar 20, 2017
1 parent 4dd5d14 commit 04d3f5e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/librato/rack/validating_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ class ValidatingQueue < Librato::Metrics::Queue
DEFAULT_TAGS_LIMIT = 4
METRIC_NAME_REGEX = /\A[-.:_\w]{1,255}\z/
TAGS_KEY_REGEX = /\A[-.:_\w]{1,64}\z/
TAGS_VALUE_REGEX = /\A[-.:_\w]{1,256}\z/
TAGS_VALUE_REGEX = /\A[-.:_\w\s]{1,255}\z/

attr_accessor :logger

# screen all measurements for validity before sending
def submit
validate_measurements

super
end

# screen all measurements for validity before sending
def validate_measurements
@queued[:measurements].delete_if do |entry|
name = entry[:name].to_s
tags = entry[:tags]
Expand All @@ -27,8 +33,6 @@ def submit
false # preserve
end
end

super
end

private
Expand Down
56 changes: 56 additions & 0 deletions test/unit/rack/validating_queue_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'test_helper'

module Librato
class Rack
class ValidatingQueueTest < Minitest::Test
def setup
@queue = ValidatingQueue.new
end

def test_valid_metric_name_tag_name_tag_value
@queue.add valid_metric_name: {
value: rand(100),
tags: { valid_tag_name: 'valid_tag_value' }
}
@queue.validate_measurements
refute_empty @queue.queued[:measurements]
end

def test_valid_tag_value_with_whitespace
@queue.add valid_metric_name: {
value: rand(100),
tags: { valid_tag_name: 'valid tag value' }
}
@queue.validate_measurements
refute_empty @queue.queued[:measurements]
end

def test_invalid_metric_name
@queue.add 'invalid metric name' => {
value: rand(100),
tags: { valid_tag_name: 'valid_tag_value' }
}
@queue.validate_measurements
assert_empty @queue.queued[:measurements]
end

def test_invalid_tag_name
@queue.add valid_metric_name: {
value: rand(100),
tags: { 'invalid_tag_name!' => 'valid_tag_value' }
}
@queue.validate_measurements
assert_empty @queue.queued[:measurements]
end

def test_invalid_tag_value
@queue.add valid_metric_name: {
value: rand(100),
tags: { valid_tag_name: 'invalid_tag_value!' }
}
@queue.validate_measurements
assert_empty @queue.queued[:measurements]
end
end
end
end

0 comments on commit 04d3f5e

Please sign in to comment.