Files
tasks/common/log/throttle_logger.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

91 lines
2.1 KiB
Go

package log
import (
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/quotas"
)
const extraSkipForThrottleLogger = 3
type throttledLogger struct {
limiter quotas.RateLimiter
logger Logger
}
var _ Logger = (*throttledLogger)(nil)
// NewThrottledLogger returns an implementation of logger that throttles the
// log messages being emitted. The underlying implementation uses a token bucket
// rate limiter and stops emitting logs once the bucket runs out of tokens
//
// Fatal/Panic logs are always emitted without any throttling
func NewThrottledLogger(logger Logger, rps quotas.RateFn) *throttledLogger {
if sl, ok := logger.(SkipLogger); ok {
logger = sl.Skip(extraSkipForThrottleLogger)
}
limiter := quotas.NewDefaultOutgoingRateLimiter(rps)
tl := &throttledLogger{
limiter: limiter,
logger: logger,
}
return tl
}
func (tl *throttledLogger) Debug(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.Debug(msg, tags...)
})
}
func (tl *throttledLogger) Info(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.Info(msg, tags...)
})
}
func (tl *throttledLogger) Warn(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.Warn(msg, tags...)
})
}
func (tl *throttledLogger) Error(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.Error(msg, tags...)
})
}
func (tl *throttledLogger) DPanic(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.DPanic(msg, tags...)
})
}
func (tl *throttledLogger) Panic(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.Panic(msg, tags...)
})
}
func (tl *throttledLogger) Fatal(msg string, tags ...tag.Tag) {
tl.rateLimit(func() {
tl.logger.Fatal(msg, tags...)
})
}
// Return a logger with the specified key-value pairs set, to be included in a subsequent normal logging call
func (tl *throttledLogger) With(tags ...tag.Tag) Logger {
result := &throttledLogger{
limiter: tl.limiter,
logger: With(tl.logger, tags...),
}
return result
}
func (tl *throttledLogger) rateLimit(f func()) {
if ok := tl.limiter.Allow(); ok {
f()
}
}