History

fix(api): skip access logging for health probes

chaoshou committed 2 months ago

Commit e026cbf1 parent 20e3f732 files · +46 −2Browse files

Unified · Split

2 files
  1. internal/api/server_routes.go +2 −2
  2. internal/api/server_test.go +44 −0
internal/api/server_routes.go+2−2
@@ -48,8 +48,8 @@ func (s *Server) setupRoutes() { · 47 unmodified lines
48 48
49 c.JSON(http.StatusOK, gin.H{"status": "ok"})49 c.JSON(http.StatusOK, gin.H{"status": "ok"})
50 }50 }
51 s.engine.GET("/healthz", healthzHandler)51 s.engine.GET("/healthz", logging.SkipGinRequestLogging, healthzHandler)
52 s.engine.HEAD("/healthz", healthzHandler)52 s.engine.HEAD("/healthz", logging.SkipGinRequestLogging, healthzHandler)
53 53
54 s.engine.GET("/management.html", s.serveManagementControlPanel)54 s.engine.GET("/management.html", s.serveManagementControlPanel)
55 openaiHandlers := openai.NewOpenAIAPIHandler(s.handlers)55 openaiHandlers := openai.NewOpenAIAPIHandler(s.handlers)
internal/api/server_test.go+44−0
@@ -32,6 +32,8 @@ import ( · 31 unmodified lines
32 coreusage "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage"32 coreusage "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage"
33 sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config"33 sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config"
34 "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"34 "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
35 log "github.com/sirupsen/logrus"
36 logtest "github.com/sirupsen/logrus/hooks/test"
35 "gopkg.in/yaml.v3"37 "gopkg.in/yaml.v3"
36)38)
37 39
@@ -666,6 +668,48 @@ func TestHealthz(t *testing.T) { · 628 unmodified lines
666 })668 })
667}669}
668 670
671func TestHealthzSkipsAccessLogging(t *testing.T) {
672 server := newTestServer(t)
673 logger := log.StandardLogger()
674 previousHooks := logger.ReplaceHooks(make(log.LevelHooks))
675 previousLevel := logger.GetLevel()
676 hook := logtest.NewLocal(logger)
677 logger.SetLevel(log.InfoLevel)
678 t.Cleanup(func() {
679 logger.ReplaceHooks(previousHooks)
680 logger.SetLevel(previousLevel)
681 })
682
683 for _, method := range []string{http.MethodGet, http.MethodHead} {
684 t.Run(method, func(t *testing.T) {
685 hook.Reset()
686 recorder := httptest.NewRecorder()
687 server.engine.ServeHTTP(recorder, httptest.NewRequest(method, "/healthz", nil))
688 if recorder.Code != http.StatusOK {
689 t.Fatalf("health probe status = %d, want 200", recorder.Code)
690 }
691 for _, entry := range hook.AllEntries() {
692 if _, isAccessLog := entry.Data["request_id"]; isAccessLog && strings.Contains(entry.Message, `"/healthz"`) {
693 t.Errorf("health probe emitted an access log: %s", entry.Message)
694 }
695 }
696
697 hook.Reset()
698 ordinary := httptest.NewRecorder()
699 server.engine.ServeHTTP(ordinary, httptest.NewRequest(http.MethodGet, "/healthz-access-log-control", nil))
700 if ordinary.Code != http.StatusNotFound {
701 t.Fatalf("control status = %d, want 404", ordinary.Code)
702 }
703 for _, entry := range hook.AllEntries() {
704 if _, isAccessLog := entry.Data["request_id"]; isAccessLog && strings.Contains(entry.Message, `"/healthz-access-log-control"`) {
705 return
706 }
707 }
708 t.Error("ordinary request did not emit an access log after health probe")
709 })
710 }
711}
712
669func TestCodexLiveRoutesRequireAuthAndAreRegistered(t *testing.T) {713func TestCodexLiveRoutesRequireAuthAndAreRegistered(t *testing.T) {
670 server := newTestServer(t)714 server := newTestServer(t)
671 715