Skip to content

Commit

Permalink
reduce-stacktrace-size-in-logs (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serhii Zakharov authored Sep 18, 2021
1 parent 5430dda commit 86992f1
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 10 deletions.
5 changes: 4 additions & 1 deletion pkg/gatherers/clusterconfig/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ var (
// logMaxLongTailLines sets maximum number of lines of the long log file
logMaxLongTailLines = int64(2000)
// logLinesOffset sets the maximum offset if a stacktrace message was found in the logs
logLinesOffset = 20
logLinesOffset = 20
logStackTraceMaxLines = 40
logStackTraceBeginningLimit = 35
logStackTraceEndLimit = 5

defaultNamespaces = []string{"default", "kube-system", "kube-public", "openshift"}
datahubGroupVersionResource = schema.GroupVersionResource{
Expand Down
45 changes: 37 additions & 8 deletions pkg/gatherers/clusterconfig/operators_pods_and_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/openshift/insights-operator/pkg/record"
"github.com/openshift/insights-operator/pkg/recorder"
"github.com/openshift/insights-operator/pkg/utils"
"github.com/openshift/insights-operator/pkg/utils/check"
"github.com/openshift/insights-operator/pkg/utils/marshal"
)
Expand Down Expand Up @@ -316,18 +317,46 @@ func getContainerLogs(ctx context.Context,

// getLogWithStacktracing search for the first stack trace line and offset it by logLinesOffset
func getLogWithStacktracing(logArray []string) string {
var limit int
for idx := range logArray {
line := logArray[idx]
var stackTraceStart, stackTraceEnd int

for index, line := range logArray {
if found := stackTraceRegex.MatchString(line); found {
limit = idx - logLinesOffset
if limit < 0 {
limit = 0
}
stackTraceStart = index
break
}
}

for i := range logArray {
index := len(logArray) - 1 - i
line := logArray[index]
if found := stackTraceRegex.MatchString(line); found {
stackTraceEnd = index
break
}
}
return strings.Join(logArray[limit:], "\n")

if stackTraceEnd < stackTraceStart {
stackTraceEnd = stackTraceStart
}

stackTraceLen := stackTraceEnd - stackTraceStart

var result string

if stackTraceLen > logStackTraceMaxLines {
// add the beginning of the stacktrace
from := utils.MaxInt(0, stackTraceStart-logLinesOffset)
to := utils.MinInt(len(logArray), from+logStackTraceBeginningLimit)
result = strings.Join(logArray[from:to], "\n")
// add the message
result += fmt.Sprintf("\n... (%v stacktrace lines suppressed) ...\n", stackTraceLen-logStackTraceMaxLines)
// add the end of the stacktrace with all the following logs
result += strings.Join(logArray[utils.MaxInt(0, stackTraceEnd-logStackTraceEndLimit):], "\n")
} else {
result = strings.Join(logArray[utils.MaxInt(0, stackTraceStart-logLinesOffset):], "\n")
}

return result
}

// getContainerLogString fetch the container log from API and return it as String
Expand Down
Loading

0 comments on commit 86992f1

Please sign in to comment.