gyfora commented on code in PR #1260:
URL: 
https://github.com/apache/flink-kubernetes-operator/pull/1260#discussion_r4718615617


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/JobStatusObserver.java:
##########
@@ -231,35 +274,70 @@ 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());
+
+        // The full stack trace tells apart exceptions that only differ in the 
truncated text
         String identityKey =
-                "jobmanager-exception-"
-                        + 
Integer.toHexString(Objects.hash(eventMessage.toString()));
+                "jobmanager-exception-" + 
Integer.toHexString(Objects.hash(stacktrace));
         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 unless that is negative.
+     *
+     * @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}; negative
+     *     values disable the limit and 0 keeps no text
+     * @return the text, truncated if it exceeds either limit
+     */
+    @VisibleForTesting
+    static String truncateExceptionText(String text, int maxLength, int 
maxLines) {

Review Comment:
   We could get rid of a lot of complication if we translate the previous 
maxLines config into a fixed maxLength. These utilities should exist 
somewhere...



-- 
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]

Reply via email to