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}, {}, :diagnostics => [])

Constant Summary collapse

BREAKDOWN_CATEGORIES =
%w(database view external_http cache queue application unknown).freeze
DIAGNOSTIC_SEVERITIES =
%w(error warning info suggestion).freeze
MAX_DIAGNOSTICS =
20

Instance Method Summary collapse

Constructor Details

#initialize(metric_type, dimensions, boundaries) ⇒ MetricAggregate

Returns a new instance of MetricAggregate.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chronos/core/metric_aggregate.rb', line 20

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 = {}
  @diagnostics = {}
  @severity_counts = {}
  @query_analysis = nil
end

Instance Method Details

#observe(duration, error, breakdown, signals, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chronos/core/metric_aggregate.rb', line 38

def observe(duration, error, breakdown, signals, options = {})
  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(options[:status])
  add_diagnostics(options[:diagnostics])
  retain_query_analysis(options[:query_analysis])
  self
end

#to_hObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chronos/core/metric_aggregate.rb', line 55

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, "percentiles_ms" => percentiles, "histogram" => histogram,
    "breakdown_ms" => rounded_hash(@breakdown), "signals" => @signals,
    "status_codes" => @status_codes, "severity_counts" => @severity_counts,
    "diagnostics" => @diagnostics.values, "query_analysis" => @query_analysis || {}
  }
end