Class: Metriks::Reporter::NewRelic::Sampler

Inherits:
NewRelic::Agent::Sampler
  • Object
show all
Defined in:
lib/metriks/reporter/new_relic/sampler.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sampler

Returns a new instance of Sampler.



6
7
8
9
# File 'lib/metriks/reporter/new_relic/sampler.rb', line 6

def initialize(options={})
  @registry = options[:registry] || Metriks::Registry.default
  @cache = Hash.new
end

Instance Method Details

#newrelic_name(name) ⇒ Object



46
47
48
# File 'lib/metriks/reporter/new_relic/sampler.rb', line 46

def newrelic_name(name)
  "Custom/" + name.gsub('.', '/')
end

#pollObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/metriks/reporter/new_relic/sampler.rb', line 11

def poll
  @registry.each do |name, metric|
    case metric
    when defined?(Metriks::Gauge) && metric.kind_of?(Metriks::Gauge)
      NewRelic::Agent.record_metric(newrelic_name(name), metric.value)
  
    when Metriks::Timer
      count = value_update(name, :count, metric.count) do |old,new|
        new - old
      end
      min = value_update(name, :min, metric.min) do |old,new|
        new < old ? new : old
      end
      max = value_update(name, :max, metric.max) do |old,new|
        new > old ? new : old
      end
      value = {
        :total => metric.mean,
        :count => count,
        :min => min,
        :max => max,
        # NOTE: we lose the total sum of squares, not in the registry
        :sum_of_squares => 0
      }
      ::NewRelic::Agent.record_metric(newrelic_name(name), value)

    when Metriks::Counter
      count = value_update(name, :count, metric.count) do |old,new|
        new - old
      end
      ::NewRelic::Agent.increment_metric(newrelic_name(name), count)
    end
  end
end

#value_update(metric_name, field_name, new_value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/metriks/reporter/new_relic/sampler.rb', line 50

def value_update(metric_name, field_name, new_value)
  cache_key = "#{metric_name}-#{field_name}"
  if @cache[cache_key] == nil
    update = new_value
  else
    update = yield(@cache[cache_key], new_value)
  end
  @cache[cache_key] = update
  return update
end