Class: OpenTelemetry::Instrumentation::Gruf::Interceptors::Server

Inherits:
Gruf::Interceptors::ServerInterceptor
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/gruf/interceptors/server.rb

Instance Method Summary collapse

Instance Method Details

#callObject



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
# File 'lib/opentelemetry/instrumentation/gruf/interceptors/server.rb', line 12

def call
  return yield if instrumentation_config.empty?

  method = request.method_name

  return yield if instrumentation_config[:grpc_ignore_methods_on_server].include?(method)

  service_name = request.service.service_name.to_s
  method_name = request.method_key.to_s
  route = "/#{service_name}/#{method_name.camelize}"
   = request.active_call.

  attributes = {
    OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'grpc',
    OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => service_name,
    OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => method_name,
    OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE => instrumentation_config[:peer_service],
    'net.sock.peer.addr' => request.active_call.instance_variable_get(:@wrapped)&.peer
  }.compact

  attributes.merge!((.transform_keys(&:to_s)))

  extracted_context = OpenTelemetry.propagation.extract()
  OpenTelemetry::Context.with_current(extracted_context) do
    instrumentation_tracer.in_span(route, attributes: attributes, kind: OpenTelemetry::Trace::SpanKind::SERVER) do |span|
      yield.tap do
        span&.set_attribute(OpenTelemetry::SemanticConventions::Trace::RPC_GRPC_STATUS_CODE, 0)
      end
    end
  end
end