Class: SolidObserver::BaseMetric

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/solid_observer/base_metric.rb

Overview

BaseMetric provides the foundation for time-series metrics storage.

NOTE: Metrics functionality is planned for a future release. The database connection will be configured by the Engine (similar to BaseEvent) when metrics are fully implemented.

Direct Known Subclasses

QueueMetric

Constant Summary collapse

PERIOD_TYPES =
%w[minute hour day].freeze

Class Method Summary collapse

Class Method Details

.increment(metric:, period: Time.current.beginning_of_hour, period_type: "hour", by: 1) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/solid_observer/base_metric.rb', line 29

def increment(metric:, period: Time.current.beginning_of_hour, period_type: "hour", by: 1)
  record = find_or_create_by!(
    metric_name: metric,
    period_start: period,
    period_type: period_type
  )
  record.increment!(:value, by)
  record
rescue ActiveRecord::RecordNotUnique
  retry
end

.record(metric:, value:, period: Time.current.beginning_of_hour, period_type: "hour") ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solid_observer/base_metric.rb', line 41

def record(metric:, value:, period: Time.current.beginning_of_hour, period_type: "hour")
  upsert(
    {
      metric_name: metric,
      value: value,
      period_start: period,
      period_type: period_type
    },
    unique_by: [:metric_name, :period_start, :period_type]
  )
  find_by!(
    metric_name: metric,
    period_start: period,
    period_type: period_type
  )
end