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

Laushinka/server logs incoming 9556 #9617

Merged
merged 1 commit into from
May 2, 2022
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
11 changes: 8 additions & 3 deletions components/public-api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/public-api-server/middleware"
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"net/http"
Expand All @@ -33,12 +34,16 @@ func main() {
}

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

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

return nil
}

func HelloWorldHandler(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`hello world`))
}
34 changes: 34 additions & 0 deletions components/public-api-server/middleware/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package middleware

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

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

func NewLoggingMiddleware(l *logrus.Entry) 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)

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

return logging
}
}
37 changes: 37 additions & 0 deletions components/public-api-server/middleware/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

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"
)

func TestLoggingMiddleware(t *testing.T) {
logInMemory := &bytes.Buffer{}
logger := logrus.New()
logger.SetOutput(logInMemory)
logger.SetFormatter(&logrus.JSONFormatter{})

expectedBody := `hello world`

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

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

require.HTTPStatusCode(t, someHandler, http.MethodGet, "/", nil, http.StatusOK)
require.HTTPBodyContains(t, someHandler, http.MethodGet, "/", nil, "hello")
}