Skip to content

Commit f671c7e

Browse files
committed
update debugserver http handler and gorouter SetMinLevel
1 parent e9dc698 commit f671c7e

File tree

3 files changed

+116
-13
lines changed

3 files changed

+116
-13
lines changed

src/code.cloudfoundry.org/gorouter/cmd/gorouter/main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ var (
5656
// It implements the lager.Sink interface, allowing it to be used with
5757
// lager's logging system. The Log method logs the message and source
5858
// using the slog.Logger, and the LogLevel method returns the current
59-
// logging level.
59+
// logging level.
6060

6161
type zapLevelSink struct {
6262
logger *slog.Logger
6363
}
6464

65-
func (s *zapLevelSink) Log(lf lager.LogFormat) {
66-
s.logger.Info("zapLevelSink Log()", slog.String("Zap Level Sink log invoked", lf.Message),
65+
func (z *zapLevelSink) Log(lf lager.LogFormat) {
66+
z.logger.Info("zapLevelSink Log()", slog.String("Zap Level Sink log invoked", lf.Message),
6767
slog.String("source", lf.Source))
6868
}
6969

@@ -88,23 +88,34 @@ func (z *zapLevelSink) LogLevel() lager.LogLevel {
8888
// before using this sink, and it will log an info message when the
8989
// logging level is updated.
9090

91-
func (s *zapLevelSink) SetMinLevel(level lager.LogLevel) {
91+
func (z *zapLevelSink) SetMinLevel(level lager.LogLevel) {
9292
var zapLevel zapcore.Level
9393
switch level {
9494
case lager.DEBUG:
9595
zapLevel = zapcore.DebugLevel
9696
case lager.INFO:
9797
zapLevel = zapcore.InfoLevel
9898
case lager.ERROR, lager.FATAL:
99+
// For lager.ERROR and lager.FATAL, we use zapcore.ErrorLevel.
100+
// This is because zapcore.ErrorLevel captures both error and fatal logs.
101+
// Using zapcore.FatalLevel would cause the application to exit immediately.
102+
// Because zapcore.FatalLevel calls os.Exit(1) after logging the message.
99103
zapLevel = zapcore.ErrorLevel
100104
default:
101105
zapLevel = zapcore.InfoLevel
102106
}
103107

104108
grlog.SetLoggingLevel(zapLevel.String())
105109

106-
if s.logger != nil {
107-
s.logger.Info("zapcore log level updated", slog.String("new-level", zapLevel.String()))
110+
// Print the new log level to the logger to confirm the change.
111+
// This is useful for debugging and confirming that the log level has been updated.
112+
if z.logger != nil {
113+
// We cannot use z.logger.Info() directly here because it won't print the
114+
// log level when it is set to zapcore.ErrorLevel or zapcore.FatalLevel.
115+
// Instead, we use slog.New() to log the message. This ensures that the
116+
// log level change is always logged, regardless of the current log level.
117+
tmpLogger := slog.New(slog.NewTextHandler(os.Stdout, nil))
118+
tmpLogger.Info("Gorouter logger -> zapcore log level updated.", slog.String("new-level", z.LogLevel().String()))
108119
}
109120
}
110121

src/code.cloudfoundry.org/gorouter/integration/main_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package integration
22

33
import (
44
"bufio"
5+
"bytes"
56
"crypto/tls"
67
"encoding/json"
78
"errors"
@@ -618,6 +619,78 @@ var _ = Describe("Router Integration", func() {
618619
Consistently(contentsFunc).ShouldNot(ContainSubstring("Component Router registered successfully"))
619620
})
620621

622+
Context("It starts up a debugserver", func() {
623+
var (
624+
testState *testState
625+
contentsFunc func() string = func() string {
626+
return string(gorouterSession.Out.Contents())
627+
}
628+
)
629+
630+
BeforeEach(func() {
631+
632+
testState = NewTestState()
633+
testState.cfg.DebugAddr = "127.0.0.1:17017"
634+
gorouterSession = testState.StartGorouter()
635+
})
636+
637+
It("can change the debugserver's logging level", func() {
638+
639+
Consistently(contentsFunc).ShouldNot(ContainSubstring(`{log_level":0,"timestamp"`))
640+
641+
request, err := http.NewRequest("POST", fmt.Sprintf("http://%s/log-level", testState.cfg.DebugAddr), bytes.NewBufferString("debug"))
642+
Expect(err).NotTo(HaveOccurred())
643+
644+
response, err := http.DefaultClient.Do(request)
645+
Expect(err).NotTo(HaveOccurred())
646+
647+
Expect(response.StatusCode).To(Equal(http.StatusOK))
648+
response.Body.Close()
649+
650+
Consistently(contentsFunc).Should(ContainSubstring(`{"log_level":0,"timestamp"`))
651+
652+
// And back to info level
653+
gorouterSession.Out.Clear()
654+
request, err = http.NewRequest("POST", fmt.Sprintf("http://%s/log-level", testState.cfg.DebugAddr), bytes.NewBufferString("info"))
655+
Expect(err).NotTo(HaveOccurred())
656+
657+
response, err = http.DefaultClient.Do(request)
658+
Expect(err).NotTo(HaveOccurred())
659+
660+
Expect(response.StatusCode).To(Equal(http.StatusOK))
661+
response.Body.Close()
662+
663+
//Terminate everything just to generate some info logs
664+
testState.StopAndCleanup()
665+
666+
Consistently(contentsFunc).ShouldNot(ContainSubstring(`{"log_level":0,"timestamp"`))
667+
Eventually(contentsFunc).Should(ContainSubstring(`{"log_level":1,"timestamp"`))
668+
669+
})
670+
671+
It("Does not accept invalid debug levels", func() {
672+
673+
Consistently(contentsFunc).ShouldNot(ContainSubstring(`{log_level":0,"timestamp"`))
674+
675+
gorouterSession.Out.Clear()
676+
677+
request, err := http.NewRequest("POST", fmt.Sprintf("http://%s/log-level", testState.cfg.DebugAddr), bytes.NewBufferString("meow"))
678+
Expect(err).NotTo(HaveOccurred())
679+
680+
response, err := http.DefaultClient.Do(request)
681+
Expect(err).NotTo(HaveOccurred())
682+
683+
Expect(response.StatusCode).ToNot(Equal(http.StatusOK))
684+
response.Body.Close()
685+
686+
Expect(gorouterSession.ExitCode()).To(Equal(-1))
687+
688+
Consistently(contentsFunc).ShouldNot(ContainSubstring(`{"log_level":0,"timestamp"`))
689+
Eventually(contentsFunc).Should(ContainSubstring(`{"log_level":1,"timestamp"`))
690+
})
691+
692+
})
693+
621694
Describe("loggregator metrics emitted", func() {
622695
var (
623696
fakeMetron test_util.FakeMetron

src/code.cloudfoundry.org/vendor/code.cloudfoundry.org/debugserver/server.go

Lines changed: 26 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)