requestid

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package requestid handles a unique request correlation identifier via metadata, similary to the `X-Request-Id` header in HTTP.

Example (Other)

Example_other show how to set the interceptors of requestid on a gRPC server

package main

import (
	"google.golang.org/grpc"

	"github.com/jucrouzet/grpcutils/internal/pkg/foobar"
	"github.com/jucrouzet/grpcutils/pkg/requestid"
)

func main() {
	server := grpc.NewServer(
		// Can be used directly
		grpc.UnaryInterceptor(requestid.UnaryServerInterceptor),
		// Or chained with other interceptors
		grpc.ChainStreamInterceptor(
			requestid.StreamServerInterceptor,
			// other interceptors...
		),
	)
	foobar.RegisterDummyServiceServer(server, &foobar.UnimplementedDummyServiceServer{})
}
Output:

Index

Examples

Constants

View Source
const (
	// MetadataName is the name of the metadata that holds an unique identifier for the call.
	MetadataName = "request-id"
)

Variables

This section is empty.

Functions

func AppendToOutgoingContext

func AppendToOutgoingContext(ctx context.Context, id ...string) context.Context

AppendToOutgoingContext generates an outgoing gRPC context with a correlation identifier. If `id` is passed, it will be used, else a random identifier will be generated.

Example

ExampleAppendToOutgoingContext shows how to send a request correlation identifier to a client gRPC call

package main

import (
	"context"

	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"

	"github.com/jucrouzet/grpcutils/internal/pkg/foobar"
	"github.com/jucrouzet/grpcutils/pkg/requestid"
)

func main() {
	conn, err := grpc.DialContext(ctx, "127.0.0.1:1234")
	if err != nil {
		panic(err)
	}
	client := foobar.NewDummyServiceClient(conn)

	// Add a request id to call
	ctx = requestid.AppendToOutgoingContext(ctx, "i'm an unique id")
	var header metadata.MD
	_, err = client.Foo(ctx, &foobar.Empty{}, grpc.Header(&header))
	// as request correlation identifier is sent in response header,
	// requestid.GetFromMeta(header) => "i'm an unique id"
}

var ctx context.Context
Output:

func GetFromContext

func GetFromContext(ctx context.Context) string

GetFromContext returns the request correlation identifier from a gRPC incoming context

Example

ExampleGetFromContext shows how to get the request identifier in a gRPC method handler

package main

import (
	"context"
	"fmt"

	"github.com/jucrouzet/grpcutils/pkg/requestid"
)

func main() {
	requestId := requestid.GetFromContext(ctx)
	fmt.Println(requestId)
}

var ctx context.Context
Output:

func GetFromMeta

func GetFromMeta(md metadata.MD) string

GetFromMeta returns the request correlation identifier from a gRPC metadata map

func StreamServerInterceptor

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

StreamServerInterceptor is a server stream interceptor that ensures that method calls has a request correlation identifier metadata, adds it if not, and add a request correlation identifier metadata to response's header.

func UnaryServerInterceptor

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

UnaryServerInterceptor is a server unary interceptor that ensures that method calls has a request correlation identifier metadata, adds it if not, and add a request correlation identifier metadata to response's header.

Types

This section is empty.

Jump to

Keyboard shortcuts

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