metriks-librato_metrics 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  require 'metriks/librato_metrics_reporter'
2
2
 
3
3
  module MetriksLibratoMetrics
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.4'
5
5
  end
@@ -16,6 +16,8 @@ module Metriks
16
16
  @interval = options[:interval] || 60
17
17
  @time_tracker = Metriks::TimeTracker.new(@interval)
18
18
  @on_error = options[:on_error] || proc { |ex| }
19
+ @sanitizer = options[:sanitize] || proc { |key| key.to_s.gsub(/ +/, '_') }
20
+ @timeout = options[:timeout] || 10
19
21
 
20
22
  @data = {}
21
23
  @sent = {}
@@ -92,7 +94,7 @@ module Metriks
92
94
  when Net::HTTPSuccess, Net::HTTPRedirection
93
95
  # OK
94
96
  else
95
- res.error!
97
+ raise RequestFailedError.new(req, res, @data.dup)
96
98
  end
97
99
  ensure
98
100
  @data.clear
@@ -103,7 +105,7 @@ module Metriks
103
105
 
104
106
  @registry.each do |name, metric|
105
107
  next if name.nil? || name.empty?
106
- name = name.to_s.gsub(/ +/, '_')
108
+ name = sanitize_name(name)
107
109
 
108
110
  case metric
109
111
  when Metriks::Meter
@@ -171,5 +173,40 @@ module Metriks
171
173
  end
172
174
  end
173
175
  end
176
+
177
+ def sanitize_name(name)
178
+ case @sanitizer
179
+ when String
180
+ return name.gsub(/[^.:_\-0-9a-zA-Z]/, @sanitizer)[0...255]
181
+ when Proc
182
+ sanitized = @sanitizer.call(name)
183
+ else
184
+ raise RuntimeError, "The :sanitize option must be a replacement " \
185
+ "string or a Proc that will be passed the metric name to sanitize."
186
+ end
187
+
188
+ if sanitized.size > 255 || sanitized =~ /[^.:_\-0-9a-zA-Z]/
189
+ raise InvalidKeyError, "Librato metric names must match " \
190
+ "/[.:_\-0-9a-zA-Z]+/, and must be less than 255 characters."
191
+ else
192
+ sanitized
193
+ end
194
+ end
195
+
196
+ class InvalidKeyError < StandardError; end
197
+
198
+ class RequestFailedError < StandardError
199
+ attr_reader :req, :res, :data
200
+
201
+ def initialize(req, res, data = nil)
202
+ @req, @res = req, res
203
+ @data = data
204
+ end
205
+
206
+ def to_s
207
+ res.code + ' ' + res.message.dump
208
+ end
209
+ end
210
+
174
211
  end
175
212
  end
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'metriks-librato_metrics'
16
- s.version = '1.0.3'
17
- s.date = '2015-03-13'
16
+ s.version = '1.0.4'
17
+ s.date = '2015-09-25'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
20
20
  ## as you like.
@@ -37,4 +37,39 @@ class LibratoMetricsReporterTest < Test::Unit::TestCase
37
37
  @reporter.expects(:submit).never
38
38
  @reporter.write
39
39
  end
40
+
41
+ def test_raises_on_invalid_keys
42
+ err = Metriks::LibratoMetricsReporter::InvalidKeyError
43
+ %w[invalid.utf8—key invalid.ascii?key invalid.punctuation/key].each do |key|
44
+ assert_raise(err, "error on invalid key #{key}") do
45
+ @registry.counter(key)
46
+ @reporter.write
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_sanitizes_by_string
52
+ @reporter = build_reporter(:sanitize => ".")
53
+ @registry.counter("invalid.ascii?key")
54
+ @reporter.expects(:submit)
55
+ @reporter.write
56
+ assert_equal(@reporter.data["gauges[0][name]"], "invalid.ascii.key")
57
+ end
58
+
59
+ def test_sanitizes_by_proc
60
+ @reporter = build_reporter(:sanitize => Proc.new { |key| "bats" })
61
+ @registry.counter("invalid.ascii?key")
62
+ @reporter.expects(:submit)
63
+ @reporter.write
64
+ assert_equal(@reporter.data["gauges[0][name]"], "bats")
65
+ end
66
+
67
+ def test_raises_on_invalid_sanitized_key
68
+ @reporter = build_reporter(:sanitize => Proc.new { |key| "bats" * 100 })
69
+ assert_raise Metriks::LibratoMetricsReporter::InvalidKeyError do
70
+ @registry.counter("invalid.ascii?key")
71
+ @reporter.write
72
+ end
73
+ end
74
+
40
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metriks-librato_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-13 00:00:00.000000000 Z
12
+ date: 2015-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metriks