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
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.
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/openc3/utilities/metric.rb', line 46 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.
43 44 45 |
# File 'lib/openc3/utilities/metric.rb', line 43 def data @data end |
#microservice ⇒ Object (readonly)
Returns the value of attribute microservice.
41 42 43 |
# File 'lib/openc3/utilities/metric.rb', line 41 def microservice @microservice end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
44 45 46 |
# File 'lib/openc3/utilities/metric.rb', line 44 def mutex @mutex end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
42 43 44 |
# File 'lib/openc3/utilities/metric.rb', line 42 def scope @scope end |
Instance Method Details
#graceful_kill ⇒ Object
117 118 |
# File 'lib/openc3/utilities/metric.rb', line 117 def graceful_kill end |
#set(name:, value:, type: nil, unit: nil, help: nil, labels: nil, time_ms: nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/openc3/utilities/metric.rb', line 64 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
76 77 78 79 80 |
# File 'lib/openc3/utilities/metric.rb', line 76 def set_multiple(data) @mutex.synchonize do @data.merge!(data) end end |
#shutdown ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/openc3/utilities/metric.rb', line 106 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/openc3/utilities/metric.rb', line 82 def update_thread_body @@update_sleeper = Sleeper.new while true start_time = Time.now @@mutex.synchronize do @@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 |