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.
-
#db_shard ⇒ Object
readonly
Returns the value of attribute db_shard.
-
#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:, db_shard: nil) ⇒ 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:, db_shard: nil) ⇒ Metric
Returns a new instance of Metric.
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 |
# File 'lib/openc3/utilities/metric.rb', line 44 def initialize(microservice:, scope:, db_shard: nil) @scope = scope @microservice = microservice @data = {} @mutex = Mutex.new if db_shard @db_shard = db_shard else # Look up db_shard from MicroserviceModel begin json = Store.hget('openc3_microservices', microservice) @db_shard = json ? JSON.parse(json)['db_shard'].to_i : 0 rescue @db_shard = 0 end end # 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.
41 42 43 |
# File 'lib/openc3/utilities/metric.rb', line 41 def data @data end |
#db_shard ⇒ Object (readonly)
Returns the value of attribute db_shard.
40 41 42 |
# File 'lib/openc3/utilities/metric.rb', line 40 def db_shard @db_shard end |
#microservice ⇒ Object (readonly)
Returns the value of attribute microservice.
38 39 40 |
# File 'lib/openc3/utilities/metric.rb', line 38 def microservice @microservice end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
42 43 44 |
# File 'lib/openc3/utilities/metric.rb', line 42 def mutex @mutex end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
39 40 41 |
# File 'lib/openc3/utilities/metric.rb', line 39 def scope @scope end |
Class Method Details
.add_update_generator(object) ⇒ Object
136 137 138 |
# File 'lib/openc3/utilities/metric.rb', line 136 def self.add_update_generator(object) @@update_generators << object end |
Instance Method Details
#graceful_kill ⇒ Object
133 134 |
# File 'lib/openc3/utilities/metric.rb', line 133 def graceful_kill end |
#set(name:, value:, type: nil, unit: nil, help: nil, labels: nil, time_ms: nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/openc3/utilities/metric.rb', line 74 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
86 87 88 89 90 |
# File 'lib/openc3/utilities/metric.rb', line 86 def set_multiple(data) @mutex.synchronize do @data.merge!(data) end end |
#shutdown ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/openc3/utilities/metric.rb', line 122 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openc3/utilities/metric.rb', line 92 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 json['db_shard'] = instance.db_shard 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 |