Copilot commented on code in PR #1260:
URL:
https://github.com/apache/flink-kubernetes-operator/pull/1260#discussion_r4694136821
##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/JobStatusObserver.java:
##########
@@ -235,35 +235,68 @@ private void emitJobManagerExceptionEvent(
.forEach((k, v) -> annotations.put("failure-label-" + k,
v));
}
- StringBuilder eventMessage = new StringBuilder();
+ var operatorConfig = ctx.getOperatorConfig();
String stacktrace = exception.getStacktrace();
- if (stacktrace != null && !stacktrace.isBlank()) {
- String[] lines = stacktrace.split("\n");
- for (int i = 0; i < Math.min(maxStackTraceLines, lines.length);
i++) {
- eventMessage.append(lines[i]).append("\n");
- }
- if (lines.length > maxStackTraceLines) {
- eventMessage
- .append("... (")
- .append(lines.length - maxStackTraceLines)
- .append(" more lines)");
- }
- }
+ String eventMessage =
+ stacktrace == null || stacktrace.isBlank()
+ ? ""
+ : truncateExceptionText(
+ stacktrace,
+
operatorConfig.getReportedExceptionEventsMaxStackTraceLength(),
+
operatorConfig.getReportedExceptionEventsMaxStackTraceLines());
String identityKey =
- "jobmanager-exception-"
- +
Integer.toHexString(Objects.hash(eventMessage.toString()));
+ "jobmanager-exception-" +
Integer.toHexString(Objects.hash(eventMessage));
eventRecorder.triggerEventWithAnnotations(
ctx.getResource(),
EventRecorder.Type.Warning,
EventRecorder.Reason.JobException,
- eventMessage.toString().trim(),
+ eventMessage.trim(),
EventRecorder.Component.Job,
identityKey,
ctx.getKubernetesClient(),
K8sAnnotationsSanitizer.sanitizeAnnotations(annotations));
}
+ /**
+ * Truncates exception text for an event message and appends the number of
characters left out.
+ * The text is cut to {@code maxLength} characters, so that a single line
of any length stays
+ * bounded, and to {@code maxLines} lines when set.
+ *
+ * @param text the exception text to truncate
+ * @param maxLength the maximum number of characters to keep, negative
values disable the limit
+ * @param maxLines the deprecated line limit, applied in addition to
{@code maxLength} when set;
+ * values of 0 or less keep no text
+ * @return the text, truncated if it exceeds either limit
+ */
+ @VisibleForTesting
+ static String truncateExceptionText(String text, int maxLength, @Nullable
Integer maxLines) {
+ int end = text.length();
+ if (maxLines != null) {
+ end = endOfLines(text, maxLines);
+ }
+ if (maxLength >= 0) {
+ end = Math.min(end, maxLength);
+ }
Review Comment:
The limit is a UTF-16 index and can split a surrogate pair: for example,
truncating `A😀B` at 2 leaves an unpaired high surrogate. The resulting event
text is invalid Unicode and can be corrupted during encoding. Move the boundary
back when it falls inside a surrogate pair.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]