Class: ElasticAPM::Instrumenter Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/elastic_apm/instrumenter.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.

Defined Under Namespace

Classes: Current

Constant Summary collapse

TRANSACTION_KEY =

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.

:__elastic_instrumenter_transaction_key
SPAN_KEY =

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.

:__elastic_instrumenter_spans_key

Constants included from Logging

Logging::LEVELS, Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Constructor Details

#initialize(config, metrics:, stacktrace_builder:, &enqueue) ⇒ Instrumenter

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



59
60
61
62
63
64
65
66
# File 'lib/elastic_apm/instrumenter.rb', line 59

def initialize(config, metrics:, stacktrace_builder:, &enqueue)
  @config = config
  @stacktrace_builder = stacktrace_builder
  @enqueue = enqueue
  @metrics = metrics

  @current = Current.new
end

Instance Attribute Details

#enqueueObject (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.



68
69
70
# File 'lib/elastic_apm/instrumenter.rb', line 68

def enqueue
  @enqueue
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.



68
69
70
# File 'lib/elastic_apm/instrumenter.rb', line 68

def stacktrace_builder
  @stacktrace_builder
end

Instance Method Details

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



168
169
170
# File 'lib/elastic_apm/instrumenter.rb', line 168

def current_span
  current_spans.last
end

#current_spansObject

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.

spans



164
165
166
# File 'lib/elastic_apm/instrumenter.rb', line 164

def current_spans
  @current.spans
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.

transactions



99
100
101
# File 'lib/elastic_apm/instrumenter.rb', line 99

def current_transaction
  @current.transaction
end

#current_transaction=(transaction) ⇒ 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.



103
104
105
# File 'lib/elastic_apm/instrumenter.rb', line 103

def current_transaction=(transaction)
  @current.transaction = transaction
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 rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/elastic_apm/instrumenter.rb', line 238

def end_span(span = nil)
  if span
    current_spans.delete(span)
  else
    span = current_spans.pop
  end

  return unless span

  span.done

  enqueue.call span

  update_span_metrics(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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/elastic_apm/instrumenter.rb', line 146

def end_transaction(result = nil)
  return nil unless (transaction = current_transaction)

  self.current_transaction = nil

  transaction.done result

  if transaction.sampled? || @config.version < Config::ServerInfo::VERSION_8_0
    enqueue.call transaction
  end

  update_transaction_metrics(transaction)

  transaction
end

#handle_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.



86
87
88
89
# File 'lib/elastic_apm/instrumenter.rb', line 86

def handle_forking!
  stop
  start
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.



275
276
277
278
279
# File 'lib/elastic_apm/instrumenter.rb', line 275

def inspect
  '<ElasticAPM::Instrumenter ' \
    "current_transaction=#{current_transaction.inspect}" \
    '>'
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.



265
266
267
268
# File 'lib/elastic_apm/instrumenter.rb', line 265

def set_custom_context(context)
  return unless current_transaction
  current_transaction.context.custom.merge!(context)
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.

metadata



258
259
260
261
262
263
# File 'lib/elastic_apm/instrumenter.rb', line 258

def set_label(key, value)
  return unless current_transaction

  key = key.to_s.gsub(/[."*]/, '_').to_sym
  current_transaction.context.labels[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.



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

def set_user(user)
  return unless current_transaction
  current_transaction.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.



70
71
72
73
74
75
# File 'lib/elastic_apm/instrumenter.rb', line 70

def start
  debug 'Starting instrumenter'
  # We call register! on @subscriber in case the
  # instrumenter was stopped and started again
  @subscriber&.register!
end

#start_span(name, type = nil, subtype: nil, action: nil, backtrace: nil, context: nil, trace_context: nil, parent: nil, sync: nil, exit_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:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/ParameterLists



175
176
177
178
179
180
181
182
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/elastic_apm/instrumenter.rb', line 175

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

  transaction =
    case parent
    when Span
      parent.transaction
    when Transaction
      parent
    else
      current_transaction
    end
  return unless transaction
  return unless transaction.sampled?
  return unless transaction.inc_started_spans!

  parent ||= (current_span || current_transaction)

  # To not mess with breakdown metric stats, exit spans MUST not add
  # sub-spans unless they share the same type and subtype.
  if parent && parent.is_a?(Span) && parent.exit_span?
    if parent.type != type || parent.subtype != subtype
      debug "Skipping new span '#{name}' as its parent is an exit_span"
      return
    end
  end

  span = Span.new(
    name: name,
    subtype: subtype,
    action: action,
    transaction: transaction,
    parent: parent,
    trace_context: trace_context,
    type: type,
    context: context,
    stacktrace_builder: stacktrace_builder,
    sync: sync,
    exit_span: exit_span
  )

  if backtrace && transaction.span_frames_min_duration
    span.original_backtrace = backtrace
  end

  current_spans.push span

  span.start
end

#start_transaction(name = nil, type = nil, config:, 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.



107
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
138
139
140
141
142
143
144
# File 'lib/elastic_apm/instrumenter.rb', line 107

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

  if (transaction = current_transaction)
    raise ExistingTransactionError,
      "Transactions may not be nested.\n" \
      "Already inside #{transaction.inspect}"
  end

  if trace_context
    sampled = trace_context.recorded?
    sample_rate = trace_context.tracestate.sample_rate
  else
    sampled = random_sample?(config)
    sample_rate = sampled ? config.transaction_sample_rate : 0
  end

  transaction =
    Transaction.new(
      name,
      type,
      context: context,
      trace_context: trace_context,
      sampled: sampled,
      sample_rate: sample_rate,
      config: config
    )

  transaction.start

  self.current_transaction = transaction
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.



77
78
79
80
81
82
83
84
# File 'lib/elastic_apm/instrumenter.rb', line 77

def stop
  debug 'Stopping instrumenter'

  self.current_transaction = nil
  current_spans.pop until current_spans.empty?

  @subscriber&.unregister!
end

#subscriber=(subscriber) ⇒ 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.



91
92
93
94
95
# File 'lib/elastic_apm/instrumenter.rb', line 91

def subscriber=(subscriber)
  debug 'Registering ActiveSupport::Notifications subscriber'
  @subscriber = subscriber
  @subscriber.register!
end