MCPcopy
hub / github.com/grafana/dskit / TestNewGoKitWithRateLimitedAndFields

Function TestNewGoKitWithRateLimitedAndFields

log/gokit_test.go:75–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

73}
74
75func TestNewGoKitWithRateLimitedAndFields(t *testing.T) {
76 buf := bytes.NewBuffer(nil)
77 var lvl Level
78 require.NoError(t, lvl.Set("info"))
79 now := log.DefaultTimestampUTC()
80 logger := NewGoKitWithWriter(LogfmtFormat, buf)
81
82 rateLimitedLogger := NewRateLimitedLogger(logger, 1, 2, prometheus.DefaultRegisterer)
83
84 rateLimitedLogger = log.With(rateLimitedLogger, "ts", now, "caller", log.Caller(5))
85 rateLimitedLogger = level.NewFilter(rateLimitedLogger, lvl.Option)
86
87 for i := 0; i < 1000; i++ {
88 level.Info(rateLimitedLogger).Log("msg", LazySprintf("info %d", i))
89 level.Debug(rateLimitedLogger).Log("msg", LazySprintf("debug %d", i))
90 }
91
92 format := "ts=%s caller=gokit_test.go:%d level=%s msg=\"info %d\""
93 for i := 0; i < 1000; i++ {
94 if i < 2 {
95 require.True(t, bytes.Contains(buf.Bytes(), []byte(fmt.Sprintf(format, now, 88, "info", i))))
96 } else {
97 require.False(t, bytes.Contains(buf.Bytes(), []byte(fmt.Sprintf(format, now, 88, "info", i))))
98 }
99
100 require.False(t, bytes.Contains(buf.Bytes(), []byte(fmt.Sprintf(format, now, 89, "debug", i))))
101 }
102}
103
104type lazySprintfWithCount struct {
105 next Sprintf

Callers

nothing calls this directly

Calls 6

SetMethod · 0.95
NewGoKitWithWriterFunction · 0.85
NewRateLimitedLoggerFunction · 0.85
LazySprintfFunction · 0.85
WithMethod · 0.80
LogMethod · 0.45

Tested by

no test coverage detected