requestid

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

RequestID

This package supports the case where each request needs to be assigned its own unique request_id, allowing the request to be easily traced in logs as it goes from one service to another.

The Request-Id server interceptor will check for a Request-Id from incoming metadata, generating one if not present and inserting it into the context. Then it will also add it as a field to the context logger provided by the grpc_logrus package.

Request IDs are UUIDv4 values generated by Google's UUID package.

Adding support for Request-ID

You can enable support for Request-Ids in your gRPC-Server by adding the interceptor to the middleware chain. However, the ordering is important. If you also use the grpc_logrus interceptor, the request-id middleware should be later in the middleware chain, but should also be before any other service middlewares to ensure it is present in the context to be included in those requests.

import (
  ...
  ...
  "github.com/infobloxopen/atlas-app-toolkit/requestid"
)
func main() {
    server := grpc.NewServer(
      grpc.UnaryInterceptor(
        grpc_middleware.ChainUnaryServer(  // middleware chain
          ...
          [grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logger)),]
          requestid.UnaryServerInterceptor(),  // Request-Id middleware
          ...
          ),
        ),
      )
    ...
}

Extracting the Request-ID

Once the middleware is included, the following function

rid, ok := requestid.FromContext(ctx)

can extract the request-id anywhere it is needed. The ok field returns whether the request id was actually found in the provided context.

Documentation

Index

Constants

View Source
const (
	DeprecatedRequestIDKey = "Request-Id"
	DefaultRequestIDKey    = "request_id"
)

DefaultRequestIDKey is the metadata key name for request ID

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) (string, bool)

FromContext returns the Request-Id information from ctx if it exists.

func HandleRequestID added in v0.19.2

func HandleRequestID(ctx context.Context) (reqID string)

HandleRequestID either extracts a existing and valid request ID from the context or generates a new one

func NewContext

func NewContext(ctx context.Context, reqID string) context.Context

NewContext creates a new context with Request-Id attached if not exists.

func StreamServerInterceptor added in v0.17.1

func StreamServerInterceptor() grpc.StreamServerInterceptor

func UnaryServerInterceptor

func UnaryServerInterceptor() grpc.UnaryServerInterceptor

UnaryServerInterceptor returns grpc.UnaryServerInterceptor that should be used as a middleware to generate/include Request-Id in headers and context for tracing and tracking user's request.

Returned middleware populates Request-Id from gRPC metadata if they defined in a testRequest message else creates a new one.

Types

This section is empty.

Jump to

Keyboard shortcuts

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