requestid

package
v1.11.0 Latest Latest
Warning

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

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

Documentation

Overview

Package requestid is a grpc middleware for keeping track of chained requests and calls within a transaction.

The package is using on github.com/grpc-ecosystem/go-grpc-middleware/tags

Example
package main

import (
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
	"google.golang.org/grpc"

	"github.com/SKF/go-utility/grpc-interceptor/requestid"
)

func main() {
	_ = grpc.NewServer(
		grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
			grpc_ctxtags.StreamServerInterceptor(),
			requestid.StreamServerInterceptor("LOG_NAME"),
		)),
		grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
			grpc_ctxtags.UnaryServerInterceptor(),
			requestid.UnaryServerInterceptor("LOG_NAME"),
		)),
	)
}
Output:

Index

Examples

Constants

View Source
const RequestChainKey = "request.chain"
View Source
const RequestIDKey = "request.id"
View Source
const RequestTransactionIDKey = "request.transaction.id"

Variables

This section is empty.

Functions

func ExtendContext added in v1.1.0

func ExtendContext(ctx context.Context, serviceName string) context.Context

ExtendContext extends the context with a Request ID Metadata.

Example
package main

import (
	"context"

	"github.com/SKF/go-utility/grpc-interceptor/requestid"
)

func main() {
	outgoingGrpcCallContext := context.Background()
	requestid.ExtendContext(outgoingGrpcCallContext, "Example")
}
Output:

func StreamClientInterceptor added in v1.1.0

func StreamClientInterceptor(serviceName string) grpc.StreamClientInterceptor

StreamClientInterceptor returns a new streaming client interceptor that adds the Request ID Metadata to the call.

func StreamServerInterceptor

func StreamServerInterceptor(serviceName string) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor that adds the Request ID Metadata to the call.

func UnaryClientInterceptor added in v1.1.0

func UnaryClientInterceptor(serviceName string) grpc.UnaryClientInterceptor

UnaryClientInterceptor returns a new unary client interceptor that adds the Request ID Metadata to the call.

func UnaryServerInterceptor

func UnaryServerInterceptor(serviceName string) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that adds the Request ID Metadata to the call.

Types

type Request

type Request struct {
	ID            uuid.UUID `json:"id"`
	Chain         []string  `json:"chain"`
	TransactionID uuid.UUID `json:"transactionId"`
}

Request is a data holder for the different Request ID Metadata

func Extract

func Extract(ctx context.Context) (request Request)

Extract will get the Request ID Metadata out of the context.

Example
package main

import (
	"context"

	"github.com/SKF/go-utility/grpc-interceptor/requestid"
	"github.com/SKF/go-utility/log"
)

func main() {
	var grpcCallContext context.Context
	log.WithField("request", requestid.Extract(grpcCallContext)).
		Infof("Request ID Metadata")
}
Output:

Jump to

Keyboard shortcuts

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