Skip to content

Commit

Permalink
added a rate limited writer for klog (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed May 24, 2023
1 parent d604eef commit c8e0d2c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ require (
golang.org/x/exp v0.0.0-20221106115401-f9659909a136 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.2.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,8 @@ golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxb
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.2.0 h1:52I/1L54xyEQAYdtcSuxtiT84KGYTBGXwayxmIpNJhE=
golang.org/x/time v0.2.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/mod/semver"
"golang.org/x/sys/unix"
"golang.org/x/time/rate"
"k8s.io/klog/v2"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -74,6 +75,9 @@ func machineID() string {
}

func main() {
klog.LogToStderr(false)
klog.SetOutput(&RateLimitedLogOutput{limiter: rate.NewLimiter(10, 100)})

klog.Infoln("agent version:", version)

hostname, kv, err := uname()
Expand Down Expand Up @@ -128,3 +132,14 @@ type logger struct{}
func (l logger) Println(v ...interface{}) {
klog.Errorln(v...)
}

type RateLimitedLogOutput struct {
limiter *rate.Limiter
}

func (o *RateLimitedLogOutput) Write(data []byte) (int, error) {
if !o.limiter.Allow() {
return len(data), nil
}
return os.Stderr.Write(data)
}

0 comments on commit c8e0d2c

Please sign in to comment.