Class: Atatus::Instrumenter Private

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

:__atatus_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.

:__atatus_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/atatus/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/atatus/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/atatus/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.



166
167
168
# File 'lib/atatus/instrumenter.rb', line 166

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



162
163
164
# File 'lib/atatus/instrumenter.rb', line 162

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/atatus/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/atatus/instrumenter.rb', line 103

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/ParameterLists rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/atatus/instrumenter.rb', line 225

def end_span
  return unless (span = current_spans.pop)

  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
# File 'lib/atatus/instrumenter.rb', line 146

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

  self.current_transaction = nil

  transaction.done result

  enqueue.call transaction

  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/atatus/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.



266
267
268
269
270
# File 'lib/atatus/instrumenter.rb', line 266

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



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

def set_company(company)
  return unless current_transaction
  current_transaction.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.



246
247
248
249
# File 'lib/atatus/instrumenter.rb', line 246

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



239
240
241
242
243
244
# File 'lib/atatus/instrumenter.rb', line 239

def set_label(key, value)
  return unless current_transaction

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



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

def set_response_body(response_body)
  return unless current_transaction
  current_transaction.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.



251
252
253
254
# File 'lib/atatus/instrumenter.rb', line 251

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/atatus/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) ⇒ 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



173
174
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
# File 'lib/atatus/instrumenter.rb', line 173

def start_span(
  name,
  type = nil,
  subtype: nil,
  action: nil,
  backtrace: nil,
  context: nil,
  trace_context: nil,
  parent: nil,
  sync: 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)

  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
  )

  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/atatus/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
    samled = trace_context.recorded?
    sample_rate = trace_context.tracestate.sample_rate
  else
    sampled = random_sample?(config)
    sample_rate = config.transaction_sample_rate
  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/atatus/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/atatus/instrumenter.rb', line 91

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