From 04d3f5e206fb0902fd3087c035567fb882363a42 Mon Sep 17 00:00:00 2001 From: Chance Feick Date: Mon, 20 Mar 2017 15:53:19 -0700 Subject: [PATCH] Validating Queue (#72) --- lib/librato/rack/validating_queue.rb | 12 ++++-- test/unit/rack/validating_queue_test.rb | 56 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 test/unit/rack/validating_queue_test.rb diff --git a/lib/librato/rack/validating_queue.rb b/lib/librato/rack/validating_queue.rb index 449d4ed..8a8a68f 100644 --- a/lib/librato/rack/validating_queue.rb +++ b/lib/librato/rack/validating_queue.rb @@ -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] @@ -27,8 +33,6 @@ def submit false # preserve end end - - super end private diff --git a/test/unit/rack/validating_queue_test.rb b/test/unit/rack/validating_queue_test.rb new file mode 100644 index 0000000..98b963a --- /dev/null +++ b/test/unit/rack/validating_queue_test.rb @@ -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