From fa4c493520e43abef8da18d0b424b5ee607a245f Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 14 Dec 2021 18:02:26 +0100 Subject: [PATCH 1/4] Fix NPE in RocketMQ instrumentation --- .../rocketmq/RockerMqConsumerAttributeExtractor.java | 4 +++- .../rocketmq/RockerMqProducerAttributeExtractor.java | 9 +++++++-- .../RockerMqProducerExperimentalAttributeExtractor.java | 4 +++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqConsumerAttributeExtractor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqConsumerAttributeExtractor.java index 717ce86ceecc..002f34a2ca73 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqConsumerAttributeExtractor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqConsumerAttributeExtractor.java @@ -61,9 +61,11 @@ protected String conversationId(MessageExt request) { return null; } + @Nullable @Override protected Long messagePayloadSize(MessageExt request) { - return (long) request.getBody().length; + byte[] body = request.getBody(); + return body == null ? null : (long) body.length; } @Nullable diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerAttributeExtractor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerAttributeExtractor.java index 06ddc4fd4e31..f6c6b9069074 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerAttributeExtractor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerAttributeExtractor.java @@ -10,6 +10,8 @@ import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import javax.annotation.Nullable; import org.apache.rocketmq.client.hook.SendMessageContext; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; class RockerMqProducerAttributeExtractor extends MessagingAttributesExtractor { @@ -28,9 +30,11 @@ protected String destinationKind(SendMessageContext sendMessageContext) { return SemanticAttributes.MessagingDestinationKindValues.TOPIC; } + @Nullable @Override protected String destination(SendMessageContext sendMessageContext) { - return sendMessageContext.getMessage().getTopic(); + Message message = sendMessageContext.getMessage(); + return message == null ? null : message.getTopic(); } @Override @@ -77,6 +81,7 @@ protected Long messagePayloadCompressedSize(SendMessageContext sendMessageContex @Nullable @Override protected String messageId(SendMessageContext request, @Nullable Void unused) { - return request.getSendResult().getMsgId(); + SendResult sendResult = request.getSendResult(); + return sendResult == null ? null : sendResult.getMsgId(); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerExperimentalAttributeExtractor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerExperimentalAttributeExtractor.java index 50c9c7e2cd7b..3578cebbbc3f 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerExperimentalAttributeExtractor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RockerMqProducerExperimentalAttributeExtractor.java @@ -22,7 +22,9 @@ class RockerMqProducerExperimentalAttributeExtractor @Override public void onStart(AttributesBuilder attributes, SendMessageContext request) { - set(attributes, MESSAGING_ROCKETMQ_TAGS, request.getMessage().getTags()); + if (request.getMessage() != null) { + set(attributes, MESSAGING_ROCKETMQ_TAGS, request.getMessage().getTags()); + } set(attributes, MESSAGING_ROCKETMQ_BROKER_ADDRESS, request.getBrokerAddr()); } From e0c0fd8a3771bf07f1483530d58a3952efb61569 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 14 Dec 2021 18:49:54 +0100 Subject: [PATCH 2/4] fix tests --- .../instrumentation/rocketmq/AbstractRocketMqClientTest.groovy | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 85bf8d5552e6..c882f8b96a38 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -85,10 +85,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { "$SemanticAttributes.MESSAGING_SYSTEM" "rocketmq" "$SemanticAttributes.MESSAGING_DESTINATION" sharedTopic "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "topic" - "$SemanticAttributes.MESSAGING_MESSAGE_ID" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" String - "messaging.rocketmq.send_result" "SEND_OK" } } span(1) { From 1e51aa9bc49c5069016e5770a86f29209a61314c Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Wed, 15 Dec 2021 13:51:45 +0100 Subject: [PATCH 3/4] add exception --- .../instrumentation/rocketmq/TracingSendMessageHookImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java index ad34c45bf03a..013c5d8fc85b 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java @@ -42,7 +42,7 @@ public void sendMessageAfter(SendMessageContext context) { } if (context.getMqTraceContext() instanceof Context) { Context otelContext = (Context) context.getMqTraceContext(); - instrumenter.end(otelContext, context, null, null); + instrumenter.end(otelContext, context, null, context.getException()); } } } From cd6d4c92a380b12d6f687c3aae819f086c1b67a4 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Wed, 15 Dec 2021 14:08:57 +0100 Subject: [PATCH 4/4] Changed the condition a bit --- .../instrumentation/rocketmq/TracingSendMessageHookImpl.java | 3 ++- .../instrumentation/rocketmq/AbstractRocketMqClientTest.groovy | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java index 013c5d8fc85b..9d027d7c3dac 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java @@ -40,7 +40,8 @@ public void sendMessageAfter(SendMessageContext context) { if (context == null) { return; } - if (context.getMqTraceContext() instanceof Context) { + if (context.getMqTraceContext() instanceof Context + && (context.getSendResult() != null || context.getException() != null)) { Context otelContext = (Context) context.getMqTraceContext(); instrumenter.end(otelContext, context, null, context.getException()); } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index c882f8b96a38..85bf8d5552e6 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetry/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -85,8 +85,10 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { "$SemanticAttributes.MESSAGING_SYSTEM" "rocketmq" "$SemanticAttributes.MESSAGING_DESTINATION" sharedTopic "$SemanticAttributes.MESSAGING_DESTINATION_KIND" "topic" + "$SemanticAttributes.MESSAGING_MESSAGE_ID" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" String + "messaging.rocketmq.send_result" "SEND_OK" } } span(1) {