-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: suppress controller runtime first N failures on the console
As the controllers might fail with transient errors on machine startup, but errors are always retried, persisten errors will anyway show up in the console. The full `talosctl logs controller-runtime` are not suppressed. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
- Loading branch information
Showing
6 changed files
with
195 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package logging | ||
|
||
import ( | ||
"sync/atomic" | ||
|
||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// NewControllerErrorSuppressor creates a new controller error suppressor. | ||
// | ||
// It suppresses error logs for a given controller unless it logs >= threshold errors. | ||
// The idea is that all controllers reconcile errors, so if the error is not transient, | ||
// it will be reported enough time to hit the threshold, but transient errors will be | ||
// suppressed. | ||
// | ||
// The suppressor records the controller name by inspecting a log field named "controller" | ||
// passed via `logger.With()` call. | ||
func NewControllerErrorSuppressor(core zapcore.Core, threshold int64) zapcore.Core { | ||
return &consoleSampler{ | ||
Core: core, | ||
threshold: threshold, | ||
} | ||
} | ||
|
||
type consoleSampler struct { | ||
zapcore.Core | ||
|
||
hits *atomic.Int64 | ||
threshold int64 | ||
controller string | ||
} | ||
|
||
var _ zapcore.Core = (*consoleSampler)(nil) | ||
|
||
func (s *consoleSampler) Level() zapcore.Level { | ||
return zapcore.LevelOf(s.Core) | ||
} | ||
|
||
func (s *consoleSampler) With(fields []zapcore.Field) zapcore.Core { | ||
controller := s.controller | ||
num := s.hits | ||
|
||
for _, field := range fields { | ||
if field.Key == "controller" { | ||
if field.String != controller { | ||
controller = field.String | ||
num = new(atomic.Int64) | ||
} | ||
|
||
break | ||
} | ||
} | ||
|
||
return &consoleSampler{ | ||
threshold: s.threshold, | ||
controller: controller, | ||
hits: num, | ||
Core: s.Core.With(fields), | ||
} | ||
} | ||
|
||
func (s *consoleSampler) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { | ||
if !s.Enabled(ent.Level) { | ||
return ce | ||
} | ||
|
||
if ent.Level == zapcore.ErrorLevel && s.controller != "" { | ||
if s.hits.Add(1) <= s.threshold { | ||
// suppress the log | ||
return ce | ||
} | ||
} | ||
|
||
return s.Core.Check(ent, ce) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package logging_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
"go.uber.org/zap/zaptest/observer" | ||
|
||
"github.com/siderolabs/talos/pkg/logging" | ||
) | ||
|
||
func assertLogged(t *testing.T, core zapcore.Core, logs *observer.ObservedLogs, entries []zapcore.Entry, expectedCount int) { | ||
t.Helper() | ||
|
||
for _, entry := range entries { | ||
if ce := core.Check(entry, nil); ce != nil { | ||
ce.Write() | ||
} | ||
} | ||
|
||
assert.Len(t, logs.TakeAll(), expectedCount) | ||
} | ||
|
||
func TestErrorSuppressor(t *testing.T) { | ||
t.Parallel() | ||
|
||
core, logs := observer.New(zapcore.InfoLevel) | ||
|
||
const threshold = 2 | ||
|
||
core = logging.NewControllerErrorSuppressor(core, threshold) | ||
|
||
// warn/info messages are not affected | ||
assertLogged(t, core, logs, []zapcore.Entry{ | ||
{Level: zapcore.InfoLevel, Message: "abc"}, | ||
{Level: zapcore.WarnLevel, Message: "def"}, | ||
{Level: zapcore.DebugLevel, Message: "message"}, // below level | ||
}, 2) | ||
|
||
// different controllers, suppress counters are independent | ||
controllerCore1 := core.With([]zapcore.Field{{Key: "controller", String: "c1"}}) | ||
controllerCore2 := core.With([]zapcore.Field{{Key: "controller", String: "c2"}}) | ||
|
||
assertLogged(t, controllerCore1, logs, []zapcore.Entry{ | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, // suppressed | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, // suppressed | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, | ||
}, 1) | ||
|
||
assertLogged(t, controllerCore2, logs, []zapcore.Entry{ | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, // suppressed | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, // suppressed | ||
}, 0) | ||
|
||
assertLogged(t, controllerCore1, logs, []zapcore.Entry{ | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, // not suppressed, over threshold | ||
}, 1) | ||
|
||
assertLogged(t, controllerCore1.With([]zapcore.Field{{Key: "foo", String: "bar"}}), logs, []zapcore.Entry{ | ||
{Level: zapcore.ErrorLevel, Message: "controller failed"}, // .With() without 'controller' field keeps the counter value from the parent | ||
}, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters