Skip to content

Commit

Permalink
Merge branch 'master' into ws-error-status
Browse files Browse the repository at this point in the history
  • Loading branch information
alfrunes committed Jan 22, 2024
2 parents 1cac297 + b10f353 commit 8a2789c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 475 deletions.
21 changes: 0 additions & 21 deletions accesslog/exclude.go

This file was deleted.

18 changes: 16 additions & 2 deletions accesslog/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package accesslog

import (
"bytes"
"context"
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -34,6 +36,8 @@ import (
type AccessLogFormat string

const (
StatusClientClosedConnection = 499

DefaultLogFormat = "%t %S\033[0m \033[36;1m%Dμs\033[0m \"%r\" \033[1;30m%u \"%{User-Agent}i\"\033[0m"
SimpleLogFormat = "%s %Dμs %r %u %{User-Agent}i"

Expand Down Expand Up @@ -85,7 +89,9 @@ func collectTrace() string {
return traceback.String()
}

func (mw *AccessLogMiddleware) LogFunc(startTime time.Time, w rest.ResponseWriter, r *rest.Request) {
func (mw *AccessLogMiddleware) LogFunc(
ctx context.Context, startTime time.Time,
w rest.ResponseWriter, r *rest.Request) {
util := &accessLogUtil{w, r}
fields := logrus.Fields{
"type": r.Proto,
Expand All @@ -97,6 +103,13 @@ func (mw *AccessLogMiddleware) LogFunc(startTime time.Time, w rest.ResponseWrite
"qs": r.URL.RawQuery,
}
statusCode := util.StatusCode()
select {
case <-ctx.Done():
if errors.Is(ctx.Err(), context.Canceled) {
statusCode = StatusClientClosedConnection
}
default:
}

if panic := recover(); panic != nil {
trace := collectTrace()
Expand Down Expand Up @@ -144,9 +157,10 @@ func (mw *AccessLogMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFu
// This middleware depends on RecorderMiddleware to work
mw.recorder = new(rest.RecorderMiddleware)
return func(w rest.ResponseWriter, r *rest.Request) {
ctx := r.Request.Context()
startTime := time.Now()
r.Env["START_TIME"] = &startTime
defer mw.LogFunc(startTime, w, r)
defer mw.LogFunc(ctx, startTime, w, r)
// call the handler inside recorder context
mw.recorder.MiddlewareFunc(h)(w, r)
}
Expand Down
17 changes: 15 additions & 2 deletions accesslog/middleware_gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package accesslog

import (
"context"
"fmt"
"net/http"
"time"
Expand All @@ -30,7 +31,11 @@ type AccessLogger struct {
DisableLog func(c *gin.Context) bool
}

func (a AccessLogger) LogFunc(c *gin.Context, startTime time.Time) {
func (a AccessLogger) LogFunc(
ctx context.Context,
c *gin.Context,
startTime time.Time,
) {
logCtx := logrus.Fields{
"clientip": c.ClientIP(),
"method": c.Request.Method,
Expand Down Expand Up @@ -67,6 +72,13 @@ func (a AccessLogger) LogFunc(c *gin.Context, startTime time.Time) {
latency = latency.Round(time.Microsecond)
}
code := c.Writer.Status()
select {
case <-ctx.Done():
if errors.Is(ctx.Err(), context.Canceled) {
code = StatusClientClosedConnection
}
default:
}
logCtx["responsetime"] = latency.String()
logCtx["status"] = c.Writer.Status()
logCtx["byteswritten"] = c.Writer.Size()
Expand Down Expand Up @@ -97,8 +109,9 @@ func (a AccessLogger) LogFunc(c *gin.Context, startTime time.Time) {
}

func (a AccessLogger) Middleware(c *gin.Context) {
ctx := c.Request.Context()
startTime := time.Now()
defer a.LogFunc(c, startTime)
defer a.LogFunc(ctx, c, startTime)
c.Next()
}

Expand Down
28 changes: 27 additions & 1 deletion accesslog/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package accesslog

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -29,6 +30,7 @@ import (
func TestMiddlewareLegacy(t *testing.T) {
testCases := []struct {
Name string
CTX context.Context

HandlerFunc rest.HandlerFunc

Expand All @@ -48,6 +50,25 @@ func TestMiddlewareLegacy(t *testing.T) {
"responsetime=",
"ts=",
},
}, {
Name: "canceled context",

CTX: func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}(),
HandlerFunc: func(w rest.ResponseWriter, r *rest.Request) {
w.WriteHeader(http.StatusNoContent)
},
Fields: []string{
"status=499",
`path=/test`,
`qs="foo=bar"`,
"method=GET",
"responsetime=",
"ts=",
},
}, {
Name: "error, panic in handler",

Expand Down Expand Up @@ -99,7 +120,12 @@ func TestMiddlewareLegacy(t *testing.T) {
api.SetApp(app)
handler := api.MakeHandler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(
ctx := context.Background()
if tc.CTX != nil {
ctx = tc.CTX
}
req, _ := http.NewRequestWithContext(
ctx,
http.MethodGet,
"http://localhost/test?foo=bar",
nil,
Expand Down
26 changes: 13 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ go 1.18
require (
github.com/ant0ine/go-json-rest v3.3.2+incompatible
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.4.0
github.com/google/uuid v1.5.0
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/v9 v9.3.0
github.com/redis/go-redis/v9 v9.3.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.17.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.13.0
go.mongodb.org/mongo-driver v1.13.1
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
)

Expand All @@ -21,7 +21,7 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -42,11 +42,11 @@ require (
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
Expand All @@ -59,12 +59,12 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 8a2789c

Please sign in to comment.