Skip to content

Commit 33fe7c1

Browse files
author
Laurie T. Malau
committed
logging middleware
1 parent 84a4ed5 commit 33fe7c1

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

components/public-api-server/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"github.com/gitpod-io/gitpod/common-go/baseserver"
99
"github.com/gitpod-io/gitpod/common-go/log"
10+
"github.com/gitpod-io/gitpod/public-api-server/middleware"
1011
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
1112
v1 "github.com/gitpod-io/gitpod/public-api/v1"
1213
"net/http"
@@ -33,12 +34,16 @@ func main() {
3334
}
3435

3536
func register(srv *baseserver.Server) error {
36-
srv.HTTPMux().HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
37-
_, _ = w.Write([]byte(`hello world`))
38-
})
37+
logger := log.New()
38+
m := middleware.NewLoggingMiddleware(logger)
39+
srv.HTTPMux().Handle("/", m(http.HandlerFunc(HelloWorldHandler)))
3940

4041
v1.RegisterWorkspacesServiceServer(srv.GRPC(), apiv1.NewWorkspaceService())
4142
v1.RegisterPrebuildsServiceServer(srv.GRPC(), v1.UnimplementedPrebuildsServiceServer{})
4243

4344
return nil
4445
}
46+
47+
func HelloWorldHandler(w http.ResponseWriter, _ *http.Request) {
48+
w.Write([]byte(`hello world`))
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package middleware
6+
7+
import (
8+
"github.com/sirupsen/logrus"
9+
"net/http"
10+
"time"
11+
)
12+
13+
type Middleware func(handler http.Handler) http.Handler
14+
15+
func NewLoggingMiddleware(l *logrus.Entry) Middleware {
16+
17+
return func(next http.Handler) http.Handler {
18+
logging := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19+
start := time.Now()
20+
uri := r.RequestURI
21+
method := r.Method
22+
duration := time.Since(start)
23+
next.ServeHTTP(w, r)
24+
25+
l.WithFields(logrus.Fields{
26+
"uri": uri,
27+
"method": method,
28+
"duration": duration,
29+
}).Infof("Handled HTTP request %s %s", method, uri)
30+
})
31+
32+
return logging
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package middleware
6+
7+
import (
8+
"bytes"
9+
"github.com/sirupsen/logrus"
10+
_ "github.com/sirupsen/logrus/hooks/test"
11+
"github.com/stretchr/testify/require"
12+
"net/http"
13+
"net/http/httptest"
14+
"testing"
15+
)
16+
17+
func TestLoggingMiddleware(t *testing.T) {
18+
logInMemory := &bytes.Buffer{}
19+
logger := logrus.New()
20+
logger.SetOutput(logInMemory)
21+
logger.SetFormatter(&logrus.JSONFormatter{})
22+
23+
expectedBody := `hello world`
24+
25+
someHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
26+
w.Write([]byte(expectedBody))
27+
})
28+
req := httptest.NewRequest("GET", "/", nil)
29+
rec := httptest.NewRecorder() // this records the response
30+
31+
m := NewLoggingMiddleware(logrus.NewEntry(logger))
32+
wrappedHandler := m(someHandler)
33+
wrappedHandler.ServeHTTP(rec, req)
34+
35+
require.HTTPStatusCode(t, someHandler, http.MethodGet, "/", nil, http.StatusOK)
36+
require.HTTPBodyContains(t, someHandler, http.MethodGet, "/", nil, "hello")
37+
}

0 commit comments

Comments
 (0)