Class: Griffin::Interceptors::Server::NewRelicInterceptor

Inherits:
GRPC::ServerInterceptor
  • Object
show all
Defined in:
lib/griffin/interceptors/server/newrelic_interceptor.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(ignored_services: []) ⇒ NewRelicInterceptor

Returns a new instance of NewRelicInterceptor.



9
10
11
# File 'lib/griffin/interceptors/server/newrelic_interceptor.rb', line 9

def initialize(ignored_services: [])
  @ignored_services = ignored_services.map(&:service_name)
end

Instance Method Details

#request_response(method: nil, call: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/griffin/interceptors/server/newrelic_interceptor.rb', line 15

def request_response(method: nil, call: nil, **)
  return yield unless NewRelic::Agent.instance.started?

  service_name = call.service_name

  return yield if @ignored_services.include?(service_name)

  # gRPC's HTTP method is fixed. https://github.com/grpc/grpc/blob/af89e8c00e796f3398b09b7daed693df2b14da56/doc/PROTOCOL-HTTP2.md
  request = Request.new("/#{service_name}/#{method.name}", call.['user-agent'], 'POST')

  in_transaction("#{service_name}/#{method.name}", request) do |txn|
    yield.tap do
      # gRPC always returns HTTP status code 200.
      txn.http_response_code = '200'
    end
  end
end