Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for attributesFromError method #1801

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions internal/net/grpc/interceptor/server/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,7 @@ func MetricInterceptors() (grpc.UnaryServerInterceptor, grpc.StreamServerInterce
}

record := func(ctx context.Context, method string, err error, latency float64) {
code := codes.OK // default error is success when error is nil
if err != nil {
st, _ := status.FromError(err)
if st != nil {
code = st.Code()
}
}
attrs := []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, method),
attribute.String(gRPCStatus, code.String()),
}
attrs := attributesFromError(method, err)
latencyHistgram.Record(ctx, latency, attrs...)
completedRPCCnt.Add(ctx, 1, attrs...)
}
Expand All @@ -83,3 +73,17 @@ func MetricInterceptors() (grpc.UnaryServerInterceptor, grpc.StreamServerInterce
return err
}, nil
}

func attributesFromError(method string, err error) []attribute.KeyValue {
code := codes.OK // default error is success when error is nil
if err != nil {
st, _ := status.FromError(err)
if st != nil {
code = st.Code()
}
}
return []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, method),
attribute.String(gRPCStatus, code.String()),
}
}
96 changes: 96 additions & 0 deletions internal/net/grpc/interceptor/server/metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
package metric

import (
"context"
"reflect"
"testing"

"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/net/grpc"
"github.com/vdaas/vald/internal/net/grpc/codes"
"github.com/vdaas/vald/internal/net/grpc/status"
"github.com/vdaas/vald/internal/observability/attribute"
"github.com/vdaas/vald/internal/test/goleak"
)

Expand Down Expand Up @@ -92,3 +96,95 @@ func TestMetricInterceptors(t *testing.T) {
})
}
}

func Test_attributesFromError(t *testing.T) {
type T = []attribute.KeyValue
type args struct {
method string
err error
}
type want struct {
obj T
}
type test struct {
name string
args args
want want
checkFunc func(w want, obj T) error
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}

defaultCheckFunc := func(w want, obj T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", obj, w.obj)
}
return nil
}

tests := []test{
{
name: "return []attribute.KeyValue when err is nil",
args: args{
method: "InsertRPC",
err: nil,
},
want: want{
obj: []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, "InsertRPC"),
attribute.String(gRPCStatus, codes.OK.String()),
},
},
},
{
name: "return []attribute.KeyValue when err is not nil",
args: args{
method: "InsertRPC",
err: context.DeadlineExceeded,
},
want: want{
obj: []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, "InsertRPC"),
attribute.String(gRPCStatus, codes.DeadlineExceeded.String()),
},
},
},
{
name: "return []attribute.KeyValue when err type is wrapped error",
args: args{
method: "InsertRPC",
err: status.WrapWithInvalidArgument("Insert API failed", errors.ErrIncompatibleDimensionSize(100, 940)),
},
want: want{
obj: []attribute.KeyValue{
attribute.String(gRPCMethodKeyName, "InsertRPC"),
attribute.String(gRPCStatus, codes.InvalidArgument.String()),
},
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(tt, test.args)
}

checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}

got := attributesFromError(test.args.method, test.args.err)
if err := checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}