class StatsD::Instrument::DatagramBuilder

@note This class is part of the new Client implementation that is intended

to become the new default in the next major release of this library.

Attributes

default_tags[R]
prefix[R]

Public Class Methods

datagram_class() click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 26
def self.datagram_class
  StatsD::Instrument::Datagram
end
new(prefix: nil, default_tags: nil) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 30
def initialize(prefix: nil, default_tags: nil)
  @prefix = prefix.nil? ? "" : "#{normalize_name(prefix)}."
  @default_tags = normalize_tags(default_tags)
end
unsupported_datagram_types(*types) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 18
def self.unsupported_datagram_types(*types)
  types.each do |type|
    define_method(type) do |_, _, _, _|
      raise NotImplementedError, "Type #{type} metrics are not supported by #{self.class.name}."
    end
  end
end

Public Instance Methods

c(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 35
def c(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 'c', sample_rate, tags)
end
d(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 55
def d(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 'd', sample_rate, tags)
end
g(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 39
def g(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 'g', sample_rate, tags)
end
h(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 51
def h(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 'h', sample_rate, tags)
end
kv(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 59
def kv(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 'kv', sample_rate, tags)
end
latency_metric_type() click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 63
def latency_metric_type
  :ms
end
ms(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 43
def ms(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 'ms', sample_rate, tags)
end
s(name, value, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 47
def s(name, value, sample_rate, tags)
  generate_generic_datagram(name, value, 's', sample_rate, tags)
end

Protected Instance Methods

generate_generic_datagram(name, value, type, sample_rate, tags) click to toggle source
# File lib/statsd/instrument/datagram_builder.rb, line 94
def generate_generic_datagram(name, value, type, sample_rate, tags)
  tags = normalize_tags(tags) + default_tags
  datagram = +"#{@prefix}#{normalize_name(name)}:#{value}|#{type}"
  datagram << "|@#{sample_rate}" if sample_rate && sample_rate < 1
  datagram << "|##{tags.join(',')}" unless tags.empty?
  datagram
end
normalize_name(name) click to toggle source

Utility function to remove invalid characters from a StatsD metric name

# File lib/statsd/instrument/datagram_builder.rb, line 88
def normalize_name(name)
  # Fast path when no normalization is needed to avoid copying the string
  return name unless /[:|@]/.match?(name)
  name.tr(':|@', '_')
end
normalize_tags(tags) click to toggle source

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 remove unsupported characters

@param tags [Array<String>, Hash<String, String>, nil] Tags specified in any form. @return [Array<String>, nil] the list of tags in canonical form.

# File lib/statsd/instrument/datagram_builder.rb, line 78
def normalize_tags(tags)
  return [] unless tags
  tags = tags.map { |k, v| "#{k}:#{v}" } 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