Class: StatsD::Instrument::Metric
- Inherits:
-
Object
- Object
- StatsD::Instrument::Metric
- Defined in:
- lib/statsd/instrument/metric.rb
Overview
The Metric class represents a metric sample to be send by a backend.
Constant Summary collapse
- TYPES =
The metric types that are supported by this library. Note that every StatsD server implementation only supports a subset of them.
{ c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', }
Instance Attribute Summary collapse
-
#name ⇒ String
The name of the metric.
-
#sample_rate ⇒ Float
The sample rate to use for the metric.
-
#tags ⇒ Array<String>, ...
The tags to associate with the metric.
-
#type ⇒ Symbol
The metric type.
-
#value ⇒ Numeric, String
The value to collect for the metric.
Class Method Summary collapse
-
.normalize_tags(tags) ⇒ Array<String>?
Utility function to convert tags to the canonical form.
Instance Method Summary collapse
-
#default_value ⇒ Numeric, String
The default value for this metric, which will be used if it is not set.
-
#initialize(options = {}) ⇒ Metric
constructor
Initializes a new metric instance.
- #inspect ⇒ String
- #to_s ⇒ String
Constructor Details
#initialize(options = {}) ⇒ Metric
Initializes a new metric instance. Normally, you don’t want to call this method directly, but use one of the metric collection methods on the StatsD module.
46 47 48 49 50 51 52 53 54 |
# File 'lib/statsd/instrument/metric.rb', line 46 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 ⇒ String
Returns The name of the metric. StatsD#prefix will automatically be applied to the metric in the constructor, unless the :no_prefix option is set.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/statsd/instrument/metric.rb', line 29 class StatsD::Instrument::Metric attr_accessor :type, :name, :value, :sample_rate, :tags # Initializes a new metric instance. # Normally, you don't want to call this method directly, but use one of the metric collection # methods on the {StatsD} module. # # @option options [Symbol] :type The type of the metric. # @option options [String] :name The name of the metric without prefix. # @option options [Boolean] :no_prefix Set to <tt>true</tt> if you don't want to apply {StatsD#prefix} # @option options [Numeric, String, nil] :value The value to collect for the metric. If set to # <tt>nil>/tt>, {#default_value} will be used. # @option options [Numeric, nil] :sample_rate The sample rate to use. If not set, it will use # {StatsD#default_sample_rate}. # @option options [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric. # See {.normalize_tags} for more information. 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 # The default value for this metric, which will be used if it is not set. # # A default value is only defined for counter metrics (<tt>1</tt>). For all other # metric types, this emthod will raise an <tt>ArgumentError</tt>. # # @return [Numeric, String] The default value for this metric. # @raise ArgumentError if the metric type doesn't have a default value def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end # @private # @return [String] def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end # @private # @return [String] def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end # The metric types that are supported by this library. Note that every StatsD server # implementation only supports a subset of them. TYPES = { c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', } # Utility function to convert tags to the canonical form. # # - Tags specified as key value pairs will be converted into an array # - Tags are normalized to only use word characters and underscores. # # @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. # @return [Array<String>, nil] the list of tags in canonical form. def self.() return unless = .map { |k, v| k.to_s + ":".freeze + v.to_s } if .is_a?(Hash) .map { |tag| tag.tr('|,'.freeze, ''.freeze) } end end |
#sample_rate ⇒ Float
The sample rate to use for the metric. How the sample rate is handled differs per backend. The UDP backend will actually sample metric submissions based on the sample rate, while the logger backend will just include the sample rate in its output for debugging purposes.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/statsd/instrument/metric.rb', line 29 class StatsD::Instrument::Metric attr_accessor :type, :name, :value, :sample_rate, :tags # Initializes a new metric instance. # Normally, you don't want to call this method directly, but use one of the metric collection # methods on the {StatsD} module. # # @option options [Symbol] :type The type of the metric. # @option options [String] :name The name of the metric without prefix. # @option options [Boolean] :no_prefix Set to <tt>true</tt> if you don't want to apply {StatsD#prefix} # @option options [Numeric, String, nil] :value The value to collect for the metric. If set to # <tt>nil>/tt>, {#default_value} will be used. # @option options [Numeric, nil] :sample_rate The sample rate to use. If not set, it will use # {StatsD#default_sample_rate}. # @option options [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric. # See {.normalize_tags} for more information. 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 # The default value for this metric, which will be used if it is not set. # # A default value is only defined for counter metrics (<tt>1</tt>). For all other # metric types, this emthod will raise an <tt>ArgumentError</tt>. # # @return [Numeric, String] The default value for this metric. # @raise ArgumentError if the metric type doesn't have a default value def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end # @private # @return [String] def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end # @private # @return [String] def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end # The metric types that are supported by this library. Note that every StatsD server # implementation only supports a subset of them. TYPES = { c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', } # Utility function to convert tags to the canonical form. # # - Tags specified as key value pairs will be converted into an array # - Tags are normalized to only use word characters and underscores. # # @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. # @return [Array<String>, nil] the list of tags in canonical form. def self.() return unless = .map { |k, v| k.to_s + ":".freeze + v.to_s } if .is_a?(Hash) .map { |tag| tag.tr('|,'.freeze, ''.freeze) } end end |
#tags ⇒ Array<String>, ...
Only the Datadog implementation supports tags.
The tags to associate with the metric.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/statsd/instrument/metric.rb', line 29 class StatsD::Instrument::Metric attr_accessor :type, :name, :value, :sample_rate, :tags # Initializes a new metric instance. # Normally, you don't want to call this method directly, but use one of the metric collection # methods on the {StatsD} module. # # @option options [Symbol] :type The type of the metric. # @option options [String] :name The name of the metric without prefix. # @option options [Boolean] :no_prefix Set to <tt>true</tt> if you don't want to apply {StatsD#prefix} # @option options [Numeric, String, nil] :value The value to collect for the metric. If set to # <tt>nil>/tt>, {#default_value} will be used. # @option options [Numeric, nil] :sample_rate The sample rate to use. If not set, it will use # {StatsD#default_sample_rate}. # @option options [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric. # See {.normalize_tags} for more information. 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 # The default value for this metric, which will be used if it is not set. # # A default value is only defined for counter metrics (<tt>1</tt>). For all other # metric types, this emthod will raise an <tt>ArgumentError</tt>. # # @return [Numeric, String] The default value for this metric. # @raise ArgumentError if the metric type doesn't have a default value def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end # @private # @return [String] def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end # @private # @return [String] def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end # The metric types that are supported by this library. Note that every StatsD server # implementation only supports a subset of them. TYPES = { c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', } # Utility function to convert tags to the canonical form. # # - Tags specified as key value pairs will be converted into an array # - Tags are normalized to only use word characters and underscores. # # @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. # @return [Array<String>, nil] the list of tags in canonical form. def self.() return unless = .map { |k, v| k.to_s + ":".freeze + v.to_s } if .is_a?(Hash) .map { |tag| tag.tr('|,'.freeze, ''.freeze) } end end |
#type ⇒ Symbol
Returns The metric type. Must be one of TYPES.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/statsd/instrument/metric.rb', line 29 class StatsD::Instrument::Metric attr_accessor :type, :name, :value, :sample_rate, :tags # Initializes a new metric instance. # Normally, you don't want to call this method directly, but use one of the metric collection # methods on the {StatsD} module. # # @option options [Symbol] :type The type of the metric. # @option options [String] :name The name of the metric without prefix. # @option options [Boolean] :no_prefix Set to <tt>true</tt> if you don't want to apply {StatsD#prefix} # @option options [Numeric, String, nil] :value The value to collect for the metric. If set to # <tt>nil>/tt>, {#default_value} will be used. # @option options [Numeric, nil] :sample_rate The sample rate to use. If not set, it will use # {StatsD#default_sample_rate}. # @option options [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric. # See {.normalize_tags} for more information. 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 # The default value for this metric, which will be used if it is not set. # # A default value is only defined for counter metrics (<tt>1</tt>). For all other # metric types, this emthod will raise an <tt>ArgumentError</tt>. # # @return [Numeric, String] The default value for this metric. # @raise ArgumentError if the metric type doesn't have a default value def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end # @private # @return [String] def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end # @private # @return [String] def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end # The metric types that are supported by this library. Note that every StatsD server # implementation only supports a subset of them. TYPES = { c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', } # Utility function to convert tags to the canonical form. # # - Tags specified as key value pairs will be converted into an array # - Tags are normalized to only use word characters and underscores. # # @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. # @return [Array<String>, nil] the list of tags in canonical form. def self.() return unless = .map { |k, v| k.to_s + ":".freeze + v.to_s } if .is_a?(Hash) .map { |tag| tag.tr('|,'.freeze, ''.freeze) } end end |
#value ⇒ Numeric, String
Returns The value to collect for the metric. Depending on the metric type, value can be a string, integer, or float.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/statsd/instrument/metric.rb', line 29 class StatsD::Instrument::Metric attr_accessor :type, :name, :value, :sample_rate, :tags # Initializes a new metric instance. # Normally, you don't want to call this method directly, but use one of the metric collection # methods on the {StatsD} module. # # @option options [Symbol] :type The type of the metric. # @option options [String] :name The name of the metric without prefix. # @option options [Boolean] :no_prefix Set to <tt>true</tt> if you don't want to apply {StatsD#prefix} # @option options [Numeric, String, nil] :value The value to collect for the metric. If set to # <tt>nil>/tt>, {#default_value} will be used. # @option options [Numeric, nil] :sample_rate The sample rate to use. If not set, it will use # {StatsD#default_sample_rate}. # @option options [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric. # See {.normalize_tags} for more information. 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 # The default value for this metric, which will be used if it is not set. # # A default value is only defined for counter metrics (<tt>1</tt>). For all other # metric types, this emthod will raise an <tt>ArgumentError</tt>. # # @return [Numeric, String] The default value for this metric. # @raise ArgumentError if the metric type doesn't have a default value def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end # @private # @return [String] def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end # @private # @return [String] def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end # The metric types that are supported by this library. Note that every StatsD server # implementation only supports a subset of them. TYPES = { c: 'increment', ms: 'measure', g: 'gauge', h: 'histogram', kv: 'key/value', s: 'set', } # Utility function to convert tags to the canonical form. # # - Tags specified as key value pairs will be converted into an array # - Tags are normalized to only use word characters and underscores. # # @param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. # @return [Array<String>, nil] the list of tags in canonical form. def self.() return unless = .map { |k, v| k.to_s + ":".freeze + v.to_s } if .is_a?(Hash) .map { |tag| tag.tr('|,'.freeze, ''.freeze) } end end |
Class Method Details
.normalize_tags(tags) ⇒ Array<String>?
Utility function to convert tags to the canonical form.
-
Tags specified as key value pairs will be converted into an array
-
Tags are normalized to only use word characters and underscores.
103 104 105 106 107 |
# File 'lib/statsd/instrument/metric.rb', line 103 def self.() return unless = .map { |k, v| k.to_s + ":".freeze + v.to_s } if .is_a?(Hash) .map { |tag| tag.tr('|,'.freeze, ''.freeze) } end |
Instance Method Details
#default_value ⇒ Numeric, String
The default value for this metric, which will be used if it is not set.
A default value is only defined for counter metrics (1). For all other metric types, this emthod will raise an ArgumentError.
63 64 65 66 67 68 |
# File 'lib/statsd/instrument/metric.rb', line 63 def default_value case type when :c; 1 else raise ArgumentError, "A value is required for metric type #{type.inspect}." end end |
#inspect ⇒ String
81 82 83 |
# File 'lib/statsd/instrument/metric.rb', line 81 def inspect "#<StatsD::Instrument::Metric #{self.to_s}>" end |
#to_s ⇒ String
72 73 74 75 76 77 |
# File 'lib/statsd/instrument/metric.rb', line 72 def to_s str = "#{TYPES[type]} #{name}:#{value}" str << " @#{sample_rate}" if sample_rate != 1.0 str << " " << .map { |t| "##{t}"}.join(' ') if str end |