Class: Atatus::Agent Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/atatus/agent.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

LOCK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mutex.new

Constants included from Logging

Logging::LEVELS, Logging::PREFIX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(config) ⇒ Agent

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Agent.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/atatus/agent.rb', line 79

def initialize(config)
  @stacktrace_builder = StacktraceBuilder.new(config)
  @context_builder = ContextBuilder.new(config)
  @error_builder = ErrorBuilder.new(self)

  @central_config = CentralConfig.new(config)
  @collector = Collector::Base.new(config)
  @metrics = Metrics.new(config) { |event| enqueue event }
  @instrumenter = Instrumenter.new(
    config,
    metrics: metrics,
    stacktrace_builder: stacktrace_builder
  ) { |event| enqueue event }
  @pid = Process.pid
end

Instance Attribute Details

#central_configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def central_config
  @central_config
end

#collectorObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def collector
  @collector
end

#configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def config
  @config
end

#context_builderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def context_builder
  @context_builder
end

#error_builderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def error_builder
  @error_builder
end

#instrumenterObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def instrumenter
  @instrumenter
end

#metricsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def metrics
  @metrics
end

#stacktrace_builderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/atatus/agent.rb', line 95

def stacktrace_builder
  @stacktrace_builder
end

Class Method Details

.instanceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

life cycle



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

def self.instance # rubocop:disable Style/TrivialAccessors
  @instance
end

.running?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


75
76
77
# File 'lib/atatus/agent.rb', line 75

def self.running?
  !!@instance
end

