Skip to content

Commit 39733a0

Browse files
zhyeeNaR82n
andauthored
define new category constants (#179)
* define new category constants * update * test: cover new categories and caller skip cache * test: keep category tests compatible with go1.19 --------- Co-authored-by: vircoys <vircoys@gmail.com>
1 parent d134bfa commit 39733a0

5 files changed

Lines changed: 82 additions & 6 deletions

File tree

logger/logger.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import (
99
"os"
1010
"sync"
1111

12+
"github.com/GuanceCloud/cliutils"
1213
"go.uber.org/zap"
1314
"go.uber.org/zap/zapcore"
1415
"golang.org/x/time/rate"
15-
16-
"github.com/GuanceCloud/cliutils"
1716
)
1817

1918
const (
@@ -73,6 +72,14 @@ func (l *Logger) Name() string {
7372
return l.name
7473
}
7574

75+
func (l *Logger) Sugar() *zap.SugaredLogger {
76+
return l.zsl
77+
}
78+
79+
func (l *Logger) SetSugar(sugar *zap.SugaredLogger) {
80+
l.zsl = sugar
81+
}
82+
7683
func (l *Logger) Infof(fmt string, args ...any) {
7784
l.zsl.Infof(fmt, args...)
7885
}

logger/logger_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,26 @@ func TestBasic(t *T.T) {
523523
})
524524
}
525525

526+
func TestDefaultSloggerSkipNUsesDistinctCacheKey(t *testing.T) {
527+
Reset()
528+
529+
opt := &Option{
530+
Path: "stdout",
531+
Level: DEBUG,
532+
Flags: OPT_ENC_CONSOLE | OPT_SHORT_CALLER,
533+
}
534+
535+
require.NoError(t, InitRoot(opt))
536+
537+
base := DefaultSLogger("same-name")
538+
withSkip := DefaultSloggerSkipN("same-name", 3)
539+
540+
require.NotNil(t, base.Sugar())
541+
require.NotNil(t, withSkip.Sugar())
542+
assert.NotSame(t, base.Sugar(), withSkip.Sugar())
543+
assert.Equal(t, int64(2), TotalSLoggers())
544+
}
545+
526546
type BufferSync struct {
527547
io.ReadWriter
528548
}

logger/slogger.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func DefaultSLogger(name string) *Logger {
7373
return &Logger{zsl: slogger(name, 1)}
7474
}
7575

76+
func DefaultSloggerSkipN(name string, callerSkip int) *Logger {
77+
return &Logger{zsl: slogger(name, callerSkip)}
78+
}
79+
7680
func TotalSLoggers() int64 {
7781
return atomic.LoadInt64(&totalSloggers)
7882
}
@@ -88,9 +92,10 @@ func slogger(name string, callerSkip int) *zap.SugaredLogger {
8892
panic("should not been here")
8993
}
9094

95+
cacheKey := sloggerCacheKey(name, callerSkip)
9196
newlog := getSugarLogger(r, name, callerSkip)
9297
if root != nil {
93-
l, loaded := slogs.LoadOrStore(name, newlog)
98+
l, loaded := slogs.LoadOrStore(cacheKey, newlog)
9499
if !loaded {
95100
atomic.AddInt64(&totalSloggers, 1)
96101
}
@@ -108,3 +113,7 @@ func getSugarLogger(l *zap.Logger, name string, callerSkip int) *zap.SugaredLogg
108113
return l.Sugar().Named(name)
109114
}
110115
}
116+
117+
func sloggerCacheKey(name string, callerSkip int) string {
118+
return fmt.Sprintf("%s|skip=%d", name, callerSkip)
119+
}

point/category.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func (c Category) URL() string {
2424
}
2525

