grpc_json_sniffer

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

README

gRPC JSON Sniffer for Go

gRPC JSON Sniffer is a Go module designed to capture and visualize gRPC messages in real-time. It intercepts gRPC calls using grpc.StreamServerInterceptor and grpc.UnaryServerInterceptor or grpc.StreamClientInterceptor and grpc.UnaryClientInterceptor, logs the calls to a JSON file, and provides a web-based interface for viewing and analyzing the captured messages. Captured messages can be filtered using CEL (Common Expression Language) queries.

For more information about interceptors, see grpc-go documentation.

gRPC JSON Sniffer Web UI

Usage

Integration into a gRPC Server

To integrate the JSON Sniffer into your gRPC server, import the package and create a new JSON interceptor. Then add the interceptor to your server options:

import grpc_json_sniffer "github.com/tsaarni/grpc-json-sniffer"

func setupGrpcServer() {
    // Create the interceptor. By default, logging is disabled.
    interceptor, err := grpc_json_sniffer.NewGrpcJsonInterceptor()
    if err != nil {
        // Handle error.
    }

    // Add interceptors to the gRPC server options.
    opts := []grpc.ServerOption{
        grpc.StreamInterceptor(interceptor.StreamServerInterceptor()),
        grpc.UnaryInterceptor(interceptor.UnaryServerInterceptor()),

        // Or use github.com/grpc-ecosystem/go-grpc-middleware to chain existing interceptors:
        // grpc.ChainStreamInterceptor(interceptor.StreamServerInterceptor(), <other>)
        // grpc.ChainUnaryInterceptor(interceptor.UnaryServerInterceptor(), <other>)
    }

    // Create new gRPC server with the options.
    s := grpc.NewServer(opts...)
    // ... further setup.
}

See example/server/server.go for full example.

Integration into a gRPC Client

Similar to the server, the JSON Sniffer can be integrated into a gRPC client.

import grpc_json_sniffer "github.com/tsaarni/grpc-json-sniffer"

func setupGrpcClient() {
	interceptor, err := grpc_json_sniffer.NewGrpcJsonInterceptor()
	// ...
	conn, err := grpc.NewClient(grpcServerAddress,
		// ...
		grpc.WithUnaryInterceptor(interceptor.UnaryClientInterceptor()),
		grpc.WithStreamInterceptor(interceptor.StreamClientInterceptor()),
	)
	// ...
}

See example/client/client.go for full example.

Configuration

By default the interceptor does not capture any messages. Its functionality is enabled by following environment variables:

  • GRPC_JSON_SNIFFER_FILE - Setting this variable enables the interceptor to log messages to a JSON file, for example /tmp/grpc_capture.json.
  • GRPC_JSON_SNIFFER_ADDR - Setting this variable enables the web server to serve the web viewer and captured messages, for example localhost:8080.

Alternatively, the interceptor can be configured programmatically using options:

interceptor, err := grpc_json_sniffer.NewGrpcJsonInterceptor(
    grpc_json_sniffer.WithFilename("/tmp/grpc_capture.json"),
    grpc_json_sniffer.WithAddr("localhost:8080"),
)

Standalone Viewer

The JSON Sniffer can be used to view previously captured messages. To install the viewer, run:

go install github.com/tsaarni/grpc-json-sniffer/cmd/grpc-json-sniffer-viewer

Then start the viewer with the path to the JSON file:

$ grpc-json-sniffer-viewer grpc_server_capture.json
Starting gRPC JSON sniffer viewer on localhost:8080

The server bind localhost:8080 by default. The address can be changed using the -addr flag:

$ grpc-json-sniffer-viewer -addr <address> <filename>

Contributing

Please refer to CONTRIBUTING.md.

Credits

  • This project uses cel-js by Marc Bachmann for CEL (Common Expression Language) parsing and evaluation, licensed under MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAddr

func WithAddr(addr string) func(*grpcJsonInterceptorOptions)

WithAddr sets the address for the GrpcJsonInterceptor.

Example:

interceptor, err := NewGrpcJsonInterceptor(WithAddr("localhost:8080"))

func WithFilename

func WithFilename(filename string) func(*grpcJsonInterceptorOptions)

WithFilename sets the filename for the GrpcJsonInterceptor.

Example:

interceptor, err := NewGrpcJsonInterceptor(WithFilename("grpc_messages.json"))

Types

type GrpcJsonInterceptor

type GrpcJsonInterceptor struct {
	// contains filtered or unexported fields
}

GrpcJsonInterceptor intercepts gRPC calls and logs the request and response messages as JSON to a file. It also serves a web viewer for the logged messages.

func NewGrpcJsonInterceptor

func NewGrpcJsonInterceptor(options ...func(*grpcJsonInterceptorOptions)) (*GrpcJsonInterceptor, error)

NewGrpcJsonInterceptor creates a new GrpcJsonInterceptor instance.

It can be configured using the environment variables: - GRPC_JSON_SNIFFER_FILE: enables JSON logging to a specified file. - GRPC_JSON_SNIFFER_ADDR: enables serving the web viewer at a specified address.

Alternatively, it can be configured through options: - WithFilename: enables JSON logging to a specified file. - WithAddr: enables serving the web viewer at a specified address.

func (*GrpcJsonInterceptor) StreamClientInterceptor

func (i *GrpcJsonInterceptor) StreamClientInterceptor() grpc.StreamClientInterceptor

StreamClientInterceptor returns a gRPC stream client interceptor that logs the request and response messages as JSON.

func (*GrpcJsonInterceptor) StreamServerInterceptor

func (i *GrpcJsonInterceptor) StreamServerInterceptor() func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

StreamServerInterceptor returns a gRPC stream server interceptor that logs the request and response messages as JSON.

func (*GrpcJsonInterceptor) UnaryClientInterceptor

func (i *GrpcJsonInterceptor) UnaryClientInterceptor() grpc.UnaryClientInterceptor

UnaryClientInterceptor returns a gRPC unary client interceptor that logs the request and response messages as JSON.

func (*GrpcJsonInterceptor) UnaryServerInterceptor

func (i *GrpcJsonInterceptor) UnaryServerInterceptor() func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

UnaryServerInterceptor returns a gRPC unary server interceptor that logs the request and response messages as JSON.

type GrpcWebViewer

type GrpcWebViewer struct {
	// contains filtered or unexported fields
}

func NewGrpcWebViewer

func NewGrpcWebViewer(addr string, messages string) *GrpcWebViewer

func (*GrpcWebViewer) Serve

func (v *GrpcWebViewer) Serve()

func (*GrpcWebViewer) ServeHTTP

func (v *GrpcWebViewer) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
cmd
example
client command
server command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL