Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat:implement ServeGRPCAndHTTP supporting generic HTTP Handlers and …
Browse files Browse the repository at this point in the history
…refactor ServeGRPCAndMetrics to use it (#7)

* feat:implement ServeGRPCAndHTTP supporting generic HTTP Handlers and refactor ServeGRPCAndMetrics to use it

* feat(auth):add support for authentification when creating a new gRPC service

* chore(go.mod):trigger go mod tidy sum update

* refactor: use default interceptors and pass extra extra-interceptors as arguments

* readme:add plain readme

* fix:fix lint issue

* chore(go.mod):add missing deps

* chore(go.mod):fix spurious pkg version
  • Loading branch information
ab116699 authored Feb 18, 2022
1 parent 1f36397 commit 60a9103
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Optable Package Lib

`optable-pkglib` is a set of useful packages for serving and maintaing gRPC services.

## Install

```sh

go get github.com/optable/optable-pkglib

```
15 changes: 11 additions & 4 deletions lifecycle/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ func ServeWithGracefulShutdown(ctx context.Context, listen net.Listener, server
return shutdownCompleted
}

// ServeGrpcAndMetrics behaves like ServeWithGracefulShutdown excepts that it
// also starts a prometheus HTTP1 service on the same Listener to expose
// ServeGRPCAndHTTP behaves like ServeWithGracefulShutdown excepts that it
// also starts an HTTP1 service on the same Listener to expose
// metrics.
func ServeGrpcAndMetrics(ctx context.Context, l net.Listener, server *grpc.Server, shutdownTimeout time.Duration) <-chan error {
func ServeGRPCAndHTTP(ctx context.Context, l net.Listener, handler http.Handler, server *grpc.Server, shutdownTimeout time.Duration) <-chan error {
errs := make(chan error, 1)

go func() {
Expand Down Expand Up @@ -101,7 +101,7 @@ func ServeGrpcAndMetrics(ctx context.Context, l net.Listener, server *grpc.Serve
// Serve requests for the prometheus HTTP metric handler.
group.Go(func() error {
httpServer := &http.Server{
Handler: promhttp.Handler(),
Handler: handler,
}
if err := httpServer.Serve(httpL); err != nil && !isClosedErr(err) {
return fmt.Errorf("Failed serving http: %w", err)
Expand Down Expand Up @@ -130,6 +130,13 @@ func ServeGrpcAndMetrics(ctx context.Context, l net.Listener, server *grpc.Serve
return errs
}

// ServeGRPCAndMetrics behaves like ServeWithGracefulShutdown excepts that it
// also starts a prometheus HTTP1 service on the same Listener to expose
// metrics.
func ServeGRPCAndMetrics(ctx context.Context, l net.Listener, server *grpc.Server, shutdownTimeout time.Duration) <-chan error {
return ServeGRPCAndHTTP(ctx, l, promhttp.Handler(), server, shutdownTimeout)
}

func isClosedErr(err error) bool {
return errors.Is(err, net.ErrClosed) || errors.Is(err, cmux.ErrServerClosed)
}
31 changes: 16 additions & 15 deletions service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import (
"google.golang.org/grpc"
)

// NewGrpcService creates a grpc service with various defaults middlewares.
// NewGRPCService creates a grpc service with various defaults middlewares.
// Notably, the logging and metrics are automatically registered for sane
// defaults of observability.
func NewGrpcService(ctx context.Context, service interface{}, descriptors []*grpc.ServiceDesc) (*grpc.Server, error) {
func NewGRPCService(ctx context.Context, service interface{}, descriptors []*grpc.ServiceDesc, unaryIntercepts []grpc.UnaryServerInterceptor, streamIntercepts []grpc.StreamServerInterceptor) (*grpc.Server, error) {
if len(descriptors) == 0 {
return nil, errors.New("Missing descriptors")
}

// By using prometheus.DefaultRegister we benefits from the go runtime
// defaults metrics and Linux processes metrics.
registry := prometheus.DefaultRegisterer
Expand All @@ -34,19 +33,21 @@ func NewGrpcService(ctx context.Context, service interface{}, descriptors []*grp
}

logger := zerolog.Ctx(ctx)
defaultStreamInterceptors := []grpc.StreamServerInterceptor{
logging.StreamServerInterceptor(grpczerolog.InterceptorLogger(*logger)),
metrics.StreamServerInterceptor(m),
recovery.StreamServerInterceptor(),
}
defaultUnaryInterceptors := []grpc.UnaryServerInterceptor{
logging.UnaryServerInterceptor(grpczerolog.InterceptorLogger(*logger)),
metrics.UnaryServerInterceptor(m),
recovery.UnaryServerInterceptor(),
}

defaultUnaryInterceptors = append(defaultUnaryInterceptors, unaryIntercepts...)
defaultStreamInterceptors = append(defaultStreamInterceptors, streamIntercepts...)

server := grpc.NewServer(
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(grpczerolog.InterceptorLogger(*logger)),
metrics.StreamServerInterceptor(m),
recovery.StreamServerInterceptor(),
),
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpczerolog.InterceptorLogger(*logger)),
metrics.UnaryServerInterceptor(m),
recovery.UnaryServerInterceptor(),
),
)
server := grpc.NewServer(grpc.ChainStreamInterceptor(defaultStreamInterceptors...), grpc.ChainUnaryInterceptor(defaultUnaryInterceptors...))

for _, desc := range descriptors {
logger.Info().Msgf("Registering grpc service: %s", desc.ServiceName)
Expand Down

0 comments on commit 60a9103

Please sign in to comment.