gnodet-bot commented on code in PR #28806:
URL: https://github.com/apache/camel/pull/28806#discussion_r4437987596


##########
components/camel-telemetry/src/main/java/org/apache/camel/telemetry/TraceProcessorsInterceptStrategy.java:
##########
@@ -74,25 +74,31 @@ public void process(Exchange exchange) throws Exception {
         @Override
         public boolean process(Exchange exchange, AsyncCallback callback) {
             String processorName = processorDefinition.getId() + "-" + 
processorDefinition.getShortName();
-            boolean trace = shouldTrace(processorName, exchange);
-            if (trace) {
+            boolean started = false;
+            if (shouldTrace(processorName, exchange)) {
                 try {
                     tracer.beginProcessorSpan(exchange, processorName);
+                    started = true;
                 } catch (Exception e) {
                     exchange.setException(e);
                 }
             }
+            // only end the span when it was begun, as ending pulls the span 
on top of the exchange's span stack
+            final boolean end = started;
             return processor.process(exchange, doneSync -> {
                 try {
-                    callback.done(doneSync);
-                } finally {
-                    if (trace) {
+                    if (end) {
+                        // end the span before the route continues: when the 
processor completed on another thread (such
+                        // as an asynchronous delay), callback.done runs the 
next steps of the route inside this call,
+                        // and they must not get this span as their parent
                         try {
                             tracer.endProcessorSpan(exchange, processorName);
                         } catch (Exception e) {
                             exchange.setException(e);

Review Comment:
   ⚠️ **Minor:** `exchange.setException(e)` unconditionally replaces any 
existing exception on the exchange. Before this reordering, `endProcessorSpan` 
ran *after* `callback.done` (which triggers the next route steps), so any 
business exception was already propagated. Now it runs *before*, meaning a 
span-close failure could silently mask a business exception that was already 
set.
   
   Consider either logging the failure instead, or preserving the original:
   
   ```suggestion
                           } catch (Exception e) {
                               if (exchange.getException() == null) {
                                   exchange.setException(e);
                               } else {
                                   LOG.warn("Failed to end processor span for 
'{}': {}", processorName, e.getMessage(), e);
                               }
   ```



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