Skip to content

Commit

Permalink
Merge 5b093a7 into 472d9ee
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 committed Apr 2, 2020
2 parents 472d9ee + 5b093a7 commit 454c3e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
22 changes: 22 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ func MakeEndpointLoggingMW(logger log.Logger) cs.Middleware {
}
}

// MakeEndpointLoggingNoInputMW makes a logging middleware.
func MakeEndpointLoggingNoInputMW(logger log.Logger) cs.Middleware {
return func(next cs.Endpoint) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
res, err := next(ctx, req)
logger.Debug(ctx, "res", res)
return res, err
}
}
}

// MakeEndpointLoggingNoOutputMW makes a logging middleware.
func MakeEndpointLoggingNoOutputMW(logger log.Logger) cs.Middleware {
return func(next cs.Endpoint) cs.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
logger.Debug(ctx, "req", req)
res, err := next(ctx, req)
return res, err
}
}
}

// MakeEndpointInstrumentingMW makes a middleware that measure the endpoints response time and
// send the metrics to influx DB.
func MakeEndpointInstrumentingMW(m metrics.Metrics, histoName string) cs.Middleware {
Expand Down
35 changes: 23 additions & 12 deletions middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,34 @@ func TestEndpointLoggingMW(t *testing.T) {
defer mockCtrl.Finish()
var mockLogger = mock.NewLogger(mockCtrl)

var m = MakeEndpointLoggingMW(mockLogger)(dummyEndpoint)
var mFull = MakeEndpointLoggingMW(mockLogger)(dummyEndpoint)
var mNoInput = MakeEndpointLoggingNoInputMW(mockLogger)(dummyEndpoint)
var mNoOutput = MakeEndpointLoggingNoOutputMW(mockLogger)(dummyEndpoint)

// Context with correlation ID.
var corrID = strconv.FormatUint(rand.Uint64(), 10)
var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID)

// With correlation ID.
var req = "req"
mockLogger.EXPECT().Debug(gomock.Any(), "req", req).Return(nil).Times(1)
mockLogger.EXPECT().Debug(gomock.Any(), "res", nil).Return(nil).Times(1)
m(ctx, req)

// Without correlation ID.
var f = func() {
m(context.Background(), nil)
}
assert.Panics(t, f)

t.Run("Full logging with correlation ID", func(t *testing.T) {
mockLogger.EXPECT().Debug(gomock.Any(), "req", req).Return(nil).Times(1)
mockLogger.EXPECT().Debug(gomock.Any(), "res", nil).Return(nil).Times(1)
mFull(ctx, req)
})
t.Run("Full logging without correlation ID", func(t *testing.T) {
var f = func() {
mFull(context.Background(), nil)
}
assert.Panics(t, f)
})
t.Run("No input logging with correlation ID", func(t *testing.T) {
mockLogger.EXPECT().Debug(gomock.Any(), "res", nil).Return(nil).Times(1)
mNoInput(ctx, req)
})
t.Run("No output logging with correlation ID", func(t *testing.T) {
mockLogger.EXPECT().Debug(gomock.Any(), "req", req).Return(nil).Times(1)
mNoOutput(ctx, req)
})
}

func TestEndpointInstrumentingMW(t *testing.T) {
Expand Down

0 comments on commit 454c3e7

Please sign in to comment.