File: client_interceptor.rb

package info (click to toggle)
ruby-gitlab-labkit 0.36.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 444 kB
  • sloc: ruby: 1,705; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 1,397 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

# Disable the UnusedMethodArgument linter, since we need to declare the kwargs
# in the methods, but we don't actually use them.
require "grpc"
require "singleton"

module Labkit
  module Correlation
    module GRPC
      # ClientInterceptor is used to inject the correlation_id into the metadata
      # or a GRPC call for onward propagation to the server
      class ClientInterceptor < ::GRPC::ClientInterceptor
        include Labkit::Correlation::GRPC::GRPCCommon
        include Singleton

        def request_response(request:, call:, method:, metadata:)
          inject_correlation_id_into_metadata(metadata)

          yield
        end

        def client_streamer(requests:, call:, method:, metadata:)
          inject_correlation_id_into_metadata(metadata)

          yield
        end

        def server_streamer(request:, call:, method:, metadata:)
          inject_correlation_id_into_metadata(metadata)

          yield
        end

        def bidi_streamer(requests:, call:, method:, metadata:)
          inject_correlation_id_into_metadata(metadata)

          yield
        end

        private

        def inject_correlation_id_into_metadata(metadata, &block)
          metadata[CORRELATION_METADATA_KEY] = Labkit::Correlation::CorrelationId.current_id if Labkit::Correlation::CorrelationId.current_id
        end
      end
    end
  end
end