How to Build a Go gRPC Unary Interceptor with Jaeger Tracing

This tutorial explains how to create a Go unary gRPC interceptor that captures request metadata and reports tracing information to Jaeger, covering type definitions, implementation steps, service setup, and testing procedures.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a Go gRPC Unary Interceptor with Jaeger Tracing

When developing gRPC services we often need common concerns such as logging, tracing, and authentication. These can be handled with gRPC interceptors. This article shows how to implement a Go unary interceptor that reports tracing information to Jaeger.

Original Type Definition

In the gRPC source package (interceptor.go) you can find the definition of a unary interceptor:

// UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server.
// info contains all the information of this RPC the interceptor can operate on.
// handler is the wrapper of the service method implementation. It is the responsibility of the interceptor to invoke handler to complete the RPC.
type UnaryServerInterceptor func(ctx context.Context, req any, info *UnaryServerInfo, handler UnaryHandler) (resp any, err error)

The interceptor is a function that receives four parameters and returns two values. The parameters are:

ctx: the context object.

req: request parameters.

info: RPC metadata such as service name and method name.

handler: the actual RPC method to be called.

Implementing a function with this signature and invoking the handler allows you to create a custom interceptor.

Implementing the Interceptor

First create a new project, e.g., grpcdemo.

Service Definition

Create a hello.proto file in the project directory and define a service.

Define a Makefile:

protos:
  protoc --proto_path=./ --go_out=pb --go-grpc_out=pb --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative ./*.proto

tidy:
  go mod tidy

run:
  go mod tidy
  go run main.go

Generate the Go code:

make protos

Step 1: create tracing.go to initialize the tracer.

Step 2: add the relevant code in main.go. The program starts a gRPC server listening on port 8091, initializes tracing before the server starts, and registers the custom interceptor that reports tracing data.

Start Jaeger Service

Refer to the official Jaeger documentation for the exact startup commands.

Test

Use the Goland gRPC plugin to send requests:

# GRPC localhost:8091/pb.HelloService/Hello
{
  "name": "ZhangSan"
}

# GRPC localhost:8091/pb.HelloService/HelloAgain
{
  "name": "ZhangSan"
}

Test results:

Test result
Test result

Open Jaeger UI to view the tracing information. The traces are successfully reported to the Jaeger service.

Jaeger UI
Jaeger UI
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

gRPCtracingjaegerUnary Interceptor
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.