This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch sl in repository https://gitbox.apache.org/repos/asf/camel.git
commit bdfea5bc8c64fcf3cf4c0642006601838ce2a259 Author: Claus Ibsen <[email protected]> AuthorDate: Mon Jun 10 15:18:25 2024 +0200 CAMEL-20842: Add endpoint service location to backlog tracing --- .../camel/spi/BacklogTracerEventMessage.java | 26 ++++++++++ .../apache/camel/spi/EndpointServiceLocation.java | 3 +- .../debugger/DefaultBacklogTracerEventMessage.java | 57 ++++++++++++++++++++++ .../camel/impl/engine/CamelInternalProcessor.java | 11 +++++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/BacklogTracerEventMessage.java b/core/camel-api/src/main/java/org/apache/camel/spi/BacklogTracerEventMessage.java index d8ef6204bee..bf2e88adee9 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/BacklogTracerEventMessage.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/BacklogTracerEventMessage.java @@ -137,6 +137,32 @@ public interface BacklogTracerEventMessage { */ String getEndpointUri(); + /** + * Gets the endpoint remote address such as URL, hostname, connection-string, or cloud region, that are component + * specific. + * + * @return the address or null if no address can be determined. + * @see EndpointServiceLocation + */ + String getEndpointServiceUrl(); + + /** + * Get the endpoint protocol the service is using such as http, amqp, tcp. + * + * @see EndpointServiceLocation + */ + String getEndpointServiceProtocol(); + + /** + * Optional endpoint metadata that is relevant to the service as key value pairs. Notice that the metadata is not + * supposed to contain sensitive security details such as access token, api keys, or passwords. Only share + * information that can be safely accessed and written to logs. + * + * @return optional metadata or null if no data + * @see EndpointServiceLocation + */ + Map<String, String> getEndpointServiceMetadata(); + /** * Dumps the event message as XML using the {@link #ROOT_TAG} as root tag. * <p/> diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/EndpointServiceLocation.java b/core/camel-api/src/main/java/org/apache/camel/spi/EndpointServiceLocation.java index 3dfb0c9efde..1a32da721f9 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/EndpointServiceLocation.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/EndpointServiceLocation.java @@ -32,8 +32,7 @@ import java.util.Map; public interface EndpointServiceLocation { /** - * Gets the remote address such as URL, hostname, connection-string, - * or cloud region, that are component specific. + * Gets the remote address such as URL, hostname, connection-string, or cloud region, that are component specific. * * @return the address or null if no address can be determined. */ diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogTracerEventMessage.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogTracerEventMessage.java index 9fec3c74143..5db7cc4a226 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogTracerEventMessage.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/DefaultBacklogTracerEventMessage.java @@ -41,6 +41,9 @@ public final class DefaultBacklogTracerEventMessage implements BacklogTracerEven private final String exchangeId; private final String threadName; private String endpointUri; + private String endpointServiceUrl; + private String endpointServiceProtocol; + private Map<String, String> endpointServiceMetadata; private final boolean rest; private final boolean template; private final String messageAsXml; @@ -194,6 +197,30 @@ public final class DefaultBacklogTracerEventMessage implements BacklogTracerEven this.endpointUri = endpointUri; } + public String getEndpointServiceUrl() { + return endpointServiceUrl; + } + + public void setEndpointServiceUrl(String endpointServiceUrl) { + this.endpointServiceUrl = endpointServiceUrl; + } + + public String getEndpointServiceProtocol() { + return endpointServiceProtocol; + } + + public void setEndpointServiceProtocol(String endpointServiceProtocol) { + this.endpointServiceProtocol = endpointServiceProtocol; + } + + public Map<String, String> getEndpointServiceMetadata() { + return endpointServiceMetadata; + } + + public void setEndpointServiceMetadata(Map<String, String> endpointServiceMetadata) { + this.endpointServiceMetadata = endpointServiceMetadata; + } + @Override public String toString() { return "DefaultBacklogTracerEventMessage[" + exchangeId + " at " + toNode + "]"; @@ -239,6 +266,22 @@ public final class DefaultBacklogTracerEventMessage implements BacklogTracerEven sb.append(prefix).append(" <toNode>").append(routeId).append("</toNode>\n"); } sb.append(prefix).append(" <exchangeId>").append(exchangeId).append("</exchangeId>\n"); + if (endpointServiceUrl != null) { + sb.append(prefix).append(" <endpointService>\n"); + sb.append(prefix).append(" <serviceUrl>").append(endpointServiceUrl).append("</serviceUrl>\n"); + if (endpointServiceProtocol != null) { + sb.append(prefix).append(" <serviceProtocol>").append(endpointServiceProtocol) + .append("</serviceProtocol>\n"); + } + if (endpointServiceMetadata != null) { + sb.append(prefix).append(" <serviceMetadata>\n"); + endpointServiceMetadata.forEach((k, v) -> { + sb.append(prefix).append(" <").append(k).append(">").append(v).append("</").append(k).append(">\n"); + }); + sb.append(prefix).append(" </serviceMetadata>\n"); + } + sb.append(prefix).append(" </endpointService>\n"); + } sb.append(prefix).append(messageAsXml).append("\n"); if (exceptionAsXml != null) { sb.append(prefix).append(exceptionAsXml).append("\n"); @@ -277,6 +320,9 @@ public final class DefaultBacklogTracerEventMessage implements BacklogTracerEven if (toNode != null) { jo.put("nodeId", toNode); } + if (exchangeId != null) { + jo.put("exchangeId", exchangeId); + } if (timestamp > 0) { jo.put("timestamp", timestamp); } @@ -284,6 +330,17 @@ public final class DefaultBacklogTracerEventMessage implements BacklogTracerEven jo.put("threadName", getProcessingThreadName()); jo.put("done", isDone()); jo.put("failed", isFailed()); + if (endpointServiceUrl != null) { + JsonObject es = new JsonObject(); + es.put("serviceUrl", endpointServiceUrl); + if (endpointServiceProtocol != null) { + es.put("serviceProtocol", endpointServiceProtocol); + } + if (endpointServiceMetadata != null) { + es.put("serviceMetadata", endpointServiceMetadata); + } + jo.put("endpointService", es); + } try { // parse back to json object and avoid double message root JsonObject msg = (JsonObject) Jsoner.deserialize(messageAsJSon); diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java index 56a061be2e8..235fd733b83 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java @@ -45,6 +45,7 @@ import org.apache.camel.spi.BacklogDebugger; import org.apache.camel.spi.CamelEvent; import org.apache.camel.spi.CamelInternalProcessorAdvice; import org.apache.camel.spi.Debugger; +import org.apache.camel.spi.EndpointServiceLocation; import org.apache.camel.spi.EventNotifier; import org.apache.camel.spi.InflightRepository; import org.apache.camel.spi.InternalProcessor; @@ -646,6 +647,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor implements In DefaultBacklogTracerEventMessage pseudoFirst = new DefaultBacklogTracerEventMessage( true, false, backlogTracer.incrementTraceCounter(), created, source, routeId, null, exchangeId, rest, template, messageAsXml, messageAsJSon); + if (exchange.getFromEndpoint() instanceof EndpointServiceLocation esl) { + pseudoFirst.setEndpointServiceUrl(esl.getServiceUrl()); + pseudoFirst.setEndpointServiceProtocol(esl.getServiceProtocol()); + pseudoFirst.setEndpointServiceMetadata(esl.getServiceMetadata()); + } backlogTracer.traceEvent(pseudoFirst); exchange.getExchangeExtension().addOnCompletion(createOnCompletion(source, pseudoFirst)); } @@ -720,6 +726,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor implements In if (uri != null) { data.setEndpointUri(uri); } + if (endpoint instanceof EndpointServiceLocation esl) { + data.setEndpointServiceUrl(esl.getServiceUrl()); + data.setEndpointServiceProtocol(esl.getServiceProtocol()); + data.setEndpointServiceMetadata(esl.getServiceMetadata()); + } if (!data.isFirst() && backlogTracer.isIncludeException()) { // we want to capture if there was an exception