2626
func (c Category) Alias() string {
27-
if x, ok := categoryAias[c]; !ok {
27+
if x, ok := categoryAlias[c]; !ok {
2828
return CUnknown
2929
} else {
3030
return x
3131
}
3232
}
3333

3434
func CatAlias(c string) Category {
35-
for k, v := range categoryAias {
35+
for k, v := range categoryAlias {
3636
if c == v {
3737
return k
3838
}
@@ -73,6 +73,8 @@ func AllCategories() []Category {
7373
Profiling,
7474
DynamicDWCategory,
7575
DialTesting,
76+
ExecutionLog,
77+
LLM,
7678
}
7779
}
7880

@@ -93,6 +95,8 @@ const (
9395
Security
9496
Profiling
9597
DialTesting
98+
ExecutionLog
99+
LLM
96100

97101
SUnknownCategory = "unknown"
98102
SDynamicDWCategory = "dynamic_dw" // NOTE: not used
@@ -109,6 +113,8 @@ const (
109113
SSecurity = "security"
110114
SProfiling = "profiling"
111115
SDialTesting = "dialtesting"
116+
SExecutionLog = "execution_log"
117+
SLlm = "llm"
112118

113119
URLUnknownCategory = "/v1/write/unknown"
114120
URLDynamicDWCategory = "/v1/write/dynamic_dw" // NOTE: not used
@@ -125,6 +131,8 @@ const (
125131
URLSecurity = "/v1/write/security"
126132
URLProfiling = "/v1/write/profiling"
127133
URLDialTesting = "/v1/write/dialtesting" // NOTE: not used
134+
URLExecutionLog = "/v1/write/siem_logging"
135+
URLLlm = "/v1/write/langfuse-v2"
128136

129137
CUnknown = "UNKNOWN"
130138
CDynamicDW = "DYNAMIC_DW"
@@ -140,6 +148,8 @@ const (
140148
CS = "S"
141149
CP = "P"
142150
CDT = "DT"
151+
CEL = "EL"
152+
CLlm = "LLM"
143153
)
144154

145155
var (
@@ -160,12 +170,15 @@ var (
160170

161171
DialTesting: URLDialTesting,
162172

173+
ExecutionLog: URLExecutionLog,
174+
LLM: URLLlm,
175+
163176
DynamicDWCategory: URLDynamicDWCategory,
164177

165178
UnknownCategory: URLUnknownCategory,
166179
}
167180

168-
categoryAias = map[Category]string{
181+
categoryAlias = map[Category]string{
169182
Metric: CM,
170183
Network: CN,
171184
KeyEvent: CE,
@@ -178,6 +191,8 @@ var (
178191
Security: CS,
179192
Profiling: CP,
180193
DialTesting: CDT,
194+
ExecutionLog: CEL,
195+
LLM: CLlm,
181196
UnknownCategory: CUnknown,
182197
DynamicDWCategory: CDynamicDW,
183198
}
@@ -196,6 +211,8 @@ var (
196211
Security: SSecurity,
197212
Profiling: SProfiling,
198213
DialTesting: SDialTesting,
214+
ExecutionLog: SExecutionLog,
215+
LLM: SLlm,
199216
UnknownCategory: SUnknownCategory,
200217
DynamicDWCategory: SDynamicDWCategory,
201218
}

point/category_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func TestURL(t *testing.T) {
2828
{c: "/v1/write/keyevent", expect: KeyEvent},
2929
{c: "/v1/write/tracing", expect: Tracing},
3030
{c: "/v1/write/dynamic_dw", expect: DynamicDWCategory},
31+
{c: "/v1/write/siem_logging", expect: ExecutionLog},
32+
{c: "/v1/write/langfuse-v2", expect: LLM},
3133
}
3234

3335
for _, tc := range cases {
@@ -52,6 +54,8 @@ func TestAlias(t *testing.T) {
5254
{c: "P", expect: Profiling},
5355
{c: "E", expect: KeyEvent},
5456
{c: "T", expect: Tracing},
57+
{c: "EL", expect: ExecutionLog},
58+
{c: "LLM", expect: LLM},
5559
{c: "Dynamic_dw", expect: UnknownCategory},
5660
}
5761

@@ -79,6 +83,8 @@ func TestString(t *testing.T) {
7983
{c: "keyevent", expect: KeyEvent},
8084
{c: "tracing", expect: Tracing},
8185
{c: "dynamic_dw", expect: DynamicDWCategory},
86+
{c: "execution_log", expect: ExecutionLog},
87+
{c: "llm", expect: LLM},
8288
}
8389

8490
for _, tc := range cases {
@@ -87,3 +93,20 @@ func TestString(t *testing.T) {
8793
})
8894
}
8995
}
96+
97+
func TestAllCategoriesIncludesNewCategories(t *testing.T) {
98+
all := AllCategories()
99+
100+
assert.True(t, containsCategory(all, ExecutionLog))
101+
assert.True(t, containsCategory(all, LLM))
102+
}
103+
104+
func containsCategory(all []Category, target Category) bool {
105+
for _, c := range all {
106+
if c == target {
107+
return true
108+
}
109+
}
110+
111+
return false
112+
}

0 commit comments

Comments
 (0)