mirror of
https://github.com/grafana/grafana.git
synced 2026-08-14 07:04:57 -05:00
Logger: Add feature toggle for errors in HTTP request logs (#64425)
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
// Copyright 2013 Martini Authors
|
||||
// Copyright 2014 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package loggermw
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
Middleware() web.Middleware
|
||||
}
|
||||
|
||||
type loggerImpl struct {
|
||||
cfg *setting.Cfg
|
||||
flags featuremgmt.FeatureToggles
|
||||
}
|
||||
|
||||
func Provide(
|
||||
cfg *setting.Cfg,
|
||||
flags featuremgmt.FeatureToggles,
|
||||
) Logger {
|
||||
return &loggerImpl{
|
||||
cfg: cfg,
|
||||
flags: flags,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *loggerImpl) Middleware() web.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
// we have to init the context with the counter here to update the request
|
||||
r = r.WithContext(log.InitCounter(r.Context()))
|
||||
// put the start time on context so we can measure it later.
|
||||
r = r.WithContext(log.InitstartTime(r.Context(), time.Now()))
|
||||
|
||||
if l.flags.IsEnabled(featuremgmt.FlagUnifiedRequestLog) {
|
||||
r = r.WithContext(errutil.SetUnifiedLogging(r.Context()))
|
||||
}
|
||||
|
||||
rw := web.Rw(w, r)
|
||||
next.ServeHTTP(rw, r)
|
||||
|
||||
duration := time.Since(start)
|
||||
timeTaken := duration / time.Millisecond
|
||||
ctx := contexthandler.FromContext(r.Context())
|
||||
if ctx != nil && ctx.PerfmonTimer != nil {
|
||||
ctx.PerfmonTimer.Observe(float64(timeTaken))
|
||||
}
|
||||
|
||||
if ctx != nil {
|
||||
logParams, logger := l.prepareLogParams(ctx, duration)
|
||||
logger.LogFunc(ctx.Logger)("Request Completed", logParams...)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (l *loggerImpl) prepareLogParams(c *contextmodel.ReqContext, duration time.Duration) ([]any, errutil.LogLevel) {
|
||||
rw := c.Resp
|
||||
r := c.Req
|
||||
|
||||
status := rw.Status()
|
||||
lvl := errutil.LevelInfo
|
||||
|
||||
switch {
|
||||
case status == http.StatusOK, status == http.StatusNotModified:
|
||||
if !l.cfg.RouterLogging {
|
||||
lvl = errutil.LevelNever
|
||||
}
|
||||
case status >= http.StatusInternalServerError:
|
||||
lvl = errutil.LevelError
|
||||
}
|
||||
|
||||
logParams := []any{
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"status", status,
|
||||
"remote_addr", c.RemoteAddr(),
|
||||
"time_ms", int64(duration / time.Millisecond),
|
||||
"duration", duration.String(),
|
||||
"size", rw.Size(),
|
||||
}
|
||||
|
||||
referer, err := SanitizeURL(r.Referer())
|
||||
// We add an empty referer when there's a parsing error, hence this is before the err check.
|
||||
logParams = append(logParams, "referer", referer)
|
||||
if err != nil {
|
||||
logParams = append(logParams, "refererParsingErr", fmt.Errorf("received invalid referer in request headers, removed for log forgery prevention: %w", err))
|
||||
lvl = lvl.HighestOf(errutil.LevelWarn)
|
||||
}
|
||||
|
||||
if l.flags.IsEnabled(featuremgmt.FlagDatabaseMetrics) {
|
||||
logParams = append(logParams, "db_call_count", log.TotalDBCallCount(c.Req.Context()))
|
||||
}
|
||||
|
||||
if handler, exist := middleware.RouteOperationName(c.Req); exist {
|
||||
logParams = append(logParams, "handler", handler)
|
||||
}
|
||||
|
||||
logParams = append(logParams, errorLogParams(c.Error)...)
|
||||
|
||||
return logParams, lvl
|
||||
}
|
||||
|
||||
func errorLogParams(err error) []any {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var gfErr errutil.Error
|
||||
if !errors.As(err, &gfErr) {
|
||||
return []any{"error", err.Error()}
|
||||
}
|
||||
|
||||
return []any{
|
||||
"errorReason", gfErr.Reason,
|
||||
"errorMessageID", gfErr.MessageID,
|
||||
"error", gfErr.LogMessage,
|
||||
}
|
||||
}
|
||||
|
||||
var sensitiveQueryStrings = [...]string{
|
||||
"auth_token",
|
||||
}
|
||||
|
||||
func SanitizeURL(s string) (string, error) {
|
||||
if s == "" {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
u, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to sanitize URL")
|
||||
}
|
||||
|
||||
// strip out sensitive query strings
|
||||
values := u.Query()
|
||||
for _, query := range sensitiveQueryStrings {
|
||||
values.Del(query)
|
||||
}
|
||||
u.RawQuery = values.Encode()
|
||||
|
||||
return u.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package loggermw
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func Test_sanitizeURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "Receiving empty string should return it",
|
||||
input: "",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "Receiving valid URL string should return it parsed",
|
||||
input: "https://grafana.com/",
|
||||
want: "https://grafana.com/",
|
||||
},
|
||||
{
|
||||
name: "Receiving invalid URL string should return empty string",
|
||||
input: "this is not a valid URL",
|
||||
want: "",
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
url, err := SanitizeURL(tt.input)
|
||||
if tt.expectError {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
assert.Equalf(t, tt.want, url, "SanitizeURL(%v)", tt.input)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_prepareLog(t *testing.T) {
|
||||
type opts struct {
|
||||
Features []any
|
||||
RouterLogging bool
|
||||
}
|
||||
|
||||
grafanaFlavoredErr := errutil.NewBase(errutil.StatusNotFound, "test.notFound").Errorf("got error")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
opts opts
|
||||
req *http.Request
|
||||
response web.ResponseWriter
|
||||
duration time.Duration
|
||||
error error
|
||||
|
||||
expectFields map[string]any
|
||||
expectAbsence map[string]struct{}
|
||||
expectedLevel errutil.LogLevel
|
||||
}{
|
||||
{
|
||||
name: "base case",
|
||||
req: mustRequest(http.NewRequest(http.MethodGet, "/", nil)),
|
||||
response: mockResponseWriter{},
|
||||
|
||||
expectFields: map[string]any{
|
||||
"method": "GET",
|
||||
"path": "/",
|
||||
"status": 0,
|
||||
"remote_addr": "",
|
||||
"time_ms": 0,
|
||||
"duration": "0s",
|
||||
"size": 0,
|
||||
"referer": "",
|
||||
},
|
||||
expectAbsence: map[string]struct{}{
|
||||
"error": {},
|
||||
"db_call_count": {},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "base case",
|
||||
req: mustRequest(http.NewRequest(http.MethodGet, "/", nil)),
|
||||
response: mockResponseWriter{},
|
||||
|
||||
expectFields: map[string]any{
|
||||
"method": "GET",
|
||||
"path": "/",
|
||||
"status": 0,
|
||||
"remote_addr": "",
|
||||
"time_ms": 0,
|
||||
"duration": "0s",
|
||||
"size": 0,
|
||||
"referer": "",
|
||||
},
|
||||
expectAbsence: map[string]struct{}{
|
||||
"error": {},
|
||||
"db_call_count": {},
|
||||
},
|
||||
expectedLevel: errutil.LevelInfo,
|
||||
},
|
||||
{
|
||||
name: "regular Go error",
|
||||
req: mustRequest(http.NewRequest(http.MethodGet, "/", nil)),
|
||||
response: mockResponseWriter{
|
||||
status: http.StatusInternalServerError,
|
||||
},
|
||||
error: fmt.Errorf("got an error"),
|
||||
|
||||
expectFields: map[string]any{
|
||||
"status": http.StatusInternalServerError,
|
||||
"error": "got an error",
|
||||
},
|
||||
expectAbsence: map[string]struct{}{
|
||||
"errorReason": {},
|
||||
"errorMessageID": {},
|
||||
},
|
||||
expectedLevel: errutil.LevelError,
|
||||
},
|
||||
{
|
||||
name: "Grafana-style error",
|
||||
req: mustRequest(http.NewRequest(http.MethodGet, "/", nil)),
|
||||
response: mockResponseWriter{
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
error: grafanaFlavoredErr,
|
||||
expectFields: map[string]any{
|
||||
"status": http.StatusNotFound,
|
||||
"error": "got error",
|
||||
"errorReason": errutil.StatusNotFound,
|
||||
"errorMessageID": "test.notFound",
|
||||
},
|
||||
expectedLevel: errutil.LevelInfo,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := setting.NewCfg()
|
||||
cfg.RouterLogging = tc.opts.RouterLogging
|
||||
l := Provide(cfg, featuremgmt.WithFeatures(tc.opts.Features...))
|
||||
|
||||
service, ok := l.(*loggerImpl)
|
||||
require.Truef(t, ok, "expected service to be of type (*loggerImpl), got (%T)", l)
|
||||
|
||||
c := &contextmodel.ReqContext{
|
||||
Context: &web.Context{
|
||||
Req: tc.req,
|
||||
Resp: tc.response,
|
||||
},
|
||||
Error: tc.error,
|
||||
}
|
||||
|
||||
logs, level := service.prepareLogParams(c, tc.duration)
|
||||
|
||||
require.Zero(t, len(logs)%2, "Each key must have an accompanying value")
|
||||
kv := map[any]any{}
|
||||
for i := 0; i < len(logs); i += 2 {
|
||||
kv[logs[i]] = logs[i+1]
|
||||
}
|
||||
|
||||
for key, val := range tc.expectFields {
|
||||
assert.Contains(t, kv, key)
|
||||
if val != nil {
|
||||
assert.EqualValues(t, val, kv[key])
|
||||
}
|
||||
}
|
||||
for key := range tc.expectAbsence {
|
||||
assert.NotContains(t, kv, key)
|
||||
}
|
||||
|
||||
if tc.expectedLevel != "" {
|
||||
assert.Equal(t, tc.expectedLevel, level)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustRequest(r *http.Request, err error) *http.Request {
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("expected no error when creating request, got: %w", err))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
type mockResponseWriter struct {
|
||||
web.ResponseWriter
|
||||
|
||||
status int
|
||||
size int
|
||||
}
|
||||
|
||||
func (m mockResponseWriter) Status() int {
|
||||
return m.status
|
||||
}
|
||||
func (m mockResponseWriter) Size() int {
|
||||
return m.size
|
||||
}
|
||||
Reference in New Issue
Block a user