summaryrefslogtreecommitdiff
path: root/lib/gitlab/tracing/common.rb
blob: 55d6272829d96f311e139baab39a3150c4c733cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

module Gitlab
  module Tracing
    module Common
      def tracer
        OpenTracing.global_tracer
      end

      # Convience method for running a block with a span
      def start_active_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 = Gitlab::CorrelationId.current_id
        if correlation_id
          span.set_tag('correlation_id', correlation_id)
        end

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

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

        if exception.is_a? Exception
          span.log_kv(
            'event':        'error',
            'error.kind':   exception.class.to_s,
            'error.object': exception,
            'message':      exception.message,
            'stack':        exception.backtrace.join("\n")
          )
        else
          span.log_kv(
            'event':        'error',
            'error.kind':   exception.class.to_s,
            'error.object': exception
          )
        end
      end
    end
  end
end