Class: Yodeler::Metric

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, value, opts = {}) ⇒ Metric

Returns a new instance of Metric.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yodeler/metric.rb', line 12

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/yodeler/metric.rb', line 8

def options
  @options
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/yodeler/metric.rb', line 6

def prefix
  @prefix
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



6
7
8
# File 'lib/yodeler/metric.rb', line 6

def sample_rate
  @sample_rate
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/yodeler/metric.rb', line 6

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/yodeler/metric.rb', line 5

def type
  @type
end

#uuidObject (readonly)

Returns the value of attribute uuid.



7
8
9
# File 'lib/yodeler/metric.rb', line 7

def uuid
  @uuid
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/yodeler/metric.rb', line 5

def value
  @value
end

Instance Method Details

#nameObject



25
26
27
# File 'lib/yodeler/metric.rb', line 25

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

#sample?Boolean

Returns Should this metric be sampled.

Returns:

  • (Boolean)

    Should this metric be sampled



30
31
32
# File 'lib/yodeler/metric.rb', line 30

def sample?
  @_sample ||= !(rand > @sample_rate)
end

#to_hashObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yodeler/metric.rb', line 34

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