Class: Chronos::Agent

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

Overview

Runtime composition root for the framework-independent Chronos agent.

Examples:

agent.notify(RuntimeError.new("failed"))

Constant Summary collapse

DEFAULT_FLUSH_TIMEOUT =
5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ Agent

Returns a new instance of Agent.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chronos/agent.rb', line 19

def initialize(config, options = {})
  @config = config
  @logger = options[:logger] || Internal::SafeLogger.new(config.logger)
  @context_store = options[:context_store] || build_context_store(config.context_store)
  unless Ports::ContextStore.compatible?(@context_store)
    raise ArgumentError, "context store does not implement the Chronos context-store port"
  end
  @transport = options[:transport] || Adapters::NetHttpTransport.new(config, @logger)
  unless Ports::Transport.compatible?(@transport)
    raise ArgumentError, "transport does not implement the Chronos transport port"
  end
  pipeline_options = {}
  pipeline_options[:queue] = options[:queue] if options[:queue]
  pipeline_options[:worker_pool] = options[:worker_pool] if options[:worker_pool]
  @delivery_pipeline = options[:delivery_pipeline] || Application::DeliveryPipeline.new(
    config,
    @transport,
    @logger,
    pipeline_options
  )
  initialize_capture(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/chronos/agent.rb', line 17

def config
  @config
end

Instance Method Details

#add_breadcrumb(attributes = {}) ⇒ Object



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

def add_breadcrumb(attributes = {})
  context = @context_store.get
  buffer = context[:__chronos_breadcrumbs]
  buffer ||= Core::BreadcrumbBuffer.new(@config.breadcrumb_capacity, @config.breadcrumb_max_bytes)
  buffer.add(attributes)
  @context_store.set(context.merge(:__chronos_breadcrumbs => buffer))
  true
rescue StandardError => error
  @logger.warn("Chronos breadcrumb failed: #{error.class}")
  false
end

#apm_integration_optionsObject



82
83
84
85
86
87
88
# File 'lib/chronos/agent.rb', line 82

def apm_integration_options
  {
    :enabled => @config.apm_enabled,
    :slow_query_threshold_ms => @config.apm_slow_query_threshold_ms,
    :root_directory => @config.root_directory
  }
end

#close(timeout = DEFAULT_FLUSH_TIMEOUT) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/chronos/agent.rb', line 134

def close(timeout = DEFAULT_FLUSH_TIMEOUT)
  @telemetry.flush
  @delivery_pipeline.close(timeout)
rescue StandardError => error
  @logger.warn("Chronos close failed: #{error.class}")
  false
end

#diagnosticsObject



142
143
144
145
146
# File 'lib/chronos/agent.rb', line 142

def diagnostics
  details = @delivery_pipeline.diagnostics
  details[:apm] = @telemetry.diagnostics
  details[:queue].merge(details)
end

#flush(timeout = DEFAULT_FLUSH_TIMEOUT) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/chronos/agent.rb', line 126

def flush(timeout = DEFAULT_FLUSH_TIMEOUT)
  @telemetry.flush
  @delivery_pipeline.flush(timeout)
rescue StandardError => error
  @logger.warn("Chronos flush failed: #{error.class}")
  false
end

#notify(exception, context = {}) ⇒ Object



42
43
44
# File 'lib/chronos/agent.rb', line 42

def notify(exception, context = {})
  @capture.call(exception, context_for_capture(context))
end

#notify_once(exception, context = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chronos/agent.rb', line 105

def notify_once(exception, context = {})
  execution = @context_store.get
  captured = execution[:__chronos_captured_exceptions] || {}
  keys = [[:object, exception.object_id], [:message, exception.message.to_s]]
  return false if keys.any? { |key| captured[key] }

  keys.each { |key| captured[key] = true }
  @context_store.set(execution.merge(:__chronos_captured_exceptions => captured))
  notify(exception, context)
rescue StandardError
  false
end

#notify_sync(exception, context = {}) ⇒ Object



46
47
48
# File 'lib/chronos/agent.rb', line 46

def notify_sync(exception, context = {})
  @capture.call_sync(exception, context_for_capture(context))
end

#propagation_contextObject

Returns the correlation subset safe for an integration-owned process boundary.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chronos/agent.rb', line 91

def propagation_context
  current = context_hash(@context_store.get)
  nested = context_hash(current[:context] || current["context"])
  request = context_hash(nested["request"] || nested[:request])
  values = {
    "trace_id" => nested["trace_id"] || nested[:trace_id],
    "request_id" => nested["request_id"] || nested[:request_id] ||
                    request["request_id"] || request[:request_id]
  }
  values.delete_if { |_key, value| value.to_s.empty? }
rescue StandardError
  {}
end

#rails_integration_options(environment = nil, console = false) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/chronos/agent.rb', line 118

def rails_integration_options(environment = nil, console = false)
  current_environment = (environment || @config.environment).to_s
  enabled = @config.rails_enabled
  enabled = false if console && !@config.rails_capture_in_console
  enabled = false if current_environment == "test" && !@config.rails_capture_in_test
  {:enabled => enabled, :include_user_agent => @config.rails_capture_user_agent}
end

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



66
67
68
# File 'lib/chronos/agent.rb', line 66

def record_event(event_type, payload = {}, context = {})
  @telemetry.call(event_type, payload, telemetry_context(context))
end

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chronos/agent.rb', line 70

def record_event_once(key, event_type, payload = {}, context = {})
  execution = @context_store.get
  captured = execution[:__chronos_captured_events] || {}
  return false if captured[key.to_s]

  captured[key.to_s] = true
  @context_store.set(execution.merge(:__chronos_captured_events => captured))
  record_event(event_type, payload, context)
rescue StandardError
  false
end

#with_context(context = {}, &block) ⇒ Object



50
51
52
# File 'lib/chronos/agent.rb', line 50

def with_context(context = {}, &block)
  @context_store.with_context(context, &block)
end