Skip to content

Commit

Permalink
truncate output
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
Pavel Okhlopkov committed Oct 25, 2024
1 parent dfab60f commit c31a625
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (pj *proxyJSONLogger) Write(p []byte) (int, error) {
return len(p), nil
}

// join all parts of json
pj.buf = append(pj.buf, p...)

var line interface{}
Expand All @@ -108,11 +109,21 @@ func (pj *proxyJSONLogger) Write(p []byte) (int, error) {
logMap[k] = v
}

logLine, _ := json.Marshal(logMap)
logLineRaw, _ := json.Marshal(logMap)

logLine := string(logLineRaw)

if len(logLine) > 10000 {
logLine = fmt.Sprintf("%s:truncated", string(logLine[:10000]))
}

truncetedLog, _ := json.Marshal(map[string]string{
"truncated": logLine,
})

logEntry := pj.WithField(app.ProxyJsonLogKey, true)

logEntry.Log(log.FatalLevel, string(logLine))
logEntry.Log(log.FatalLevel, string(truncetedLog))

return len(p), nil
}
Expand Down Expand Up @@ -144,6 +155,10 @@ func (pj *proxyJSONLogger) writerScanner(p []byte) {
continue
}

if len(str) > 10000 {
str = fmt.Sprintf("%s:truncated", str[:10000])
}

pj.Entry.Info(str)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestRunAndLogLines(t *testing.T) {
cmd := exec.Command("cat", f.Name())
_, err = RunAndLogLines(cmd, map[string]string{"a": "b"})
assert.NoError(t, err)
assert.Contains(t, buf.String(), `\",\"output\":\"stdout\"}" a=b output=stdout proxyJsonLog=true`)
assert.Contains(t, buf.String(), `:truncated\"}" a=b output=stdout proxyJsonLog=true`)

buf.Reset()
})
Expand Down

0 comments on commit c31a625

Please sign in to comment.