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 ¶
- Constants
- func AppendToOutgoingContext(ctx context.Context, id ...string) context.Context
- func GetFromContext(ctx context.Context) string
- func GetFromMeta(md metadata.MD) string
- func StreamServerInterceptor(srv interface{}, stream grpc.ServerStream, _ *grpc.StreamServerInfo, ...) error
- func UnaryServerInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, ...) (interface{}, error)
Examples ¶
Constants ¶
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 ¶
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 ¶
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 ¶
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.