Class: ElasticAPM::OpenTracing::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_apm/opentracing.rb

Overview

A custom tracer to use the OpenTracing API with ElasticAPM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracer

Returns a new instance of Tracer.



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

def initialize
  @scope_manager = ScopeManager.new
end

Instance Attribute Details

#scope_managerObject (readonly)

Returns the value of attribute scope_manager.



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

def scope_manager
  @scope_manager
end

Instance Method Details

#active_spanObject



220
221
222
# File 'lib/elastic_apm/opentracing.rb', line 220

def active_span
  scope_manager.active&.span
end

#extract(format, carrier) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/elastic_apm/opentracing.rb', line 321

def extract(format, carrier)
  case format
  when ::OpenTracing::FORMAT_RACK
    SpanContext.from_header(
      carrier['HTTP_ELASTIC_APM_TRACEPARENT']
    )
  when ::OpenTracing::FORMAT_TEXT_MAP
    SpanContext.from_header(
      carrier['elastic-apm-traceparent']
    )
  else
    warn 'Only extraction from HTTP headers via Rack or in ' \
      'text map format are available'
    nil
  end
rescue ElasticAPM::TraceContext::InvalidTraceparentHeader
  nil
end

#inject(span_context, format, carrier) ⇒ Object

rubocop:enable Metrics/ParameterLists



311
312
313
314
315
316
317
318
319
# File 'lib/elastic_apm/opentracing.rb', line 311

def inject(span_context, format, carrier)
  case format
  when ::OpenTracing::FORMAT_RACK, ::OpenTracing::FORMAT_TEXT_MAP
    carrier['elastic-apm-traceparent'] =
      span_context.traceparent.to_header
  else
    warn 'Only injection via HTTP headers and Rack is available'
  end
end

#start_active_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: {}, ignore_active_scope: false, finish_on_close: true) ⇒ Object

rubocop:disable Metrics/ParameterLists



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/elastic_apm/opentracing.rb', line 225

def start_active_span(
  operation_name,
  child_of: nil,
  references: nil,
  start_time: Time.now,
  tags: {},
  ignore_active_scope: false,
  finish_on_close: true,
  **
)
  span = start_span(
    operation_name,
    child_of: child_of,
    references: references,
    start_time: start_time,
    tags: tags,
    ignore_active_scope: ignore_active_scope
  )
  scope = scope_manager.activate(span, finish_on_close: finish_on_close)

  if block_given?
    begin
      return yield scope
    ensure
      scope.close
    end
  end

  scope
end

#start_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: {}, ignore_active_scope: false) ⇒ Object

rubocop:disable Metrics/ParameterLists



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/elastic_apm/opentracing.rb', line 258

def start_span(
  operation_name,
  child_of: nil,
  references: nil,
  start_time: Time.now,
  tags: {},
  ignore_active_scope: false,
  **
)
  span_context = prepare_span_context(
    child_of: child_of,
    references: references,
    ignore_active_scope: ignore_active_scope
  )

  if span_context
    trace_context =
      span_context.respond_to?(:trace_context) &&
      span_context.trace_context
  end

  elastic_span =
    if ElasticAPM.current_transaction
      ElasticAPM.start_span(
        operation_name,
        trace_context: trace_context
      )
    else
      ElasticAPM.start_transaction(
        operation_name,
        trace_context: trace_context
      )
    end

  # if no Elastic APM agent is running or transaction not sampled
  unless elastic_span
    return ::OpenTracing::Span::NOOP_INSTANCE
  end

  span_context ||=
    SpanContext.from_trace_context(elastic_span.trace_context)

  tags.each do |key, value|
    elastic_span.context.labels[key] = value
  end

  elastic_span.start Util.micros(start_time)

  Span.new(elastic_span, span_context)
end