Skip to content

Commit

Permalink
[public-api-server] Use logging middleware globally
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed Mar 3, 2023
1 parent f334af8 commit c8ec88d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
19 changes: 9 additions & 10 deletions components/public-api-server/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@
package middleware

import (
"github.com/sirupsen/logrus"
"net/http"
"time"

"github.com/gitpod-io/gitpod/common-go/log"
"github.com/sirupsen/logrus"
)

type Middleware func(handler http.Handler) http.Handler

func NewLoggingMiddleware(l *logrus.Entry) Middleware {

func NewLoggingMiddleware() Middleware {
return func(next http.Handler) http.Handler {
logging := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
uri := r.RequestURI
method := r.Method
duration := time.Since(start)
next.ServeHTTP(w, r)
duration := time.Since(start)

l.WithFields(logrus.Fields{
"uri": uri,
"method": method,
log.WithFields(logrus.Fields{
"uri": r.RequestURI,
"method": r.Method,
"duration": duration,
}).Infof("Handled HTTP request %s %s", method, uri)
}).Debug("Handled HTTP request")
})

return logging
Expand Down
11 changes: 6 additions & 5 deletions components/public-api-server/middleware/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ package middleware

import (
"bytes"
"github.com/sirupsen/logrus"
_ "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"

"github.com/sirupsen/logrus"
_ "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
)

func TestLoggingMiddleware(t *testing.T) {
Expand All @@ -23,12 +24,12 @@ func TestLoggingMiddleware(t *testing.T) {
expectedBody := `hello world`

someHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(expectedBody))
_, _ = w.Write([]byte(expectedBody))
})
req := httptest.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder() // this records the response

m := NewLoggingMiddleware(logrus.NewEntry(logger))
m := NewLoggingMiddleware()
wrappedHandler := m(someHandler)
wrappedHandler.ServeHTTP(rec, req)

Expand Down
8 changes: 0 additions & 8 deletions components/public-api-server/pkg/identityprovider/idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"time"

Expand Down Expand Up @@ -55,13 +54,6 @@ type Service struct {

func (kp *Service) Router() http.Handler {
mux := chi.NewRouter()
mux.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, _ := httputil.DumpRequest(r, false)
log.WithField("req", string(c)).Debug("IDP request")
h.ServeHTTP(w, r)
})
})
mux.Get(oidc.DiscoveryEndpoint, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
keysURL, err := url.JoinPath(kp.IssuerBaseURL, "keys")
if err != nil {
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 @@ -27,6 +27,7 @@ import (

"github.com/gitpod-io/gitpod/common-go/baseserver"
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
"github.com/gitpod-io/gitpod/public-api-server/middleware"
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
"github.com/gitpod-io/gitpod/public-api-server/pkg/billingservice"
Expand Down Expand Up @@ -175,6 +176,7 @@ func register(srv *baseserver.Server, deps *registerDependencies) error {
}

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

handlerOptions := []connect.HandlerOption{
connect.WithInterceptors(
Expand Down

0 comments on commit c8ec88d

Please sign in to comment.