From 38afc6c34d493e8305abd58d61700899ed34b96c Mon Sep 17 00:00:00 2001 From: Daniel Legt Date: Mon, 1 Jun 2026 12:04:36 +0300 Subject: [PATCH] feat(admin): exclude health check entries from admin logs Filter out automated health check log entries (such as `/health`, `/healthz`, and `/api/v1/health`) from the admin logs view. This reduces noise in the dashboard caused by frequent container health pings. Also added corresponding unit tests to verify the filtering behavior. --- backend/libs/handlers/accounts_test.go | 11 +++++++++++ backend/libs/handlers/admin.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/backend/libs/handlers/accounts_test.go b/backend/libs/handlers/accounts_test.go index 8476fb3..6ce1b33 100644 --- a/backend/libs/handlers/accounts_test.go +++ b/backend/libs/handlers/accounts_test.go @@ -1038,6 +1038,7 @@ func TestAdminLogsAndBansPagesRender(t *testing.T) { lines := strings.Join([]string{ `{"date":"2026-05-31","time":"12:34:56","source":"user-upload","severity":"user_activity","code":2001,"log":"upload response sent","ip":"127.0.0.1","box_id":"box123"}`, `{"date":"2026-05-31","time":"12:35:56","source":"http","severity":"dev","code":200,"log":"http request","remote_addr":"172.30.0.1:48358","box_id":"box456"}`, + `{"date":"2026-05-31","time":"12:36:56","source":"http","severity":"dev","code":200,"log":"http request","method":"GET","path":"/health","ip":"127.0.0.1","user_agent":"Wget"}`, "", }, "\n") if err := os.WriteFile(logPath, []byte(lines), 0o644); err != nil { @@ -1058,6 +1059,16 @@ func TestAdminLogsAndBansPagesRender(t *testing.T) { if strings.Contains(logsBody, "172.30.0.1:48358") { t.Fatalf("AdminLogs rendered remote address with port: %s", logsBody) } + healthRequest := httptest.NewRequest(http.MethodGet, "/admin/logs", nil) + healthRequest.AddCookie(&http.Cookie{Name: userSessionCookieName, Value: adminToken}) + healthResponse := httptest.NewRecorder() + app.AdminLogs(healthResponse, healthRequest) + if healthResponse.Code != http.StatusOK { + t.Fatalf("AdminLogs health status = %d, body = %s", healthResponse.Code, healthResponse.Body.String()) + } + if strings.Contains(healthResponse.Body.String(), "/health") || strings.Contains(healthResponse.Body.String(), "Wget") { + t.Fatalf("AdminLogs rendered container health ping: %s", healthResponse.Body.String()) + } bansRequest := httptest.NewRequest(http.MethodGet, "/admin/bans", nil) bansRequest.AddCookie(&http.Cookie{Name: userSessionCookieName, Value: adminToken}) diff --git a/backend/libs/handlers/admin.go b/backend/libs/handlers/admin.go index 9b25171..2683bf0 100644 --- a/backend/libs/handlers/admin.go +++ b/backend/libs/handlers/admin.go @@ -1731,11 +1731,29 @@ func readLogEntries(file string) ([]adminLogEntry, error) { if err := json.Unmarshal(line, &raw); err != nil { continue } + if isHealthCheckLogEntry(raw) { + continue + } entries = append(entries, logEntryFromMap(raw)) } return entries, scanner.Err() } +func isHealthCheckLogEntry(raw map[string]any) bool { + path := strings.TrimSpace(firstLogString(raw, "path", "route")) + if path == "" { + return false + } + fields := strings.Fields(path) + if len(fields) > 0 { + path = fields[len(fields)-1] + } + if idx := strings.IndexByte(path, '?'); idx >= 0 { + path = path[:idx] + } + return path == "/health" || path == "/healthz" || path == "/api/v1/health" +} + func logEntryFromMap(raw map[string]any) adminLogEntry { entry := adminLogEntry{ Date: logString(raw, "date"),