From 40e63546cbc9f31fe7995db0f680726b27f2aa48 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Sat, 5 Dec 2020 16:55:19 -0800 Subject: [PATCH] Limit number of log entries returned. Closes #423 --- controllers/admin/logs.go | 5 ++++- logging/logging.go | 17 +++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/controllers/admin/logs.go b/controllers/admin/logs.go index b7657111b..bd16051bc 100644 --- a/controllers/admin/logs.go +++ b/controllers/admin/logs.go @@ -32,7 +32,10 @@ func GetWarnings(w http.ResponseWriter, r *http.Request) { response := make([]logsResponse, 0) for i := 0; i < len(logs); i++ { - response = append(response, fromEntry(logs[i])) + logEntry := logs[i] + if logEntry != nil { + response = append(response, fromEntry(logEntry)) + } } w.Header().Set("Content-Type", "application/json") diff --git a/logging/logging.go b/logging/logging.go index 5dfe05abf..2d1ccdc0b 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -4,6 +4,7 @@ package logging // Modeled after https://github.com/sirupsen/logrus/blob/master/hooks/test/test.go import ( + "math" "os" "sync" @@ -53,11 +54,13 @@ func (l *OCLogger) Levels() []logrus.Level { func (l *OCLogger) AllEntries() []*logrus.Entry { l.mu.RLock() defer l.mu.RUnlock() + // Make a copy so the returned value won't race with future log requests - entries := make([]*logrus.Entry, len(l.Entries)) - for i := 0; i < len(l.Entries); i++ { + logCount := int(math.Min(float64(len(l.Entries)), 800.0)) + entries := make([]*logrus.Entry, logCount) + for i := 0; i < len(entries); i++ { // Make a copy, for safety - entries[i] = &l.Entries[i] + entries[len(entries)-logCount:][i] = &l.Entries[i] } return entries @@ -67,12 +70,14 @@ func (l *OCLogger) AllEntries() []*logrus.Entry { func (l *OCLogger) WarningEntries() []*logrus.Entry { l.mu.RLock() defer l.mu.RUnlock() + // Make a copy so the returned value won't race with future log requests - entries := make([]*logrus.Entry, 0) - for i := 0; i < len(l.Entries); i++ { + logCount := int(math.Min(float64(len(l.Entries)), 100.0)) + entries := make([]*logrus.Entry, logCount) + for i := 0; i < len(entries); i++ { if l.Entries[i].Level <= logrus.WarnLevel { // Make a copy, for safety - entries = append(entries, &l.Entries[i]) + entries[len(entries)-logCount:][i] = &l.Entries[i] } }