Class: Chronos::Core::MetricAggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/core/metric_aggregate.rb

Overview

Accumulates one bounded APM metric group and serializes aggregate statistics.

Examples:

metric.observe(12.0, false, {"database" => 3.0}, {})

Constant Summary collapse

BREAKDOWN_CATEGORIES =
%w(database view external_http cache queue application unknown).freeze

Instance Method Summary collapse

Constructor Details

#initialize(metric_type, dimensions, boundaries) ⇒ MetricAggregate

Returns a new instance of MetricAggregate.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chronos/core/metric_aggregate.rb', line 18

def initialize(metric_type, dimensions, boundaries)
  @metric_type = metric_type
  @dimensions = dimensions
  @boundaries = boundaries
  @count = 0
  @error_count = 0
  @total = 0.0
  @min = nil
  @max = nil
  @buckets = Array.new(boundaries.length + 1, 0)
  @breakdown = {}
  @signals = {}
  @status_codes = {}
end

Instance Method Details

#observe(duration, error, breakdown, signals, status = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chronos/core/metric_aggregate.rb', line 33

def observe(duration, error, breakdown, signals, status = nil)
  value = non_negative(duration)
  @count += 1
  @error_count += 1 if error
  @total += value
  @min = value if @min.nil? || value < @min
  @max = value if @max.nil? || value > @max
  bucket = @boundaries.index { |boundary| value <= boundary }
  @buckets[bucket || @boundaries.length] += 1
  add_breakdown(breakdown)
  add_signals(signals)
  add_status(status)
  self
end

#to_hObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/chronos/core/metric_aggregate.rb', line 48

def to_h
  {
    "metric_type" => @metric_type, "dimensions" => @dimensions,
    "count" => @count, "error_count" => @error_count,
    "error_rate" => (@error_count.to_f / @count).round(6),
    "duration_ms" => duration_summary, "histogram" => histogram,
    "breakdown_ms" => rounded_hash(@breakdown), "signals" => @signals,
    "status_codes" => @status_codes
  }
end