Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Validate user-submitted timestamps.

  • Loading branch information...
commit a328ce6924a4adbd05c76c520e126845dff7a2b2 1 parent 04bec80
@nextmat nextmat authored
View
1  lib/librato/metrics.rb
@@ -63,6 +63,7 @@ module Metrics
extend SingleForwardable
TYPES = [:counter, :gauge]
+ MIN_MEASURE_TIME = (Time.now-(3600*24*365)).to_i
# Expose class methods of Simple via Metrics itself.
#
View
1  lib/librato/metrics/errors.rb
@@ -9,6 +9,7 @@ class AgentInfoMissing < MetricsError; end
class NoMetricsQueued < MetricsError; end
class NoMetricsProvided < MetricsError; end
class NoClientProvided < MetricsError; end
+ class InvalidMeasureTime < MetricsError; end
class NetworkError < StandardError; end
View
12 lib/librato/metrics/queue.rb
@@ -29,8 +29,10 @@ def add(args)
type = :gauge
end
type = ("#{type}s").to_sym
- unless skip_measurement_times
- metric[:measure_time] ||= epoch_time
+ if metric[:measure_time]
+ check_measure_time(metric)
+ elsif !skip_measurement_times
+ metric[:measure_time] = epoch_time
end
@queued[type] ||= []
@queued[type] << metric
@@ -85,6 +87,12 @@ def size
private
+ def check_measure_time(data)
+ if data[:measure_time].to_i < Metrics::MIN_MEASURE_TIME
+ raise InvalidMeasureTime, "Measure time for submitted metric (#{data}) is invalid."
+ end
+ end
+
def submit_check
autosubmit_check # in Processor
if @autosubmit_count && self.length >= @autosubmit_count
View
26 spec/unit/metrics/queue_spec.rb
@@ -82,6 +82,32 @@ module Metrics
subject.queued.should equal_unordered(expected)
end
end
+
+ context "with a measure_time" do
+ it "should accept time objects" do
+ time = Time.now-5
+ subject.add :foo => {:measure_time => time, :value => 123}
+ subject.queued[:gauges][0][:measure_time].should == time
+ end
+
+ it "should accept integers" do
+ time = 1336574713
+ subject.add :foo => {:measure_time => time, :value => 123}
+ subject.queued[:gauges][0][:measure_time].should == time
+ end
+
+ it "should accept strings" do
+ time = '1336574713'
+ subject.add :foo => {:measure_time => time, :value => 123}
+ subject.queued[:gauges][0][:measure_time].should == time
+ end
+
+ it "should raise exception in invalid time" do
+ lambda {
+ subject.add :foo => {:measure_time => '12', :value => 123}
+ }.should raise_error(InvalidMeasureTime)
+ end
+ end
end
describe "#counters" do
Please sign in to comment.
Something went wrong with that request. Please try again.