class Datadog::Contrib::GRPC::DatadogInterceptor::Server

The DatadogInterceptor::Server implements the tracing strategy for gRPC server-side endpoints. When the datadog fields have been added to the gRPC call metadata, this middleware component will extract any client-side tracing information, attempting to associate its tracing context with a parent client-side context

Public Instance Methods

trace(keywords) { || ... } click to toggle source
# File lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb, line 15
def trace(keywords)
  options = {
    span_type: Datadog::Ext::HTTP::TYPE_INBOUND,
    service: service_name,
    resource: format_resource(keywords[:method])
  }
  metadata = keywords[:call].metadata

  set_distributed_context!(tracer, metadata)

  tracer.trace(Ext::SPAN_SERVICE, options) do |span|
    annotate!(span, metadata)

    yield
  end
end

Private Instance Methods

annotate!(span, metadata) click to toggle source
# File lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb, line 43
def annotate!(span, metadata)
  metadata.each do |header, value|
    next if reserved_headers.include?(header)
    span.set_tag(header, value)
  end

  # Set analytics sample rate
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?

  # Measure service stats
  Contrib::Analytics.set_measured(span)
rescue StandardError => e
  Datadog.logger.debug("GRPC client trace failed: #{e}")
end
format_resource(proto_method) click to toggle source
# File lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb, line 64
def format_resource(proto_method)
  proto_method.owner
              .to_s
              .downcase
              .split('::')
              .<<(proto_method.name)
              .join('.')
end
reserved_headers() click to toggle source
# File lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb, line 58
def reserved_headers
  [Datadog::Ext::DistributedTracing::GRPC_METADATA_TRACE_ID,
   Datadog::Ext::DistributedTracing::GRPC_METADATA_PARENT_ID,
   Datadog::Ext::DistributedTracing::GRPC_METADATA_SAMPLING_PRIORITY]
end
set_distributed_context!(tracer, metadata) click to toggle source
# File lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb, line 34
def set_distributed_context!(tracer, metadata)
  tracer.provider.context = Datadog::GRPCPropagator
                            .extract(metadata)
rescue StandardError => e
  Datadog.logger.debug(
    "unable to propagate GRPC metadata to context: #{e}"
  )
end