.start(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/atatus/agent.rb', line 46

def self.start(config)
  return @instance if @instance

  config = Config.new(config) unless config.is_a?(Config)

  LOCK.synchronize do
    return @instance if @instance

    unless config.enabled?
      config.logger.debug format(
        "%sAgent disabled with `enabled: false'",
        Logging::PREFIX
      )
      return
    end

    @instance = new(config).start
  end
end

.stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
73
# File 'lib/atatus/agent.rb', line 66

def self.stop
  LOCK.synchronize do
    return unless @instance

    @instance.stop
    @instance = nil
  end
end

Instance Method Details

#add_filter(key, callback) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

filters



297
298
299
# File 'lib/atatus/agent.rb', line 297

def add_filter(key, callback)
  # transport.add_filter(key, callback)
end

#build_context(rack_env:, for_type:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



261
262
263
# File 'lib/atatus/agent.rb', line 261

def build_context(rack_env:, for_type:)
  @context_builder.build(rack_env: rack_env, for_type: for_type)
end

#current_spanObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



175
176
177
# File 'lib/atatus/agent.rb', line 175

def current_span
  instrumenter.current_span
end

#current_transactionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

instrumentation



171
172
173
# File 'lib/atatus/agent.rb', line 171

def current_transaction
  instrumenter.current_transaction
end

#detect_forking!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/atatus/agent.rb', line 307

def detect_forking!
  return if @pid == Process.pid

  config.logger.debug(
    "Forked process detected, restarting threads in process [PID:#{Process.pid}]")

  central_config.handle_forking!
  collector.handle_forking!
  instrumenter.handle_forking!
  metrics.handle_forking!

  @pid = Process.pid
end

#end_span(span = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:enable Metrics/ParameterLists



233
234
235
# File 'lib/atatus/agent.rb', line 233

def end_span(span = nil)
  instrumenter.end_span(span)
end

#end_transaction(result = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



197
198
199
# File 'lib/atatus/agent.rb', line 197

def end_transaction(result = nil)
  instrumenter.end_transaction(result)
end

#enqueue(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

transport



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/atatus/agent.rb', line 156

def enqueue(obj)
  case obj
  when Atatus::Transaction
    collector.add_txn(obj)
  when Atatus::Span
    collector.add_span(obj)
  when Atatus::Error
    collector.add_error(obj)
  when Atatus::Metricset
    collector.add_metrics(obj)
  end      
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

misc



303
304
305
# File 'lib/atatus/agent.rb', line 303

def inspect
  super.split.first + '>'
end

#report(exception, context: nil, handled: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

errors



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/atatus/agent.rb', line 267

def report(exception, context: nil, handled: true)
  return unless config.recording?
  detect_forking!
  return if config.filter_exception_types.include?(exception.class.to_s)

  error = @error_builder.build_exception(
    exception,
    context: context,
    handled: handled
  )
  enqueue error
  error.id
end

#report_message(message, context: nil, backtrace: nil, **attrs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/atatus/agent.rb', line 281

def report_message(message, context: nil, backtrace: nil, **attrs)
  return unless config.recording?
  detect_forking!

  error = @error_builder.build_log(
    message,
    context: context,
    backtrace: backtrace,
    **attrs
  )
  enqueue error
  error.id
end

#set_company(company) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



249
250
251
# File 'lib/atatus/agent.rb', line 249

def set_company(company)
  instrumenter.set_company(company)
end

#set_custom_context(context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



241
242
243
# File 'lib/atatus/agent.rb', line 241

def set_custom_context(context)
  instrumenter.set_custom_context(context)
end

#set_destination(address: nil, port: nil, service: nil, cloud: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



257
258
259
# File 'lib/atatus/agent.rb', line 257

def set_destination(address: nil, port: nil, service: nil, cloud: nil)
  current_span&.set_destination(address: nil, port: nil, service: nil, cloud: nil)
end

#set_label(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



237
238
239
# File 'lib/atatus/agent.rb', line 237

def set_label(key, value)
  instrumenter.set_label(key, value)
end

#set_response_body(response_body) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



253
254
255
# File 'lib/atatus/agent.rb', line 253

def set_response_body(response_body)
  instrumenter.set_response_body(response_body)
end

#set_user(user) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



245
246
247
# File 'lib/atatus/agent.rb', line 245

def set_user(user)
  instrumenter.set_user(user)
end

#startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/atatus/agent.rb', line 108

def start
  unless config.disable_start_message?
    config.logger.info format(
      '[%s] Starting atatus ruby agent',
      VERSION
    )
  end

  if config.license_key.nil? || config.app_name.nil?
    if config.license_key.nil? && config.app_name.nil?
      error 'Atatus configuration license_key and app_name are missing'
    elsif config.license_key.nil?
      error 'Atatus configuration license_key is missing'
    elsif config.app_name.nil?
      error 'Atatus configuration app_name is missing'
    end
  end

  central_config.start
  collector.start
  instrumenter.start
  metrics.start

  config.enabled_instrumentations.each do |lib|
    debug "Requiring spy: #{lib}"
    require "atatus/spies/#{lib}"
  end

  self
end

#start_span(name = nil, type = nil, subtype: nil, action: nil, backtrace: nil, context: nil, trace_context: nil, parent: nil, sync: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/ParameterLists



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/atatus/agent.rb', line 202

def start_span(
  name = nil,
  type = nil,
  subtype: nil,
  action: nil,
  backtrace: nil,
  context: nil,
  trace_context: nil,
  parent: nil,
  sync: nil
)
  detect_forking!

  # We don't check config.recording? because the span
  # will not be created if there's no transaction.
  # We want to use the recording value from the config
  # that existed when start_transaction was called. ~estolfo
  instrumenter.start_span(
    name,
    type,
    subtype: subtype,
    action: action,
    backtrace: backtrace,
    context: context,
    trace_context: trace_context,
    parent: parent,
    sync: sync
  )
end

#start_transaction(name = nil, type = nil, context: nil, trace_context: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/atatus/agent.rb', line 179

def start_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil
)
  return unless config.recording?
  detect_forking!

  instrumenter.start_transaction(
    name,
    type,
    config: config,
    context: context,
    trace_context: trace_context
  )
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



139
140
141
142
143
144
145
146
147
148
# File 'lib/atatus/agent.rb', line 139

def stop
  info 'Stopping agent'

  central_config.stop
  metrics.stop
  instrumenter.stop
  collector.stop

  self
end