Skip to content

Commit 7ddba92

Browse files
louyutingsczyh30
authored andcommitted
Optimize logging of loading rules in rule managers
1 parent b65899d commit 7ddba92

File tree

6 files changed

+43
-55
lines changed

6 files changed

+43
-55
lines changed

core/circuitbreaker/rule_manager.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package circuitbreaker
33
import (
44
"fmt"
55
"reflect"
6-
"strings"
76
"sync"
87

98
"github.com/alibaba/sentinel-golang/logging"
@@ -284,15 +283,13 @@ func rulesFrom(rm map[string][]*Rule) []*Rule {
284283
return rules
285284
}
286285

287-
func logRuleUpdate(rules map[string][]*Rule) {
288-
sb := strings.Builder{}
289-
sb.WriteString("Circuit breaking rules loaded: [")
290-
291-
for _, r := range rulesFrom(rules) {
292-
sb.WriteString(r.String() + ",")
286+
func logRuleUpdate(m map[string][]*Rule) {
287+
rs := rulesFrom(m)
288+
if len(rs) == 0 {
289+
logging.Info("[CircuitBreakerRuleManager] Circuit breaking rules were cleared")
290+
} else {
291+
logging.Info("[CircuitBreakerRuleManager] Circuit breaking rules were loaded", "rules", rs)
293292
}
294-
sb.WriteString("]")
295-
logging.Info(sb.String())
296293
}
297294

298295
// Note: this function is not thread-safe.

core/flow/rule_manager.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package flow
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"sync"
76

@@ -56,19 +55,11 @@ func init() {
5655
}
5756

5857
func logRuleUpdate(m TrafficControllerMap) {
59-
bs, err := json.Marshal(rulesFrom(m))
60-
if err != nil {
61-
if len(m) == 0 {
62-
logging.Info("[FlowRuleManager] Flow rules were cleared")
63-
} else {
64-
logging.Info("[FlowRuleManager] Flow rules were loaded")
65-
}
58+
rs := rulesFrom(m)
59+
if len(rs) == 0 {
60+
logging.Info("[FlowRuleManager] Flow rules were cleared")
6661
} else {
67-
if len(m) == 0 {
68-
logging.Info("[FlowRuleManager] Flow rules were cleared")
69-
} else {
70-
logging.Info("[FlowRuleManager] Flow rules were loaded", "rules", string(bs))
71-
}
62+
logging.Info("[FlowRuleManager] Flow rules were loaded", "rules", rs)
7263
}
7364
}
7465

core/flow/slot.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func selectNodeByRelStrategy(rule *Rule, node base.StatNode) base.StatNode {
6060
func checkInLocal(tc *TrafficShapingController, node base.StatNode, acquireCount uint32, flag int32) *base.TokenResult {
6161
actual := selectNodeByRelStrategy(tc.rule, node)
6262
if actual == nil {
63-
logging.Error(errors.Errorf("nil resource node"), "no resource node for flow rule", "rule", tc.rule)
63+
logging.FrequentErrorOnce.Do(func() {
64+
logging.Error(errors.Errorf("nil resource node"), "no resource node for flow rule", "rule", tc.rule)
65+
})
6466
return base.NewTokenResultPass()
6567
}
6668
return tc.PerformChecking(node, acquireCount, flag)

core/hotspot/rule_manager.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package hotspot
22

33
import (
44
"fmt"
5-
"strings"
65
"sync"
76

87
"github.com/alibaba/sentinel-golang/logging"
@@ -197,14 +196,12 @@ func onRuleUpdate(rules []*Rule) (err error) {
197196
}
198197

199198
func logRuleUpdate(m trafficControllerMap) {
200-
sb := strings.Builder{}
201-
sb.WriteString("Hotspot parameter flow control rules loaded: [")
202-
203-
for _, r := range rulesFrom(m) {
204-
sb.WriteString(r.String() + ",")
199+
rs := rulesFrom(m)
200+
if len(rs) == 0 {
201+
logging.Info("[HotspotRuleManager] Hotspot rules were cleared")
202+
} else {
203+
logging.Info("[HotspotRuleManager] Hotspot rules were loaded", "rules", rs)
205204
}
206-
sb.WriteString("]")
207-
logging.Info(sb.String())
208205
}
209206

210207
func rulesFrom(m trafficControllerMap) []*Rule {

core/isolation/rule_manager.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package isolation
22

33
import (
4-
"encoding/json"
54
"sync"
65

76
"github.com/alibaba/sentinel-golang/logging"
@@ -113,19 +112,11 @@ func rulesFrom(m map[string][]*Rule) []*Rule {
113112
}
114113

115114
func logRuleUpdate(m map[string][]*Rule) {
116-
bs, err := json.Marshal(rulesFrom(m))
117-
if err != nil {
118-
if len(m) == 0 {
119-
logging.Info("[IsolationRuleManager] Isolation rules were cleared")
120-
} else {
121-
logging.Info("[IsolationRuleManager] Isolation rules were loaded")
122-
}
115+
rs := rulesFrom(m)
116+
if len(rs) == 0 {
117+
logging.Info("[IsolationRuleManager] Isolation rules were cleared")
123118
} else {
124-
if len(m) == 0 {
125-
logging.Info("[IsolationRuleManager] Isolation rules were cleared")
126-
} else {
127-
logging.Info("[IsolationRuleManager] Isolation rules were loaded", "rules", string(bs))
128-
}
119+
logging.Info("[IsolationRuleManager] Isolation rules were loaded", "rules", rs)
129120
}
130121
}
131122

logging/logging.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"runtime"
1010
"strings"
11+
"sync"
1112
"time"
1213
)
1314

@@ -25,12 +26,14 @@ const (
2526
// RecordLogFileName represents the default file name of the record log.
2627
RecordLogFileName = "sentinel-record.log"
2728
DefaultDirName = "logs" + string(os.PathSeparator) + "csp" + string(os.PathSeparator)
29+
GlobalCallerDepth = 4
2830
)
2931

3032
var (
31-
globalLogLevel = InfoLevel
32-
globalCallerDepth = 4
33-
globalLogger = NewConsoleLogger()
33+
globalLogLevel = InfoLevel
34+
globalLogger = NewConsoleLogger()
35+
36+
FrequentErrorOnce = &sync.Once{}
3437
)
3538

3639
func GetGlobalLoggerLevel() Level {
@@ -169,12 +172,19 @@ func AssembleMsg(depth int, logLevel, msg string, err error, keysAndValues ...in
169172
sb.WriteString(kStr)
170173
sb.WriteByte('"')
171174
sb.WriteByte(':')
172-
if vbs, err := json.Marshal(v); err != nil {
175+
vStr, vIsStr := v.(string)
176+
if !vIsStr {
177+
if vbs, err := json.Marshal(v); err != nil {
178+
sb.WriteByte('"')
179+
sb.WriteString(fmt.Sprintf("%+v", v))
180+
sb.WriteByte('"')
181+
} else {
182+
sb.WriteString(string(vbs))
183+
}
184+
} else {
173185
sb.WriteByte('"')
174-
sb.WriteString(fmt.Sprintf("%+v", v))
186+
sb.WriteString(vStr)
175187
sb.WriteByte('"')
176-
} else {
177-
sb.WriteString(string(vbs))
178188
}
179189
i = i + 2
180190
}
@@ -191,29 +201,29 @@ func (l *DefaultLogger) Debug(msg string, keysAndValues ...interface{}) {
191201
if DebugLevel < globalLogLevel {
192202
return
193203
}
194-
l.log.Print(AssembleMsg(globalCallerDepth, "DEBUG", msg, nil, keysAndValues...))
204+
l.log.Print(AssembleMsg(GlobalCallerDepth, "DEBUG", msg, nil, keysAndValues...))
195205
}
196206

197207
func (l *DefaultLogger) Info(msg string, keysAndValues ...interface{}) {
198208
if InfoLevel < globalLogLevel {
199209
return
200210
}
201-
l.log.Print(AssembleMsg(globalCallerDepth, "INFO", msg, nil, keysAndValues...))
211+
l.log.Print(AssembleMsg(GlobalCallerDepth, "INFO", msg, nil, keysAndValues...))
202212
}
203213

204214
func (l *DefaultLogger) Warn(msg string, keysAndValues ...interface{}) {
205215
if WarnLevel < globalLogLevel {
206216
return
207217
}
208218

209-
l.log.Print(AssembleMsg(globalCallerDepth, "WARNING", msg, nil, keysAndValues...))
219+
l.log.Print(AssembleMsg(GlobalCallerDepth, "WARNING", msg, nil, keysAndValues...))
210220
}
211221

212222
func (l *DefaultLogger) Error(err error, msg string, keysAndValues ...interface{}) {
213223
if ErrorLevel < globalLogLevel {
214224
return
215225
}
216-
l.log.Print(AssembleMsg(globalCallerDepth, "ERROR", msg, err, keysAndValues...))
226+
l.log.Print(AssembleMsg(GlobalCallerDepth, "ERROR", msg, err, keysAndValues...))
217227
}
218228

219229
func Debug(msg string, keysAndValues ...interface{}) {

0 commit comments

Comments
 (0)