summaryrefslogtreecommitdiff
path: root/lib/gitlab/tracing/grpc_interceptor.rb
blob: 911294d48411b8ab3fc443d222b181a76c7a4996 (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

require 'opentracing'
require 'grpc'

module Gitlab
  module Tracing
    class GRPCInterceptor < GRPC::ClientInterceptor
      include Common
      include Singleton

      def initialize
      end

      def request_response(request:, call:, method:, metadata:)
        wrap_with_tracing(method, 'unary', metadata) do
          yield
        end
      end

      def client_streamer(requests:, call:, method:, metadata:)
        wrap_with_tracing(method, 'client_stream', metadata) do
          yield
        end
      end

      def server_streamer(request:, call:, method:, metadata:)
        wrap_with_tracing(method, 'server_stream', metadata) do
          yield
        end
      end

      def bidi_streamer(requests:, call:, method:, metadata:)
        wrap_with_tracing(method, 'bidi_stream', metadata) do
          yield
        end
      end

      private

      def wrap_with_tracing(method, grpc_type, metadata)
        start_active_span(
          operation_name: method,
          tags: {
            'component':  'grpc',
            'span.kind':  'client',
            'grpc.method': method,
            'grpc.type':   grpc_type
          }) do |span|
          OpenTracing.inject(span.context, OpenTracing::FORMAT_TEXT_MAP, metadata)
          yield
        end
      end
    end
  end
end