Skip to content

Integration Grpc Extension

Aryeh Citron edited this page Apr 21, 2026 · 17 revisions

Integration: gRPC Extension

Track gRPC client calls in your test diagrams using the TestTrackingDiagrams.Extensions.Grpc NuGet package. This extension intercepts all gRPC call types (unary, server-streaming, client-streaming, duplex-streaming) via the standard Grpc.Core.Interceptors.Interceptor API.

Installation

dotnet add package TestTrackingDiagrams.Extensions.Grpc

Quick Start

using Grpc.Net.Client;
using TestTrackingDiagrams.Extensions.Grpc;

var channel = GrpcChannel.ForAddress("https://localhost:5001");
var invoker = channel.WithTestTracking(new GrpcTrackingOptions
{
    ServiceName = "GreeterService",
    CallingServiceName = "MyApi",
    Verbosity = GrpcTrackingVerbosity.Detailed,
    CurrentTestInfoFetcher = () => (TestContext.CurrentTestName, TestContext.CurrentTestId)
});

var client = new Greeter.GreeterClient(invoker);

Or using the interceptor directly:

var invoker = channel.Intercept(new GrpcTrackingInterceptor(options));
var client = new Greeter.GreeterClient(invoker);

How It Works

GrpcTrackingInterceptor extends Grpc.Core.Interceptors.Interceptor, overriding all five call types:

Call Type Override Tracking
Async Unary AsyncUnaryCall Full request + response
Blocking Unary BlockingUnaryCall Full request + response
Server Streaming AsyncServerStreamingCall Request + call initiation
Client Streaming AsyncClientStreamingCall Call initiation + response
Duplex Streaming AsyncDuplexStreamingCall Call initiation + response

Streaming calls are tracked at the initiation level (not per-message) to keep diagrams clean.

Supported Operations

Method Type Enum Value
Unary GrpcOperation.UnaryCall
Server Streaming GrpcOperation.ServerStreamingCall
Client Streaming GrpcOperation.ClientStreamingCall
Duplex Streaming GrpcOperation.DuplexStreamingCall

Message Serialization

Request and response messages are serialized for diagram content:

  • Protobuf IMessage: Uses Google.Protobuf.JsonFormatter.Default.Format() for JSON output
  • Other types: Falls back to .ToString()
  • Summarised verbosity: Content omitted entirely

gRPC Status Code Mapping

gRPC status codes are mapped to HTTP status codes for consistent diagram logging:

gRPC Status HTTP Status
OK 200 OK
NotFound 404 Not Found
PermissionDenied 403 Forbidden
Unauthenticated 401 Unauthorized
InvalidArgument 400 Bad Request
DeadlineExceeded 408 Request Timeout
AlreadyExists 409 Conflict
ResourceExhausted 429 Too Many Requests
Unavailable 503 Service Unavailable
Unimplemented 501 Not Implemented
Cancelled 408 Request Timeout
Other 500 Internal Server Error

Verbosity Levels

GrpcTrackingVerbosity.Summarised

Minimal output:

  • Label: Method name only (e.g. SayHello)
  • URI: grpc:///ServiceName/
  • Content: Omitted
  • Headers: Omitted

GrpcTrackingVerbosity.Detailed

Balanced output:

  • Label: Method name with streaming annotation (e.g. SayHello, StreamMessages (server-stream))
  • URI: grpc:///ServiceName/MethodName
  • Content: Serialized protobuf messages
  • Headers: Omitted

GrpcTrackingVerbosity.Raw

Full output:

  • Label: /package.Service/Method [UnaryCall]
  • URI: grpc:///package.Service/Method
  • Content: Serialized protobuf messages
  • Headers: Included (non-binary metadata entries)

Configuration Options

new GrpcTrackingOptions
{
    // Display name in diagrams (default: "GrpcService")
    ServiceName = "GreeterService",

    // Calling service name in diagrams (default: "Caller")
    CallingServiceName = "MyApi",

    // Verbosity level (default: Detailed)
    Verbosity = GrpcTrackingVerbosity.Detailed,

    // Required: provides test context
    CurrentTestInfoFetcher = () => (testName, testId),

    // Use proto service name instead of ServiceName (default: false)
    UseProtoServiceNameInDiagram = false
}

UseProtoServiceNameInDiagram

When true, the proto service name from the method definition (e.g. greet.Greeter) is used as the service name in diagrams instead of the configured ServiceName. Useful when tracking multiple gRPC services through a single interceptor.

ITrackingComponent

GrpcTrackingInterceptor implements ITrackingComponent and auto-registers with TrackingComponentRegistry:

  • ComponentName: "GrpcTrackingInterceptor ({ServiceName})"
  • WasInvoked: true after first call
  • InvocationCount: Total calls processed (all call types)

URI Scheme

Verbosity Format Example
Summarised grpc:///ServiceName/ grpc:///greet.Greeter/
Detailed grpc:///ServiceName/MethodName grpc:///greet.Greeter/SayHello
Raw grpc:///FullMethodPath grpc:////greet.Greeter/SayHello

See Also

Home


Demo


Getting Started

Common Tasks

Integration Guides

Extensions

Configuration

Features

Reference

Clone this wiki locally