Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: add hooks for init router to enable better logging #1247

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions backend/pkg/api/connect/interceptor/error_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,18 @@ func (*ErrorLogInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFu
}

func (*ErrorLogInterceptor) statusCode(protocol string, serverErr error) string {
grpcProtocol := "grpc"
grpcwebProtocol := "grpc_web"
connectProtocol := "connect_rpc"
httpProtocol := "http"

// Following the respective specifications, use integers and "status_code" for
// gRPC codes in contrast to strings and "error_code" for Connect codes.
// TODO: Check protocol case for gRPC gateway
switch protocol {
case grpcProtocol, grpcwebProtocol, httpProtocol:
case connect.ProtocolGRPC, connect.ProtocolGRPCWeb, httpProtocol:
if serverErr != nil {
return connect.CodeOf(serverErr).String()
}
return "ok"
case connectProtocol:
case connect.ProtocolConnect:
if connect.IsNotModifiedError(serverErr) {
// A "not modified" error is special: it's code is technically "unknown" but
// it would be misleading to label it as an unknown error since it's not really
Expand Down
4 changes: 4 additions & 0 deletions backend/pkg/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type RouteHooks interface {
// The hook can modify the interceptors slice, i.e. adding new interceptors, removing some, re-ordering, and return it in ConnectConfig.
// The hook can return additional connect services that shall be mounted by OSS.
ConfigConnectRPC(ConfigConnectRPCRequest) ConfigConnectRPCResponse

// InitConnectRPCRouter is used to initialize the ConnectRPC router with any top level middleware.
InitConnectRPCRouter(router chi.Router)
}

// AuthorizationHooks include all functions which allow you to intercept the requests at various
Expand Down Expand Up @@ -210,6 +213,7 @@ func (*defaultHooks) ConfigAPIRouterPostRegistration(_ chi.Router) {}
func (*defaultHooks) ConfigInternalRouter(_ chi.Router) {}
func (*defaultHooks) ConfigRouter(_ chi.Router) {}
func (*defaultHooks) ConfigGRPCGateway(_ *runtime.ServeMux) {}
func (*defaultHooks) InitConnectRPCRouter(_ chi.Router) {}
func (*defaultHooks) ConfigConnectRPC(req ConfigConnectRPCRequest) ConfigConnectRPCResponse {
return ConfigConnectRPCResponse{
Interceptors: req.BaseInterceptors,
Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/api/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type RouteHooks interface {
// The hook can modify the interceptors slice, i.e. adding new interceptors, removing some, re-ordering, and return it in ConnectConfig.
// The hook can return additional connect services that shall be mounted by OSS.
ConfigConnectRPC(ConfigConnectRPCRequest) ConfigConnectRPCResponse

// InitConnectRPCRouter is used to initialize the ConnectRPC router with any top level middleware.
InitConnectRPCRouter(router chi.Router)
}

// AuthorizationHooks include all functions which allow you to intercept the requests at various
Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (api *API) setupConnectWithGRPCGateway(r chi.Router) {
interceptor.NewRequestValidationInterceptor(v, api.Logger.Named("validator")),
interceptor.NewEndpointCheckInterceptor(&api.Cfg.Console.API, api.Logger.Named("endpoint_checker")),
}

api.Hooks.Route.InitConnectRPCRouter(r)

r.Use(observerInterceptor.WrapHandler)

// Setup gRPC-Gateway
Expand Down
Loading