Skip to content

[public-api] Explicit panic handler #17105

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

Merged
merged 1 commit into from
Mar 31, 2023
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
16 changes: 11 additions & 5 deletions components/public-api-server/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ type Middleware func(handler http.Handler) http.Handler
func NewLoggingMiddleware() Middleware {
return func(next http.Handler) http.Handler {
logging := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := log.ToContext(r.Context(), log.Log.WithContext(r.Context()))
log.AddFields(ctx, logrus.Fields{
"protocol": "http",
"uri": r.RequestURI,
"method": r.Method,
})

start := time.Now()
next.ServeHTTP(w, r)
duration := time.Since(start)

log.WithFields(logrus.Fields{
"uri": r.RequestURI,
"method": r.Method,
"duration": duration,
}).Debug("Handled HTTP request")
log.AddFields(ctx, logrus.Fields{
"duration_seconds": duration.Seconds(),
})
log.Extract(ctx).Debug("Handled HTTP request")
})

return logging
Expand Down
4 changes: 4 additions & 0 deletions components/public-api-server/pkg/server/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func NewLogInterceptor(entry *logrus.Entry) connect.UnaryInterceptorFunc {
}

func filterHeaders(headers http.Header) http.Header {
if headers == nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added as an extra safeguard should the headers not be present.

return nil
}

cloned := headers.Clone()
cloned.Del("Authorization")
cloned.Del("Cookie")
Expand Down
2 changes: 2 additions & 0 deletions components/public-api-server/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/gitpod-io/gitpod/common-go/experiments"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/go-chi/chi/v5"
chi_middleware "github.com/go-chi/chi/v5/middleware"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"

Expand Down Expand Up @@ -176,6 +177,7 @@ func register(srv *baseserver.Server, deps *registerDependencies) error {
}

rootHandler := chi.NewRouter()
rootHandler.Use(chi_middleware.Recoverer)
rootHandler.Use(middleware.NewLoggingMiddleware())

handlerOptions := []connect.HandlerOption{
Expand Down