Skip to content

Commit

Permalink
chore(sever): add http_path field in request log (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd authored May 2, 2024
1 parent a050ec0 commit 6bef30c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
30 changes: 24 additions & 6 deletions internal/server/auth.go → internal/server/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"net/http"

ctx_logrus "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
"github.com/sirupsen/logrus"
Expand All @@ -11,9 +12,13 @@ import (

type authenticatedUserEmailContextKey struct{}

var logrusActorKey = "actor"
const (
logrusActorKey = "actor"

func withAuthenticatedUserEmail(headerKey string) grpc.UnaryServerInterceptor {
grpcgatewayHTTPPathKey = "http-path"
)

func headerAuthInterceptor(headerKey string) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if md, ok := metadata.FromIncomingContext(ctx); ok {
if v := md.Get(headerKey); len(v) > 0 {
Expand All @@ -26,14 +31,27 @@ func withAuthenticatedUserEmail(headerKey string) grpc.UnaryServerInterceptor {
}
}

func withLogrusContext() grpc.UnaryServerInterceptor {
func enrichRequestMetadata(ctx context.Context, req *http.Request) metadata.MD {
return metadata.New(map[string]string{
grpcgatewayHTTPPathKey: req.URL.Path,
})
}

func enrichLogrusFields() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
fields := make(logrus.Fields, 0)

if userEmail, ok := ctx.Value(authenticatedUserEmailContextKey{}).(string); ok {
ctx_logrus.AddFields(ctx, logrus.Fields{
logrusActorKey: userEmail,
})
fields[logrusActorKey] = userEmail
}

if md, ok := metadata.FromIncomingContext(ctx); ok {
fields["http_path"] = md[grpcgatewayHTTPPathKey][0]
}

if len(fields) > 0 {
ctx_logrus.AddFields(ctx, fields)
}
return handler(ctx, req)
}
}
5 changes: 3 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func RunServer(config *Config) error {
),
grpc_logrus.UnaryServerInterceptor(logrusEntry),
authInterceptor,
withLogrusContext(),
enrichLogrusFields(),
otelgrpc.UnaryServerInterceptor(),
)),
)
Expand Down Expand Up @@ -134,6 +134,7 @@ func RunServer(config *Config) error {
DiscardUnknown: true,
},
}),
runtime.WithMetadata(enrichRequestMetadata),
)

// grpcPort has to be same as config.Port till the time guardian service can support both grpc and http in two different ports
Expand Down Expand Up @@ -216,7 +217,7 @@ func makeHeaderMatcher(c *Config) func(key string) (string, bool) {

func getAuthInterceptor(config *Config) (grpc.UnaryServerInterceptor, error) {
// default fallback to user email on header
authInterceptor := withAuthenticatedUserEmail(config.Auth.Default.HeaderKey)
authInterceptor := headerAuthInterceptor(config.Auth.Default.HeaderKey)

if config.Auth.Provider == "oidc" {
idtokenValidator, err := idtoken.NewValidator(context.Background())
Expand Down

0 comments on commit 6bef30c

Please sign in to comment.