Skip to content

Commit 45afa9d

Browse files
committed
feat: add rule name hit
1 parent 8dbc6d7 commit 45afa9d

File tree

8 files changed

+46
-15
lines changed

8 files changed

+46
-15
lines changed

core/base/context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type EntryContext struct {
3030
StatNode StatNode
3131

3232
Input *SentinelInput
33+
34+
// checker name of the hit rule
35+
RuleChecker string
3336
// the result of rule slots check
3437
RuleCheckResult *TokenResult
3538
// reserve for storing some intermediate data from the Entry execution process

core/circuitbreaker/slot.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020

2121
const (
2222
RuleCheckSlotOrder = 5000
23+
24+
name = "circuitbreaker"
2325
)
2426

2527
var (
@@ -52,6 +54,11 @@ func (b *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
5254

5355
func checkPass(ctx *base.EntryContext) (bool, *Rule) {
5456
breakers := getBreakersOfResource(ctx.Resource.Name())
57+
if len(breakers) == 0 {
58+
return true, nil
59+
}
60+
61+
ctx.RuleChecker = name
5562
for _, breaker := range breakers {
5663
passed := breaker.TryPass(ctx)
5764
if !passed {

core/flow/slot.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
package flow
1616

1717
import (
18+
"github.com/pkg/errors"
19+
1820
"github.com/alibaba/sentinel-golang/core/base"
1921
"github.com/alibaba/sentinel-golang/core/stat"
2022
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
2123
"github.com/alibaba/sentinel-golang/logging"
2224
"github.com/alibaba/sentinel-golang/util"
23-
"github.com/pkg/errors"
2425
)
2526

2627
const (
2728
RuleCheckSlotOrder = 2000
29+
30+
name = "flow"
2831
)
2932

3033
var (
@@ -57,6 +60,8 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
5760
logging.Warn("[FlowSlot Check]Nil traffic controller found", "resourceName", res)
5861
continue
5962
}
63+
64+
ctx.RuleChecker = name
6065
r := canPassCheck(tc, ctx.StatNode, ctx.Input.BatchCount)
6166
if r == nil {
6267
// nil means pass

core/hotspot/slot.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
const (
2323
RuleCheckSlotOrder = 4000
24+
25+
name = "hotspot"
2426
)
2527

2628
var (
@@ -45,6 +47,8 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
4547
if arg == nil {
4648
continue
4749
}
50+
51+
ctx.RuleChecker = name
4852
r := canPassCheck(tc, arg, batch)
4953
if r == nil {
5054
continue

core/isolation/slot.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
package isolation
1616

1717
import (
18+
"github.com/pkg/errors"
19+
1820
"github.com/alibaba/sentinel-golang/core/base"
1921
"github.com/alibaba/sentinel-golang/logging"
20-
"github.com/pkg/errors"
2122
)
2223

2324
const (
2425
RuleCheckSlotOrder = 3000
26+
27+
name = "isolation"
2528
)
2629

2730
var (
@@ -59,6 +62,8 @@ func checkPass(ctx *base.EntryContext) (bool, *Rule, uint32) {
5962
for _, rule := range getRulesOfResource(ctx.Resource.Name()) {
6063
threshold := rule.Threshold
6164
if rule.MetricType == Concurrency {
65+
ctx.RuleChecker = name
66+
6267
if cur := statNode.CurrentConcurrency(); cur >= 0 {
6368
curCount = uint32(cur)
6469
} else {

core/stat/stat_slot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232
handledCounter = metric_exporter.NewCounter(
3333
"handled_total",
3434
"Total handled count",
35-
[]string{"resource", "result", "block_type", "traffic_type"})
35+
[]string{"resource", "result", "block_type", "rule_name"})
3636
)
3737

3838
func init() {
@@ -52,7 +52,7 @@ func (s *Slot) OnEntryPassed(ctx *base.EntryContext) {
5252
s.recordPassFor(InboundNode(), ctx.Input.BatchCount)
5353
}
5454

55-
handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultPass, "", ctx.Resource.FlowType().String())
55+
handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultPass, "", ctx.RuleChecker)
5656
}
5757

5858
func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
@@ -62,7 +62,7 @@ func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockErro
6262
}
6363

6464
handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultBlock,
65-
blockError.BlockType().String(), ctx.Resource.FlowType().String())
65+
blockError.BlockType().String(), ctx.RuleChecker)
6666
}
6767

6868
func (s *Slot) OnCompleted(ctx *base.EntryContext) {

core/system/slot.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
const (
2424
RuleCheckSlotOrder = 1000
25+
26+
name = "system"
2527
)
2628

2729
var (
@@ -42,7 +44,7 @@ func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
4244
rules := getRules()
4345
result := ctx.RuleCheckResult
4446
for _, rule := range rules {
45-
passed, msg, snapshotValue := s.doCheckRule(rule)
47+
passed, msg, snapshotValue := s.doCheckRule(ctx, rule)
4648
if passed {
4749
continue
4850
}
@@ -56,9 +58,12 @@ func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
5658
return result
5759
}
5860

59-
func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, string, float64) {
61+
func (s *AdaptiveSlot) doCheckRule(ctx *base.EntryContext, rule *Rule) (bool, string, float64) {
6062
var msg string
6163

64+
prevChecker := ctx.RuleChecker
65+
ctx.RuleChecker = name
66+
6267
threshold := rule.TriggerCount
6368
switch rule.MetricType {
6469
case InboundQPS:
@@ -101,6 +106,7 @@ func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, string, float64) {
101106
}
102107
return true, "", c
103108
default:
109+
ctx.RuleChecker = prevChecker
104110
msg = "system undefined metric type, pass by default"
105111
return true, msg, 0.0
106112
}

core/system/slot_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package system
1717
import (
1818
"testing"
1919

20+
"github.com/stretchr/testify/assert"
21+
2022
"github.com/alibaba/sentinel-golang/core/base"
2123
"github.com/alibaba/sentinel-golang/core/stat"
2224
"github.com/alibaba/sentinel-golang/core/system_metric"
2325
"github.com/alibaba/sentinel-golang/util"
24-
"github.com/stretchr/testify/assert"
2526
)
2627

2728
func TestCheckNilInput(t *testing.T) {
@@ -60,14 +61,14 @@ func TestDoCheckRuleConcurrency(t *testing.T) {
6061
TriggerCount: 0.5}
6162

6263
t.Run("TrueConcurrency", func(t *testing.T) {
63-
isOK, _, v := sas.doCheckRule(rule)
64+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
6465
assert.True(t, util.Float64Equals(float64(0.0), v))
6566
assert.Equal(t, true, isOK)
6667
})
6768

6869
t.Run("FalseConcurrency", func(t *testing.T) {
6970
stat.InboundNode().IncreaseConcurrency()
70-
isOK, _, v := sas.doCheckRule(rule)
71+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
7172
assert.True(t, util.Float64Equals(float64(1.0), v))
7273
assert.Equal(t, false, isOK)
7374
stat.InboundNode().DecreaseConcurrency()
@@ -80,15 +81,15 @@ func TestDoCheckRuleLoad(t *testing.T) {
8081
TriggerCount: 0.5}
8182

8283
t.Run("TrueLoad", func(t *testing.T) {
83-
isOK, _, v := sas.doCheckRule(rule)
84+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
8485
assert.True(t, util.Float64Equals(system_metric.NotRetrievedLoadValue, v))
8586
assert.Equal(t, true, isOK)
8687
})
8788

8889
t.Run("BBRTrueLoad", func(t *testing.T) {
8990
rule.Strategy = BBR
9091
system_metric.SetSystemLoad(1.0)
91-
isOK, _, v := sas.doCheckRule(rule)
92+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
9293
assert.Equal(t, true, isOK)
9394
assert.True(t, util.Float64Equals(float64(1.0), v))
9495
system_metric.SetSystemLoad(system_metric.NotRetrievedLoadValue)
@@ -103,15 +104,15 @@ func TestDoCheckRuleCpuUsage(t *testing.T) {
103104
}
104105

105106
t.Run("TrueCpuUsage", func(t *testing.T) {
106-
isOK, _, v := sas.doCheckRule(rule)
107+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
107108
assert.True(t, util.Float64Equals(system_metric.NotRetrievedCpuUsageValue, v))
108109
assert.Equal(t, true, isOK)
109110
})
110111

111112
t.Run("BBRTrueCpuUsage", func(t *testing.T) {
112113
rule.Strategy = BBR
113114
system_metric.SetSystemCpuUsage(0.8)
114-
isOK, _, v := sas.doCheckRule(rule)
115+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
115116
assert.Equal(t, true, isOK)
116117
assert.True(t, util.Float64Equals(0.8, v))
117118
system_metric.SetSystemCpuUsage(system_metric.NotRetrievedCpuUsageValue)
@@ -123,7 +124,7 @@ func TestDoCheckRuleDefault(t *testing.T) {
123124
rule := &Rule{MetricType: MetricTypeSize,
124125
TriggerCount: 0.5}
125126

126-
isOK, _, v := sas.doCheckRule(rule)
127+
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
127128
assert.Equal(t, true, isOK)
128129
assert.True(t, util.Float64Equals(float64(0.0), v))
129130
}

0 commit comments

Comments
 (0)