-
Notifications
You must be signed in to change notification settings - Fork 227
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
Fix Gorouter's debugserver endpoint to allow log level changes at runtime #452
Open
MarcPaquette
wants to merge
3
commits into
main
Choose a base branch
from
WIP_debugserver-loglevel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,14 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"code.cloudfoundry.org/lager/v3" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/exp/zapslog" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var ( | ||
conf dynamicLoggingConfig | ||
Conf DynamicLoggingConfig | ||
baseLogger *slog.Logger | ||
writeSyncer = &dynamicWriter{w: os.Stdout} | ||
mutex sync.Mutex | ||
|
@@ -23,7 +24,7 @@ var ( | |
/* | ||
dynamicLoggingConfig holds dynamic configuration for the time encoding and logging level. | ||
*/ | ||
type dynamicLoggingConfig struct { | ||
type DynamicLoggingConfig struct { | ||
encoding string | ||
level zap.AtomicLevel | ||
} | ||
|
@@ -74,10 +75,10 @@ SetTimeEncoder dynamically sets the time encoder at runtime: | |
All other values: The encoder is set to an Epoch encoder | ||
*/ | ||
func SetTimeEncoder(enc string) { | ||
conf.encoding = enc | ||
Conf.encoding = enc | ||
} | ||
|
||
func (e *dynamicLoggingConfig) encodeTime(t time.Time, pae zapcore.PrimitiveArrayEncoder) { | ||
func (e *DynamicLoggingConfig) encodeTime(t time.Time, pae zapcore.PrimitiveArrayEncoder) { | ||
switch e.encoding { | ||
case "rfc3339": | ||
RFC3339Formatter()(t, pae) | ||
|
@@ -91,11 +92,33 @@ SetLoggingLevel dynamically sets the logging level at runtime. See https://githu | |
for possible logging levels. | ||
*/ | ||
func SetLoggingLevel(level string) { | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
Comment on lines
+95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ameowlia |
||
zapLevel, err := zapcore.ParseLevel(level) | ||
if err != nil { | ||
panic(err) | ||
} | ||
conf.level.SetLevel(zapLevel) | ||
Conf.level.SetLevel(zapLevel) | ||
} | ||
|
||
// This exists to be able to export the logging level configs to the debugserver | ||
func (loggingConf DynamicLoggingConfig) SetMinLevel(level lager.LogLevel) { | ||
Conf.level.SetLevel(toZapLevel(level)) | ||
} | ||
|
||
func toZapLevel(level lager.LogLevel) zapcore.Level { | ||
switch level { | ||
case lager.DEBUG: | ||
return zapcore.DebugLevel | ||
case lager.INFO: | ||
return zapcore.InfoLevel | ||
MarcPaquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case lager.ERROR: | ||
return zapcore.ErrorLevel | ||
case lager.FATAL: | ||
return zapcore.FatalLevel | ||
default: | ||
return zapcore.InfoLevel | ||
} | ||
} | ||
|
||
type Logger interface { | ||
|
@@ -108,22 +131,22 @@ timestamp format and writeSyncer. | |
func initializeLogger() *slog.Logger { | ||
zapLevel := zap.InfoLevel | ||
|
||
conf = dynamicLoggingConfig{encoding: "epoch", level: zap.NewAtomicLevelAt(zapLevel)} | ||
Conf = DynamicLoggingConfig{encoding: "epoch", level: zap.NewAtomicLevelAt(zapLevel)} | ||
|
||
zapConfig := zapcore.EncoderConfig{ | ||
MessageKey: "message", | ||
LevelKey: "log_level", | ||
EncodeLevel: numberLevelFormatter, | ||
TimeKey: "timestamp", | ||
EncodeTime: conf.encodeTime, | ||
EncodeTime: Conf.encodeTime, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
StacktraceKey: "stack_trace", | ||
} | ||
|
||
zapCore := zapcore.NewCore( | ||
zapcore.NewJSONEncoder(zapConfig), | ||
writeSyncer, | ||
conf.level, | ||
Conf.level, | ||
) | ||
|
||
zapHandler := zapslog.NewHandler(zapCore, zapslog.WithCaller(true)) | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this exported now? I assume for the change in main.go?
exposing the variable means anyone anywhere can change it. what about adding a getter function instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the satisfy the debugserver's reconfigurable sink: https://github.com/cloudfoundry/debugserver/blob/main/server.go#L24-L26
The base gorouter logger is inited at the package level and privately puts us in a bit of an odd spot with further changes to the logger. I exported the config as we need the
debugserver
to actually change the config viaSetMinLevel()
Basically since the
grlogger.baseLogger
is initialized on importation viainit()
and private to the package, I pushed the config out as public as we need something to satisfy theReconfigurableSinkInterface
. A getter doesn't quite fit the pattern. There is an argument for pushing it further down, but this was a bit simpler.I went with the config as it's the area where adding the method to satisfy the
ReconfigurableSinkInterface
was the least intrusive.