Class: StatsD::Instrument::Metric
- Inherits:
-
Object
- Object
- StatsD::Instrument::Metric
- Defined in:
- lib/statsd/instrument/metric.rb
Constant Summary collapse
- TYPES =
{ c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', }
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#sample_rate ⇒ Object
Returns the value of attribute sample_rate.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #default_value ⇒ Object
-
#initialize(options = {}) ⇒ Metric
constructor
A new instance of Metric.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Metric
Returns a new instance of Metric.
5 6 7 8 9 10 11 12 13 |
# File 'lib/statsd/instrument/metric.rb', line 5 def initialize( = {}) @type = [:type] or raise ArgumentError, "Metric :type is required." @name = [:name] or raise ArgumentError, "Metric :name is required." @name = StatsD.prefix ? "#{StatsD.prefix}.#{@name}" : @name unless [:no_prefix] @value = [:value] || default_value @sample_rate = [:sample_rate] || StatsD.default_sample_rate @tags = StatsD::Instrument::Metric.([:tags]) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/statsd/instrument/metric.rb', line 3 def name @name end |
#sample_rate ⇒ Object
Returns the value of attribute sample_rate.
3 4 5 |
# File 'lib/statsd/instrument/metric.rb', line 3 def sample_rate @sample_rate end |
#tags ⇒ Object
Returns the value of attribute tags.
3 4 5 |
# File 'lib/statsd/instrument/metric.rb', line 3 def @tags end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/statsd/instrument/metric.rb', line 3 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/statsd/instrument/metric.rb', line 3 def value @value end |
Class Method Details
.normalize_tags(tags) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/statsd/instrument/metric.rb', line 42 def self.() return if .nil? = .map { |k, v| "#{k}:#{v}" } if .is_a?(Hash) .map do |tag| components = tag.split(':', 2) components.map { |c| c.gsub(/[^\w\.-]+/, '_') }.join(':') end end |
Instance Method Details
#default_value ⇒ Object
15 16 17 18 19 20 |
# File 'lib/statsd/instrument/metric.rb', line 15 def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end |
#inspect ⇒ Object
29 30 31 |
# File 'lib/statsd/instrument/metric.rb', line 29 def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end |
#to_s ⇒ Object
22 23 24 25 26 27 |
# File 'lib/statsd/instrument/metric.rb', line 22 def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end |