Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/middleware/logging.go
2498 views
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
"net/http"
9
"time"
10
11
"github.com/gitpod-io/gitpod/common-go/log"
12
"github.com/sirupsen/logrus"
13
)
14
15
type Middleware func(handler http.Handler) http.Handler
16
17
func NewLoggingMiddleware() Middleware {
18
return func(next http.Handler) http.Handler {
19
logging := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20
ctx := log.ToContext(r.Context(), log.Log.WithContext(r.Context()))
21
log.AddFields(ctx, logrus.Fields{
22
"protocol": "http",
23
"uri": r.RequestURI,
24
"method": r.Method,
25
})
26
27
start := time.Now()
28
next.ServeHTTP(w, r)
29
duration := time.Since(start)
30
31
log.AddFields(ctx, logrus.Fields{
32
"duration_seconds": duration.Seconds(),
33
})
34
log.Extract(ctx).Debug("Handled HTTP request")
35
})
36
37
return logging
38
}
39
}
40
41