1# Go gRPC Middleware 2 3[![Travis Build](https://travis-ci.org/grpc-ecosystem/go-grpc-middleware.svg?branch=master)](https://travis-ci.org/grpc-ecosystem/go-grpc-middleware) 4[![Go Report Card](https://goreportcard.com/badge/github.com/grpc-ecosystem/go-grpc-middleware)](https://goreportcard.com/report/github.com/grpc-ecosystem/go-grpc-middleware) 5[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware) 6[![SourceGraph](https://sourcegraph.com/github.com/grpc-ecosystem/go-grpc-middleware/-/badge.svg)](https://sourcegraph.com/github.com/grpc-ecosystem/go-grpc-middleware/?badge) 7[![codecov](https://codecov.io/gh/grpc-ecosystem/go-grpc-middleware/branch/master/graph/badge.svg)](https://codecov.io/gh/grpc-ecosystem/go-grpc-middleware) 8[![Apache 2.0 License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) 9[![quality: production](https://img.shields.io/badge/quality-production-orange.svg)](#status) 10[![Slack](https://img.shields.io/badge/slack-%23grpc--middleware-brightgreen)](https://slack.com/share/IRUQCFC23/9Tm7hxRFVKKNoajQfMOcUiIk/enQtODc4ODI4NTIyMDcxLWM5NDA0ZTE4Njg5YjRjYWZkMTI5MzQwNDY3YzBjMzE1YzdjOGM5ZjI1NDNiM2JmNzI2YjM5ODE5OTRiNTEyOWE) 11 12[gRPC Go](https://github.com/grpc/grpc-go) Middleware: interceptors, helpers, utilities. 13 14## Middleware 15 16[gRPC Go](https://github.com/grpc/grpc-go) recently acquired support for 17Interceptors, i.e. [middleware](https://medium.com/@matryer/writing-middleware-in-golang-and-how-go-makes-it-so-much-fun-4375c1246e81#.gv7tdlghs) 18that is executed either on the gRPC Server before the request is passed onto the user's application logic, or on the gRPC client around the user call. It is a perfect way to implement 19common patterns: auth, logging, message, validation, retries or monitoring. 20 21These are generic building blocks that make it easy to build multiple microservices easily. 22The purpose of this repository is to act as a go-to point for such reusable functionality. It contains 23some of them itself, but also will link to useful external repos. 24 25`grpc_middleware` itself provides support for chaining interceptors, here's an example: 26 27```go 28import "github.com/grpc-ecosystem/go-grpc-middleware" 29 30myServer := grpc.NewServer( 31 grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( 32 grpc_recovery.StreamServerInterceptor(), 33 grpc_ctxtags.StreamServerInterceptor(), 34 grpc_opentracing.StreamServerInterceptor(), 35 grpc_prometheus.StreamServerInterceptor, 36 grpc_zap.StreamServerInterceptor(zapLogger), 37 grpc_auth.StreamServerInterceptor(myAuthFunction), 38 )), 39 grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( 40 grpc_recovery.UnaryServerInterceptor(), 41 grpc_ctxtags.UnaryServerInterceptor(), 42 grpc_opentracing.UnaryServerInterceptor(), 43 grpc_prometheus.UnaryServerInterceptor, 44 grpc_zap.UnaryServerInterceptor(zapLogger), 45 grpc_auth.UnaryServerInterceptor(myAuthFunction), 46 )), 47) 48``` 49 50## Interceptors 51 52*Please send a PR to add new interceptors or middleware to this list* 53 54#### Auth 55 * [`grpc_auth`](auth) - a customizable (via `AuthFunc`) piece of auth middleware 56 57#### Logging 58 * [`grpc_ctxtags`](tags/) - a library that adds a `Tag` map to context, with data populated from request body 59 * [`grpc_zap`](logging/zap/) - integration of [zap](https://github.com/uber-go/zap) logging library into gRPC handlers. 60 * [`grpc_logrus`](logging/logrus/) - integration of [logrus](https://github.com/sirupsen/logrus) logging library into gRPC handlers. 61 * [`grpc_kit`](logging/kit/) - integration of [go-kit](https://github.com/go-kit/kit/tree/master/log) logging library into gRPC handlers. 62 * [`grpc_grpc_logsettable`](logging/settable/) - a wrapper around `grpclog.LoggerV2` that allows to replace loggers in runtime (thread-safe). 63 64#### Monitoring 65 * [`grpc_prometheus`⚡](https://github.com/grpc-ecosystem/go-grpc-prometheus) - Prometheus client-side and server-side monitoring middleware 66 * [`otgrpc`⚡](https://github.com/grpc-ecosystem/grpc-opentracing/tree/master/go/otgrpc) - [OpenTracing](http://opentracing.io/) client-side and server-side interceptors 67 * [`grpc_opentracing`](tracing/opentracing) - [OpenTracing](http://opentracing.io/) client-side and server-side interceptors with support for streaming and handler-returned tags 68 69#### Client 70 * [`grpc_retry`](retry/) - a generic gRPC response code retry mechanism, client-side middleware 71 72#### Server 73 * [`grpc_validator`](validator/) - codegen inbound message validation from `.proto` options 74 * [`grpc_recovery`](recovery/) - turn panics into gRPC errors 75 * [`ratelimit`](ratelimit/) - grpc rate limiting by your own limiter 76 77 78## Status 79 80This code has been running in *production* since May 2016 as the basis of the gRPC micro services stack at [Improbable](https://improbable.io). 81 82Additional tooling will be added, and contributions are welcome. 83 84## License 85 86`go-grpc-middleware` is released under the Apache 2.0 license. See the [LICENSE](LICENSE) file for details. 87