Module: Labkit::Tracing::Common

Included in:
GRPCInterceptor, RackMiddleware, Rails::RailsCommon, Sidekiq::SidekiqCommon
Defined in:
lib/labkit/tracing/common.rb

Instance Method Summary collapse

Instance Method Details

#in_tracing_span(operation_name:, tags:, child_of: nil) ⇒ Object

Convience method for running a block with a span



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/labkit/tracing/common.rb', line 13

def in_tracing_span(operation_name:, tags:, child_of: nil)
  scope = tracer.start_active_span(operation_name, child_of: child_of, tags: tags)
  span = scope.span

  # Add correlation details to the span if we have them
  correlation_id = Labkit::Correlation::CorrelationId.current_id
  span.set_tag('correlation_id', correlation_id) if correlation_id

  begin
    yield span
  rescue StandardError => e
    log_exception_on_span(span, e)
    raise e
  ensure
    scope.close
  end
end

#kv_tags_for_exception(exception) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/labkit/tracing/common.rb', line 44

def kv_tags_for_exception(exception)
  case exception
  when Exception
    {
      :"event" => 'error',
      :"error.kind" => exception.class.to_s,
      :"message" => Labkit::Logging::Sanitizer.sanitize_field(exception.message),
      :"stack" => exception.backtrace&.join('\n')
    }
  else
    { :"event" => 'error', :"error.kind" => exception.class.to_s, :"error.object" => Labkit::Logging::Sanitizer.sanitize_field(exception.to_s) }
  end
end

#log_exception_on_span(span, exception) ⇒ Object



39
40
41
42
# File 'lib/labkit/tracing/common.rb', line 39

def log_exception_on_span(span, exception)
  span.set_tag('error', true)
  span.log_kv(kv_tags_for_exception(exception))
end

#postnotify_span(operation_name, start_time, end_time, tags: nil, child_of: nil, exception: nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/labkit/tracing/common.rb', line 31

def postnotify_span(operation_name, start_time, end_time, tags: nil, child_of: nil, exception: nil)
  span = OpenTracing.start_span(operation_name, start_time: start_time, tags: tags, child_of: child_of)

  log_exception_on_span(span, exception) if exception

  span.finish(end_time: end_time)
end

#tracerObject



8
9
10
# File 'lib/labkit/tracing/common.rb', line 8

def tracer
  OpenTracing.global_tracer
end