Path: blob/main/components/public-api-server/middleware/logging.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package middleware56import (7"net/http"8"time"910"github.com/gitpod-io/gitpod/common-go/log"11"github.com/sirupsen/logrus"12)1314type Middleware func(handler http.Handler) http.Handler1516func NewLoggingMiddleware() Middleware {17return func(next http.Handler) http.Handler {18logging := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {19ctx := log.ToContext(r.Context(), log.Log.WithContext(r.Context()))20log.AddFields(ctx, logrus.Fields{21"protocol": "http",22"uri": r.RequestURI,23"method": r.Method,24})2526start := time.Now()27next.ServeHTTP(w, r)28duration := time.Since(start)2930log.AddFields(ctx, logrus.Fields{31"duration_seconds": duration.Seconds(),32})33log.Extract(ctx).Debug("Handled HTTP request")34})3536return logging37}38}394041