Class: Chronos::Application::ApmAggregator

Inherits:
Object
  • Object
show all
Includes:
ApmErrorClassifier
Defined in:
lib/chronos/application/apm_aggregator.rb

Overview

Aggregates bounded request, query, job, and external HTTP observations into metric batches.

Examples:

aggregator.record("request", payload, context)
batches = aggregator.flush

Constant Summary collapse

METRIC_TYPES =
%w(request query job external_http).freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ApmAggregator

Returns a new instance of ApmAggregator.



20
21
22
23
24
25
26
27
# File 'lib/chronos/application/apm_aggregator.rb', line 20

def initialize(config)
  @config = config
  @mutex = Mutex.new
  @groups = {}
  @transactions = {}
  @observations = 0
  @dropped_groups = 0
end

Instance Method Details

#diagnosticsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chronos/application/apm_aggregator.rb', line 51

def diagnostics
  @mutex.synchronize do
    {
      "groups" => @groups.length, "dropped_groups" => @dropped_groups,
      "transactions" => @transactions.length,
      "tracked_queries" => @transactions.values.inject(0) do |total, transaction|
        total + transaction["queries"].length
      end
    }
  end
end

#flushObject



45
46
47
48
49
# File 'lib/chronos/application/apm_aggregator.rb', line 45

def flush
  @mutex.synchronize { drain_locked }
rescue StandardError
  []
end

#record(event_type, payload = {}, context = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chronos/application/apm_aggregator.rb', line 29

def record(event_type, payload = {}, context = {})
  return [] unless @config.apm_enabled

  @mutex.synchronize do
    type = event_type.to_s
    data = hash(payload)
    execution = hash(context)
    observe_component(type, data, execution)
    add_metric(type, data, execution) if aggregate_metric?(type, data)
    @observations += 1 if aggregate_metric?(type, data)
    @observations >= @config.apm_flush_count ? drain_locked : []
  end
rescue StandardError
  []
end