Class: Sqreen::MetricsStore
- Inherits:
-
Object
- Object
- Sqreen::MetricsStore
- Defined in:
- lib/sqreen/metrics_store.rb,
lib/sqreen/metrics_store/unknown_metric.rb,
lib/sqreen/metrics_store/unregistered_metric.rb,
lib/sqreen/metrics_store/already_registered_metric.rb
Overview
This store and register metrics
Defined Under Namespace
Classes: AlreadyRegisteredMetric, UnknownMetric, UnregisteredMetric
Constant Summary collapse
- NAME_KEY =
definition keys
'name'.freeze
- KIND_KEY =
'kind'.freeze
- PERIOD_KEY =
'period'.freeze
- OPTIONS_KEY =
'options'.freeze
Instance Attribute Summary collapse
-
#metrics ⇒ Object
readonly
All known metrics.
-
#store ⇒ Object
readonly
Currently ready samples.
Instance Method Summary collapse
-
#create_metric(definition, rule = nil, mklass = nil) ⇒ Object
Definition contains a name,period and aggregate at least.
-
#initialize ⇒ MetricsStore
constructor
A new instance of MetricsStore.
- #metric?(name) ⇒ Boolean
-
#publish(flush = true, at = Sqreen.time) ⇒ Object
Drains every metrics and returns the store content.
- #update(name, at, key, value) ⇒ Object
Constructor Details
#initialize ⇒ MetricsStore
Returns a new instance of MetricsStore.
27 28 29 30 31 |
# File 'lib/sqreen/metrics_store.rb', line 27 def initialize @store = [] @metrics = {} # name => (metric, period, start) @mutex = Mutex.new end |
Instance Attribute Details
#metrics ⇒ Object (readonly)
All known metrics
25 26 27 |
# File 'lib/sqreen/metrics_store.rb', line 25 def metrics @metrics end |
#store ⇒ Object (readonly)
Currently ready samples
23 24 25 |
# File 'lib/sqreen/metrics_store.rb', line 23 def store @store end |
Instance Method Details
#create_metric(definition, rule = nil, mklass = nil) ⇒ Object
Definition contains a name,period and aggregate at least
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sqreen/metrics_store.rb', line 37 def create_metric(definition, rule = nil, mklass = nil) @mutex.lock name = definition[NAME_KEY] kind = definition[KIND_KEY] klass = valid_metric(kind, name) opts = definition[OPTIONS_KEY] metric = mklass || klass.new(opts) @metrics[name] = [ metric, definition[PERIOD_KEY], nil # Start ] metric.name = name metric.rule = rule metric.period = definition[PERIOD_KEY] metric ensure @mutex.unlock end |
#metric?(name) ⇒ Boolean
59 60 61 |
# File 'lib/sqreen/metrics_store.rb', line 59 def metric?(name) @metrics.key?(name) end |
#publish(flush = true, at = Sqreen.time) ⇒ Object
Drains every metrics and returns the store content
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sqreen/metrics_store.rb', line 76 def publish(flush = true, at = Sqreen.time) @mutex.lock @metrics.each do |name, (_, period, start)| next_sample(name, at) if flush || !start.nil? && (start + period) < at end out = @store @store = [] out ensure @mutex.unlock end |
#update(name, at, key, value) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/sqreen/metrics_store.rb', line 64 def update(name, at, key, value) @mutex.lock metric, period, start = @metrics[name] raise UnregisteredMetric, "Unknown metric #{name}" unless metric next_sample(name, at) if start.nil? || (start + period) < at metric.update(key, value) ensure @mutex.unlock end |