Class: StatsD::Instrument::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd/instrument/metric.rb

Overview

The Metric class represents a metric sample to be send by a backend.

Defined Under Namespace

Modules: RubyBackports

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',
  d: 'distribution',
  kv: 'key/value',
  s: 'set',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, value, sample_rate, tags, metadata) ⇒ 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.

Parameters:

  • type (Symbol)

    The type of the metric.

  • name (Hash)

    a customizable set of options

  • value (Hash)

    a customizable set of options

  • sample_rate (Hash)

    a customizable set of options

  • tags (Hash)

    a customizable set of options

Options Hash (name):

  • :name (String)

    The name of the metric without prefix.

Options Hash (value):

  • The (Numeric, String, nil)

    value to collect for the metric.

Options Hash (sample_rate):

Options Hash (tags):

  • :tags (Array<String>, Hash<String, String>, nil)

    The tags to apply to this metric. See normalize_tags for more information.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/statsd/instrument/metric.rb', line 85

def initialize(type, name, value, sample_rate, tags, ) # rubocop:disable Metrics/ParameterLists
  raise ArgumentError, "Metric :type is required." unless type
  raise ArgumentError, "Metric :name is required." unless name
  raise ArgumentError, "Metric :value is required." unless value

  @type = type
  @name = normalize_name(name)
  @value = value
  @sample_rate = sample_rate
  @tags = StatsD::Instrument::Metric.normalize_tags(tags)
  if StatsD.legacy_singleton_client.default_tags
    @tags = Array(@tags) + StatsD.legacy_singleton_client.default_tags
  end
  @metadata = 
end

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



72
73
74
# File 'lib/statsd/instrument/metric.rb', line 72

def 
  @metadata
end

#nameString

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 or is overridden by the :prefix option. Note that :no_prefix has greater precedence than :prefix.

Returns:

  • (String)

    The name of the metric. StatsD#prefix will automatically be applied to the metric in the constructor, unless the :no_prefix option is set or is overridden by the :prefix option. Note that :no_prefix has greater precedence than :prefix.



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/statsd/instrument/metric.rb', line 33

class StatsD::Instrument::Metric
  unless Regexp.method_defined?(:match?) # for ruby 2.3
    module RubyBackports
      refine Regexp do
        def match?(str)
          (self =~ str) != nil
        end
      end
    end

    using RubyBackports
  end

  def self.new(type:, name:, value: default_value(type), tags: nil, metadata: nil,
    sample_rate: StatsD.legacy_singleton_client.default_sample_rate)

    # pass keyword arguments as positional arguments for performance reasons,
    # since MRI's C implementation of new turns keyword arguments into a hash
    super(type, name, value, sample_rate, 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>.
  #
  #
  # 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 self.default_value(type)
    case type
    when :c then 1
    else raise ArgumentError, "A value is required for metric type #{type.inspect}."
    end
  end

  attr_accessor :type, :name, :value, :sample_rate, :tags, :metadata

  # 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.
  #
  # @param type [Symbol] The type of the metric.
  # @option name [String] :name The name of the metric without prefix.
  # @option value [Numeric, String, nil] The value to collect for the metric.
  # @option sample_rate [Numeric, nil] The sample rate to use. If not set, it will use
  #   {StatsD#default_sample_rate}.
  # @option tags [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric.
  #   See {.normalize_tags} for more information.
  def initialize(type, name, value, sample_rate, tags, ) # rubocop:disable Metrics/ParameterLists
    raise ArgumentError, "Metric :type is required." unless type
    raise ArgumentError, "Metric :name is required." unless name
    raise ArgumentError, "Metric :value is required." unless value

    @type = type
    @name = normalize_name(name)
    @value = value
    @sample_rate = sample_rate
    @tags = StatsD::Instrument::Metric.normalize_tags(tags)
    if StatsD.legacy_singleton_client.default_tags
      @tags = Array(@tags) + StatsD.legacy_singleton_client.default_tags
    end
    @metadata = 
  end

  # @private
  # @return [String]
  def to_s
    str = +"#{name}:#{value}|#{type}"
    str << "|@#{sample_rate}" if sample_rate && sample_rate != 1.0
    str << "|#" << tags.join(',') if tags && !tags.empty?
    str
  end

  # @private
  # @return [String]
  def inspect
    "#<StatsD::Instrument::Metric #{self}>"
  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',
    d: 'distribution',
    kv: 'key/value',
    s: 'set',
  }

  # Strip metric names of special characters used by StatsD line protocol, replace with underscore
  #
  # @param name [String]
  # @return [String]
  def normalize_name(name)
    # fast path when no normalization is needed to avoid copying the string
    return name unless /[:|@]/.match?(name)

    name.tr(':|@', '_')
  end

  # 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.normalize_tags(tags)
    return unless tags
    tags = tags.map { |k, v| k.to_s + ":" + v.to_s } if tags.is_a?(Hash)

    # fast path when no string replacement is needed
    return tags unless tags.any? { |tag| /[|,]/.match?(tag) }

    tags.map { |tag| tag.tr('|,', '') }
  end
