Skip to content

Commit

Permalink
style: updated interceptor order
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Dec 11, 2024
1 parent 58ba3d5 commit 51231e6
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions pkg/util/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ func GrpcClientWithOtel(address string) (*grpc.ClientConn, error) {
func InitServerDefaults(kcClient gocloak.KeycloakClient, realm string) (*grpc.Server, *runtime.ServeMux) {
opts := []logging.Option{
logging.WithCodes(logging.DefaultErrorToCode),
logging.WithFieldsFromContextAndCallMeta(logTraceData),
logging.WithFieldsFromContextAndCallMeta(logSroData),
}

return grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(interceptorLogger(log.Logger), opts...),
selector.UnaryServerInterceptor(auth.UnaryServerInterceptor(sroauth.AuthFunc(kcClient, realm)), selector.MatchFunc(sroauth.NotPublicServiceMatcher)),
logging.UnaryServerInterceptor(interceptorLogger(log.Logger), logging.WithFieldsFromContextAndCallMeta(logRequestorData)),
logging.UnaryServerInterceptor(interceptorLogger(log.Logger), opts...),
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(interceptorLogger(log.Logger), opts...),
selector.StreamServerInterceptor(auth.StreamServerInterceptor(sroauth.AuthFunc(kcClient, realm)), selector.MatchFunc(sroauth.NotPublicServiceMatcher)),
logging.StreamServerInterceptor(interceptorLogger(log.Logger), logging.WithFieldsFromContextAndCallMeta(logRequestorData)),
logging.StreamServerInterceptor(interceptorLogger(log.Logger), opts...),
)),
runtime.NewServeMux()
}
Expand Down Expand Up @@ -79,16 +77,11 @@ func interceptorLogger(l logrus.FieldLogger) logging.Logger {
})
}

func logTraceData(ctx context.Context, c interceptors.CallMeta) logging.Fields {
func logSroData(ctx context.Context, c interceptors.CallMeta) logging.Fields {
out := logging.Fields{}
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() {
out = append(out, "traceId", spanCtx.TraceID().String())
}
return out
}

func logRequestorData(ctx context.Context, c interceptors.CallMeta) logging.Fields {
out := logging.Fields{}
if claims, ok := sroauth.RetrieveClaims(ctx); ok {
out = append(out, "requestor", fmt.Sprintf("%s:%s", claims.Username, claims.Subject))
}
Expand Down

0 comments on commit 51231e6

Please sign in to comment.