xrequestid

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: MIT Imports: 9 Imported by: 0

README

xrequestid

xrequestid is an grpc interceptor which receives request id from metadata and set the request id to context. If request is is not found in metadata, generate a random request id by github.com/renstrom/shortuuid.

El Toro Fork Changelog

  • Use uuid instead of shortuuid since we are not passing it through context and not the url
  • Fixed the Compilation errors due to the depricated metadata.FromContext function
  • Added a go.mod and replaced imports with the go mod equivilent
  • Add an option PersistHeaders if you would like to add any incoming headers to the outgoing context
    • Use DefaultXRequestIDKey variable for the xrequest ID to be persisted
    • Example: xrequestid.PersistHeaders([]string{xrequestid.DefaultXRequestIDKey, "authorization"})
  • Add interceptor option to Log the incoming request along with the Request ID to the logrus entry in context.
  • Dependencies Added:
    • github.com/sirupsen/logrus
    • github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus
  • Note: Put the server interceptor before the github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus interceptor to have the request ID and data included in the response log

Usage

import (
 "github.com/eltorocorp/go-grpc-request-id-interceptor/xrequestid"
 "golang.org/x/net/context"
)

func main() {
 uIntOpt := grpc.UnaryInterceptor(xrequestid.UnaryServerInterceptor())
 sIntOpt := grpc.StreamInterceptor(xrequestid.StreamServerInterceptor())
 grpc.NewServer(uIntOpt, sIntOpt)
}

func foo(ctx context.Context) {
 requestID := xrequestid.FromContext(ctx)
 fmt.printf("requestID :%s", requestID)
}
Chaining Request ID

If request id is passed by metadata, the request id is used as is by default. xrequestid.ChainRequestID() is an option to chain multiple request ids by generating new request id for each request and concatenating it to original request ids.

func main() {
 uInt := xrequestid.UnaryServerInterceptor(xrequestid.ChainRequestID()))
 sInt := xrequestid.StreamServerInterceptor(xrequestid.ChainRequestID()))
}
Validating Request ID

It is important to validate request id in order to protect from abusing X-Request-ID. You can define own validator for request id, and set by xrequestid.RequestIDValidator().

func customRequestIDValidator(requestID string) bool {
 if len(requestID) < 4 {
  return false
 }
 return true
}

func main() {
 uInt := xrequestid.UnaryServerInterceptor(
  xrequestid.RequestIDValidator(customRequestIDValidator),
 ))
 sInt := xrequestid.StreamServerInterceptor(
  xrequestid.RequestIDValidator(customRequestIDValidator),
    ))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultXRequestIDKey = "x-request-id"

DefaultXRequestIDKey is metadata key name for request ID

Functions

func ContextWithID

func ContextWithID(ctx context.Context, requestID string) context.Context

Create a context with the private requestIDKey{} for testing

func FromContext

func FromContext(ctx context.Context) string

func HandleRequestID

func HandleRequestID(ctx context.Context, validator requestIDValidator) string

func HandleRequestIDChain

func HandleRequestIDChain(ctx context.Context, validator requestIDValidator) string

func StreamServerInterceptor

func StreamServerInterceptor(opt ...Option) grpc.StreamServerInterceptor

func UnaryServerInterceptor

func UnaryServerInterceptor(opt ...Option) grpc.UnaryServerInterceptor

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

func ChainRequestID

func ChainRequestID() Option

func LogRequest

func LogRequest() Option

Logs the incoming request with the request id and the method destination

func PersistHeaders

func PersistHeaders(headers []string) Option

Attach the request id to the outgoing context

func RequestIDValidator

func RequestIDValidator(validator requestIDValidator) Option

RequestIDValidator is validator function that returns true if request id is valid, or false if invalid.

Jump to

Keyboard shortcuts

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