File: server_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 (52 lines) | stat: -rw-r--r-- 1,455 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
50
51
52
# 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"

module Labkit
  module Correlation
    module GRPC
      # ServerInterceptor is a server-side GRPC interceptor
      # for injecting GRPC calls with a correlation-id passed from
      # a GRPC client to the GRPC Ruby Service
      class ServerInterceptor < ::GRPC::ServerInterceptor
        include Labkit::Correlation::GRPC::GRPCCommon

        def request_response(request: nil, call: nil, method: nil)
          wrap_with_correlation_id(call) do
            yield
          end
        end

        def client_streamer(call: nil, method: nil)
          wrap_with_correlation_id(call) do
            yield
          end
        end

        def server_streamer(request: nil, call: nil, method: nil)
          wrap_with_correlation_id(call) do
            yield
          end
        end

        def bidi_streamer(requests: nil, call: nil, method: nil)
          wrap_with_correlation_id(call) do
            yield
          end
        end

        private

        def wrap_with_correlation_id(call, &block)
          correlation_id = call.metadata[CORRELATION_METADATA_KEY]
          correlation_id ||= Labkit::Correlation::CorrelationId.current_or_new_id

          Labkit::Correlation::CorrelationId.use_id(correlation_id, &block)
        end
      end
    end
  end
end