Class: Atatus::Metrics::Registry Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/atatus/metrics.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

TIMEOUT_INTERVAL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

seconds

5

Constants included from Logging

Logging::LEVELS, Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(config, &block) ⇒ Registry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Registry.



37
38
39
40
# File 'lib/atatus/metrics.rb', line 37

def initialize(config, &block)
  @config = config
  @callback = block
end

Instance Attribute Details

#callbackObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
# File 'lib/atatus/metrics.rb', line 42

def callback
  @callback
end

#configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
# File 'lib/atatus/metrics.rb', line 42

def config
  @config
end

#setsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
# File 'lib/atatus/metrics.rb', line 42

def sets
  @sets
end

Instance Method Details

#collectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



119
120
121
122
123
124
125
# File 'lib/atatus/metrics.rb', line 119

def collect
  sets.each_value.each_with_object([]) do |set, arr|
    samples = set.collect
    next unless samples
    arr.concat(samples)
  end
end

#collect_and_sendObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



110
111
112
113
114
115
116
117
# File 'lib/atatus/metrics.rb', line 110

def collect_and_send
  return unless @config.recording?
  metricsets = collect
  metricsets.compact!
  metricsets.each do |m|
    callback.call(m)
  end
end

#get(key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



106
107
108
# File 'lib/atatus/metrics.rb', line 106

def get(key)
  sets.fetch(key)
end

#handle_forking!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/atatus/metrics.rb', line 94

def handle_forking!
  # Note that ideally we would be able to check if the @timer_task died
  # and restart it. You can't simply check @timer_task.running? because
  # it will only return the state of the TimerTask, not whether the
  # internal thread used to manage the execution interval has died.
  # This is a limitation of the Concurrent::TimerTask object.
  # Therefore, our only option when forked is to stop and start.
  # ~estolfo
  stop
  start
end

#running?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


90
91
92
# File 'lib/atatus/metrics.rb', line 90

def running?
  !!@running
end

#startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/atatus/metrics.rb', line 43

def start
  unless config.collect_metrics?
    debug 'Skipping metrics'
    return
  end

  debug 'Starting metrics'

  # Only set the @sets once, in case we stop
  # and start again.
  @sets ||= {
    system: CpuMemSet,
    # vm: VMSet,
    breakdown: BreakdownSet,
    transaction: TransactionSet
  }.each_with_object({}) do |(key, kls), sets|
    debug "Adding metrics collector '#{kls}'"
    sets[key] = kls.new(config)
  end

  @timer_task = Concurrent::TimerTask.execute(
    run_now: true,
    execution_interval: config.metrics_interval,
    timeout_interval: TIMEOUT_INTERVAL
  ) do
    begin
      collect_and_send
      true
    rescue StandardError => e
      error 'Error while collecting metrics: %e', e.inspect
      debug { e.backtrace.join("\n") }
      false
    end
  end

  @running = true
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
85
86
87
88
# File 'lib/atatus/metrics.rb', line 81

def stop
  return unless running?

  debug 'Stopping metrics'

  @timer_task.shutdown
  @running = false
end