Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
d80tb7 committed Jan 5, 2025
1 parent 67d3db0 commit b708709
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 44 deletions.
5 changes: 4 additions & 1 deletion internal/common/armadacontext/armada_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func FromGrpcCtx(ctx context.Context) *Context {
if ok {
return armadaCtx
}
return New(ctx, logrus.NewEntry(logrus.StandardLogger()))
logger := logrus.NewEntry(logrus.StandardLogger()).
WithField("user", ctx.Value("user")).
WithField("requestId", ctx.Value("requestId"))
return New(ctx, logger)
}

// New returns an armada context that encapsulates both a go context and a logger
Expand Down
2 changes: 2 additions & 0 deletions internal/common/auth/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func CreateGrpcMiddlewareAuthFunction(authService AuthService) func(ctx context.
if err != nil {
return nil, err
}
// record username for request logging
ctx = context.WithValue(ctx, "user", principal.GetName())
return WithPrincipal(ctx, principal), nil
}
}
Expand Down
37 changes: 0 additions & 37 deletions internal/common/grpc/armadacontext/interceptors.go

This file was deleted.

10 changes: 4 additions & 6 deletions internal/common/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/armadaproject/armada/internal/common/armadaerrors"
"github.com/armadaproject/armada/internal/common/auth"
"github.com/armadaproject/armada/internal/common/certs"
grpc_armadacontext "github.com/armadaproject/armada/internal/common/grpc/armadacontext"
"github.com/armadaproject/armada/internal/common/grpc/configuration"
"github.com/armadaproject/armada/internal/common/requestid"
)
Expand Down Expand Up @@ -56,7 +55,6 @@ func CreateGrpcServer(
srvMetrics.UnaryServerInterceptor(),
requestid.UnaryServerInterceptor(false),
grpc_auth.UnaryServerInterceptor(authFunction),
grpc_armadacontext.UnaryServerInterceptor(),
grpc_logging.UnaryServerInterceptor(InterceptorLogger(), loggerOpts...),
armadaerrors.UnaryServerInterceptor(2000),
grpc_recovery.UnaryServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)),
Expand All @@ -65,7 +63,6 @@ func CreateGrpcServer(
srvMetrics.StreamServerInterceptor(),
requestid.StreamServerInterceptor(false),
grpc_auth.StreamServerInterceptor(authFunction),
grpc_armadacontext.StreamServerInterceptor(),
grpc_logging.StreamServerInterceptor(InterceptorLogger(), loggerOpts...),
armadaerrors.StreamServerInterceptor(2000),
grpc_recovery.StreamServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)),
Expand Down Expand Up @@ -146,14 +143,15 @@ func panicRecoveryHandler(p interface{}) (err error) {

func InterceptorLogger() grpc_logging.Logger {
return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) {
armadaCtx := armadacontext.FromGrpcCtx(ctx)
logFields := make(map[string]any, len(fields)/2)
logFields := make(map[string]any, len(fields)/2+2)
logFields["user"] = ctx.Value("user")
logFields["requestId"] = ctx.Value("requestId")
i := grpc_logging.Fields(fields).Iterator()
for i.Next() {
k, v := i.At()
logFields[k] = v
}
l := armadaCtx.WithFields(logFields)
l := log.WithFields(logFields)
switch lvl {
case grpc_logging.LevelDebug:
l.Debug(msg)
Expand Down
1 change: 1 addition & 0 deletions internal/common/requestid/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func FromContextOrMissing(ctx context.Context) string {
// The second return value is true if the operation was successful.
func AddToIncomingContext(ctx context.Context, id string) (context.Context, bool) {
if md, ok := metadata.FromIncomingContext(ctx); ok {
ctx = context.WithValue(ctx, "requestId", id)
md.Set(MetadataKey, id)
return metadata.NewIncomingContext(ctx, md), true
}
Expand Down

0 comments on commit b708709

Please sign in to comment.