Class: OpenC3::MetricModel

Inherits:
EphemeralModel show all
Includes:
DbShardedModel
Defined in:
lib/openc3/models/metric_model.rb

Constant Summary collapse

PRIMARY_KEY =
'__openc3__metric'.freeze
METRIC_EXPIRE_SECONDS =

Expire metrics after 1 hour

3600

Instance Attribute Summary collapse

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DbShardedModel

#_db_sharded_create, #_db_sharded_destroy, included

Methods inherited from EphemeralModel

store, store_queued

Methods inherited from Model

#check_disable_erb, #deploy, #destroyed?, #diff, filter, find_all_by_plugin, from_json, get_all_models, get_model, handle_config, store, store_queued, #undeploy, #update

Constructor Details

#initialize(name:, values: {}, db_shard: 0, scope:) ⇒ MetricModel

Returns a new instance of MetricModel.



74
75
76
77
78
# File 'lib/openc3/models/metric_model.rb', line 74

def initialize(name:, values: {}, db_shard: 0, scope:)
  super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope)
  @values = values
  @db_shard = db_shard.to_i
end

Instance Attribute Details

#db_shardObject

Returns the value of attribute db_shard.



29
30
31
# File 'lib/openc3/models/metric_model.rb', line 29

def db_shard
  @db_shard
end

#valuesObject

Returns the value of attribute values.



28
29
30
# File 'lib/openc3/models/metric_model.rb', line 28

def values
  @values
end

Class Method Details

._collect_db_shards(scope:) ⇒ Object

Collect all unique db_shard values from MicroserviceModels.



38
39
40
41
42
43
44
45
# File 'lib/openc3/models/metric_model.rb', line 38

def self._collect_db_shards(scope:)
  db_shards = Set.new([0])
  Store.hgetall('openc3_microservices').each do |name, json|
    next if scope and name.split("__")[0] != scope
    db_shards << (JSON.parse(json, allow_nan: true, create_additions: true)['db_shard'] || 0).to_i
  end
  db_shards
end

._lookup_db_shard(name, scope:) ⇒ Object

Look up db_shard from the corresponding MicroserviceModel.



32
33
34
35
# File 'lib/openc3/models/metric_model.rb', line 32

def self._lookup_db_shard(name, scope:) # NOSONAR
  json = Store.hget('openc3_microservices', name)
  json ? (JSON.parse(json, allow_nan: true, create_additions: true)['db_shard'] || 0).to_i : 0
end

.all(scope:) ⇒ Object



57
58
59
# File 'lib/openc3/models/metric_model.rb', line 57

def self.all(scope:)
  _db_sharded_all("#{scope}#{PRIMARY_KEY}", scope: scope)
end

.destroy(scope:, name:) ⇒ Object



69
70
71
72
# File 'lib/openc3/models/metric_model.rb', line 69

def self.destroy(scope:, name:)
  db_shard = _db_shard_for_name(name, scope: scope)
  store.instance(db_shard: db_shard).hdel("#{scope}#{PRIMARY_KEY}", name)
end

.get(name:, scope:) ⇒ Object

NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work



49
50
51
# File 'lib/openc3/models/metric_model.rb', line 49

def self.get(name:, scope:)
  _db_sharded_get("#{scope}#{PRIMARY_KEY}", name: name, scope: scope)
end

.names(scope:) ⇒ Object



53
54
55
# File 'lib/openc3/models/metric_model.rb', line 53

def self.names(scope:)
  _db_sharded_names("#{scope}#{PRIMARY_KEY}", scope: scope)
end

