1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
"go.uber.org/zap/zapcore"
13
// NewControllerErrorSuppressor creates a new controller error suppressor.
15
// It suppresses error logs for a given controller unless it logs >= threshold errors.
16
// The idea is that all controllers reconcile errors, so if the error is not transient,
17
// it will be reported enough time to hit the threshold, but transient errors will be
20
// The suppressor records the controller name by inspecting a log field named "controller"
21
// passed via `logger.With()` call.
22
func NewControllerErrorSuppressor(core zapcore.Core, threshold int64) zapcore.Core {
23
return &consoleSampler{
29
type consoleSampler struct {
37
var _ zapcore.Core = (*consoleSampler)(nil)
39
func (s *consoleSampler) Level() zapcore.Level {
40
return zapcore.LevelOf(s.Core)
43
func (s *consoleSampler) With(fields []zapcore.Field) zapcore.Core {
44
controller := s.controller
47
for _, field := range fields {
48
if field.Key == "controller" {
49
if field.String != controller {
50
controller = field.String
51
num = new(atomic.Int64)
58
return &consoleSampler{
59
threshold: s.threshold,
60
controller: controller,
62
Core: s.Core.With(fields),
66
func (s *consoleSampler) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
67
if !s.Enabled(ent.Level) {
71
if ent.Level == zapcore.ErrorLevel && s.controller != "" {
72
if s.hits.Add(1) <= s.threshold {
78
return s.Core.Check(ent, ce)