From c02a8718bb3019d19426dca0d78366fdb7d9bdd7 Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Fri, 14 Oct 2022 15:09:46 -0300 Subject: [PATCH 1/9] W-11859222: A.8.2 Respect semantic conventions of span generation in JMS --- pom.xml | 5 +++ .../jms/internal/operation/JmsConsume.java | 40 +++++++++++++++++-- .../jms/internal/operation/JmsPublish.java | 33 +++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 9059632b..0e5a94ee 100644 --- a/pom.xml +++ b/pom.xml @@ -222,6 +222,11 @@ mule-sdk-compatibility-api ${muleSdkCompatibilityApiVersion} + + org.mule.sdk + mule-sdk-api + ${muleSdkApiVersion} + diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java b/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java index 27f913ba..562673c4 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java @@ -8,6 +8,8 @@ import static org.mule.extensions.jms.internal.common.JmsCommons.EXAMPLE_CONTENT_TYPE; import static org.mule.extensions.jms.internal.common.JmsCommons.EXAMPLE_ENCODING; +import static org.mule.jms.commons.internal.common.JmsCommons.getDestinationType; + import static org.slf4j.LoggerFactory.getLogger; import org.mule.extensions.jms.api.config.ConsumerAckMode; @@ -20,7 +22,6 @@ import org.mule.extensions.jms.internal.connection.session.JmsSessionManager; import org.mule.extensions.jms.internal.metadata.JmsOutputResolver; import org.mule.jms.commons.api.AttributesOutputResolver; -import org.mule.jms.commons.api.message.JmsAttributes; import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; import org.mule.runtime.api.connection.ConnectionException; import org.mule.runtime.api.lifecycle.Disposable; @@ -35,12 +36,15 @@ import org.mule.runtime.extension.api.annotation.param.display.Example; import org.mule.runtime.extension.api.annotation.param.display.Summary; import org.mule.runtime.extension.api.runtime.operation.Result; -import org.mule.runtime.extension.api.runtime.process.CompletionCallback; +import org.mule.runtime.extension.api.runtime.parameter.CorrelationInfo; import org.mule.runtime.extension.api.tx.OperationTransactionalAction; +import org.mule.sdk.compatibility.api.utils.ForwardCompatibilityHelper; +import java.util.Locale; import java.util.concurrent.TimeUnit; import javax.inject.Inject; +import javax.jms.ConnectionMetaData; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; @@ -56,6 +60,7 @@ public final class JmsConsume implements Initialisable, Disposable { private static final Logger LOGGER = getLogger(JmsConsume.class); + private static final String SPAN_OPERATION_NAME = "receive"; @Inject private JmsSessionManager sessionManager; @@ -65,6 +70,9 @@ public final class JmsConsume implements Initialisable, Disposable { private org.mule.jms.commons.internal.operation.JmsConsume jmsConsume; + @Inject + private java.util.Optional forwardCompatibilityHelper; + /** * Operation that allows the user to consume a single {@link Message} from a given {@link Destination}. * @@ -97,8 +105,10 @@ public Result consume(@Config JmsConfig config, defaultValue = "10000") @Summary("Maximum time to wait for a message to arrive before timeout") Long maximumWait, @Optional( defaultValue = "MILLISECONDS") @Example("MILLISECONDS") @Summary("Time unit to be used in the maximumWaitTime configuration") TimeUnit maximumWaitUnit, - OperationTransactionalAction transactionalAction) + OperationTransactionalAction transactionalAction, + CorrelationInfo correlationInfo) throws JmsExtensionException, ConnectionException { + addSpanInfo(connection, destination, consumerType, correlationInfo); return (Result) jmsConsume.consume(config, connection, destination, consumerType, ackMode, selector, contentType, encoding, maximumWait, maximumWaitUnit, transactionalAction); @@ -107,11 +117,33 @@ public Result consume(@Config JmsConfig config, @Override public void initialise() { - jmsConsume = new org.mule.jms.commons.internal.operation.JmsConsume(sessionManager, schedulerService); + jmsConsume = + new org.mule.jms.commons.internal.operation.JmsConsume(sessionManager, schedulerService); } @Override public void dispose() { jmsConsume.dispose(); } + + private void addSpanInfo(JmsTransactionalConnection connection, String destination, + org.mule.jms.commons.api.destination.ConsumerType consumerType, + org.mule.runtime.extension.api.runtime.parameter.CorrelationInfo correlationInfo) { + forwardCompatibilityHelper + .ifPresent(fch -> { + ConnectionMetaData connectionMetaData; + try { + connectionMetaData = connection.get().getMetaData(); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.system", connectionMetaData.getJMSProviderName().toLowerCase(Locale.ROOT)); + } catch (JMSException ignored) { + LOGGER.info("Span connection metadata could not be fetched"); + } + fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("span.kind", "CONSUMER"); + fch.getDistributedTraceContextManager(correlationInfo).setCurrentSpanName(destination + " " + SPAN_OPERATION_NAME); + fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("messaging.destination", destination); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.destination_kind", getDestinationType(consumerType).toLowerCase(Locale.ROOT)); + }); + } } diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java b/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java index 981d4979..47bdca6d 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java @@ -7,6 +7,7 @@ package org.mule.extensions.jms.internal.operation; import static org.mule.extensions.jms.internal.common.JmsCommons.QUEUE; + import static org.slf4j.LoggerFactory.getLogger; import org.mule.extensions.jms.api.config.JmsProducerConfig; @@ -33,9 +34,14 @@ import org.mule.runtime.extension.api.runtime.parameter.OutboundCorrelationStrategy; import org.mule.runtime.extension.api.runtime.process.CompletionCallback; import org.mule.runtime.extension.api.tx.OperationTransactionalAction; +import org.mule.sdk.compatibility.api.utils.ForwardCompatibilityHelper; + +import java.util.Locale; import javax.inject.Inject; +import javax.jms.ConnectionMetaData; import javax.jms.Destination; +import javax.jms.JMSException; import javax.jms.Message; import org.slf4j.Logger; @@ -48,6 +54,7 @@ public final class JmsPublish implements Initialisable, Disposable { private static final Logger LOGGER = getLogger(JmsPublish.class); + private static final String SPAN_OPERATION_NAME = "send"; @Inject private JmsSessionManager jmsSessionManager; @@ -55,6 +62,9 @@ public final class JmsPublish implements Initialisable, Disposable { @Inject private SchedulerService schedulerService; + @Inject + private java.util.Optional forwardCompatibilityHelper; + private org.mule.jms.commons.internal.operation.JmsPublish jmsPublish; /** @@ -85,6 +95,29 @@ public void publish(@Config JmsConfig config, @Connection JmsTransactionalConnec CompletionCallback completionCallback) throws JmsExtensionException { + forwardCompatibilityHelper + .ifPresent(fch -> { + ConnectionMetaData connectionMetaData; + try { + connectionMetaData = connection.get().getMetaData(); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.system", connectionMetaData.getJMSProviderName().toLowerCase(Locale.ROOT)); + } catch (JMSException ignored) { + LOGGER.info("Span connection metadata could not be fetched"); + } + fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("span.kind", "PRODUCER"); + fch.getDistributedTraceContextManager(correlationInfo).setCurrentSpanName(destination + " " + SPAN_OPERATION_NAME); + fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("messaging.destination", destination); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.destination_kind", destinationType.toString().toLowerCase(Locale.ROOT)); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.conversation_id", messageBuilder.getCorrelationId()); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.message_payload_size_bytes", + messageBuilder.getBody().getByteLength().toString()); + fch.getDistributedTraceContextManager(correlationInfo) + .addCurrentSpanAttribute("messaging.message_payload_compressed_size_bytes", messageBuilder.getCorrelationId()); + }); jmsPublish.publish(config, connection, destination, destinationType, messageBuilder, overrides, transactionalAction, sendCorrelationId, correlationInfo, completionCallback); } From a40134a4f4bc9235ce68a476ba72463ed11b54d7 Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Mon, 14 Nov 2022 16:22:37 -0300 Subject: [PATCH 2/9] Refactor and tests --- .../jms/internal/operation/JmsConsume.java | 30 ++--- .../jms/internal/operation/JmsPublish.java | 33 ++---- .../tracing/JmsConsumeSpanCustomizer.java | 40 +++++++ .../tracing/JmsPublishSpanCustomizer.java | 50 ++++++++ .../profiling/tracing/JmsSpanCustomizer.java | 42 +++++++ .../extensions/jms/test/AllureConstants.java | 2 + .../tracing/JmsSpanCustomizerTestCase.java | 110 ++++++++++++++++++ 7 files changed, 260 insertions(+), 47 deletions(-) create mode 100644 src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java create mode 100644 src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java create mode 100644 src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java create mode 100644 src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java b/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java index 562673c4..7d3d6a0f 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java @@ -8,6 +8,7 @@ import static org.mule.extensions.jms.internal.common.JmsCommons.EXAMPLE_CONTENT_TYPE; import static org.mule.extensions.jms.internal.common.JmsCommons.EXAMPLE_ENCODING; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.JmsConsumeSpanCustomizer.getJmsConsumeSpanCustomizer; import static org.mule.jms.commons.internal.common.JmsCommons.getDestinationType; import static org.slf4j.LoggerFactory.getLogger; @@ -40,13 +41,10 @@ import org.mule.runtime.extension.api.tx.OperationTransactionalAction; import org.mule.sdk.compatibility.api.utils.ForwardCompatibilityHelper; -import java.util.Locale; import java.util.concurrent.TimeUnit; import javax.inject.Inject; -import javax.jms.ConnectionMetaData; import javax.jms.Destination; -import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; @@ -60,7 +58,6 @@ public final class JmsConsume implements Initialisable, Disposable { private static final Logger LOGGER = getLogger(JmsConsume.class); - private static final String SPAN_OPERATION_NAME = "receive"; @Inject private JmsSessionManager sessionManager; @@ -108,7 +105,7 @@ public Result consume(@Config JmsConfig config, OperationTransactionalAction transactionalAction, CorrelationInfo correlationInfo) throws JmsExtensionException, ConnectionException { - addSpanInfo(connection, destination, consumerType, correlationInfo); + customizeCurrentSpan(connection, destination, consumerType, correlationInfo); return (Result) jmsConsume.consume(config, connection, destination, consumerType, ackMode, selector, contentType, encoding, maximumWait, maximumWaitUnit, transactionalAction); @@ -126,24 +123,11 @@ public void dispose() { jmsConsume.dispose(); } - private void addSpanInfo(JmsTransactionalConnection connection, String destination, - org.mule.jms.commons.api.destination.ConsumerType consumerType, - org.mule.runtime.extension.api.runtime.parameter.CorrelationInfo correlationInfo) { + private void customizeCurrentSpan(JmsTransactionalConnection connection, String destination, + org.mule.jms.commons.api.destination.ConsumerType consumerType, + org.mule.runtime.extension.api.runtime.parameter.CorrelationInfo correlationInfo) { forwardCompatibilityHelper - .ifPresent(fch -> { - ConnectionMetaData connectionMetaData; - try { - connectionMetaData = connection.get().getMetaData(); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.system", connectionMetaData.getJMSProviderName().toLowerCase(Locale.ROOT)); - } catch (JMSException ignored) { - LOGGER.info("Span connection metadata could not be fetched"); - } - fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("span.kind", "CONSUMER"); - fch.getDistributedTraceContextManager(correlationInfo).setCurrentSpanName(destination + " " + SPAN_OPERATION_NAME); - fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("messaging.destination", destination); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.destination_kind", getDestinationType(consumerType).toLowerCase(Locale.ROOT)); - }); + .ifPresent(fch -> getJmsConsumeSpanCustomizer().customizeSpan(fch.getDistributedTraceContextManager(correlationInfo), + connection, destination, consumerType)); } } diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java b/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java index 47bdca6d..fcb07697 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java @@ -7,6 +7,7 @@ package org.mule.extensions.jms.internal.operation; import static org.mule.extensions.jms.internal.common.JmsCommons.QUEUE; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.JmsPublishSpanCustomizer.getJmsPublishSpanCustomizer; import static org.slf4j.LoggerFactory.getLogger; @@ -54,7 +55,6 @@ public final class JmsPublish implements Initialisable, Disposable { private static final Logger LOGGER = getLogger(JmsPublish.class); - private static final String SPAN_OPERATION_NAME = "send"; @Inject private JmsSessionManager jmsSessionManager; @@ -95,33 +95,18 @@ public void publish(@Config JmsConfig config, @Connection JmsTransactionalConnec CompletionCallback completionCallback) throws JmsExtensionException { - forwardCompatibilityHelper - .ifPresent(fch -> { - ConnectionMetaData connectionMetaData; - try { - connectionMetaData = connection.get().getMetaData(); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.system", connectionMetaData.getJMSProviderName().toLowerCase(Locale.ROOT)); - } catch (JMSException ignored) { - LOGGER.info("Span connection metadata could not be fetched"); - } - fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("span.kind", "PRODUCER"); - fch.getDistributedTraceContextManager(correlationInfo).setCurrentSpanName(destination + " " + SPAN_OPERATION_NAME); - fch.getDistributedTraceContextManager(correlationInfo).addCurrentSpanAttribute("messaging.destination", destination); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.destination_kind", destinationType.toString().toLowerCase(Locale.ROOT)); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.conversation_id", messageBuilder.getCorrelationId()); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.message_payload_size_bytes", - messageBuilder.getBody().getByteLength().toString()); - fch.getDistributedTraceContextManager(correlationInfo) - .addCurrentSpanAttribute("messaging.message_payload_compressed_size_bytes", messageBuilder.getCorrelationId()); - }); + customizeCurrentSpan(connection, destination, destinationType, messageBuilder, correlationInfo); jmsPublish.publish(config, connection, destination, destinationType, messageBuilder, overrides, transactionalAction, sendCorrelationId, correlationInfo, completionCallback); } + private void customizeCurrentSpan(JmsTransactionalConnection connection, String destination, DestinationType destinationType, + JmsMessageBuilder messageBuilder, CorrelationInfo correlationInfo) { + forwardCompatibilityHelper + .ifPresent(fch -> getJmsPublishSpanCustomizer().customizeSpan(fch.getDistributedTraceContextManager(correlationInfo), + connection, destination, destinationType, messageBuilder)); + } + @Override public void dispose() { diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java new file mode 100644 index 00000000..99eb66ad --- /dev/null +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ +package org.mule.extensions.jms.internal.operation.profiling.tracing; + +import static org.mule.jms.commons.internal.common.JmsCommons.getDestinationType; + +import org.mule.jms.commons.api.destination.ConsumerType; +import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; +import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; + +import java.util.Locale; + +public class JmsConsumeSpanCustomizer extends JmsSpanCustomizer { + + private static final String SPAN_OPERATION_NAME = "receive"; + public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; + + /** + * @return a new instance of a {@link JmsConsumeSpanCustomizer}. + */ + public static JmsConsumeSpanCustomizer getJmsConsumeSpanCustomizer() { + return new JmsConsumeSpanCustomizer(); + } + + public void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, JmsTransactionalConnection connection, + String destination, ConsumerType consumerType) { + super.customizeSpan(distributedTraceContextManager, connection, destination); + distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, getDestinationType(consumerType).toLowerCase(Locale.ROOT)); + } + + @Override + protected String getSpanOperation() { + return SPAN_OPERATION_NAME; + } +} diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java new file mode 100644 index 00000000..a815e599 --- /dev/null +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ +package org.mule.extensions.jms.internal.operation.profiling.tracing; + +import org.mule.extensions.jms.api.destination.DestinationType; +import org.mule.extensions.jms.api.message.JmsMessageBuilder; +import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; +import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; + +import java.util.Locale; + +public class JmsPublishSpanCustomizer extends JmsSpanCustomizer { + + private static final String SPAN_OPERATION_NAME = "send"; + public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; + public static final String MESSAGING_CONVERSATION_ID = "messaging.conversation_id"; + public static final String MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = "messaging.message_payload_size_bytes"; + public static final String MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES = "messaging.message_payload_compressed_size_bytes"; + + /** + * @return a new instance of a {@link JmsPublishSpanCustomizer}. + */ + public static JmsPublishSpanCustomizer getJmsPublishSpanCustomizer() { + return new JmsPublishSpanCustomizer(); + } + + public void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, JmsTransactionalConnection connection, + String destination, + DestinationType destinationType, JmsMessageBuilder messageBuilder) { + super.customizeSpan(distributedTraceContextManager, connection, destination); + distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, destinationType.toString().toLowerCase(Locale.ROOT)); + distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_CONVERSATION_ID, messageBuilder.getCorrelationId()); + if (messageBuilder.getBody().getByteLength().isPresent()) { + distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, + String.valueOf(messageBuilder.getBody().getByteLength().getAsLong())); + } + } + + @Override + protected String getSpanOperation() { + return SPAN_OPERATION_NAME; + } +} diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java new file mode 100644 index 00000000..3129a2b1 --- /dev/null +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ +package org.mule.extensions.jms.internal.operation.profiling.tracing; + +import static org.slf4j.LoggerFactory.getLogger; + +import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; +import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; + +import java.util.Locale; + +import javax.jms.ConnectionMetaData; +import javax.jms.JMSException; + +import org.slf4j.Logger; + +public abstract class JmsSpanCustomizer { + + private static final Logger LOGGER = getLogger(JmsSpanCustomizer.class); + public static final String MESSAGING_SYSTEM = "messaging.system"; + public static final String MESSAGING_DESTINATION = "messaging.destination"; + + protected void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, + JmsTransactionalConnection connection, String destination) { + ConnectionMetaData connectionMetaData; + try { + connectionMetaData = connection.get().getMetaData(); + distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_SYSTEM, connectionMetaData.getJMSProviderName()); + } catch (JMSException ignored) { + LOGGER.info("Span connection metadata could not be fetched"); + } + distributedTraceContextManager.setCurrentSpanName(destination + " " + getSpanOperation()); + distributedTraceContextManager.addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); + } + + protected abstract String getSpanOperation(); +} diff --git a/src/test/java/org/mule/extensions/jms/test/AllureConstants.java b/src/test/java/org/mule/extensions/jms/test/AllureConstants.java index 0ae23c07..10cdee18 100644 --- a/src/test/java/org/mule/extensions/jms/test/AllureConstants.java +++ b/src/test/java/org/mule/extensions/jms/test/AllureConstants.java @@ -29,6 +29,8 @@ interface JmsStory { String MESSAGE_TYPES = "Durable Subscriber"; String MESSAGE_FILTERING = "Message Filtering"; + + String TRACING = "Tracing"; } } diff --git a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java new file mode 100644 index 00000000..fa4d01f3 --- /dev/null +++ b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ + +package org.mule.extensions.jms.test.internal.operation.profiling.tracing; + +import static org.mule.extensions.jms.api.destination.DestinationType.TOPIC; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.JmsConsumeSpanCustomizer.getJmsConsumeSpanCustomizer; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.JmsPublishSpanCustomizer.getJmsPublishSpanCustomizer; +import static org.mule.extensions.jms.test.AllureConstants.JmsFeature.JmsStory.TRACING; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.mule.extensions.jms.api.destination.DestinationType; +import org.mule.extensions.jms.api.message.JmsMessageBuilder; +import org.mule.extensions.jms.internal.operation.profiling.tracing.JmsConsumeSpanCustomizer; +import org.mule.extensions.jms.internal.operation.profiling.tracing.JmsPublishSpanCustomizer; +import org.mule.jms.commons.api.destination.ConsumerType; +import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; +import org.mule.runtime.api.metadata.DataType; +import org.mule.runtime.api.metadata.TypedValue; +import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; + +import java.util.OptionalLong; + +import javax.jms.Connection; +import javax.jms.ConnectionMetaData; +import javax.jms.JMSException; + +import io.qameta.allure.Description; +import io.qameta.allure.Issue; +import io.qameta.allure.Story; +import org.junit.Test; + +@Story(TRACING) +@Issue("W-11859222") +public class JmsSpanCustomizerTestCase { + + public static final String MESSAGING_SYSTEM = "messaging.system"; + public static final String MESSAGING_DESTINATION = "messaging.destination"; + public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; + public static final String MESSAGING_CONVERSATION_ID = "messaging.conversation_id"; + public static final String MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = "messaging.message_payload_size_bytes"; + + @Test + @Description("The consume span customizer informs the distributed trace context manager the correct attributes/name") + public void jmsConsumeSpanCustomizerShouldSetCorrespondingAttributes() throws JMSException { + String messagingSystem = "testActiveMq"; + String destination = "queueName"; + String expectedSpanName = destination + " " + "receive"; + + DistributedTraceContextManager distributedTraceContextManager = mock(DistributedTraceContextManager.class); + JmsTransactionalConnection jmsTransactionalConnection = mock(JmsTransactionalConnection.class); + Connection connection = mock(Connection.class); + ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class); + ConsumerType consumerType = mock(ConsumerType.class); + + when(jmsTransactionalConnection.get()).thenReturn(connection); + when(connection.getMetaData()).thenReturn(connectionMetaData); + when(connectionMetaData.getJMSProviderName()).thenReturn(messagingSystem); + when(consumerType.topic()).thenReturn(false); + + JmsConsumeSpanCustomizer jmsConsumeSpanCustomizer = getJmsConsumeSpanCustomizer(); + jmsConsumeSpanCustomizer.customizeSpan(distributedTraceContextManager, jmsTransactionalConnection, destination, consumerType); + + verify(distributedTraceContextManager).setCurrentSpanName(expectedSpanName); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "queue"); + } + + @Test + @Description("The publish span customizer informs the distributed trace context manager the correct attributes/name") + public void jmsPublishSpanCustomizerShouldSetCorrespondingAttributes() throws Exception { + String messagingSystem = "testActiveMq"; + String destination = "topicName"; + String expectedSpanName = destination + " " + "send"; + String correlationId = "correlationIdTest1"; + DestinationType destinationType = TOPIC; + TypedValue typedValue = new TypedValue(Object.class, DataType.OBJECT, OptionalLong.of(39L)); + + DistributedTraceContextManager distributedTraceContextManager = mock(DistributedTraceContextManager.class); + JmsTransactionalConnection jmsTransactionalConnection = mock(JmsTransactionalConnection.class); + Connection connection = mock(Connection.class); + ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class); + JmsMessageBuilder jmsMessageBuilder = mock(JmsMessageBuilder.class); + + when(jmsTransactionalConnection.get()).thenReturn(connection); + when(connection.getMetaData()).thenReturn(connectionMetaData); + when(connectionMetaData.getJMSProviderName()).thenReturn(messagingSystem); + when(jmsMessageBuilder.getCorrelationId()).thenReturn(correlationId); + when(jmsMessageBuilder.getBody()).thenReturn(typedValue); + + JmsPublishSpanCustomizer jmsPublishSpanCustomizer = getJmsPublishSpanCustomizer(); + jmsPublishSpanCustomizer.customizeSpan(distributedTraceContextManager, jmsTransactionalConnection, destination, + destinationType, jmsMessageBuilder); + + verify(distributedTraceContextManager).setCurrentSpanName(expectedSpanName); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "topic"); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_CONVERSATION_ID, correlationId); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, "39"); + } +} From 1ed67fa20b12a4dc7eae63eb5db9ce8289e50146 Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Tue, 15 Nov 2022 16:34:01 -0300 Subject: [PATCH 3/9] Refactor --- .../jms/internal/operation/JmsConsume.java | 5 ++--- .../jms/internal/operation/JmsPublish.java | 10 +++------- .../profiling/tracing/JmsPublishSpanCustomizer.java | 13 +------------ .../profiling/tracing/JmsSpanCustomizer.java | 2 +- .../tracing/JmsSpanCustomizerTestCase.java | 6 +----- 5 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java b/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java index 7d3d6a0f..cde28e26 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/JmsConsume.java @@ -9,7 +9,6 @@ import static org.mule.extensions.jms.internal.common.JmsCommons.EXAMPLE_CONTENT_TYPE; import static org.mule.extensions.jms.internal.common.JmsCommons.EXAMPLE_ENCODING; import static org.mule.extensions.jms.internal.operation.profiling.tracing.JmsConsumeSpanCustomizer.getJmsConsumeSpanCustomizer; -import static org.mule.jms.commons.internal.common.JmsCommons.getDestinationType; import static org.slf4j.LoggerFactory.getLogger; @@ -82,8 +81,8 @@ public final class JmsConsume implements Initialisable, Disposable { * @param selector a custom JMS selector for filtering the messages * @param contentType the {@link Message}'s content content type * @param encoding the {@link Message}'s content encoding - * @param maximumWait maximum time to wait for a message before timing out - * @param maximumWaitUnit Time unit to be used in the maximumWaitTime configurations + * @param maximumWait maximum time to wait for a message before timing out + * @param maximumWaitUnit Time unit to be used in the maximumWaitTime configurations * @return a {@link Result} with the {@link Message} content as {@link Result#getOutput} and its properties * and headers as {@link Result#getAttributes} * @throws JmsConsumeException if an error occurs diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java b/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java index fcb07697..553e0285 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/JmsPublish.java @@ -37,12 +37,8 @@ import org.mule.runtime.extension.api.tx.OperationTransactionalAction; import org.mule.sdk.compatibility.api.utils.ForwardCompatibilityHelper; -import java.util.Locale; - import javax.inject.Inject; -import javax.jms.ConnectionMetaData; import javax.jms.Destination; -import javax.jms.JMSException; import javax.jms.Message; import org.slf4j.Logger; @@ -95,16 +91,16 @@ public void publish(@Config JmsConfig config, @Connection JmsTransactionalConnec CompletionCallback completionCallback) throws JmsExtensionException { - customizeCurrentSpan(connection, destination, destinationType, messageBuilder, correlationInfo); + customizeCurrentSpan(connection, destination, destinationType, correlationInfo); jmsPublish.publish(config, connection, destination, destinationType, messageBuilder, overrides, transactionalAction, sendCorrelationId, correlationInfo, completionCallback); } private void customizeCurrentSpan(JmsTransactionalConnection connection, String destination, DestinationType destinationType, - JmsMessageBuilder messageBuilder, CorrelationInfo correlationInfo) { + CorrelationInfo correlationInfo) { forwardCompatibilityHelper .ifPresent(fch -> getJmsPublishSpanCustomizer().customizeSpan(fch.getDistributedTraceContextManager(correlationInfo), - connection, destination, destinationType, messageBuilder)); + connection, destination, destinationType)); } diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java index a815e599..fe5bcc2f 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java @@ -7,7 +7,6 @@ package org.mule.extensions.jms.internal.operation.profiling.tracing; import org.mule.extensions.jms.api.destination.DestinationType; -import org.mule.extensions.jms.api.message.JmsMessageBuilder; import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; @@ -17,9 +16,6 @@ public class JmsPublishSpanCustomizer extends JmsSpanCustomizer { private static final String SPAN_OPERATION_NAME = "send"; public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; - public static final String MESSAGING_CONVERSATION_ID = "messaging.conversation_id"; - public static final String MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = "messaging.message_payload_size_bytes"; - public static final String MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES = "messaging.message_payload_compressed_size_bytes"; /** * @return a new instance of a {@link JmsPublishSpanCustomizer}. @@ -30,17 +26,10 @@ public static JmsPublishSpanCustomizer getJmsPublishSpanCustomizer() { public void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, JmsTransactionalConnection connection, String destination, - DestinationType destinationType, JmsMessageBuilder messageBuilder) { + DestinationType destinationType) { super.customizeSpan(distributedTraceContextManager, connection, destination); distributedTraceContextManager .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, destinationType.toString().toLowerCase(Locale.ROOT)); - distributedTraceContextManager - .addCurrentSpanAttribute(MESSAGING_CONVERSATION_ID, messageBuilder.getCorrelationId()); - if (messageBuilder.getBody().getByteLength().isPresent()) { - distributedTraceContextManager - .addCurrentSpanAttribute(MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, - String.valueOf(messageBuilder.getBody().getByteLength().getAsLong())); - } } @Override diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java index 3129a2b1..5efc86df 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java @@ -30,7 +30,7 @@ protected void customizeSpan(DistributedTraceContextManager distributedTraceCont try { connectionMetaData = connection.get().getMetaData(); distributedTraceContextManager - .addCurrentSpanAttribute(MESSAGING_SYSTEM, connectionMetaData.getJMSProviderName()); + .addCurrentSpanAttribute(MESSAGING_SYSTEM, connectionMetaData.getJMSProviderName().toLowerCase(Locale.ROOT)); } catch (JMSException ignored) { LOGGER.info("Span connection metadata could not be fetched"); } diff --git a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java index fa4d01f3..a38c087e 100644 --- a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java +++ b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java @@ -44,8 +44,6 @@ public class JmsSpanCustomizerTestCase { public static final String MESSAGING_SYSTEM = "messaging.system"; public static final String MESSAGING_DESTINATION = "messaging.destination"; public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; - public static final String MESSAGING_CONVERSATION_ID = "messaging.conversation_id"; - public static final String MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = "messaging.message_payload_size_bytes"; @Test @Description("The consume span customizer informs the distributed trace context manager the correct attributes/name") @@ -98,13 +96,11 @@ public void jmsPublishSpanCustomizerShouldSetCorrespondingAttributes() throws Ex JmsPublishSpanCustomizer jmsPublishSpanCustomizer = getJmsPublishSpanCustomizer(); jmsPublishSpanCustomizer.customizeSpan(distributedTraceContextManager, jmsTransactionalConnection, destination, - destinationType, jmsMessageBuilder); + destinationType); verify(distributedTraceContextManager).setCurrentSpanName(expectedSpanName); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "topic"); - verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_CONVERSATION_ID, correlationId); - verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, "39"); } } From a86e98061a4927aa83599ffdb4ebc65d18873b5f Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Wed, 23 Nov 2022 11:22:27 -0300 Subject: [PATCH 4/9] Review --- .../tracing/JmsConsumeSpanCustomizer.java | 12 ++++++-- .../tracing/JmsPublishSpanCustomizer.java | 13 +++++++-- .../profiling/tracing/JmsSpanCustomizer.java | 29 ++++++++++++------- .../tracing/SpanCustomizerUtils.java | 23 +++++++++++++++ 4 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java index 99eb66ad..ccfa7be4 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java @@ -6,16 +6,23 @@ */ package org.mule.extensions.jms.internal.operation.profiling.tracing; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.SpanCustomizerUtils.safeExecute; import static org.mule.jms.commons.internal.common.JmsCommons.getDestinationType; +import static org.slf4j.LoggerFactory.getLogger; + import org.mule.jms.commons.api.destination.ConsumerType; import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; import java.util.Locale; +import org.slf4j.Logger; + public class JmsConsumeSpanCustomizer extends JmsSpanCustomizer { + private static final Logger LOGGER = getLogger(JmsConsumeSpanCustomizer.class); + private static final String SPAN_OPERATION_NAME = "receive"; public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; @@ -29,8 +36,9 @@ public static JmsConsumeSpanCustomizer getJmsConsumeSpanCustomizer() { public void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, JmsTransactionalConnection connection, String destination, ConsumerType consumerType) { super.customizeSpan(distributedTraceContextManager, connection, destination); - distributedTraceContextManager - .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, getDestinationType(consumerType).toLowerCase(Locale.ROOT)); + safeExecute(() -> distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, getDestinationType(consumerType).toLowerCase(Locale.ROOT)), + "Messaging destination kind data could not be added to span", LOGGER); } @Override diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java index fe5bcc2f..69481a22 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java @@ -6,14 +6,22 @@ */ package org.mule.extensions.jms.internal.operation.profiling.tracing; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.SpanCustomizerUtils.safeExecute; + +import static org.slf4j.LoggerFactory.getLogger; + import org.mule.extensions.jms.api.destination.DestinationType; import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; import java.util.Locale; +import org.slf4j.Logger; + public class JmsPublishSpanCustomizer extends JmsSpanCustomizer { + private static final Logger LOGGER = getLogger(JmsPublishSpanCustomizer.class); + private static final String SPAN_OPERATION_NAME = "send"; public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; @@ -28,8 +36,9 @@ public void customizeSpan(DistributedTraceContextManager distributedTraceContext String destination, DestinationType destinationType) { super.customizeSpan(distributedTraceContextManager, connection, destination); - distributedTraceContextManager - .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, destinationType.toString().toLowerCase(Locale.ROOT)); + safeExecute(() -> distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, destinationType.toString().toLowerCase(Locale.ROOT)), + "Messaging destination kind data could not be added to span", LOGGER); } @Override diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java index 5efc86df..26bcdfa8 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java @@ -6,6 +6,8 @@ */ package org.mule.extensions.jms.internal.operation.profiling.tracing; +import static org.mule.extensions.jms.internal.operation.profiling.tracing.SpanCustomizerUtils.safeExecute; + import static org.slf4j.LoggerFactory.getLogger; import org.mule.jms.commons.internal.connection.JmsTransactionalConnection; @@ -13,7 +15,6 @@ import java.util.Locale; -import javax.jms.ConnectionMetaData; import javax.jms.JMSException; import org.slf4j.Logger; @@ -26,17 +27,25 @@ public abstract class JmsSpanCustomizer { protected void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, JmsTransactionalConnection connection, String destination) { - ConnectionMetaData connectionMetaData; + safeExecute(() -> distributedTraceContextManager.setCurrentSpanName(destination + " " + getSpanOperation()), + "Span name according to semantic conventions could not be added to span", LOGGER); + safeExecute(() -> distributedTraceContextManager + .addCurrentSpanAttribute(MESSAGING_SYSTEM, getMessagingSystem(connection)), + "Messaging system data could not be added to span", LOGGER); + safeExecute(() -> distributedTraceContextManager.addCurrentSpanAttribute(MESSAGING_DESTINATION, destination), + "Messaging destination data could not be added to span", LOGGER); + } + + protected abstract String getSpanOperation(); + + private String getMessagingSystem(JmsTransactionalConnection connection) { try { - connectionMetaData = connection.get().getMetaData(); - distributedTraceContextManager - .addCurrentSpanAttribute(MESSAGING_SYSTEM, connectionMetaData.getJMSProviderName().toLowerCase(Locale.ROOT)); - } catch (JMSException ignored) { + if (connection != null && connection.get() != null) { + return connection.get().getMetaData().getJMSProviderName().toLowerCase(Locale.ROOT); + } + } catch (JMSException e) { LOGGER.info("Span connection metadata could not be fetched"); } - distributedTraceContextManager.setCurrentSpanName(destination + " " + getSpanOperation()); - distributedTraceContextManager.addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); + return null; } - - protected abstract String getSpanOperation(); } diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java new file mode 100644 index 00000000..52542c1d --- /dev/null +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java @@ -0,0 +1,23 @@ +package org.mule.extensions.jms.internal.operation.profiling.tracing; + +import org.slf4j.Logger; + +public class SpanCustomizerUtils { + + /** + * Safely executes a piece of logic. + * + * @param toExecute the piece of logic to execute. + * @param loggingMessage the logging message if a throwable + * @param logger logger used for informing tracing errors. + */ + public static void safeExecute(Runnable toExecute, String loggingMessage, Logger logger) { + try { + toExecute.run(); + } catch (Throwable e) { + if (logger.isWarnEnabled()) { + logger.warn(loggingMessage, e); + } + } + } +} From 7bd8fda1e9015324cfcc83e44fb5992da3b6e11d Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Tue, 6 Dec 2022 12:06:33 -0300 Subject: [PATCH 5/9] W-12041210: Provide a way to set the status of a current span and span Kind from a connector (needed in sdk api) --- .../profiling/tracing/JmsConsumeSpanCustomizer.java | 6 ++++++ .../profiling/tracing/JmsPublishSpanCustomizer.java | 6 ++++++ .../operation/profiling/tracing/JmsSpanCustomizer.java | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java index ccfa7be4..775a0dfb 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java @@ -24,6 +24,7 @@ public class JmsConsumeSpanCustomizer extends JmsSpanCustomizer { private static final Logger LOGGER = getLogger(JmsConsumeSpanCustomizer.class); private static final String SPAN_OPERATION_NAME = "receive"; + private static final String SPAN_KIND_NAME = "CONSUMER"; public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; /** @@ -45,4 +46,9 @@ public void customizeSpan(DistributedTraceContextManager distributedTraceContext protected String getSpanOperation() { return SPAN_OPERATION_NAME; } + + @Override + protected String getSpanKind() { + return SPAN_KIND_NAME; + } } diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java index 69481a22..20b3d367 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java @@ -23,6 +23,7 @@ public class JmsPublishSpanCustomizer extends JmsSpanCustomizer { private static final Logger LOGGER = getLogger(JmsPublishSpanCustomizer.class); private static final String SPAN_OPERATION_NAME = "send"; + private static final String SPAN_KIND_NAME = "PRODUCER"; public static final String MESSAGING_DESTINATION_KIND = "messaging.destination_kind"; /** @@ -45,4 +46,9 @@ public void customizeSpan(DistributedTraceContextManager distributedTraceContext protected String getSpanOperation() { return SPAN_OPERATION_NAME; } + + @Override + protected String getSpanKind() { + return SPAN_KIND_NAME; + } } diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java index 26bcdfa8..92108358 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java @@ -7,6 +7,7 @@ package org.mule.extensions.jms.internal.operation.profiling.tracing; import static org.mule.extensions.jms.internal.operation.profiling.tracing.SpanCustomizerUtils.safeExecute; +import static org.mule.jms.commons.internal.common.JmsCommons.getDestinationType; import static org.slf4j.LoggerFactory.getLogger; @@ -24,6 +25,7 @@ public abstract class JmsSpanCustomizer { private static final Logger LOGGER = getLogger(JmsSpanCustomizer.class); public static final String MESSAGING_SYSTEM = "messaging.system"; public static final String MESSAGING_DESTINATION = "messaging.destination"; + public static final String SPAN_KIND = "span.kind.override"; protected void customizeSpan(DistributedTraceContextManager distributedTraceContextManager, JmsTransactionalConnection connection, String destination) { @@ -34,10 +36,14 @@ protected void customizeSpan(DistributedTraceContextManager distributedTraceCont "Messaging system data could not be added to span", LOGGER); safeExecute(() -> distributedTraceContextManager.addCurrentSpanAttribute(MESSAGING_DESTINATION, destination), "Messaging destination data could not be added to span", LOGGER); + safeExecute(() -> distributedTraceContextManager.addCurrentSpanAttribute(SPAN_KIND, getSpanKind()), + "Span kind could not be added to span", LOGGER); } protected abstract String getSpanOperation(); + protected abstract String getSpanKind(); + private String getMessagingSystem(JmsTransactionalConnection connection) { try { if (connection != null && connection.get() != null) { From 0d3a4dad14acdc63f17f9f7a538c5a84c5e6cdaa Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Wed, 23 Nov 2022 12:40:04 -0300 Subject: [PATCH 6/9] Removing unnecessary if condition --- .../operation/profiling/tracing/JmsSpanCustomizer.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java index 92108358..73925dae 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java @@ -46,9 +46,7 @@ protected void customizeSpan(DistributedTraceContextManager distributedTraceCont private String getMessagingSystem(JmsTransactionalConnection connection) { try { - if (connection != null && connection.get() != null) { - return connection.get().getMetaData().getJMSProviderName().toLowerCase(Locale.ROOT); - } + return connection.get().getMetaData().getJMSProviderName().toLowerCase(Locale.ROOT); } catch (JMSException e) { LOGGER.info("Span connection metadata could not be fetched"); } From 554d48d8e9a5ec29025d07647b95758facb4ace5 Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Tue, 15 Nov 2022 16:38:38 -0300 Subject: [PATCH 7/9] Fix --- .../profiling/tracing/JmsSpanCustomizerTestCase.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java index a38c087e..d4f5ee6f 100644 --- a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java +++ b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java @@ -26,6 +26,7 @@ import org.mule.runtime.api.metadata.TypedValue; import org.mule.sdk.api.runtime.source.DistributedTraceContextManager; +import java.util.Locale; import java.util.OptionalLong; import javax.jms.Connection; @@ -67,7 +68,7 @@ public void jmsConsumeSpanCustomizerShouldSetCorrespondingAttributes() throws JM jmsConsumeSpanCustomizer.customizeSpan(distributedTraceContextManager, jmsTransactionalConnection, destination, consumerType); verify(distributedTraceContextManager).setCurrentSpanName(expectedSpanName); - verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem.toLowerCase(Locale.ROOT)); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "queue"); } @@ -99,7 +100,7 @@ public void jmsPublishSpanCustomizerShouldSetCorrespondingAttributes() throws Ex destinationType); verify(distributedTraceContextManager).setCurrentSpanName(expectedSpanName); - verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_SYSTEM, messagingSystem.toLowerCase(Locale.ROOT)); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "topic"); } From b487dab1cad8ce4908b5b0fd6cf854bb09c0f8e7 Mon Sep 17 00:00:00 2001 From: sofiamorseletto Date: Wed, 23 Nov 2022 12:02:31 -0300 Subject: [PATCH 8/9] Added test --- .../tracing/JmsSpanCustomizerTestCase.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java index d4f5ee6f..49df22aa 100644 --- a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java +++ b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java @@ -13,6 +13,7 @@ import static org.mule.extensions.jms.test.AllureConstants.JmsFeature.JmsStory.TRACING; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -104,4 +105,33 @@ public void jmsPublishSpanCustomizerShouldSetCorrespondingAttributes() throws Ex verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "topic"); } + + @Test + @Description("The correct functioning of the connector should not be hindered by tracing") + public void jmsSpanCustomizerShouldSetCorrespondingAttributesEvenIfThereIsAnException() throws JMSException { + String messagingSystem = "testActiveMq"; + String destination = "queueName"; + String expectedSpanName = destination + " " + "receive"; + + DistributedTraceContextManager distributedTraceContextManager = mock(DistributedTraceContextManager.class); + JmsTransactionalConnection jmsTransactionalConnection = mock(JmsTransactionalConnection.class); + Connection connection = mock(Connection.class); + ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class); + ConsumerType consumerType = mock(ConsumerType.class); + + when(jmsTransactionalConnection.get()).thenReturn(connection); + // This will cause a NullPointerException + when(connection.getMetaData()).thenReturn(null); + when(connectionMetaData.getJMSProviderName()).thenReturn(messagingSystem); + when(consumerType.topic()).thenReturn(false); + + JmsConsumeSpanCustomizer jmsConsumeSpanCustomizer = getJmsConsumeSpanCustomizer(); + jmsConsumeSpanCustomizer.customizeSpan(distributedTraceContextManager, jmsTransactionalConnection, destination, consumerType); + + verify(distributedTraceContextManager).setCurrentSpanName(expectedSpanName); + verify(distributedTraceContextManager, never()).addCurrentSpanAttribute(MESSAGING_SYSTEM, + messagingSystem.toLowerCase(Locale.ROOT)); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION, destination); + verify(distributedTraceContextManager).addCurrentSpanAttribute(MESSAGING_DESTINATION_KIND, "queue"); + } } From 5b00a0d0328ba5248fd0f6f06681a8eb80d5a8f3 Mon Sep 17 00:00:00 2001 From: "juan.flores" Date: Mon, 12 Aug 2024 12:55:35 -0300 Subject: [PATCH 9/9] added changes in Licenses Headers --- .../profiling/tracing/JmsConsumeSpanCustomizer.java | 2 +- .../profiling/tracing/JmsPublishSpanCustomizer.java | 2 +- .../operation/profiling/tracing/JmsSpanCustomizer.java | 2 +- .../operation/profiling/tracing/SpanCustomizerUtils.java | 6 ++++++ .../profiling/tracing/JmsSpanCustomizerTestCase.java | 3 +-- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java index 775a0dfb..554c9d52 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsConsumeSpanCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * Copyright 2023 Salesforce, Inc. All rights reserved. * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java index 20b3d367..2e18b345 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsPublishSpanCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * Copyright 2023 Salesforce, Inc. All rights reserved. * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java index 73925dae..b09670f3 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/JmsSpanCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * Copyright 2023 Salesforce, Inc. All rights reserved. * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. diff --git a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java index 52542c1d..40af13a0 100644 --- a/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java +++ b/src/main/java/org/mule/extensions/jms/internal/operation/profiling/tracing/SpanCustomizerUtils.java @@ -1,3 +1,9 @@ +/* + * Copyright 2023 Salesforce, Inc. All rights reserved. + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ package org.mule.extensions.jms.internal.operation.profiling.tracing; import org.slf4j.Logger; diff --git a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java index 49df22aa..9557d357 100644 --- a/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java +++ b/src/test/java/org/mule/extensions/jms/test/internal/operation/profiling/tracing/JmsSpanCustomizerTestCase.java @@ -1,10 +1,9 @@ /* - * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com + * Copyright 2023 Salesforce, Inc. All rights reserved. * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ - package org.mule.extensions.jms.test.internal.operation.profiling.tracing; import static org.mule.extensions.jms.api.destination.DestinationType.TOPIC;