Module: Aeternitas::Metrics

Defined in:
lib/aeternitas/metrics.rb

Overview

Provides a simplified metrics system for Aeternitas. Every metric is scoped by pollable class and logged in the ‘aeternitas_metrics` table.

Available metrics are:

- polls => Number of polling runs
- successful_polls => Number of successful polling runs
- failed_polls => Number of failed polling runs (includes IgnoredErrors,
  excludes deactivation errors and Lock errors)
- ignored_errors => Number of raised {Aeternitas::Errors::Ignored}
- deactivations => Number of deactivations
- execution_time => Job execution time in seconds
- guard_locked => Number of encountered locked guards
- guard_timeout => Time until the guard is unlocked in seconds
- guard_timeout_exceeded => Number of jobs that ran longer than the guards timeout
- pollables_created => Number of created pollables
- sources_created => Number of created sources

Examples:

Aeternitas::Metrics.log(:polls, MyPollable)
Aeternitas::Metrics.log_value(:execution_time, MyPollable, 1.25)

# Get all poll counts for MyPollable in the last day
Aeternitas::Metrics.get(:polls, MyPollable, from: 1.day.ago)

Constant Summary collapse

AVAILABLE_METRICS =

A list of all available metric names.

[
  :polls,
  :successful_polls,
  :failed_polls,
  :ignored_errors,
  :deactivations,
  :execution_time,
  :guard_locked,
  :guard_timeout,
  :guard_timeout_exceeded,
  :sources_created,
  :pollables_created
].freeze

Class Method Summary collapse

Class Method Details

.get(name, pollable_class, from: 1.hour.ago, to: Time.now) ⇒ ActiveRecord::Relation

Retrieves metric records.

Parameters:

  • name (Symbol)

    the metric

  • pollable_class (Class)

    the pollable class

  • from (Time) (defaults to: 1.hour.ago)

    begin of the time frame

  • to (Time) (defaults to: Time.now)

    end of the timeframe

Returns:

  • (ActiveRecord::Relation)

    a relation of Aeternitas::Metric records



72
73
74
75
76
77
78
# File 'lib/aeternitas/metrics.rb', line 72

def self.get(name, pollable_class, from: 1.hour.ago, to: Time.now)
  Aeternitas::Metric.where(
    name: name.to_s,
    pollable_class: pollable_class.name,
    created_at: from..to
  ).order(:created_at)
end

.log(name, pollable_class) ⇒ Object

Logs a counter metric. This creates a new metric record with a value of 1.

Parameters:

  • name (Symbol)

    the metric name

  • pollable_class (Class)

    the class of the pollable



45
46
47
# File 'lib/aeternitas/metrics.rb', line 45

def self.log(name, pollable_class)
  log_value(name, pollable_class, 1)
end

.log_value(name, pollable_class, value) ⇒ Object

Logs a value-based metric.

Parameters:

  • name (Symbol)

    the metric name

  • pollable_class (Class)

    the class of the pollable

  • value (Float)

    the value to log



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aeternitas/metrics.rb', line 53

def self.log_value(name, pollable_class, value)
  return unless Aeternitas.config.metrics_enabled && AVAILABLE_METRICS.include?(name)

  Aeternitas::Metric.create(
    name: name.to_s,
    pollable_class: pollable_class.name,
    value: value,
    created_at: Time.now
  )
rescue
  # Metrics should fail silently
end