Sha256: 311a4f0762054eee56dc31cc5fc80a477208c9c70ec1062732e5a382705df47f

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'securerandom'

module Yodeler
  class Metric
    attr_reader :type, :value
    attr_reader :sample_rate, :tags, :prefix
    attr_reader :uuid
    attr_reader :options

    TYPES = [:event, :increment, :gauge, :timing]

    def initialize(type, name, value, opts = {})
      @uuid = SecureRandom.uuid
      @type = type
      @name = name
      @value = value
      @prefix = opts.delete(:prefix)
      @sample_rate = opts.delete(:sample_rate)
      @timestamp = opts.delete(:timestamp)
      @tags = opts.delete(:tags)
      @hostname = opts.delete(:hostname)
    end

    def name
      @prefix ? [@prefix, @name].join('.') : @name
    end

    # @return [Boolean] Should this metric be sampled
    def sample?
      @_sample ||= !(rand > @sample_rate)
    end

    def to_hash
      hash = {
        uuid: uuid,
        name: name,
        type: @type,
        value: @value
      }

      hash[:timestamp] = @timestamp if @timestamp
      hash[:tags] = @tags if @tags && @tags.any?
      hash[:hostname] = @hostname if @hostname

      hash
    end

    TYPES.each do |type_meth|
      define_method("#{type_meth}?") do
        type_meth.to_sym == type
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yodeler-0.1.2 lib/yodeler/metric.rb