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

Implement throttled logger #3336

Open
swift1337 opened this issue Jan 7, 2025 · 0 comments
Open

Implement throttled logger #3336

swift1337 opened this issue Jan 7, 2025 · 0 comments
Labels
code-quality Code quality improvement zetaclient Issues related to ZetaClient

Comments

@swift1337
Copy link
Contributor

We use zerolog's sampled logger for reducing the noise that might be produced with repeated checks/tasks.

sampledLogger.Info().Msgf("WatchOutbound: outbound observation is disabled for chain %d", chainID)

I thought that we could introduce some simple wrapper that would implement throttling behaviour

logger := zerolog.New(...)

// creates a new logger wrapper that allows 1 unique log per 5 seconds
tlog := throttlelog.New(5  * time.Second)

var i int
for {
    key := fmt.Sprintf("is_even:%t", i % 2 == 0)
    i++
  
    tlog(key, logger.Info().Str("hello", "world").Int("iteration", i)).Msg("Tick")
     
    time.Sleep(time.Second)
}

// Tick hello=world iteration=0
// Tick hello=world iteration=1
// ... sleep for 5s
// Tick hello=world iteration=10
// Tick hello=world iteration=11
// ...

The possible drawback of this approach is that we need to invoke some mutex & timestamp checker on each invocation and introduce additional allocations. But practically, we use sampled loggers only in edge cases when something is broken or disabled, so it should not hurt the performance but reduce unnecessary logs volume and avoid reimplementing sync.Once-like behavior for logs.

@swift1337 swift1337 added zetaclient Issues related to ZetaClient code-quality Code quality improvement labels Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-quality Code quality improvement zetaclient Issues related to ZetaClient
Projects
None yet
Development

No branches or pull requests

1 participant