-
Notifications
You must be signed in to change notification settings - Fork 50
/
OverallLog.java
246 lines (229 loc) · 9.3 KB
/
OverallLog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.job.log;
import hudson.Main;
import hudson.console.AnnotatedLargeText;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.context.Scope;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.framework.io.ByteBuffer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
public class OverallLog extends AnnotatedLargeText<FlowExecutionOwner.Executable> {
private final static Logger logger = Logger.getLogger(OverallLog.class.getName());
private final FlowExecutionOwner.Executable context;
private final transient Tracer tracer;
private final LogsViewHeader logsViewHeader;
public OverallLog(ByteBuffer memory, LogsViewHeader logsViewHeader, Charset charset, boolean completed, FlowExecutionOwner.Executable context, Tracer tracer) {
super(memory, charset, completed, context);
this.logsViewHeader = logsViewHeader;
this.context = context;
this.tracer = tracer;
}
/**
* Invoked by `/job/:jobFullName/:runNumber/logText/progressiveHtml
*/
@Override
public void doProgressiveHtml(StaplerRequest req, StaplerResponse rsp) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.doProgressiveHtml")
.startSpan();
try (Scope scope = span.makeCurrent()) {
String start = req.getParameter("start");
if (start != null && !start.isEmpty()) {
span.setAttribute("start", start);
}
super.doProgressiveHtml(req, rsp);
String xTextSize = rsp.getHeader("X-Text-Size");
if (xTextSize != null) {
span.setAttribute("response.textSize", Long.parseLong(xTextSize));
}
String xMoreData = rsp.getHeader("X-More-Data");
if (xMoreData != null) {
span.setAttribute("response.moreData", Boolean.parseBoolean(xMoreData));
}
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
@Override
public void doProgressiveText(StaplerRequest req, StaplerResponse rsp) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.doProgressiveText")
.startSpan();
try (Scope scope = span.makeCurrent()) {
String start = req.getParameter("start");
if (start != null && !start.isEmpty()) {
span.setAttribute("start", start);
}
super.doProgressiveText(req, rsp);
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
@Override
public long writeLogTo(long start, Writer w) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.writeLogTo(writer)")
.setAttribute("start", start)
.startSpan();
try (Scope scope = span.makeCurrent()) {
long length = super.writeLogTo(start, w);
span.setAttribute("response.lengthInBytes", length);
return length;
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
/**
* Called by `/job/:jobFullName/:runNumber/consoleText` or
* `/blue/rest/organizations/:organization/pipelines/:pipeline/branches/:branch/runs/:runNumber/log?start=0`
* with `complete=true`
*/
@Override
public long writeLogTo(long start, OutputStream out) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.writeLogTo(outputStream)")
.setAttribute("start", start)
.startSpan();
try (Scope scope = span.makeCurrent()) {
long length = super.writeLogTo(start, out);
span.setAttribute("response.lengthInBytes", length);
return length;
} finally {
span.end();
}
}
/**
* Invoked by:
* * /job/:jobFullName/:runNumber/console
* * {@link org.jenkinsci.plugins.workflow.job.WorkflowRun#getLogInputStream()}
*/
@Override
public long writeRawLogTo(long start, OutputStream out) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.writeRawLogTo")
.setAttribute("start", start)
.startSpan();
try (Scope scope = span.makeCurrent()) {
long length = super.writeRawLogTo(start, out);
span.setAttribute("response.length", length);
return length;
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
/**
* Inspired by io.jenkins.plugins.pipeline_cloudwatch_logs.CloudWatchRetriever.OverallLog#writeHtmlTo
*/
@Override
public long writeHtmlTo(long start, Writer w) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.writeHtmlTo")
.setAttribute("start", start)
.startSpan();
try (Scope scope = span.makeCurrent()) {
// HEADER
if (start == 0 && !Main.isUnitTest) { // would mess up unit tests
// don't increment the outputted length with the header length
// because the outputted length is used by the logs streaming ajax call to reposition on the log stream
logsViewHeader.writeHeader(w, context, charset);
w.write("\n\n"); // TODO increment length
}
// LOG LINES
long logLinesLengthInBytes = super.writeHtmlTo(start, w);
span.setAttribute("response.lengthBytes", logLinesLengthInBytes);
return logLinesLengthInBytes;
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
@Override
public Reader readAll() throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.readAll")
.startSpan();
try (Scope scope = span.makeCurrent()) {
return super.readAll();
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
@Override
public void doProgressText(StaplerRequest req, StaplerResponse rsp) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.doProgressText")
.startSpan();
try (Scope scope = span.makeCurrent()) {
String start = req.getParameter("start");
if (start != null && !start.isEmpty()) {
span.setAttribute("request.start", start);
}
super.doProgressText(req, rsp);
String xTextSize = rsp.getHeader("X-Text-Size");
if (xTextSize != null) {
span.setAttribute("response.textSize", Long.parseLong(xTextSize));
}
String xMoreData = rsp.getHeader("X-More-Data");
if (xMoreData != null) {
span.setAttribute("response.moreData", Boolean.parseBoolean(xMoreData));
}
} catch (IOException e) {
span.recordException(e);
throw e;
} finally {
span.end();
}
}
@Override
protected Writer createWriter(StaplerRequest req, StaplerResponse rsp, long size) throws IOException {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.createWriter")
.startSpan();
try (Scope scope = span.makeCurrent()) {
return super.createWriter(req, rsp, size);
} finally {
span.end();
}
}
@Override
public void markAsComplete() {
Tracer tracer = logger.isLoggable(Level.FINE) ? this.tracer : TracerProvider.noop().get("noop");
Span span = tracer.spanBuilder("OverallLog.markAsComplete")
.startSpan();
try (Scope scope = span.makeCurrent()) {
super.markAsComplete();
} finally {
span.end();
}
}
}