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

fix: add traffic-type label to distinguish pass type, fix #501 #502

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions core/base/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type EntryContext struct {
StatNode StatNode

Input *SentinelInput

// checker name of the hit rule
RuleChecker string
// the result of rule slots check
RuleCheckResult *TokenResult
// reserve for storing some intermediate data from the Entry execution process
Expand Down
7 changes: 7 additions & 0 deletions core/circuitbreaker/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

const (
RuleCheckSlotOrder = 5000

name = "circuitbreaker"
)

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

func checkPass(ctx *base.EntryContext) (bool, *Rule) {
breakers := getBreakersOfResource(ctx.Resource.Name())
if len(breakers) == 0 {
return true, nil
}

ctx.RuleChecker = name
for _, breaker := range breakers {
passed := breaker.TryPass(ctx)
if !passed {
Expand Down
7 changes: 6 additions & 1 deletion core/flow/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
package flow

import (
"github.com/pkg/errors"

"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/stat"
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
"github.com/pkg/errors"
)

const (
RuleCheckSlotOrder = 2000

name = "flow"
)

var (
Expand Down Expand Up @@ -57,6 +60,8 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
logging.Warn("[FlowSlot Check]Nil traffic controller found", "resourceName", res)
continue
}

ctx.RuleChecker = name
r := canPassCheck(tc, ctx.StatNode, ctx.Input.BatchCount)
if r == nil {
// nil means pass
Expand Down
4 changes: 4 additions & 0 deletions core/hotspot/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

const (
RuleCheckSlotOrder = 4000

name = "hotspot"
)

var (
Expand All @@ -45,6 +47,8 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
if arg == nil {
continue
}

ctx.RuleChecker = name
r := canPassCheck(tc, arg, batch)
if r == nil {
continue
Expand Down
7 changes: 6 additions & 1 deletion core/isolation/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package isolation

import (
"github.com/pkg/errors"

"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/logging"
"github.com/pkg/errors"
)

const (
RuleCheckSlotOrder = 3000

name = "isolation"
)

var (
Expand Down Expand Up @@ -59,6 +62,8 @@ func checkPass(ctx *base.EntryContext) (bool, *Rule, uint32) {
for _, rule := range getRulesOfResource(ctx.Resource.Name()) {
threshold := rule.Threshold
if rule.MetricType == Concurrency {
ctx.RuleChecker = name

if cur := statNode.CurrentConcurrency(); cur >= 0 {
curCount = uint32(cur)
} else {
Expand Down
7 changes: 4 additions & 3 deletions core/stat/stat_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
handledCounter = metric_exporter.NewCounter(
"handled_total",
"Total handled count",
[]string{"resource", "result", "block_type"})
[]string{"resource", "result", "block_type", "rule_name"})
)

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

handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultPass, "")
handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultPass, "", ctx.RuleChecker)
}

func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
Expand All @@ -61,7 +61,8 @@ func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockErro
s.recordBlockFor(InboundNode(), ctx.Input.BatchCount)
}

handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultBlock, blockError.BlockType().String())
handledCounter.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name(), ResultBlock,
blockError.BlockType().String(), ctx.RuleChecker)
}

func (s *Slot) OnCompleted(ctx *base.EntryContext) {
Expand Down
10 changes: 8 additions & 2 deletions core/system/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

const (
RuleCheckSlotOrder = 1000

name = "system"
)

var (
Expand All @@ -42,7 +44,7 @@ func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
rules := getRules()
result := ctx.RuleCheckResult
for _, rule := range rules {
passed, msg, snapshotValue := s.doCheckRule(rule)
passed, msg, snapshotValue := s.doCheckRule(ctx, rule)
if passed {
continue
}
Expand All @@ -56,9 +58,12 @@ func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
return result
}

func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, string, float64) {
func (s *AdaptiveSlot) doCheckRule(ctx *base.EntryContext, rule *Rule) (bool, string, float64) {
var msg string

prevChecker := ctx.RuleChecker
ctx.RuleChecker = name

threshold := rule.TriggerCount
switch rule.MetricType {
case InboundQPS:
Expand Down Expand Up @@ -101,6 +106,7 @@ func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, string, float64) {
}
return true, "", c
default:
ctx.RuleChecker = prevChecker
msg = "system undefined metric type, pass by default"
return true, msg, 0.0
}
Expand Down
17 changes: 9 additions & 8 deletions core/system/slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package system
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/stat"
"github.com/alibaba/sentinel-golang/core/system_metric"
"github.com/alibaba/sentinel-golang/util"
"github.com/stretchr/testify/assert"
)

func TestCheckNilInput(t *testing.T) {
Expand Down Expand Up @@ -60,14 +61,14 @@ func TestDoCheckRuleConcurrency(t *testing.T) {
TriggerCount: 0.5}

t.Run("TrueConcurrency", func(t *testing.T) {
isOK, _, v := sas.doCheckRule(rule)
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
assert.True(t, util.Float64Equals(float64(0.0), v))
assert.Equal(t, true, isOK)
})

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

t.Run("TrueLoad", func(t *testing.T) {
isOK, _, v := sas.doCheckRule(rule)
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
assert.True(t, util.Float64Equals(system_metric.NotRetrievedLoadValue, v))
assert.Equal(t, true, isOK)
})

t.Run("BBRTrueLoad", func(t *testing.T) {
rule.Strategy = BBR
system_metric.SetSystemLoad(1.0)
isOK, _, v := sas.doCheckRule(rule)
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
assert.Equal(t, true, isOK)
assert.True(t, util.Float64Equals(float64(1.0), v))
system_metric.SetSystemLoad(system_metric.NotRetrievedLoadValue)
Expand All @@ -103,15 +104,15 @@ func TestDoCheckRuleCpuUsage(t *testing.T) {
}

t.Run("TrueCpuUsage", func(t *testing.T) {
isOK, _, v := sas.doCheckRule(rule)
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
assert.True(t, util.Float64Equals(system_metric.NotRetrievedCpuUsageValue, v))
assert.Equal(t, true, isOK)
})

t.Run("BBRTrueCpuUsage", func(t *testing.T) {
rule.Strategy = BBR
system_metric.SetSystemCpuUsage(0.8)
isOK, _, v := sas.doCheckRule(rule)
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
assert.Equal(t, true, isOK)
assert.True(t, util.Float64Equals(0.8, v))
system_metric.SetSystemCpuUsage(system_metric.NotRetrievedCpuUsageValue)
Expand All @@ -123,7 +124,7 @@ func TestDoCheckRuleDefault(t *testing.T) {
rule := &Rule{MetricType: MetricTypeSize,
TriggerCount: 0.5}

isOK, _, v := sas.doCheckRule(rule)
isOK, _, v := sas.doCheckRule(&base.EntryContext{}, rule)
assert.Equal(t, true, isOK)
assert.True(t, util.Float64Equals(float64(0.0), v))
}