Sha256: d2dad815115bb365ce6ba4d4cdb482fea65fc994b115e85a4652112b15bc1012

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 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)
      @meta = opts.delete(:meta) || {} #additional meta data to send
    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,
        meta: @meta
      }

      hash[:meta][:timestamp] = @timestamp if @timestamp
      hash[:meta][:tags] = @tags if @tags && @tags.any?
      hash[:meta][: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.3 lib/yodeler/metric.rb