interceptor

package
v0.0.0-...-3703172 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGrpcInternalError = grpc.Errorf(codes.Unknown, "internal server error")

ErrGrpcInternalError indicates an internal server error

Functions

func Logger

Logger returns an interceptor that will set on the context a *logrus.Entry that will automatically be tagged with the request_id UUIDv4.

func Timer

func Timer(m *metrics.Metrics) grpc.UnaryServerInterceptor

Timer is a grpc middleware for handling timing how long endpoints take to handle requests and send them to the client given. Emits 2 metrics to the given statsd client:

* A counter for the endpoint so you can see how many times it has been called * A timer to see how long the endpoint takes.

So given a prefix of "production.myapp", and an RPC endpoint called "CreateThing", you'd have 2 keys emitted to statsd:

* INCR production.myapp.CreateThing * TIMING production.myapp.CreateThing.duration

Types

type ErrorFielder

type ErrorFielder interface {
	error
	Fields() *logrus.Fields
}

ErrorFielder declares something that fulfills error interface and has Logrus error fields. We will use this to pass error fields from handlers to middlewares for Logs implements error interface in order to be passed around as an error

type ErrorKeyer

type ErrorKeyer interface {
	error
	Key() string
}

ErrorKeyer declares something that fulfills error interface and has a key. We will use this when we want machine readable error string alongside error message (e.g. proto errors) implements error interface in order to be passed around as an error

type FieldErr

type FieldErr struct {
	ErrFields *logrus.Fields
	// contains filtered or unexported fields
}

FieldErr implements ErrorFielder

func NewFieldErr

func NewFieldErr(fields *logrus.Fields) *FieldErr

NewFieldErr creates a new error with fields

func (*FieldErr) Error

func (e *FieldErr) Error() string

Error returns the error message Needed to fulfill interface requirement (error)

func (*FieldErr) Fields

func (e *FieldErr) Fields() *logrus.Fields

Fields returns the fields for this error

type Interceptor

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

Interceptor makes it easy to return unary interceptors for grpc servers

func New

func New() *Interceptor

New initializes and returns an Interceptor

func (*Interceptor) Middleware

func (i *Interceptor) Middleware() grpc.UnaryServerInterceptor

Middleware returns a grpc.UnaryServerHandler compatible function documented here https://github.com/grpc/grpc-go/tree/master/interceptor.go#L73 It will handle intercepting errors and unwrapping the original error for logging.

A grpc endpoint should return generic errors such as "company not found" by wrapping the original error. This chaining logic shamelessly stolen from https://github.com/mwitkow/go-grpc-middleware/blob/master/chain.go

func (*Interceptor) Use

Use adds a middleware to the chain for the server interceptor

type KeyedErr

type KeyedErr struct {
	Message  string
	ErrorKey string
	// contains filtered or unexported fields
}

KeyedErr implements ErrorKeyer

func (*KeyedErr) Error

func (e *KeyedErr) Error() string

Error returns the error message

func (*KeyedErr) Key

func (e *KeyedErr) Key() string

Key returns the error key

Jump to

Keyboard shortcuts

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