.redis_extract_p50_and_p99_seconds(value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/openc3/models/metric_model.rb', line 97

def self.redis_extract_p50_and_p99_seconds(value)
  if value
    split_value = value.to_s.split(',')
    p50 = split_value[0].split('=')[-1].to_f / 1_000_000
    p99 = split_value[-1].split('=')[-1].to_f / 1_000_000
    return p50, p99
  else
    return 0.0, 0.0
  end
end

.redis_metricsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/openc3/models/metric_model.rb', line 108

def self.redis_metrics
  # This prevents a circular dependency
  require 'openc3/models/scope_model' # NOSONAR
  require 'openc3/models/target_model' # NOSONAR

  db_shards = Set.new
  OpenC3::ScopeModel.names.each do |scope|
    targets = OpenC3::TargetModel.all(scope: scope)
    targets.each do |_target_name, target_hash|
      db_shards << target_hash['db_shard'].to_i
    end
  end

  result = {}
  db_shards.each do |index|
    db_shard_result = {}
    metrics = OpenC3::Store.instance(db_shard: index).info("all")
    db_shard_result["redis_connected_clients_total"] = metrics['connected_clients']
    db_shard_result["redis_used_memory_rss_total"] = metrics['used_memory_rss']
    db_shard_result["redis_commands_processed_total"] = metrics['total_commands_processed']
    db_shard_result["redis_iops"] = metrics['instantaneous_ops_per_sec']
    db_shard_result["redis_instantaneous_input_kbps"] = metrics['instantaneous_input_kbps']
    db_shard_result["redis_instantaneous_output_kbps"] = metrics['instantaneous_output_kbps']
    db_shard_result["redis_instantaneous_eventloop_cps"] = metrics['instantaneous_eventloop_cycles_per_sec']
    db_shard_result["redis_instantaneous_eventloop_duration_usec"] = metrics['instantaneous_eventloop_duration_usec']
    db_shard_result["redis_cpu_sys"] = metrics['used_cpu_sys']
    db_shard_result["redis_cpu_user"] = metrics['used_cpu_user']
    db_shard_result["redis_error_noauth_total"] = metrics['errorstat_NOAUTH'].to_s.split("count=")[-1].to_i
    db_shard_result["redis_error_noperm_total"] = metrics['errorstat_NOPERM'].to_s.split("count=")[-1].to_i
    db_shard_result["redis_hget_p50_seconds"], db_shard_result["redis_hget_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_hget'])
    db_shard_result["redis_hgetall_p50_seconds"], db_shard_result["redis_hgetall_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_hgetall'])
    db_shard_result["redis_hset_p50_seconds"], db_shard_result["redis_hset_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_hset'])
    db_shard_result["redis_xadd_p50_seconds"], db_shard_result["redis_xadd_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xadd'])
    db_shard_result["redis_xread_p50_seconds"], db_shard_result["redis_xread_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xread'])
    db_shard_result["redis_xrevrange_p50_seconds"], db_shard_result["redis_xrevrange_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xrevrange'])
    db_shard_result["redis_xtrim_p50_seconds"], db_shard_result["redis_xtrim_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xtrim'])

    metrics = OpenC3::EphemeralStore.instance(db_shard: index).info("all")
    db_shard_result["redis_ephemeral_connected_clients_total"] = metrics['connected_clients']
    db_shard_result["redis_ephemeral_used_memory_rss_total"] = metrics['used_memory_rss']
    db_shard_result["redis_ephemeral_commands_processed_total"] = metrics['total_commands_processed']
    db_shard_result["redis_ephemeral_iops"] = metrics['instantaneous_ops_per_sec']
    db_shard_result["redis_ephemeral_instantaneous_input_kbps"] = metrics['instantaneous_input_kbps']
    db_shard_result["redis_ephemeral_instantaneous_output_kbps"] = metrics['instantaneous_output_kbps']
    db_shard_result["redis_ephemeral_instantaneous_eventloop_cps"] = metrics['instantaneous_eventloop_cycles_per_sec']
    db_shard_result["redis_ephemeral_instantaneous_eventloop_duration_usec"] = metrics['instantaneous_eventloop_duration_usec']
    db_shard_result["redis_ephemeral_cpu_sys"] = metrics['used_cpu_sys']
    db_shard_result["redis_ephemeral_cpu_user"] = metrics['used_cpu_user']
    db_shard_result["redis_ephemeral_error_noauth_total"] = metrics['errorstat_NOAUTH'].to_s.split("count=")[-1].to_i
    db_shard_result["redis_ephemeral_error_noperm_total"] = metrics['errorstat_NOPERM'].to_s.split("count=")[-1].to_i
    db_shard_result["redis_ephemeral_hget_p50_seconds"], db_shard_result["redis_ephemeral_hget_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_hget'])
    db_shard_result["redis_ephemeral_hgetall_p50_seconds"], db_shard_result["redis_ephemeral_hgetall_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_hgetall'])
    db_shard_result["redis_ephemeral_hset_p50_seconds"], db_shard_result["redis_ephemeral_hset_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_hset'])
    db_shard_result["redis_ephemeral_xadd_p50_seconds"], db_shard_result["redis_ephemeral_xadd_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xadd'])
    db_shard_result["redis_ephemeral_xread_p50_seconds"], db_shard_result["redis_ephemeral_xread_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xread'])
    db_shard_result["redis_ephemeral_xrevrange_p50_seconds"], db_shard_result["redis_ephemeral_xrevrange_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xrevrange'])
    db_shard_result["redis_ephemeral_xtrim_p50_seconds"], db_shard_result["redis_ephemeral_xtrim_p99_seconds"] = redis_extract_p50_and_p99_seconds(metrics['latency_percentiles_usec_xtrim'])
    result[index] = db_shard_result
  end

  return result
end

.set(json, scope:, queued: true) ⇒ Object

Sets (updates) the redis hash of this model Queued defaults to true for MetricModel



63
64
65
66
67
# File 'lib/openc3/models/metric_model.rb', line 63

def self.set(json, scope:, queued: true)
  json[:scope] = scope
  json.transform_keys!(&:to_sym)
  self.new(**json).create(force: true, queued: queued, expire_seconds: METRIC_EXPIRE_SECONDS)
end

Instance Method Details

#as_json(*a) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/openc3/models/metric_model.rb', line 88

def as_json(*a)
  {
    'name' => @name,
    'updated_at' => @updated_at,
    'values' => @values.as_json(*a),
    'db_shard' => @db_shard,
  }
end

#create(update: false, force: false, queued: false, isoformat: false, expire_seconds: nil) ⇒ Object



80
81
82
# File 'lib/openc3/models/metric_model.rb', line 80

def create(update: false, force: false, queued: false, isoformat: false, expire_seconds: nil)
  _db_sharded_create(@db_shard, update: update, force: force, queued: queued, isoformat: isoformat, expire_seconds: expire_seconds)
end

#destroyObject



84
85
86
# File 'lib/openc3/models/metric_model.rb', line 84

def destroy
  _db_sharded_destroy(@db_shard)
end