Class: OpenC3::Metric

Inherits:
Object show all
Defined in:
lib/openc3/utilities/metric.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(microservice:, scope:) ⇒ Metric

Returns a new instance of Metric.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openc3/utilities/metric.rb', line 49

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

#dataObject (readonly)

Returns the value of attribute data.



46
47
48
# File 'lib/openc3/utilities/metric.rb', line 46

def data
  @data
end

#microserviceObject (readonly)

Returns the value of attribute microservice.



44
45
46
# File 'lib/openc3/utilities/metric.rb', line 44

def microservice
  @microservice
end

#mutexObject (readonly)

Returns the value of attribute mutex.



47
48
49
# File 'lib/openc3/utilities/metric.rb', line 47

def mutex
  @mutex
end

#scopeObject (readonly)

Returns the value of attribute scope.



45
46
47
# File 'lib/openc3/utilities/metric.rb', line 45

def scope
  @scope
end

Class Method Details

.add_update_generator(object) ⇒ Object



128
129
130
# File 'lib/openc3/utilities/metric.rb', line 128

def self.add_update_generator(object)
  @@update_generators << object
end

Instance Method Details

#graceful_killObject



125
126
# File 'lib/openc3/utilities/metric.rb', line 125

def graceful_kill
end

#set(name:, value:, type: nil, unit: nil, help: nil, labels: nil, time_ms: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openc3/utilities/metric.rb', line 67

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



79
80
81
82
83
# File 'lib/openc3/utilities/metric.rb', line 79

def set_multiple(data)
  @mutex.synchonize do
    @data.merge!(data)
  end
end

#shutdownObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/openc3/utilities/metric.rb', line 114

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_bodyObject



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
112
# File 'lib/openc3/utilities/metric.rb', line 85

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