end

#sample_rateFloat

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.

Returns:

  • (Float)

    The sample rate to use for this metric. This should be a value between 0 and 1. If not set, it will use the default sample rate set to StatsD#default_sample_rate.

See Also:



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/statsd/instrument/metric.rb', line 33

class StatsD::Instrument::Metric
  unless Regexp.method_defined?(:match?) # for ruby 2.3
    module RubyBackports
      refine Regexp do
        def match?(str)
          (self =~ str) != nil
        end
      end
    end

    using RubyBackports
  end

  def self.new(type:, name:, value: default_value(type), tags: nil, metadata: nil,
    sample_rate: StatsD.legacy_singleton_client.default_sample_rate)

    # pass keyword arguments as positional arguments for performance reasons,
    # since MRI's C implementation of new turns keyword arguments into a hash
    super(type, name, value, sample_rate, 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>.
  #
  #
  # 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 self.default_value(type)
    case type
    when :c then 1
    else raise ArgumentError, "A value is required for metric type #{type.inspect}."
    end
  end

  attr_accessor :type, :name, :value, :sample_rate, :tags, :metadata

  # 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.
  #
  # @param type [Symbol] The type of the metric.
  # @option name [String] :name The name of the metric without prefix.
  # @option value [Numeric, String, nil] The value to collect for the metric.
  # @option sample_rate [Numeric, nil] The sample rate to use. If not set, it will use
  #   {StatsD#default_sample_rate}.
  # @option tags [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric.
  #   See {.normalize_tags} for more information.
  def initialize(type, name, value, sample_rate, tags, ) # rubocop:disable Metrics/ParameterLists
    raise ArgumentError, "Metric :type is required." unless type
    raise ArgumentError, "Metric :name is required." unless name
    raise ArgumentError, "Metric :value is required." unless value

    @type = type
    @name = normalize_name(name)
    @value = value
    @sample_rate = sample_rate
    @tags = StatsD::Instrument::Metric.normalize_tags(tags)
    if StatsD.legacy_singleton_client.default_tags
      @tags = Array(@tags) + StatsD.legacy_singleton_client.default_tags
    end
    @metadata = 
  end

  # @private
  # @return [String]
  def to_s
    str = +"#{name}:#{value}|#{type}"
    str << "|@#{sample_rate}" if sample_rate && sample_rate != 1.0
    str << "|#" << tags.join(',') if tags && !tags.empty?
    str
  end

  # @private
  # @return [String]
  def inspect
    "#<StatsD::Instrument::Metric #{self}>"
  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',
    d: 'distribution',
    kv: 'key/value',
    s: 'set',
  }

  # Strip metric names of special characters used by StatsD line protocol, replace with underscore
  #
  # @param name [String]
  # @return [String]
  def normalize_name(name)
    # fast path when no normalization is needed to avoid copying the string
    return name unless /[:|@]/.match?(name)

    name.tr(':|@', '_')
  end

  # 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.normalize_tags(tags)
    return unless tags
    tags = tags.map { |k, v| k.to_s + ":" + v.to_s } if tags.is_a?(Hash)

    # fast path when no string replacement is needed
    return tags unless tags.any? { |tag| /[|,]/.match?(tag) }

    tags.map { |tag| tag.tr('|,', '') }
  end
end

#tagsArray<String>, ...

Note:

