Class: ElasticAPM::Agent Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/elastic_apm/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/elastic_apm/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)
  @transport = Transport::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/elastic_apm/agent.rb', line 95

def central_config
  @central_config
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/elastic_apm/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/elastic_apm/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/elastic_apm/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/elastic_apm/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/elastic_apm/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/elastic_apm/agent.rb', line 95

def stacktrace_builder
  @stacktrace_builder
end

#transportObject (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/elastic_apm/agent.rb', line 95

def transport
  @transport
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/elastic_apm/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/elastic_apm/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/elastic_apm/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/elastic_apm/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



270
271
272
# File 'lib/elastic_apm/agent.rb', line 270

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.



234
235
236
# File 'lib/elastic_apm/agent.rb', line 234

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.



156
157
158
# File 'lib/elastic_apm/agent.rb', line 156

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



152
153
154
# File 'lib/elastic_apm/agent.rb', line 152

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.



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

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!
  transport.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



214
215
216
# File 'lib/elastic_apm/agent.rb', line 214

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.



178
179
180
# File 'lib/elastic_apm/agent.rb', line 178

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



146
147
148
# File 'lib/elastic_apm/agent.rb', line 146

def enqueue(obj)
  transport.submit obj
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



276
277
278
# File 'lib/elastic_apm/agent.rb', line 276

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



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/elastic_apm/agent.rb', line 240

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.



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/elastic_apm/agent.rb', line 254

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_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.



222
223
224
# File 'lib/elastic_apm/agent.rb', line 222

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.



230
231
232
# File 'lib/elastic_apm/agent.rb', line 230

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.



218
219
220
# File 'lib/elastic_apm/agent.rb', line 218

def set_label(key, value)
  instrumenter.set_label(key, value)
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.



226
227
228
# File 'lib/elastic_apm/agent.rb', line 226

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
# File 'lib/elastic_apm/agent.rb', line 108

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

  central_config.start
  transport.start
  instrumenter.start
  metrics.start

  config.enabled_instrumentations.each do |lib|
    debug "Requiring spy: #{lib}"
    require "elastic_apm/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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/elastic_apm/agent.rb', line 183

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.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/elastic_apm/agent.rb', line 160

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.



129
130
131
132
133
134
135
136
137
138
# File 'lib/elastic_apm/agent.rb', line 129

def stop
  info 'Stopping agent'

  central_config.stop
  metrics.stop
  instrumenter.stop
  transport.stop

  self
end