Sha256: 900414a25c8018b108f42093387b014de6ee28c85b0b3d3eb83373d3642f82e3

Contents?: true

Size: 2 KB

Versions: 7

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module StatsD
  module Instrument
    # The Datagram class parses and inspects a StatsD datagrams
    #
    # @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.
    class Datagram
      attr_reader :source

      def initialize(source)
        @source = source
      end

      # @return [Float] The sample rate at which this datagram was emitted, between 0 and 1.
      def sample_rate
        parsed_datagram[:sample_rate] ? Float(parsed_datagram[:sample_rate]) : 1.0
      end

      def type
        @type ||= parsed_datagram[:type].to_sym
      end

      def name
        parsed_datagram[:name]
      end

      def value
        @value ||= case type
        when :c
          Integer(parsed_datagram[:value])
        when :g, :h, :d, :kv, :ms
          Float(parsed_datagram[:value])
        when :s
          String(parsed_datagram[:value])
        else
          parsed_datagram[:value]
        end
      end

      def tags
        @tags ||= parsed_datagram[:tags]&.split(",")
      end

      def inspect
        "#<#{self.class.name}:\"#{@source}\">"
      end

      def hash
        source.hash
      end

      def eql?(other)
        case other
        when StatsD::Instrument::Datagram
          source == other.source
        when String
          source == other
        else
          false
        end
      end

      alias_method :==, :eql?

      private

      PARSER = %r{
        \A
        (?<name>[^\:\|\@]+)\:(?<value>[^\:\|\@]+)\|(?<type>c|ms|g|s|h|d)
        (?:\|\@(?<sample_rate>\d*(?:\.\d*)?))?
        (?:\|\#(?<tags>(?:[^\|,]+(?:,[^\|,]+)*)))?
        \n? # In some implementations, the datagram may include a trailing newline.
        \z
      }x

      def parsed_datagram
        @parsed ||= if (match_info = PARSER.match(@source))
          match_info
        else
          raise ArgumentError, "Invalid StatsD datagram: #{@source}"
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
statsd-instrument-3.8.0 lib/statsd/instrument/datagram.rb
statsd-instrument-3.7.0 lib/statsd/instrument/datagram.rb
statsd-instrument-3.6.1 lib/statsd/instrument/datagram.rb
statsd-instrument-3.6.0 lib/statsd/instrument/datagram.rb
statsd-instrument-3.5.12 lib/statsd/instrument/datagram.rb
statsd-instrument-3.5.11 lib/statsd/instrument/datagram.rb
statsd-instrument-3.5.10 lib/statsd/instrument/datagram.rb