Only the Datadog implementation supports tags.

The tags to associate with the metric.

Returns:

  • (Array<String>, Hash<String, String>, nil)

    the tags to associate with the metric. You can either specify the tags as an array of strings, or a Hash of key/value pairs.

See Also:



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/statsd/instrument/metric.rb', line 33

class StatsD::Instrument::Metric
  unless Regexp.method_defined?(:match?) # for ruby 2.3
    module RubyBackports
      refine Regexp do
        def match?(str)
          (self =~ str) != nil
        end
      end
    end

    using RubyBackports
  end

  def self.new(type:, name:, value: default_value(type), tags: nil, metadata: nil,
    sample_rate: StatsD.legacy_singleton_client.default_sample_rate)

    # pass keyword arguments as positional arguments for performance reasons,
    # since MRI's C implementation of new turns keyword arguments into a hash
    super(type, name, value, sample_rate, 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>.
  #
  #
  # 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 self.default_value(type)
    case type
    when :c then 1
    else raise ArgumentError, "A value is required for metric type #{type.inspect}."
    end
  end

  attr_accessor :type, :name, :value, :sample_rate, :tags, :metadata

  # 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.
  #
  # @param type [Symbol] The type of the metric.
  # @option name [String] :name The name of the metric without prefix.
  # @option value [Numeric, String, nil] The value to collect for the metric.
  # @option sample_rate [Numeric, nil] The sample rate to use. If not set, it will use
  #   {StatsD#default_sample_rate}.
  # @option tags [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric.
  #   See {.normalize_tags} for more information.
  def initialize(type, name, value, sample_rate, tags, ) # rubocop:disable Metrics/ParameterLists
    raise ArgumentError, "Metric :type is required." unless type
    raise ArgumentError, "Metric :name is required." unless name
    raise ArgumentError, "Metric :value is required." unless value

    @type = type
    @name = normalize_name(name)
    @value = value
    @sample_rate = sample_rate
    @tags = StatsD::Instrument::Metric.normalize_tags(tags)
    if StatsD.legacy_singleton_client.default_tags
      @tags = Array(@tags) + StatsD.legacy_singleton_client.default_tags
    end
    @metadata = 
  end

  # @private
  # @return [String]
  def to_s
    str = +"#{name}:#{value}|#{type}"
    str << "|@#{sample_rate}" if sample_rate && sample_rate != 1.0
    str << "|#" << tags.join(',') if tags && !tags.empty?
    str
  end

  # @private
  # @return [String]
  def inspect
    "#<StatsD::Instrument::Metric #{self}>"
  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',
    d: 'distribution',
    kv: 'key/value',
    s: 'set',
  }

  # Strip metric names of special characters used by StatsD line protocol, replace with underscore
  #
  # @param name [String]
  # @return [String]
  def normalize_name(name)
    # fast path when no normalization is needed to avoid copying the string
    return name unless /[:|@]/.match?(name)

    name.tr(':|@', '_')
  end

  # 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.normalize_tags(tags)
    return unless tags
    tags = tags.map { |k, v| k.to_s + ":" + v.to_s } if tags.is_a?(Hash)

    # fast path when no string replacement is needed
    return tags unless tags.any? { |tag| /[|,]/.match?(tag) }

    tags.map { |tag| tag.tr('|,', '') }
  end
end

#typeSymbol

Returns The metric type. Must be one of TYPES.

Returns:

  • (Symbol)

    The metric type. Must be one of TYPES



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/statsd/instrument/metric.rb', line 33

