lib/yodeler/metric.rb in yodeler-0.1.1 vs lib/yodeler/metric.rb in yodeler-0.1.2
- old
+ new
@@ -1,36 +1,54 @@
-class Yodeler::Metric
- attr_reader :type, :value
- attr_reader :sample_rate, :tags, :prefix
- attr_reader :options
+require 'securerandom'
- def initialize(type, name, value, opts={})
- @type = type
- @name = name
- @value = value
- @prefix = opts.delete(:prefix)
- @sample_rate = opts.delete(:sample_rate)
- @options = opts
- end
+module Yodeler
+ class Metric
+ attr_reader :type, :value
+ attr_reader :sample_rate, :tags, :prefix
+ attr_reader :uuid
+ attr_reader :options
- def name
- @prefix ? [@prefix, @name].join('.') : @name
- end
+ TYPES = [:event, :increment, :gauge, :timing]
- # @return [Boolean] Should this metric be sampled
- def sample?
- @_sample ||= !(rand()> @sample_rate)
- end
+ def initialize(type, name, value, opts = {})
+ @uuid = SecureRandom.uuid
+ @type = type
+ @name = name
+ @value = value
+ @prefix = opts.delete(:prefix)
+ @sample_rate = opts.delete(:sample_rate)
+ @timestamp = opts.delete(:timestamp)
+ @tags = opts.delete(:tags)
+ @hostname = opts.delete(:hostname)
+ end
- def to_hash
- hash = {
- name: name,
- type: @type,
- value: @value,
- }
+ def name
+ @prefix ? [@prefix, @name].join('.') : @name
+ end
- hash[:tags] = options[:tags] if options[:tags] && options[:tags].any?
- hash[:hostname] = options[:hostname] if options[:hostname]
+ # @return [Boolean] Should this metric be sampled
+ def sample?
+ @_sample ||= !(rand > @sample_rate)
+ end
- hash
+ def to_hash
+ hash = {
+ uuid: uuid,
+ name: name,
+ type: @type,
+ value: @value
+ }
+
+ hash[:timestamp] = @timestamp if @timestamp
+ hash[:tags] = @tags if @tags && @tags.any?
+ hash[:hostname] = @hostname if @hostname
+
+ hash
+ end
+
+ TYPES.each do |type_meth|
+ define_method("#{type_meth}?") do
+ type_meth.to_sym == type
+ end
+ end
end
end