requestid

package
v2.36.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 10 Imported by: 0

README

RequestID grpc interceptor

This package contains Unary and Streaming grpc interceptor for RequestID. Please refer to instructions below for datails usage

Unary Server Interceptor

import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // use the interceptor
	grpcServer := grpc.NewServer(
		grpc.UnaryInterceptor(
			requestid.UnaryServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}
Using Unary Server Interceptor with custom RequestID Metadata key
import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // initialize requestid interceptor 
    // with metadata key options
    interceptor := requestid.NewInterceptor(
		WithMetadataKey("custom-requestid-key"),
	)

    // use the interceptor
	grpcServer := grpc.NewServer(
		grpc.UnaryInterceptor(
			interceptor.UnaryServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}
Using Unary Server Interceptor with custom RequestID Context key
import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // initialize requestid interceptor 
    // with context key options
    interceptor := requestid.NewInterceptor(
		WithContextKey("custom-context-key"),
	)

    // use the interceptor
	grpcServer := grpc.NewServer(
		grpc.UnaryInterceptor(
			interceptor.UnaryServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}

Streaming Server Interceptor

import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // use the interceptor
	grpcServer := grpc.NewServer(
		grpc.StreamInterceptor(
			requestid.StreamingServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}
Using Streaming Server Interceptor with custom RequestID Metadata key
import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // initialize requestid interceptor 
    // with metadata key options
    interceptor := requestid.NewInterceptor(
		WithMetadataKey("custom-requestid-key"),
	)

    // use the interceptor
    grpcServer := grpc.NewServer(
		grpc.StreamInterceptor(
			interceptor.StreamingServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}
Using Streaming Server Interceptor with custom RequestID Context key
import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // initialize requestid interceptor 
    // with context key options
    interceptor := requestid.NewInterceptor(
		WithContextKey("custom-context-key"),
	)

    // use the interceptor
	grpcServer := grpc.NewServer(
		grpc.StreamInterceptor(
			interceptor.StreamingServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}

Using both Unary and Streaming server interceptor

import(
    ...
    "github.com/kitabisa/perkakas/v2/grpcinterceptor/requestid"
    ...
)

func main(){
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "0.0.0.0", 50051))
	if err != nil {
		opt.Logger.Error(fmt.Sprintf("failed to listen %s:%d", host, port), err, nil)
	}

    // use the interceptor
	grpcServer := grpc.NewServer(
		grpc.UnaryInterceptor(
			requestid.UnaryServerInterceptor,
		),
        grpc.StreamInterceptor(
			requestid.StreamingServerInterceptor,
		),
	)

    // initialize grpc handler
	grpcHandler := grpcHandler.NewFlagHandler(opt)

	pb.RegisterFlagServer(grpcServer, grpcHandler)
	grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

	opt.Logger.Info(fmt.Sprintf("GRPC serve at %s:%d", host, port), nil)

	grpcServer.Serve(lis)
}

Documentation

Index

Constants

View Source
const (
	GrpcRequestIDKey = "x-ktbs-request-id"
)

Variables

This section is empty.

Functions

func Init added in v2.28.0

func Init()

Init creating default interceptor instance the reason of not using go init() is to prevent unwanted extra requestID interceptor instance when using this interceptor without default instance

func StreamingServerInterceptor added in v2.28.0

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

StreamingServerInterceptor calling requestID StreamingServerInterceptor with default interceptor instance

func UnaryServerInterceptor

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

UnaryServerInterceptor calling requestID UnaryServerInterceptor with default interceptor instance

Types

type Interceptor added in v2.28.0

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

func NewInterceptor added in v2.28.0

func NewInterceptor(opts ...Options) *Interceptor

func (*Interceptor) StreamingServerInterceptor added in v2.28.0

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

func (*Interceptor) UnaryServerInterceptor added in v2.28.0

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

type Options added in v2.28.0

type Options func(*Interceptor)

func WithContextKey added in v2.28.0

func WithContextKey(key string) Options

WithContextKey set requestID context value key. provides an option to use this interceptor with context value key other than "X-Ktbs-Request-ID"

func WithMetadataKey added in v2.28.0

func WithMetadataKey(key string) Options

WithMetadataKey set reqIDKey that should be send by client using grpc metadata, provides an option to use this interceptor with requestIDKey metadata other than "x-ktbs-request-id"

Jump to

Keyboard shortcuts

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