Skip to content

Commit

Permalink
feat(logger): add user context
Browse files Browse the repository at this point in the history
  • Loading branch information
haveiss committed Feb 22, 2024
1 parent 4c73dc8 commit fddef4a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/goto/compass/core/user"
"github.com/goto/compass/internal/server/health"
handlersv1beta1 "github.com/goto/compass/internal/server/v1beta1"
"github.com/goto/compass/internal/store/postgres"
Expand All @@ -19,9 +20,11 @@ import (
grpclogrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpcctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
ctx_logrus "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/newrelic/go-agent/v3/integrations/nrgrpc"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -94,6 +97,7 @@ func Serve(
grpc_interceptor.UserHeaderCtx(config.Identity.HeaderKeyUUID, config.Identity.HeaderKeyEmail),
grpcctxtags.UnaryServerInterceptor(),
grpcrecovery.UnaryServerInterceptor(),
withLogrusContext(),
)),
)
reflection.Register(grpcServer)
Expand Down Expand Up @@ -184,3 +188,16 @@ func makeHeaderMatcher(c Config) func(key string) (string, bool) {
}
}
}

func withLogrusContext() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
usr := user.FromContext(ctx)
if usr.UUID != "" {
ctx_logrus.AddFields(ctx, logrus.Fields{
"user.uuid": usr.UUID,
})
}

return handler(ctx, req)
}
}

0 comments on commit fddef4a

Please sign in to comment.