-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support to trace on grpc_logrus.DefaultMessageProducer (#547)
* add unit test * update docs after `make all` * refactor unit test, refactor option * Revert "update docs after `make all`" This reverts commit 10f2e4c.
- Loading branch information
Showing
2 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,90 @@ | ||
package grpc_logrus | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" | ||
"github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
func TestDurationToTimeMillisField(t *testing.T) { | ||
_, val := DurationToTimeMillisField(time.Microsecond * 100) | ||
assert.Equal(t, val.(float32), float32(0.1), "sub millisecond values should be correct") | ||
} | ||
|
||
func TestDefaultMessageProducer(t *testing.T) { | ||
t.Parallel() | ||
|
||
errNotFound := errors.New("not found") | ||
testcases := []struct { | ||
label string | ||
format string | ||
level logrus.Level | ||
code codes.Code | ||
err error | ||
fields logrus.Fields | ||
}{ | ||
{ | ||
label: "should emit info message without error", | ||
format: "test", | ||
level: logrus.InfoLevel, | ||
code: codes.OK, | ||
fields: logrus.Fields{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
{ | ||
label: "should emit info message with error", | ||
format: "test", | ||
level: logrus.InfoLevel, | ||
code: codes.NotFound, | ||
err: errNotFound, | ||
fields: logrus.Fields{ | ||
"foo": "bar", | ||
logrus.ErrorKey: errNotFound, | ||
}, | ||
}, | ||
{ | ||
label: "should emit trace message without error", | ||
format: "test", | ||
level: logrus.TraceLevel, | ||
code: codes.OK, | ||
fields: logrus.Fields{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
tc := tc | ||
t.Run(tc.label, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
logger, hook := test.NewNullLogger() | ||
|
||
logger.SetLevel(logrus.TraceLevel) | ||
|
||
logrusEntry := logger.WithField("foo", "bar") | ||
|
||
ctx := ctxlogrus.ToContext(context.Background(), logrusEntry) | ||
|
||
DefaultMessageProducer(ctx, tc.format, tc.level, tc.code, tc.err, logrus.Fields{}) | ||
|
||
lastEntry := hook.LastEntry() | ||
|
||
require.NotNil(t, lastEntry) | ||
|
||
assert.Equal(t, "test", lastEntry.Message) | ||
assert.Equal(t, tc.fields, lastEntry.Data) | ||
assert.Equal(t, tc.level, lastEntry.Level) | ||
}) | ||
} | ||
|
||
} |