Class: OpenC3::Metric
Constant Summary collapse
- UPDATE_INTERVAL =
The update interval. How often in seconds metrics are updated by this process
5
- @@mutex =
Mutex protecting class variables
Mutex.new
- @@instances =
Array of instances used to keep track of metrics
[]
- @@update_thread =
Thread used to post metrics across all classes
nil
- @@update_sleeper =
Sleeper used to delay update thread
nil
- @@update_generators =
Objects with a generate method to be called on each metric cycle (to generate metrics)
[]
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#microservice ⇒ Object
readonly
Returns the value of attribute microservice.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
- #graceful_kill ⇒ Object
-
#initialize(microservice:, scope:) ⇒ Metric
constructor
A new instance of Metric.
- #set(name:, value:, type: nil, unit: nil, help: nil, labels: nil, time_ms: nil) ⇒ Object
- #set_multiple(data) ⇒ Object
- #shutdown ⇒ Object
- #update_thread_body ⇒ Object
Constructor Details
#initialize(microservice:, scope:) ⇒ Metric
Returns a new instance of Metric.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/openc3/utilities/metric.rb', line 48 def initialize(microservice:, scope:) @scope = scope @microservice = microservice @data = {} @mutex = Mutex.new # Always make sure there is a update thread @@mutex.synchronize do @@instances << self unless @@update_thread @@update_thread = OpenC3.safe_thread("Metrics") do update_thread_body() end end end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
45 46 47 |
# File 'lib/openc3/utilities/metric.rb', line 45 def data @data end |
#microservice ⇒ Object (readonly)
Returns the value of attribute microservice.
43 44 45 |
# File 'lib/openc3/utilities/metric.rb', line 43 def microservice @microservice end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
46 47 48 |
# File 'lib/openc3/utilities/metric.rb', line 46 def mutex @mutex end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
44 45 46 |
# File 'lib/openc3/utilities/metric.rb', line 44 def scope @scope end |
Class Method Details
.add_update_generator(object) ⇒ Object
127 128 129 |
# File 'lib/openc3/utilities/metric.rb', line 127 def self.add_update_generator(object) @@update_generators << object end |
Instance Method Details
#graceful_kill ⇒ Object
124 125 |
# File 'lib/openc3/utilities/metric.rb', line 124 def graceful_kill end |
#set(name:, value:, type: nil, unit: nil, help: nil, labels: nil, time_ms: nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/openc3/utilities/metric.rb', line 66 def set(name:, value:, type: nil, unit: nil, help: nil, labels: nil, time_ms: nil) @mutex.synchronize do @data[name] ||= {} @data[name]['value'] = value @data[name]['type'] = type if type @data[name]['unit'] = unit if unit @data[name]['help'] = help if help @data[name]['labels'] = labels if labels @data[name]['time_ms'] = time_ms if time_ms end end |
#set_multiple(data) ⇒ Object
78 79 80 81 82 |
# File 'lib/openc3/utilities/metric.rb', line 78 def set_multiple(data) @mutex.synchronize do @data.merge!(data) end end |
#shutdown ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/openc3/utilities/metric.rb', line 113 def shutdown @@mutex.synchronize do @@instances.delete(self) if @@instances.length <= 0 @@update_sleeper.cancel if @@update_sleeper OpenC3.kill_thread(self, @@update_thread) if @@update_thread @@update_thread = nil end end end |
#update_thread_body ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/openc3/utilities/metric.rb', line 84 def update_thread_body @@update_sleeper = Sleeper.new while true start_time = Time.now @@mutex.synchronize do @@update_generators.each do |generator| generator.generate(@@instances[0]) end @@instances.each do |instance| instance.mutex.synchronize do json = {} json['name'] = instance.microservice values = instance.data json['values'] = values MetricModel.set(json, scope: instance.scope) if values.length > 0 end end end # Only check whether to update at a set interval run_time = Time.now - start_time sleep_time = UPDATE_INTERVAL - run_time sleep_time = 0 if sleep_time < 0 break if @@update_sleeper.sleep(sleep_time) end end |