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

Make filtered aggregation possible #3677

Merged
merged 3 commits into from Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions pkg/logger/logcounter.go
Expand Up @@ -2,6 +2,8 @@ package logger

import (
"sync"

"golang.org/x/exp/maps"
)

type logOrigin struct {
Expand All @@ -19,26 +21,34 @@ type logCounter struct {
func (lc *logCounter) update(lo logOrigin) {
lc.rwMutex.Lock()
defer lc.rwMutex.Unlock()

lc.data[lo]++
}

func (lc *logCounter) Lookup(key logOrigin) (count uint32, found bool) {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()

count, found = lc.data[key]
return count, found
}

func (lc *logCounter) dump(flush bool) map[logOrigin]uint32 {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()
if flush {
lc.rwMutex.Lock()
defer lc.rwMutex.Unlock()
} else {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()
}

dump := make(map[logOrigin]uint32, len(lc.data))
for k, v := range lc.data {
dump[k] = v
if flush {
delete(lc.data, k)
}
maps.Copy(dump, lc.data)

if flush {
maps.Clear(lc.data)
}

return dump
}

Expand Down
40 changes: 19 additions & 21 deletions pkg/logger/logger.go
Expand Up @@ -142,11 +142,9 @@ func (l *Logger) updateCounter(file string, line int, lvl Level, msg string) {

// aggregateLog will update the log counter if aggregation is enabled.
// It returns true if the aggregation was done.
func aggregateLog(skip int, l *Logger, lvl Level, msg string) bool {
func aggregateLog(l *Logger, lvl Level, msg string, ci *callerInfo) bool {
if l.cfg.Aggregate {
callerInfo := getCallerInfo(skip + 1)
l.updateCounter(callerInfo.file, callerInfo.line, lvl, msg)

l.updateCounter(ci.file, ci.line, lvl, msg)
return true
}

Expand Down Expand Up @@ -204,17 +202,17 @@ func formatCallFlow(funcNames []string) string {

// Debug
func debugw(skip int, l *Logger, msg string, keysAndValues ...interface{}) {
if aggregateLog(skip+1, l, DebugLevel, msg) {
ci := getCallerInfo(skip + 1)
if !shouldOutput(msg, DebugLevel, ci) {
return
}

callerInfo := getCallerInfo(skip + 1)
if !shouldOutput(msg, DebugLevel, callerInfo) {
if aggregateLog(l, DebugLevel, msg, ci) {
return
}

origin := strings.Join([]string{callerInfo.pkg, callerInfo.file, strconv.Itoa(callerInfo.line)}, ":")
calls := formatCallFlow(callerInfo.functions)
origin := strings.Join([]string{ci.pkg, ci.file, strconv.Itoa(ci.line)}, ":")
calls := formatCallFlow(ci.functions)
keysAndValues = append(keysAndValues, "origin", origin, "calls", calls)

l.l.Debugw(msg, keysAndValues...)
Expand All @@ -230,12 +228,12 @@ func (l *Logger) Debugw(msg string, keysAndValues ...interface{}) {

// Info
func infow(skip int, l *Logger, msg string, keysAndValues ...interface{}) {
if aggregateLog(skip+1, l, InfoLevel, msg) {
ci := getCallerInfo(skip + 1)
if !shouldOutput(msg, InfoLevel, ci) {
return
}

callerInfo := getCallerInfo(skip + 1)
if !shouldOutput(msg, InfoLevel, callerInfo) {
if aggregateLog(l, InfoLevel, msg, ci) {
return
}

Expand All @@ -252,12 +250,12 @@ func (l *Logger) Infow(msg string, keysAndValues ...interface{}) {

// Warn
func warnw(skip int, l *Logger, msg string, keysAndValues ...interface{}) {
if aggregateLog(skip+1, l, WarnLevel, msg) {
ci := getCallerInfo(skip + 1)
if !shouldOutput(msg, WarnLevel, ci) {
return
}

callerInfo := getCallerInfo(skip + 1)
if !shouldOutput(msg, WarnLevel, callerInfo) {
if aggregateLog(l, WarnLevel, msg, ci) {
return
}

Expand All @@ -274,12 +272,12 @@ func (l *Logger) Warnw(msg string, keysAndValues ...interface{}) {

// Error
func errorw(skip int, l *Logger, msg string, keysAndValues ...interface{}) {
if aggregateLog(skip+1, l, ErrorLevel, msg) {
ci := getCallerInfo(skip + 1)
if !shouldOutput(msg, ErrorLevel, ci) {
return
}

callerInfo := getCallerInfo(skip + 1)
if !shouldOutput(msg, ErrorLevel, callerInfo) {
if aggregateLog(l, ErrorLevel, msg, ci) {
return
}

Expand All @@ -296,12 +294,12 @@ func (l *Logger) Errorw(msg string, keysAndValues ...interface{}) {

// Fatal
func fatalw(skip int, l *Logger, msg string, keysAndValues ...interface{}) {
if aggregateLog(skip+1, l, FatalLevel, msg) {
ci := getCallerInfo(skip + 1)
if !shouldOutput(msg, FatalLevel, ci) {
return
}

callerInfo := getCallerInfo(skip + 1)
if !shouldOutput(msg, FatalLevel, callerInfo) {
if aggregateLog(l, FatalLevel, msg, ci) {
return
}

Expand Down