class StatsD::Instrument::Metric
  unless Regexp.method_defined?(:match?) # for ruby 2.3
    module RubyBackports
      refine Regexp do
        def match?(str)
          (self =~ str) != nil
        end
      end
    end

    using RubyBackports
  end

  def self.new(type:, name:, value: default_value(type), tags: nil, metadata: nil,
    sample_rate: StatsD.legacy_singleton_client.default_sample_rate)

    # pass keyword arguments as positional arguments for performance reasons,
    # since MRI's C implementation of new turns keyword arguments into a hash
    super(type, name, value, sample_rate, 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>.
  #
  #
  # 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 self.default_value(type)
    case type
    when :c then 1
    else raise ArgumentError, "A value is required for metric type #{type.inspect}."
    end
  end

  attr_accessor :type, :name, :value, :sample_rate, :tags, :metadata

  # 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.
  #
  # @param type [Symbol] The type of the metric.
  # @option name [String] :name The name of the metric without prefix.
  # @option value [Numeric, String, nil] The value to collect for the metric.
  # @option sample_rate [Numeric, nil] The sample rate to use. If not set, it will use
  #   {StatsD#default_sample_rate}.
  # @option tags [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric.
  #   See {.normalize_tags} for more information.
  def initialize(type, name, value, sample_rate, tags, ) # rubocop:disable Metrics/ParameterLists
    raise ArgumentError, "Metric :type is required." unless type
    raise ArgumentError, "Metric :name is required." unless name
    raise ArgumentError, "Metric :value is required." unless value

    @type = type
    @name = normalize_name(name)
    @value = value
    @sample_rate = sample_rate
    @tags = StatsD::Instrument::Metric.normalize_tags(tags)
    if StatsD.legacy_singleton_client.default_tags
      @tags = Array(@tags) + StatsD.legacy_singleton_client.default_tags
    end
    @metadata = 
  end

  # @private
  # @return [String]
  def to_s
    str = +"#{name}:#{value}|#{type}"
    str << "|@#{sample_rate}" if sample_rate && sample_rate != 1.0
    str << "|#" << tags.join(',') if tags && !tags.empty?
    str
  end

  # @private
  # @return [String]
  def inspect
    "#<StatsD::Instrument::Metric #{self}>"
  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',
    d: 'distribution',
    kv: 'key/value',
    s: 'set',
  }

  # Strip metric names of special characters used by StatsD line protocol, replace with underscore
  #
  # @param name [String]
  # @return [String]
  def normalize_name(name)
    # fast path when no normalization is needed to avoid copying the string
    return name unless /[:|@]/.match?(name)

    name.tr(':|@', '_')
  end

  # 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.normalize_tags(tags)
    return unless tags
    tags = tags.map { |k, v| k.to_s + ":" + v.to_s } if tags.is_a?(Hash)

    # fast path when no string replacement is needed
    return tags unless tags.any? { |tag| /[|,]/.match?(tag) }

    tags.map { |tag| tag.tr('|,', '') }
  end
end

#valueNumeric, String

Returns The value to collect for the metric. Depending on the metric type, value can be a string, integer, or float.

Returns:

  • (Numeric, String)

    The value to collect for the metric. Depending on the metric type, value can be a string, integer, or float.

See Also:

  • #default_value


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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/statsd/instrument/metric.rb', line 33

class StatsD::Instrument::Metric
  unless Regexp.method_defined?(:match?) # for ruby 2.3
    module RubyBackports
      refine Regexp do
        def match?(str)
          (self =~ str) != nil
        end
      end
    end

    using RubyBackports
  end

  def self.new(type:, name:, value: default_value(type), tags: nil, metadata: nil,
    sample_rate: StatsD.legacy_singleton_client.default_sample_rate)

    # pass keyword arguments as positional arguments for performance reasons,
    # since MRI's C implementation of new turns keyword arguments into a hash
    super(type, name, value, sample_rate, 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>.
  #
  #
  # 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 self.default_value(type)
    case type
    when :c then 1
    else raise ArgumentError, "A value is required for metric type #{type.inspect}."
    end
  end

  attr_accessor :type, :name, :value, :sample_rate, :tags, :metadata

  # 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.
  #
  # @param type [Symbol] The type of the metric.
  # @option name [String] :name The name of the metric without prefix.
  # @option value [Numeric, String, nil] The value to collect for the metric.
  # @option sample_rate [Numeric, nil] The sample rate to use. If not set, it will use
  #   {StatsD#default_sample_rate}.
  # @option tags [Array<String>, Hash<String, String>, nil] :tags The tags to apply to this metric.
  #   See {.normalize_tags} for more information.
  def initialize(type, name, value, sample_rate, tags, ) # rubocop:disable Metrics/ParameterLists
    raise ArgumentError, "Metric :type is required." unless type
    raise ArgumentError, "Metric :name is required." unless name
    raise ArgumentError, "Metric :value is required." unless value

    @type = type
    @name = normalize_name(name)
    @value = value
    @sample_rate = sample_rate
    @tags = StatsD::Instrument::Metric.normalize_tags(tags)
    if StatsD.legacy_singleton_client.default_tags
      @tags = Array(@tags) + StatsD.legacy_singleton_client.default_tags
    end
    @metadata = 
  end

  # @private
  # @return [String]
  def to_s
    str = +"#{name}:#{value}|#{type}"
    str << "|@#{sample_rate}" if sample_rate && sample_rate != 1.0
    str << "|#" << tags.join(',') if tags && !tags.empty?
    str
  end

  # @private
  # @return [String]
  def inspect
    "#<StatsD::Instrument::Metric #{self}>"
  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',
    d: 'distribution',
    kv: 'key/value',
    s: 'set',
  }

  # Strip metric names of special characters used by StatsD line protocol, replace with underscore
  #
  # @param name [String]
  # @return [String]
  def normalize_name(name)
    # fast path when no normalization is needed to avoid copying the string
    return name unless /[:|@]/.match?(name)

    name.tr(':|@', '_')
  end

  # 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.normalize_tags(tags)
    return unless tags
    tags = tags.map { |k, v| k.to_s + ":" + v.to_s } if tags.is_a?(Hash)

    # fast path when no string replacement is needed
    return tags unless tags.any? { |tag| /[|,]/.match?(tag) }

    tags.map { |tag| tag.tr('|,', '') }
  end
