Skip to content

Commit

Permalink
feat: add REST and gRPC logger middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Feb 9, 2021
1 parent 2f2c885 commit 7acb7a2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/golang/protobuf v1.4.3
github.com/gorilla/sessions v1.1.3
github.com/gorilla/websocket v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/julienschmidt/httprouter v1.2.0
github.com/markbates/pkger v0.17.1
github.com/ory/cli v0.0.11
Expand All @@ -44,6 +45,7 @@ require (
github.com/tidwall/gjson v1.6.0
github.com/tidwall/sjson v1.1.1
github.com/uber/jaeger-lib v2.4.0+incompatible // indirect
github.com/urfave/negroni v1.0.0
go.mongodb.org/mongo-driver v1.3.4 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ github.com/gotestyourself/gotestyourself v1.3.0 h1:9X3T0HDKAY/58/sEPpTkmyOg4wbb1
github.com/gotestyourself/gotestyourself v1.3.0/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQZOmDhRqsv5mZf+Jb1RnSLxcqZcI=
github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
Expand Down
7 changes: 3 additions & 4 deletions internal/driver/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"strings"

"github.com/julienschmidt/httprouter"
"github.com/ory/graceful"
"github.com/pkg/errors"
"github.com/soheilhy/cmux"
Expand All @@ -24,22 +23,22 @@ func (r *RegistryDefault) ServeAll(ctx context.Context) error {
}

func (r *RegistryDefault) ServeRead(ctx context.Context) func() error {
rt, s := r.ReadRouter().Router, r.ReadGRPCServer()
rt, s := r.ReadRouter(), r.ReadGRPCServer()

return func() error {
return multiplexPort(ctx, r.Config().ReadAPIListenOn(), rt, s)
}
}

func (r *RegistryDefault) ServeWrite(ctx context.Context) func() error {
rt, s := r.WriteRouter().Router, r.WriteGRPCServer()
rt, s := r.WriteRouter(), r.WriteGRPCServer()

return func() error {
return multiplexPort(ctx, r.Config().WriteAPIListenOn(), rt, s)
}
}

func multiplexPort(ctx context.Context, addr string, router *httprouter.Router, grpcS *grpc.Server) error {
func multiplexPort(ctx context.Context, addr string, router http.Handler, grpcS *grpc.Server) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions internal/driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"context"
"net/http"

"google.golang.org/grpc"

Expand Down Expand Up @@ -38,8 +39,8 @@ type (
HealthHandler() *healthx.Handler
Tracer() *tracing.Tracer

ReadRouter() *x.ReadRouter
WriteRouter() *x.WriteRouter
ReadRouter() http.Handler
WriteRouter() http.Handler

ReadGRPCServer() *grpc.Server
WriteGRPCServer() *grpc.Server
Expand Down
45 changes: 39 additions & 6 deletions internal/driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package driver
import (
"bytes"
"context"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/ory/x/reqlog"
"github.com/urfave/negroni"
"net/http"
"strings"

grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/julienschmidt/httprouter"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
Expand Down Expand Up @@ -188,7 +193,9 @@ func (r *RegistryDefault) allHandlers() []Handler {
return r.handlers
}

func (r *RegistryDefault) ReadRouter() *x.ReadRouter {
func (r *RegistryDefault) ReadRouter() http.Handler {
n := negroni.New(reqlog.NewMiddlewareFromLogger(r.l, "write#Ory Keto"))

br := &x.ReadRouter{Router: httprouter.New()}

r.HealthHandler().SetRoutes(br.Router, false)
Expand All @@ -197,10 +204,13 @@ func (r *RegistryDefault) ReadRouter() *x.ReadRouter {
h.RegisterReadRoutes(br)
}

return br
n.UseHandler(br)
return n
}

func (r *RegistryDefault) WriteRouter() *x.WriteRouter {
func (r *RegistryDefault) WriteRouter() http.Handler {
n := negroni.New(reqlog.NewMiddlewareFromLogger(r.l, "write#Ory Keto"))

pr := &x.WriteRouter{Router: httprouter.New()}

r.HealthHandler().SetRoutes(pr.Router, false)
Expand All @@ -209,11 +219,23 @@ func (r *RegistryDefault) WriteRouter() *x.WriteRouter {
h.RegisterWriteRoutes(pr)
}

return pr
n.UseHandler(pr)
return n
}

func (r *RegistryDefault) ReadGRPCServer() *grpc.Server {
s := grpc.NewServer()
s := grpc.NewServer(
grpc.StreamInterceptor(
grpcMiddleware.ChainStreamServer(
grpc_logrus.StreamServerInterceptor(r.l.Entry),
),
),
grpc.UnaryInterceptor(
grpcMiddleware.ChainUnaryServer(
grpc_logrus.UnaryServerInterceptor(r.l.Entry),
),
),
)

grpcHealthV1.RegisterHealthServer(s, r.HealthServer())

Expand All @@ -225,7 +247,18 @@ func (r *RegistryDefault) ReadGRPCServer() *grpc.Server {
}

func (r *RegistryDefault) WriteGRPCServer() *grpc.Server {
s := grpc.NewServer()
s := grpc.NewServer(
grpc.StreamInterceptor(
grpcMiddleware.ChainStreamServer(
grpc_logrus.StreamServerInterceptor(r.l.Entry),
),
),
grpc.UnaryInterceptor(
grpcMiddleware.ChainUnaryServer(
grpc_logrus.UnaryServerInterceptor(r.l.Entry),
),
),
)

grpcHealthV1.RegisterHealthServer(s, r.HealthServer())

Expand Down

0 comments on commit 7acb7a2

Please sign in to comment.