Skip to content

Commit 46b892a

Browse files
authored
Polish api.Tracer with new error tracing mechanism (#153)
* Polish api.Tracer with new error tracing mechanism Signed-off-by: Eric Zhao <[email protected]>
1 parent 2c544a8 commit 46b892a

File tree

6 files changed

+18
-133
lines changed

6 files changed

+18
-133
lines changed

adapter/dubbo/consumer_filter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ func (d *consumerFilter) Invoke(ctx context.Context, invoker protocol.Invoker, i
4444
func (d *consumerFilter) OnResponse(ctx context.Context, result protocol.Result, _ protocol.Invoker, _ protocol.Invocation) protocol.Result {
4545
if methodEntry := ctx.Value(MethodEntryKey); methodEntry != nil {
4646
e := methodEntry.(*base.SentinelEntry)
47-
sentinel.TraceErrorToEntry(e, result.Error())
47+
sentinel.TraceError(e, result.Error())
4848
e.Exit()
4949
}
5050
if interfaceEntry := ctx.Value(InterfaceEntryKey); interfaceEntry != nil {
5151
e := interfaceEntry.(*base.SentinelEntry)
52-
sentinel.TraceErrorToEntry(e, result.Error())
52+
sentinel.TraceError(e, result.Error())
5353
e.Exit()
5454
}
5555
return result

adapter/dubbo/provider_filter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func (d *providerFilter) Invoke(ctx context.Context, invoker protocol.Invoker, i
4242
func (d *providerFilter) OnResponse(ctx context.Context, result protocol.Result, _ protocol.Invoker, _ protocol.Invocation) protocol.Result {
4343
if methodEntry := ctx.Value(MethodEntryKey); methodEntry != nil {
4444
e := methodEntry.(*base.SentinelEntry)
45-
sentinel.TraceErrorToEntry(e, result.Error())
45+
sentinel.TraceError(e, result.Error())
4646
e.Exit()
4747
}
4848
if interfaceEntry := ctx.Value(InterfaceEntryKey); interfaceEntry != nil {
4949
e := interfaceEntry.(*base.SentinelEntry)
50-
sentinel.TraceErrorToEntry(e, result.Error())
50+
sentinel.TraceError(e, result.Error())
5151
e.Exit()
5252
}
5353
return result

adapter/grpc/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewUnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
4040

4141
err := invoker(ctx, method, req, reply, cc, opts...)
4242
if err != nil {
43-
sentinel.TraceErrorToEntry(entry, err)
43+
sentinel.TraceError(entry, err)
4444
}
4545
return err
4646
}
@@ -78,7 +78,7 @@ func NewStreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
7878

7979
cs, err := streamer(ctx, desc, cc, method, opts...)
8080
if err != nil {
81-
sentinel.TraceErrorToEntry(entry, err)
81+
sentinel.TraceError(entry, err)
8282
}
8383

8484
return cs, err

adapter/grpc/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewUnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
3737

3838
res, err := handler(ctx, req)
3939
if err != nil {
40-
sentinel.TraceErrorToEntry(entry, err)
40+
sentinel.TraceError(entry, err)
4141
}
4242
return res, err
4343
}
@@ -72,7 +72,7 @@ func NewStreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
7272

7373
err := handler(srv, ss)
7474
if err != nil {
75-
sentinel.TraceErrorToEntry(entry, err)
75+
sentinel.TraceError(entry, err)
7676
}
7777
return err
7878
}

api/tracer.go

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,20 @@ package api
22

33
import (
44
"github.com/alibaba/sentinel-golang/core/base"
5-
"github.com/alibaba/sentinel-golang/core/stat"
65
"github.com/alibaba/sentinel-golang/logging"
7-
"github.com/alibaba/sentinel-golang/util"
86
)
97

10-
var (
11-
logger = logging.GetDefaultLogger()
12-
)
13-
14-
type TraceErrorOptions struct {
15-
count uint64
16-
}
17-
18-
type TraceErrorOption func(*TraceErrorOptions)
19-
20-
// WithCount sets the error count.
21-
func WithCount(count uint64) TraceErrorOption {
22-
return func(opts *TraceErrorOptions) {
23-
opts.count = count
24-
}
25-
}
26-
27-
// TraceError records the provided error to the statistic structure of the target resource.
28-
func TraceError(resource string, err error, opts ...TraceErrorOption) {
29-
if util.IsBlank(resource) || err == nil {
30-
return
31-
}
8+
// TraceError records the provided error to the given SentinelEntry.
9+
func TraceError(entry *base.SentinelEntry, err error) {
3210
defer func() {
3311
if e := recover(); e != nil {
34-
logger.Panicf("Fail to traceError, resource: %s, panic error: %+v", resource, e)
12+
logging.GetDefaultLogger().Panicf("Failed to TraceError, panic error: %+v", e)
3513
return
3614
}
3715
}()
38-
39-
if node := stat.GetResourceNode(resource); node != nil {
40-
var options = TraceErrorOptions{
41-
count: 1,
42-
}
43-
for _, opt := range opts {
44-
opt(&options)
45-
}
46-
traceErrorToNode(node, err, options.count)
47-
}
48-
}
49-
50-
// TraceErrorToEntry records the provided error to the given SentinelEntry.
51-
func TraceErrorToEntry(entry *base.SentinelEntry, err error, opts ...TraceErrorOption) {
5216
if entry == nil || err == nil {
5317
return
5418
}
5519

56-
TraceErrorToCtx(entry.Context(), err, opts...)
57-
}
58-
59-
// TraceErrorToCtx records the provided error to the given context.
60-
func TraceErrorToCtx(ctx *base.EntryContext, err error, opts ...TraceErrorOption) {
61-
defer func() {
62-
if e := recover(); e != nil {
63-
logger.Panicf("Fail to traceErrorToCtx, parameter[ctx: %+v, err: %+v, opts: %+v], panic error: %+v", ctx, err, opts, e)
64-
return
65-
}
66-
}()
67-
68-
if ctx == nil {
69-
return
70-
}
71-
node := ctx.StatNode
72-
if node == nil {
73-
logger.Warnf("Cannot trace error: nil StatNode in EntryContext, resource: %s", ctx.Resource.String())
74-
return
75-
}
76-
77-
var options = TraceErrorOptions{
78-
count: 1,
79-
}
80-
for _, opt := range opts {
81-
opt(&options)
82-
}
83-
84-
traceErrorToNode(node, err, options.count)
85-
}
86-
87-
func traceErrorToNode(node base.StatNode, err error, cnt uint64) {
88-
if node == nil {
89-
return
90-
}
91-
if cnt <= 0 {
92-
return
93-
}
94-
node.AddMetric(base.MetricEventError, cnt)
20+
entry.SetError(err)
9521
}

api/tracer_test.go

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,46 @@ import (
66
"time"
77

88
"github.com/alibaba/sentinel-golang/core/base"
9-
"github.com/alibaba/sentinel-golang/core/stat"
109
"github.com/stretchr/testify/assert"
1110
)
1211

1312
var (
1413
testRes = base.NewResourceWrapper("a", base.ResTypeCommon, base.Inbound)
1514
)
1615

17-
func TestTraceErrorToCtx(t *testing.T) {
18-
type args struct {
19-
ctx *base.EntryContext
20-
err error
21-
count TraceErrorOption
22-
}
23-
tests := []struct {
24-
name string
25-
args args
26-
want int64
27-
}{
28-
{
29-
name: "TestTraceErrorToCtx",
30-
args: args{
31-
ctx: nil,
32-
err: nil,
33-
count: WithCount(10),
34-
},
35-
want: 10,
36-
},
37-
}
38-
testStatNode := stat.NewResourceNode("a", base.ResTypeCommon)
39-
tests[0].args.ctx = &base.EntryContext{
40-
Resource: testRes,
41-
StatNode: testStatNode,
42-
Input: nil,
43-
}
44-
tests[0].args.err = errors.New("biz error")
45-
46-
for _, tt := range tests {
47-
t.Run(tt.name, func(t *testing.T) {
48-
TraceErrorToCtx(tt.args.ctx, tt.args.err, tt.args.count)
49-
assert.True(t, tt.args.ctx.StatNode.GetSum(base.MetricEventError) == tt.want)
50-
})
51-
}
52-
}
53-
5416
func TestTraceErrorToEntry(t *testing.T) {
5517
type args struct {
5618
entry *base.SentinelEntry
5719
err error
58-
count TraceErrorOption
5920
}
21+
te := errors.New("biz error")
6022
tests := []struct {
6123
name string
6224
args args
63-
want int64
25+
want error
6426
}{
6527
{
6628
name: "TestTraceErrorToEntry",
6729
args: args{
6830
entry: nil,
6931
err: nil,
70-
count: WithCount(10),
7132
},
72-
want: 10,
33+
want: te,
7334
},
7435
}
7536

76-
testStatNode := stat.NewResourceNode("a", base.ResTypeCommon)
7737
ctx := &base.EntryContext{
7838
Resource: testRes,
79-
StatNode: testStatNode,
8039
Input: nil,
8140
}
8241
tests[0].args.entry = base.NewSentinelEntry(ctx, testRes, nil)
83-
tests[0].args.err = errors.New("biz error")
42+
tests[0].args.err = te
8443

8544
for _, tt := range tests {
8645
t.Run(tt.name, func(t *testing.T) {
87-
TraceErrorToEntry(tt.args.entry, tt.args.err, tt.args.count)
46+
TraceError(tt.args.entry, tt.args.err)
8847
time.Sleep(time.Millisecond * 10)
89-
assert.True(t, ctx.StatNode.GetSum(base.MetricEventError) == tt.want)
48+
assert.Equal(t, tests[0].args.entry.Context().Err(), tt.want)
9049
})
9150
}
9251
}

0 commit comments

Comments
 (0)