end

Class Method Details

.default_value(type) ⇒ 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.

A default value is only defined for counter metrics (1). For all other metric types, this emthod will raise an ArgumentError.

Returns:

  • (Numeric, String)

    The default value for this metric.

Raises:

  • ArgumentError if the metric type doesn't have a default value



65
66
67
68
69
70
# File 'lib/statsd/instrument/metric.rb', line 65

def self.default_value(type)
  case type
  when :c then 1
  else raise ArgumentError, "A value is required for metric type #{type.inspect}."
  end
end

.new(type:, name:, value: default_value(type), tags: nil, metadata: nil, sample_rate: StatsD.legacy_singleton_client.default_sample_rate) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/statsd/instrument/metric.rb', line 46

def self.new(type:, name:, value: default_value(type), tags: nil, metadata: nil,
  sample_rate: StatsD.legacy_singleton_client.default_sample_rate)

  # pass keyword arguments as positional arguments for performance reasons,
  # since MRI's C implementation of new turns keyword arguments into a hash
  super(type, name, value, sample_rate, tags, )
end

.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.

Parameters:

  • tags (Array<String>, Hash<String, String>, nil)

    Tags specified in any form.

Returns:

  • (Array<String>, nil)

    the list of tags in canonical form.



146
147
148
149
150
151
152
153
154
# File 'lib/statsd/instrument/metric.rb', line 146

def self.normalize_tags(tags)
  return unless tags
  tags = tags.map { |k, v| k.to_s + ":" + v.to_s } if tags.is_a?(Hash)

  # fast path when no string replacement is needed
  return tags unless tags.any? { |tag| /[|,]/.match?(tag) }

  tags.map { |tag| tag.tr('|,', '') }
end

Instance Method Details

#inspectString

Returns:

  • (String)


112
113
114
# File 'lib/statsd/instrument/metric.rb', line 112

def inspect
  "#<StatsD::Instrument::Metric #{self}>"
end

#normalize_name(name) ⇒ String

Strip metric names of special characters used by StatsD line protocol, replace with underscore

Parameters:

  • name (String)

Returns:

  • (String)


132
133
134
135
136
137
# File 'lib/statsd/instrument/metric.rb', line 132

def normalize_name(name)
  # fast path when no normalization is needed to avoid copying the string
  return name unless /[:|@]/.match?(name)

  name.tr(':|@', '_')
end

#to_sString

Returns:

  • (String)


103
104
105
106
107
108
# File 'lib/statsd/instrument/metric.rb', line 103

def to_s
  str = +"#{name}:#{value}|#{type}"
  str << "|@#{sample_rate}" if sample_rate && sample_rate != 1.0
  str << "|#" << tags.join(',') if tags && !tags.empty?
  str
end