Skip to content

Commit 8d8f990

Browse files
authored
Truncate logs to last 150KB (#47)
1 parent 6487ab8 commit 8d8f990

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/main/java/io/jenkins/plugins/pipelinegraphview/consoleview/PipelineConsoleViewAction.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package io.jenkins.plugins.pipelinegraphview.consoleview;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import hudson.console.AnnotatedLargeText;
45
import io.jenkins.plugins.pipelinegraphview.utils.AbstractPipelineViewAction;
56
import io.jenkins.plugins.pipelinegraphview.utils.PipelineStepApi;
67
import io.jenkins.plugins.pipelinegraphview.utils.PipelineStepList;
7-
import java.io.ByteArrayOutputStream;
88
import java.io.IOException;
9+
import java.io.Writer;
910
import org.jenkinsci.plugins.workflow.actions.LogAction;
1011
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
1112
import org.jenkinsci.plugins.workflow.graph.FlowNode;
1213
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
1314
import org.kohsuke.stapler.StaplerRequest;
1415
import org.kohsuke.stapler.StaplerResponse;
1516
import org.kohsuke.stapler.WebMethod;
17+
import org.kohsuke.stapler.framework.io.CharSpool;
18+
import org.kohsuke.stapler.framework.io.LineEndNormalizingWriter;
1619
import org.slf4j.Logger;
1720
import org.slf4j.LoggerFactory;
1821

1922
public class PipelineConsoleViewAction extends AbstractPipelineViewAction {
23+
public static final long LOG_THRESHOLD = 150 * 1024; // 150KB
24+
2025
private static final Logger logger = LoggerFactory.getLogger(PipelineConsoleViewAction.class);
2126
private final WorkflowRun target;
2227
private final PipelineStepApi stepApi;
@@ -68,19 +73,50 @@ public void getConsoleOutput(StaplerRequest req, StaplerResponse rsp) throws IOE
6873
String nodeId = req.getParameter("nodeId");
6974
if (nodeId != null) {
7075
logger.debug("getConsoleOutput was passed node id '" + nodeId + "'.");
71-
String nodeConsoleText = getLogForNode(nodeId);
72-
if (nodeConsoleText != null) {
73-
rsp.getWriter().append(nodeConsoleText);
76+
CharSpool spool = new CharSpool();
77+
AnnotatedLargeText<? extends FlowNode> logText = getLogForNode(nodeId);
78+
79+
if (logText != null) {
80+
long offset;
81+
if (logText.length() > LOG_THRESHOLD) {
82+
offset = logText.length() - LOG_THRESHOLD;
83+
} else {
84+
offset = 0;
85+
}
86+
87+
long receivedBytes = logText.writeLogTo(offset, spool);
88+
logger.debug("Received " + receivedBytes + " of console output.");
89+
90+
Writer writer = createWriter(req, rsp, logText.length());
91+
92+
if (offset > 0) {
93+
writer
94+
.append(
95+
"Output is truncated for performance, only showing the last 150KB of logs for this step...")
96+
.append("\n");
97+
}
98+
99+
spool.writeTo(new LineEndNormalizingWriter(writer));
74100
} else {
75101
rsp.getWriter().append("No console output for node: ").append(nodeId);
76102
}
77103
} else {
78-
logger.debug("getConsoleOutput was ot passed nodeId.");
104+
logger.debug("getConsoleOutput was not passed nodeId.");
79105
rsp.getWriter().append("Error getting console text");
80106
}
81107
}
82108

83-
private String getLogForNode(String nodeId) throws IOException {
109+
private Writer createWriter(StaplerRequest req, StaplerResponse rsp, long size)
110+
throws IOException {
111+
// when sending big text, try compression. don't bother if it's small
112+
if (size > 4096) {
113+
return rsp.getCompressedWriter(req);
114+
} else {
115+
return rsp.getWriter();
116+
}
117+
}
118+
119+
private AnnotatedLargeText<? extends FlowNode> getLogForNode(String nodeId) throws IOException {
84120
FlowExecution execution = target.getExecution();
85121
if (execution != null) {
86122
logger.debug("getConsoleOutput found execution.");
@@ -89,11 +125,7 @@ private String getLogForNode(String nodeId) throws IOException {
89125
logger.debug("getConsoleOutput found node.");
90126
LogAction log = node.getAction(LogAction.class);
91127
if (log != null) {
92-
ByteArrayOutputStream oututStream = new ByteArrayOutputStream();
93-
Long receivedBytes = log.getLogText().writeLogTo(0, oututStream);
94-
logger.debug("Received " + receivedBytes + " of console output.");
95-
// Assuming logs are is UFT-8. This seems to be what LogStorage does.
96-
return oututStream.toString("UTF-8").trim();
128+
return log.getLogText();
97129
}
98130
}
99131
}

0 commit comments

Comments
 (0)