Class: ElasticAPM::Instrumenter Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/elastic_apm/instrumenter.rb

Overview

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.

rubocop:disable Metrics/ClassLength

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



42
43
44
45
46
47
48
# File 'lib/elastic_apm/instrumenter.rb', line 42

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

  @current = Current.new
end

Instance Attribute Details

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



50
51
52
# File 'lib/elastic_apm/instrumenter.rb', line 50

def config
  @config
end

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



50
51
52
# File 'lib/elastic_apm/instrumenter.rb', line 50

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.



50
51
52
# File 'lib/elastic_apm/instrumenter.rb', line 50

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.



129
130
131
# File 'lib/elastic_apm/instrumenter.rb', line 129

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



125
126
127
# File 'lib/elastic_apm/instrumenter.rb', line 125

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



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

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.



76
77
78
# File 'lib/elastic_apm/instrumenter.rb', line 76

def current_transaction=(transaction)
  @current.transaction = transaction
end

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

rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity



175
176
177
178
179
180
181
182
183
# File 'lib/elastic_apm/instrumenter.rb', line 175

def end_span
  return unless (span = current_spans.pop)

  span.done

  enqueue.call 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.

rubocop:enable Metrics/MethodLength



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/elastic_apm/instrumenter.rb', line 111

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

  self.current_transaction = nil

  transaction.done result

  enqueue.call transaction

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



204
205
206
207
208
# File 'lib/elastic_apm/instrumenter.rb', line 204

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.



194
195
196
197
# File 'lib/elastic_apm/instrumenter.rb', line 194

def set_custom_context(context)
  return unless current_transaction
  current_transaction.context.custom.merge!(context)
end

#set_tag(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



187
188
189
190
191
192
# File 'lib/elastic_apm/instrumenter.rb', line 187

def set_tag(key, value)
  return unless current_transaction

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



199
200
201
202
# File 'lib/elastic_apm/instrumenter.rb', line 199

def set_user(user)
  return unless current_transaction
  current_transaction.context.user = Context::User.infer(config, 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.



52
53
54
# File 'lib/elastic_apm/instrumenter.rb', line 52

def start
  debug 'Starting instrumenter'
end

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

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/elastic_apm/instrumenter.rb', line 135

def start_span(
  name,
  type = nil,
  backtrace: nil,
  context: nil,
  trace_context: nil
)
  return unless (transaction = current_transaction)
  return unless transaction.sampled?

  transaction.inc_started_spans!

  if transaction.max_spans_reached?(config)
    transaction.inc_dropped_spans!
    return
  end

  parent = current_span || transaction

  span = Span.new(
    name,
    type,
    transaction_id: transaction.id,
    parent_id: parent.id,
    context: context,
    stacktrace_builder: stacktrace_builder,
    trace_context: trace_context || parent.trace_context.child
  )

  if backtrace && config.span_frames_min_duration?
    span.original_backtrace = backtrace
  end

  current_spans.push span

  span.start
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.

rubocop:disable Metrics/MethodLength



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/elastic_apm/instrumenter.rb', line 81

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

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

  sampled = trace_context ? trace_context.recorded? : random_sample?

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

  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.



56
57
58
59
60
61
62
63
# File 'lib/elastic_apm/instrumenter.rb', line 56

def stop
  debug 'Stopping instrumenter'

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

  @subscriber.unregister! if @subscriber
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.



65
66
67
68
# File 'lib/elastic_apm/instrumenter.rb', line 65

def subscriber=(subscriber)
  @subscriber = subscriber
  @subscriber.register!
end