From 0d1fbed9640cb7a3630175e4a8cce628fd14b6a5 Mon Sep 17 00:00:00 2001 From: addname Date: Fri, 12 Feb 2021 22:32:35 +0800 Subject: [PATCH 01/44] add rocketmq support --- .../rocketmq-client-4.8-javaagent.gradle | 16 ++ .../RocketMqClientApiImplInstrumentation.java | 104 +++++++++ ...tMqConcurrentlyConsumeInstrumentation.java | 70 +++++++ .../RocketMqInstrumentationModule.java | 28 +++ ...RocketMqOrderlyConsumeInstrumentation.java | 70 +++++++ .../rocketmq/RocketMqClientTest.groovy | 14 ++ .../rocketmq-client-4.8-library.gradle | 8 + .../rocketmq/RocketMqClientConfig.java | 17 ++ .../rocketmq/RocketMqConsumerTracer.java | 89 ++++++++ .../rocketmq/RocketMqProducerTracer.java | 48 +++++ .../rocketmq/SendCallbackWrapper.java | 35 ++++ .../rocketmq/TextMapExtractAdapter.java | 25 +++ .../rocketmq/TextMapInjectAdapter.java | 24 +++ .../rocketmq/RocketMqClientTest.groovy | 13 ++ .../rocketmq-client-4.8-testing.gradle | 11 + .../AbstractRocketMqClientTest.groovy | 198 ++++++++++++++++++ .../testing/src/main/java/base/BaseConf.java | 161 ++++++++++++++ .../main/java/base/IntegrationTestBase.java | 181 ++++++++++++++++ .../dledger/DLedgerProduceAndConsumeIT.java | 109 ++++++++++ settings.gradle | 5 + 20 files changed, 1226 insertions(+) create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy create mode 100644 instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy create mode 100644 instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle create mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy create mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java create mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java create mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java diff --git a/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle b/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle new file mode 100644 index 000000000000..b3f29e425e7f --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle @@ -0,0 +1,16 @@ +apply from: "$rootDir/gradle/instrumentation.gradle" + +muzzle { + pass { + group = "org.apache.rocketmq" + module = 'rocketmq-client' + versions = "[4.8.0,)" + } +} +dependencies { + library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' + implementation project(':instrumentation:rocketmq-client-4.8:library') + testImplementation project(':instrumentation:rocketmq-client-4.8:testing') + +} + diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java new file mode 100644 index 000000000000..f0bf296e91fe --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java @@ -0,0 +1,104 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; +import static io.opentelemetry.instrumentation.rocketmq.TextMapInjectAdapter.SETTER; +import static java.util.Collections.singletonMap; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import java.util.Map; +import io.opentelemetry.instrumentation.rocketmq.SendCallbackWrapper; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.rocketmq.client.producer.SendCallback; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader; + +public class RocketMqClientApiImplInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher typeMatcher() { + return named("org.apache.rocketmq.client.impl.MQClientAPIImpl"); + } + + @Override + public Map, String> transformers() { + return singletonMap( + isMethod().and(named("sendMessage")).and(takesArguments(12)), + RocketMqClientApiImplInstrumentation.class.getName() + "$SendMessageAdvice"); + } + + public static class SendMessageAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void onEnter( + @Advice.Argument(value = 0, readOnly = false) String addr, + @Advice.Argument(value = 2, readOnly = false) Message msg, + @Advice.Argument(value = 3, readOnly = false) SendMessageRequestHeader requestHeader, + @Advice.Argument(value = 6, readOnly = false) SendCallback sendCallback, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + + Context parent = Java8BytecodeBridge.currentContext(); + span = tracer().startProducerSpan(addr, msg); + Context newContext = parent.with(span); + try { + Java8BytecodeBridge.getGlobalPropagators() + .getTextMapPropagator() + .inject(newContext, requestHeader, SETTER); + } catch (IllegalStateException e) { + requestHeader = new SendMessageRequestHeader(); + requestHeader.getBornTimestamp(); + requestHeader.getDefaultTopic(); + requestHeader.getDefaultTopicQueueNums(); + requestHeader.getFlag(); + requestHeader.getProducerGroup(); + requestHeader.getMaxReconsumeTimes(); + requestHeader.getProperties(); + requestHeader.getSysFlag(); + requestHeader.getTopic(); + requestHeader.getQueueId(); + requestHeader.getReconsumeTimes(); + Java8BytecodeBridge.getGlobalPropagators() + .getTextMapPropagator() + .inject(newContext, requestHeader, SETTER); + } + + scope = newContext.makeCurrent(); + if (sendCallback != null) { + sendCallback = new SendCallbackWrapper(sendCallback, span); + } + } + + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void stopSpan( + @Advice.Thrown Throwable throwable, + @Advice.Argument(value = 6, readOnly = false) SendCallback sendCallback, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + scope.close(); + if (sendCallback == null) { + if (throwable == null) { + tracer().end(span); + } else { + tracer().endExceptionally(span, throwable); + } + } + } + } +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java new file mode 100644 index 000000000000..305eb42b38d2 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java @@ -0,0 +1,70 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; +import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; +import static java.util.Collections.singletonMap; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.named; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import java.util.List; +import java.util.Map; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; +import org.apache.rocketmq.common.message.MessageExt; + +public class RocketMqConcurrentlyConsumeInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher typeMatcher() { + return implementsInterface( + named("org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently")); + } + + @Override + public Map, String> transformers() { + return singletonMap( + isMethod().and(named("consumeMessage")), + RocketMqConcurrentlyConsumeInstrumentation.class.getName() + "$ConcurrentlyConsumeAdvice"); + } + + public static class ConcurrentlyConsumeAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void onEnter( + @Advice.Argument(0) List msgs, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + + span = tracer().startSpan(msgs); + scope = span.makeCurrent(); + } + + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void stopSpan( + @Advice.Return ConsumeConcurrentlyStatus status, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + tracer().endConcurrentlySpan(span, status); + scope.close(); + if (throwable == null) { + tracer().end(span); + } else { + tracer().endExceptionally(span, throwable); + } + } + } +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java new file mode 100644 index 000000000000..6f9332e74fd5 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java @@ -0,0 +1,28 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import static java.util.Arrays.asList; + +import com.google.auto.service.AutoService; +import io.opentelemetry.javaagent.tooling.InstrumentationModule; +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import java.util.List; + +@AutoService(InstrumentationModule.class) +public class RocketMqInstrumentationModule extends InstrumentationModule { + public RocketMqInstrumentationModule() { + super("rocketmq", "rockemq-client-4.3"); + } + + @Override + public List typeInstrumentations() { + return asList( + new RocketMqClientApiImplInstrumentation(), + new RocketMqConcurrentlyConsumeInstrumentation(), + new RocketMqOrderlyConsumeInstrumentation()); + } +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java new file mode 100644 index 000000000000..9b987ec1e28e --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java @@ -0,0 +1,70 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; +import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; +import static java.util.Collections.singletonMap; +import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; +import static net.bytebuddy.matcher.ElementMatchers.named; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import java.util.List; +import java.util.Map; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; +import org.apache.rocketmq.common.message.MessageExt; + +public class RocketMqOrderlyConsumeInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher typeMatcher() { + return implementsInterface( + named("org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly")); + } + + @Override + public Map, String> transformers() { + return singletonMap( + nameStartsWith("consumeMessage"), + RocketMqOrderlyConsumeInstrumentation.class.getName() + "$OrderlyConsumeAdvice"); + } + + public static class OrderlyConsumeAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void onEnter( + @Advice.Argument(0) List msgs, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + + span = tracer().startSpan(msgs); + scope = span.makeCurrent(); + } + + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void stopSpan( + @Advice.Return ConsumeOrderlyStatus status, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + tracer().endOrderlySpan(span, status); + scope.close(); + if (throwable == null) { + tracer().end(span); + } else { + tracer().endExceptionally(span, throwable); + } + } + } +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy new file mode 100644 index 000000000000..bbd337036f2c --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -0,0 +1,14 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ +package io.opentelemetry.instrumentation.rocketmq + +import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest +import io.opentelemetry.instrumentation.test.AgentTestTrait + + +class RocketMqClientTest extends AbstractRocketMqClientTest implements AgentTestTrait { + + +} \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle new file mode 100644 index 000000000000..b17a6c3038a7 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle @@ -0,0 +1,8 @@ +apply from: "$rootDir/gradle/instrumentation-library.gradle" + +dependencies { + implementation deps.opentelemetryTraceProps + library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' + testImplementation project(':instrumentation:rocketmq-client-4.8:testing') + +} \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java new file mode 100644 index 000000000000..3b54c33902c0 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; +import io.opentelemetry.instrumentation.api.config.Config; + +public final class RocketMqClientConfig { + + public static boolean isPropagationEnabled() { + return Config.get() + .getBooleanProperty("otel.instrumentation.rocketmq.client-propagation", true); + } + + private RocketMqClientConfig() {} +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java new file mode 100644 index 000000000000..1933fd53f004 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -0,0 +1,89 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import static io.opentelemetry.api.trace.SpanKind.CONSUMER; +import static io.opentelemetry.instrumentation.rocketmq.TextMapExtractAdapter.GETTER; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.context.Context; +import io.opentelemetry.instrumentation.api.tracer.BaseTracer; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.util.List; +import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; +import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; +import org.apache.rocketmq.common.message.MessageExt; + +public class RocketMqConsumerTracer extends BaseTracer { + + private static final RocketMqConsumerTracer TRACER = new RocketMqConsumerTracer(); + + public static RocketMqConsumerTracer tracer() { + return TRACER; + } + + @Override + protected String getInstrumentationName() { + return "io.opentelemetry.javaagent.rocketmq-client"; + } + + public Span startSpan(List msgs) { + MessageExt msg = msgs.get(0); + Span span = + tracer + .spanBuilder(spanNameOnConsume(msg)) + .setSpanKind(CONSUMER) + .setParent(extractParent(msg)) + .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") + .setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()) + .setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic") + .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "process") + .setAttribute( + SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, getStoreSize(msgs)) + .startSpan(); + onConsume(span, msg); + return span; + } + + private Context extractParent(MessageExt msg) { + if (RocketMqClientConfig.isPropagationEnabled()) { + return extract(msg.getProperties(), GETTER); + } else { + return Context.current(); + } + } + + void onConsume(Span span, MessageExt msg) { + span.setAttribute("messaging.rocketmq.tags", msg.getTags()); + span.setAttribute("messaging.rocketmq.queue_id", msg.getQueueId()); + span.setAttribute("messaging.rocketmq.queue_offset", msg.getQueueOffset()); + span.setAttribute("messaging.rocketmq.broker_address", getBrokerHost(msg)); + } + + long getStoreSize(List msgs) { + long storeSize = msgs.stream().mapToInt(item -> item.getStoreSize()).sum(); + return storeSize; + } + + String spanNameOnConsume(MessageExt msg) { + return msg.getTopic() + " process"; + } + + String getBrokerHost(MessageExt msg) { + if (msg.getStoreHost() != null) { + return msg.getStoreHost().toString().replace("/", ""); + } else { + return null; + } + } + + public void endConcurrentlySpan(Span span, ConsumeConcurrentlyStatus status) { + span.setAttribute("messaging.rocketmq.consume_concurrently_status", status.name()); + } + + public void endOrderlySpan(Span span, ConsumeOrderlyStatus status) { + span.setAttribute("messaging.rocketmq.consume_orderly_status", status.name()); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java new file mode 100644 index 000000000000..2b8ff914c2f0 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -0,0 +1,48 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; +import static io.opentelemetry.api.trace.SpanKind.PRODUCER; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.instrumentation.api.tracer.BaseTracer; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; + +public class RocketMqProducerTracer extends BaseTracer { + + private static final RocketMqProducerTracer TRACER = new RocketMqProducerTracer(); + + public static RocketMqProducerTracer tracer() { + return TRACER; + } + + @Override + protected String getInstrumentationName() { + return "io.opentelemetry.javaagent.rocketmq-client"; + } + + public Span startProducerSpan(String addr, Message msg) { + Span span = startSpan(spanNameOnProduce(msg), PRODUCER); + onProduce(span, msg, addr); + return span; + } + + public void onCallback(Span span, SendResult sendResult) { + span.setAttribute("messaging.rocketmq.callback_result", sendResult.getSendStatus().name()); + } + + public void onProduce(Span span, Message msg, String addr) { + span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq"); + span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic"); + span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()); + span.setAttribute("messaging.rocketmq.tags", msg.getTags()); + span.setAttribute("messaging.rocketmq.broker_address", addr); + } + + public String spanNameOnProduce(Message msg) { + return msg.getTopic() + " send"; + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java new file mode 100644 index 000000000000..fb3bd39bf8df --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java @@ -0,0 +1,35 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; + +import io.opentelemetry.api.trace.Span; +import org.apache.rocketmq.client.producer.SendCallback; +import org.apache.rocketmq.client.producer.SendResult; + +public class SendCallbackWrapper implements SendCallback { + private final SendCallback sendCallback; + private final Span span; + + public SendCallbackWrapper(SendCallback sendCallback, Span span) { + this.sendCallback = sendCallback; + this.span = span; + } + + @Override + public void onSuccess(SendResult sendResult) { + tracer().onCallback(span, sendResult); + tracer().end(span); + sendCallback.onSuccess(sendResult); + } + + @Override + public void onException(Throwable e) { + tracer().endExceptionally(span, e); + sendCallback.onException(e); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java new file mode 100644 index 000000000000..c343e84b7ab8 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java @@ -0,0 +1,25 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.context.propagation.TextMapPropagator; +import java.util.Map; + +public class TextMapExtractAdapter implements TextMapPropagator.Getter> { + + public static final TextMapExtractAdapter GETTER = new TextMapExtractAdapter(); + + @Override + public Iterable keys(Map carrier) { + return carrier.keySet(); + } + + @Override + public String get(Map carrier, String key) { + String obj = carrier.get(key); + return obj; + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java new file mode 100644 index 000000000000..22ec6733bd81 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java @@ -0,0 +1,24 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader; + +public class TextMapInjectAdapter implements TextMapPropagator.Setter { + + public static final TextMapInjectAdapter SETTER = new TextMapInjectAdapter(); + + @Override + public void set(SendMessageRequestHeader header, String key, String value) { + StringBuilder properties = new StringBuilder(header.getProperties()); + properties.append(key); + properties.append('\u0001'); + properties.append(value); + properties.append('\u0002'); + header.setProperties(properties.toString()); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy new file mode 100644 index 000000000000..25e5e7483400 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -0,0 +1,13 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq + +import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest +import io.opentelemetry.instrumentation.test.LibraryTestTrait + +class RocketMqClientTest extends AbstractRocketMqClientTest implements LibraryTestTrait { + +} diff --git a/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle b/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle new file mode 100644 index 000000000000..ec6d24d3ce2a --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle @@ -0,0 +1,11 @@ +apply from: "$rootDir/gradle/java.gradle" + +dependencies { + api project(':testing-common') + api group: 'org.apache.rocketmq', name: 'rocketmq-test', version: '4.8.0' + + implementation deps.guava + implementation deps.groovy + implementation deps.opentelemetryApi + implementation deps.spock +} diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy new file mode 100644 index 000000000000..5e383e2a871a --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -0,0 +1,198 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetery.instrumentation.rocketmq + +import base.BaseConf +import io.opentelemetry.instrumentation.test.InstrumentationSpecification +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes +import org.apache.rocketmq.client.producer.DefaultMQProducer +import org.apache.rocketmq.client.producer.SendCallback +import org.apache.rocketmq.client.producer.SendResult +import org.apache.rocketmq.common.message.Message +import org.apache.rocketmq.remoting.common.RemotingHelper +import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer +import org.apache.rocketmq.test.client.rmq.RMQNormalProducer +import org.apache.rocketmq.test.factory.ProducerFactory +import org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener +import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener +import spock.lang.Shared +import spock.lang.Unroll +import static io.opentelemetry.api.trace.SpanKind.PRODUCER; +import static io.opentelemetry.api.trace.SpanKind.CONSUMER; +import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan +import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace + +@Unroll +abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ + + @Shared + RMQNormalConsumer consumer; + + @Shared + RMQNormalProducer producer; + + @Shared + DefaultMQProducer defaultMQProducer; + + @Shared + String sharedTopic; + + @Shared + String brokerAddr; + + @Shared + Message msg; + + @Shared + int consumeTime = 5000; + + @Shared + def baseConf =new BaseConf(); + + def setup() { + sharedTopic =baseConf.initTopic(); + brokerAddr =baseConf.getBrokerAddr() + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)); + } + + def "test rocketmq produce callback"() { + setup: + defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr); + when: + runUnderTrace("parent") { + defaultMQProducer.send(msg, new SendCallback() { + @Override + void onSuccess(SendResult sendResult) { + } + + @Override + void onException(Throwable throwable) { + } + }); + + } + then: + assertTraces(1) { + trace(0, 2) { + basicSpan(it, 0, "parent") + span(1) { + name sharedTopic + " send" + kind PRODUCER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.callback_result" "SEND_OK" + } + } + } + cleanup: + defaultMQProducer.shutdown() + } + } + + def "test rocketmq produce and concurrently consume"() { + setup: + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); + when: + runUnderTrace("parent") { + producer.send(msg); + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + } + then: + assertTraces(1) { + trace(0, 3) { + basicSpan(it, 0, "parent") + span(1) { + name sharedTopic + " send" + kind PRODUCER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + } + } + span(2) { + name sharedTopic + " process" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.consume_concurrently_status" "CONSUME_SUCCESS" + "messaging.rocketmq.queue_id" Long + "messaging.rocketmq.queue_offset" Long + + } + } + } + cleanup: + producer.shutdown() + consumer.shutdown() + + } + } + + + def "test rocketmq produce and orderly consume"() { + setup: + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()); + when: + runUnderTrace("parent") { + producer.send(msg); + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + } + then: + assertTraces(1) { + trace(0, 3) { + basicSpan(it, 0, "parent") + span(1) { + name sharedTopic + " send" + kind PRODUCER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + } + } + span(2) { + name sharedTopic + " process" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.consume_orderly_status" "SUCCESS" + "messaging.rocketmq.queue_id" Long + "messaging.rocketmq.queue_offset" Long + + } + } + } + cleanup: + producer.shutdown() + consumer.shutdown() + + } + } +} + diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java new file mode 100644 index 000000000000..b922a700f09e --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -0,0 +1,161 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package base; + +import java.util.ArrayList; +import java.util.List; +import org.apache.log4j.Logger; +import org.apache.rocketmq.broker.BrokerController; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.common.MQVersion; +import org.apache.rocketmq.namesrv.NamesrvController; +import org.apache.rocketmq.remoting.protocol.RemotingCommand; +import org.apache.rocketmq.test.client.rmq.RMQAsyncSendProducer; +import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer; +import org.apache.rocketmq.test.client.rmq.RMQNormalProducer; +import org.apache.rocketmq.test.client.rmq.RMQTransactionalProducer; +import org.apache.rocketmq.test.clientinterface.AbstractMQConsumer; +import org.apache.rocketmq.test.clientinterface.AbstractMQProducer; +import org.apache.rocketmq.test.factory.ConsumerFactory; +import org.apache.rocketmq.test.listener.AbstractListener; +import org.apache.rocketmq.test.util.MQAdmin; +import org.apache.rocketmq.test.util.MQRandomUtils; + +public class BaseConf { + public static String nsAddr; + public static String broker1Addr; + protected static String broker1Name; + protected static String clusterName; + protected static int brokerNum; + protected static NamesrvController namesrvController; + protected static BrokerController brokerController1; + protected static List mqClients = new ArrayList(); + protected static boolean debug = false; + private static Logger log = Logger.getLogger(BaseConf.class); + + static { + System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION)); + namesrvController = IntegrationTestBase.createAndStartNamesrv(); + nsAddr = "127.0.0.1:" + namesrvController.getNettyServerConfig().getListenPort(); + brokerController1 = IntegrationTestBase.createAndStartBroker(nsAddr); + clusterName = brokerController1.getBrokerConfig().getBrokerClusterName(); + broker1Name = brokerController1.getBrokerConfig().getBrokerName(); + broker1Addr = "127.0.0.1:" + brokerController1.getNettyServerConfig().getListenPort(); + brokerNum = 2; + } + + public BaseConf() { + + } + + public static String initTopic() { + String topic = MQRandomUtils.getRandomTopic(); + IntegrationTestBase.initTopic(topic, nsAddr, clusterName); + + return topic; + } + + public static String getBrokerAddr() { + return broker1Addr; + } + + public static String initConsumerGroup() { + String group = MQRandomUtils.getRandomConsumerGroup(); + return initConsumerGroup(group); + } + + public static String initConsumerGroup(String group) { + MQAdmin.createSub(nsAddr, clusterName, group); + return group; + } + + public static RMQNormalProducer getProducer(String nsAddr, String topic) { + return getProducer(nsAddr, topic, false); + } + + public static RMQNormalProducer getProducer(String nsAddr, String topic, boolean useTLS) { + RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, useTLS); + if (debug) { + producer.setDebug(); + } + mqClients.add(producer); + return producer; + } + + public static RMQTransactionalProducer getTransactionalProducer(String nsAddr, String topic, TransactionListener transactionListener) { + RMQTransactionalProducer producer = new RMQTransactionalProducer(nsAddr, topic, false, transactionListener); + if (debug) { + producer.setDebug(); + } + mqClients.add(producer); + return producer; + } + + public static RMQNormalProducer getProducer(String nsAddr, String topic, String producerGoup, + String instanceName) { + RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, producerGoup, + instanceName); + if (debug) { + producer.setDebug(); + } + mqClients.add(producer); + return producer; + } + + public static RMQAsyncSendProducer getAsyncProducer(String nsAddr, String topic) { + RMQAsyncSendProducer producer = new RMQAsyncSendProducer(nsAddr, topic); + if (debug) { + producer.setDebug(); + } + mqClients.add(producer); + return producer; + } + + public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, + AbstractListener listener) { + return getConsumer(nsAddr, topic, subExpression, listener, false); + } + + public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, + AbstractListener listener, boolean useTLS) { + String consumerGroup = initConsumerGroup(); + return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, useTLS); + } + + public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, + String subExpression, AbstractListener listener) { + return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, false); + } + + public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, + String subExpression, AbstractListener listener, boolean useTLS) { + RMQNormalConsumer consumer = ConsumerFactory.getRMQNormalConsumer(nsAddr, consumerGroup, + topic, subExpression, listener, useTLS); + if (debug) { + consumer.setDebug(); + } + mqClients.add(consumer); + log.info(String.format("consumer[%s] start,topic[%s],subExpression[%s]", consumerGroup, + topic, subExpression)); + return consumer; + } + + public static void shutdown() { + try { + for (Object mqClient : mqClients) { + if (mqClient instanceof AbstractMQProducer) { + ((AbstractMQProducer) mqClient).shutdown(); + + } else { + ((AbstractMQConsumer) mqClient).shutdown(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + } +} diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java new file mode 100644 index 000000000000..8080a3dbb121 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -0,0 +1,181 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ +package base; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.rocketmq.broker.BrokerController; +import org.apache.rocketmq.common.BrokerConfig; +import org.apache.rocketmq.common.UtilAll; +import org.apache.rocketmq.common.namesrv.NamesrvConfig; +import org.apache.rocketmq.logging.InternalLogger; +import org.apache.rocketmq.logging.InternalLoggerFactory; +import org.apache.rocketmq.namesrv.NamesrvController; +import org.apache.rocketmq.remoting.netty.NettyClientConfig; +import org.apache.rocketmq.remoting.netty.NettyServerConfig; +import org.apache.rocketmq.store.config.MessageStoreConfig; +import org.apache.rocketmq.test.util.MQAdmin; +import org.apache.rocketmq.test.util.TestUtils; +import org.junit.Assert; + +public class IntegrationTestBase { + public static InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); + + protected static final String SEP = File.separator; + protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; + protected static final AtomicInteger BROKER_INDEX = new AtomicInteger(0); + protected static final List TMPE_FILES = new ArrayList<>(); + protected static final List BROKER_CONTROLLERS = new ArrayList<>(); + protected static final List NAMESRV_CONTROLLERS = new ArrayList<>(); + protected static int topicCreateTime = 30 * 1000; + protected static final int COMMIT_LOG_SIZE = 1024 * 1024 * 100; + protected static final int INDEX_NUM = 1000; + private static final AtomicInteger port = new AtomicInteger(40000); + + public static synchronized int nextPort() { + return port.addAndGet(random.nextInt(10) + 10); + } + protected static Random random = new Random(); + + static { + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + try { + for (BrokerController brokerController : BROKER_CONTROLLERS) { + if (brokerController != null) { + brokerController.shutdown(); + } + } + + // should destroy message store, otherwise could not delete the temp files. + for (BrokerController brokerController : BROKER_CONTROLLERS) { + if (brokerController != null) { + brokerController.getMessageStore().destroy(); + } + } + + for (NamesrvController namesrvController : NAMESRV_CONTROLLERS) { + if (namesrvController != null) { + namesrvController.shutdown(); + } + } + for (File file : TMPE_FILES) { + UtilAll.deleteFile(file); + } + } catch (Exception e) { + logger.error("Shutdown error", e); + } + } + }); + + } + + public static String createBaseDir() { + String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); + final File file = new File(baseDir); + if (file.exists()) { + logger.info(String.format("[%s] has already existed, please back up and remove it for integration tests", baseDir)); + System.exit(1); + } + TMPE_FILES.add(file); + return baseDir; + } + + public static NamesrvController createAndStartNamesrv() { + String baseDir = createBaseDir(); + NamesrvConfig namesrvConfig = new NamesrvConfig(); + NettyServerConfig nameServerNettyServerConfig = new NettyServerConfig(); + namesrvConfig.setKvConfigPath(baseDir + SEP + "namesrv" + SEP + "kvConfig.json"); + namesrvConfig.setConfigStorePath(baseDir + SEP + "namesrv" + SEP + "namesrv.properties"); + + nameServerNettyServerConfig.setListenPort(nextPort()); + NamesrvController namesrvController = new NamesrvController(namesrvConfig, nameServerNettyServerConfig); + try { + Assert.assertTrue(namesrvController.initialize()); + logger.info("Name Server Start:{}", nameServerNettyServerConfig.getListenPort()); + namesrvController.start(); + } catch (Exception e) { + logger.info("Name Server start failed", e); + System.exit(1); + } + NAMESRV_CONTROLLERS.add(namesrvController); + return namesrvController; + + } + + public static BrokerController createAndStartBroker(String nsAddr) { + String baseDir = createBaseDir(); + BrokerConfig brokerConfig = new BrokerConfig(); + MessageStoreConfig storeConfig = new MessageStoreConfig(); + brokerConfig.setBrokerName(BROKER_NAME_PREFIX + BROKER_INDEX.getAndIncrement()); + brokerConfig.setBrokerIP1("127.0.0.1"); + brokerConfig.setNamesrvAddr(nsAddr); + brokerConfig.setEnablePropertyFilter(true); + storeConfig.setStorePathRootDir(baseDir); + storeConfig.setStorePathCommitLog(baseDir + SEP + "commitlog"); + storeConfig.setMappedFileSizeCommitLog(COMMIT_LOG_SIZE); + storeConfig.setMaxIndexNum(INDEX_NUM); + storeConfig.setMaxHashSlotNum(INDEX_NUM * 4); + return createAndStartBroker(storeConfig, brokerConfig); + + } + + public static BrokerController createAndStartBroker(MessageStoreConfig storeConfig, BrokerConfig brokerConfig) { + NettyServerConfig nettyServerConfig = new NettyServerConfig(); + NettyClientConfig nettyClientConfig = new NettyClientConfig(); + nettyServerConfig.setListenPort(nextPort()); + storeConfig.setHaListenPort(nextPort()); + BrokerController brokerController = new BrokerController(brokerConfig, nettyServerConfig, nettyClientConfig, storeConfig); + try { + Assert.assertTrue(brokerController.initialize()); + logger.info("Broker Start name:{} addr:{}", brokerConfig.getBrokerName(), brokerController.getBrokerAddr()); + brokerController.start(); + } catch (Throwable t) { + logger.error("Broker start failed, will exit", t); + System.exit(1); + } + BROKER_CONTROLLERS.add(brokerController); + return brokerController; + } + + public static boolean initTopic(String topic, String nsAddr, String clusterName, int queueNumbers) { + long startTime = System.currentTimeMillis(); + boolean createResult; + + while (true) { + createResult = MQAdmin.createTopic(nsAddr, clusterName, topic, queueNumbers); + if (createResult) { + break; + } else if (System.currentTimeMillis() - startTime > topicCreateTime) { + Assert.fail(String.format("topic[%s] is created failed after:%d ms", topic, + System.currentTimeMillis() - startTime)); + break; + } else { + TestUtils.waitForMoment(500); + continue; + } + } + + return createResult; + } + + public static boolean initTopic(String topic, String nsAddr, String clusterName) { + return initTopic(topic, nsAddr, clusterName, 8); + } + + public static void deleteFile(File file) { + if (!file.exists()) { + return; + } + UtilAll.deleteFile(file); + } + +} diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java new file mode 100644 index 000000000000..9db1670cd258 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java @@ -0,0 +1,109 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package base.dledger; + +import static base.IntegrationTestBase.nextPort; + + +import java.util.UUID; +import base.BaseConf; +import base.IntegrationTestBase; +import org.apache.rocketmq.broker.BrokerController; +import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; +import org.apache.rocketmq.client.consumer.PullResult; +import org.apache.rocketmq.client.consumer.PullStatus; +import org.apache.rocketmq.client.producer.DefaultMQProducer; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.rocketmq.common.BrokerConfig; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.message.MessageQueue; +import org.apache.rocketmq.store.config.BrokerRole; +import org.apache.rocketmq.store.config.MessageStoreConfig; + +import org.apache.rocketmq.test.factory.ConsumerFactory; +import org.apache.rocketmq.test.factory.ProducerFactory; +import org.junit.Assert; +import org.junit.Test; + +public class DLedgerProduceAndConsumeIT { + + public BrokerConfig buildBrokerConfig(String cluster, String brokerName) { + BrokerConfig brokerConfig = new BrokerConfig(); + brokerConfig.setBrokerClusterName(cluster); + brokerConfig.setBrokerName(brokerName); + brokerConfig.setBrokerIP1("127.0.0.1"); + brokerConfig.setNamesrvAddr(BaseConf.nsAddr); + return brokerConfig; + } + + public MessageStoreConfig buildStoreConfig(String brokerName, String peers, String selfId) { + MessageStoreConfig storeConfig = new MessageStoreConfig(); + String baseDir = IntegrationTestBase.createBaseDir(); + storeConfig.setStorePathRootDir(baseDir); + storeConfig.setStorePathCommitLog(baseDir + "_" + "commitlog"); + storeConfig.setHaListenPort(nextPort()); + storeConfig.setMappedFileSizeCommitLog(10 * 1024 * 1024); + storeConfig.setEnableDLegerCommitLog(true); + storeConfig.setdLegerGroup(brokerName); + storeConfig.setdLegerSelfId(selfId); + storeConfig.setdLegerPeers(peers); + return storeConfig; + } + + @Test + public void testProduceAndConsume() throws Exception { + String cluster = UUID.randomUUID().toString(); + String brokerName = UUID.randomUUID().toString(); + String selfId = "n0"; + String peers = String.format("n0-localhost:%d", nextPort()); + BrokerConfig brokerConfig = buildBrokerConfig(cluster, brokerName); + MessageStoreConfig storeConfig = buildStoreConfig(brokerName, peers, selfId); + BrokerController brokerController = IntegrationTestBase.createAndStartBroker(storeConfig, brokerConfig); + Thread.sleep(3000); + + Assert.assertEquals(BrokerRole.SYNC_MASTER, storeConfig.getBrokerRole()); + + String topic = UUID.randomUUID().toString(); + String consumerGroup = UUID.randomUUID().toString(); + IntegrationTestBase.initTopic(topic, BaseConf.nsAddr, cluster, 1); + DefaultMQProducer producer = ProducerFactory.getRMQProducer(BaseConf.nsAddr); + DefaultMQPullConsumer consumer = ConsumerFactory.getRMQPullConsumer(BaseConf.nsAddr, consumerGroup); + + for (int i = 0; i < 10; i++) { + Message message = new Message(); + message.setTopic(topic); + message.setBody(("Hello" + i).getBytes()); + SendResult sendResult = producer.send(message); + Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus()); + Assert.assertEquals(0, sendResult.getMessageQueue().getQueueId()); + Assert.assertEquals(brokerName, sendResult.getMessageQueue().getBrokerName()); + Assert.assertEquals(i, sendResult.getQueueOffset()); + Assert.assertNotNull(sendResult.getMsgId()); + Assert.assertNotNull(sendResult.getOffsetMsgId()); + } + + Thread.sleep(500); + Assert.assertEquals(0, brokerController.getMessageStore().getMinOffsetInQueue(topic, 0)); + Assert.assertEquals(10, brokerController.getMessageStore().getMaxOffsetInQueue(topic, 0)); + + MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0); + PullResult pullResult= consumer.pull(messageQueue, "*", 0, 32); + Assert.assertEquals(PullStatus.FOUND, pullResult.getPullStatus()); + Assert.assertEquals(10, pullResult.getMsgFoundList().size()); + + for (int i = 0; i < 10; i++) { + MessageExt messageExt = pullResult.getMsgFoundList().get(i); + Assert.assertEquals(i, messageExt.getQueueOffset()); + Assert.assertArrayEquals(("Hello" + i).getBytes(), messageExt.getBody()); + } + + producer.shutdown(); + consumer.shutdown(); + brokerController.shutdown(); + } +} diff --git a/settings.gradle b/settings.gradle index 0accec0f2ca5..efc1dee82a49 100644 --- a/settings.gradle +++ b/settings.gradle @@ -223,6 +223,9 @@ include ':instrumentation:undertow:javaagent' include ':instrumentation:vertx-web-3.0' include ':instrumentation:vertx-reactive-3.5:javaagent' include ':instrumentation:wicket-8.0:javaagent' +include ':instrumentation:rocketmq-client-4.8:javaagent' +include ':instrumentation:rocketmq-client-4.8:library' +include ':instrumentation:rocketmq-client-4.8:testing' include ':instrumentation-core:reactor-3.1' include ':instrumentation-core:servlet-2.2' @@ -249,3 +252,5 @@ def setBuildFile(project) { rootProject.children.each { setBuildFile(it) } + + From 2f2cfcfe3cc48a0700b5a6b3b1c76dbf85a01c4b Mon Sep 17 00:00:00 2001 From: addname Date: Fri, 12 Feb 2021 23:20:17 +0800 Subject: [PATCH 02/44] merge main --- .../instrumentation/rocketmq/RocketMqProducerTracer.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index 2b8ff914c2f0..7ec664aeb10f 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.rocketmq; import static io.opentelemetry.api.trace.SpanKind.PRODUCER; import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import org.apache.rocketmq.client.producer.SendResult; @@ -25,16 +26,16 @@ protected String getInstrumentationName() { } public Span startProducerSpan(String addr, Message msg) { - Span span = startSpan(spanNameOnProduce(msg), PRODUCER); + SpanBuilder span = spanBuilder(spanNameOnProduce(msg), PRODUCER); onProduce(span, msg, addr); - return span; + return span.startSpan(); } public void onCallback(Span span, SendResult sendResult) { span.setAttribute("messaging.rocketmq.callback_result", sendResult.getSendStatus().name()); } - public void onProduce(Span span, Message msg, String addr) { + public void onProduce(SpanBuilder span, Message msg, String addr) { span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq"); span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic"); span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()); From bce0921c5988c102f48ef6980f4ba5cb4746f993 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 11:58:48 +0800 Subject: [PATCH 03/44] modify tests --- .../rocketmq/RocketMqClientTest.groovy | 1 - .../rocketmq-client-4.8-library.gradle | 1 - .../rocketmq/TracingMessageInterceptor.java | 33 ++++ .../rocketmq/RocketMqClientTest.groovy | 21 ++- .../rocketmq-client-4.8-testing.gradle | 2 + .../AbstractRocketMqClientLibraryTest.groovy | 167 ++++++++++++++++++ .../AbstractRocketMqClientTest.groovy | 7 +- 7 files changed, 225 insertions(+), 7 deletions(-) create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java create mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index bbd337036f2c..2e8fa369e144 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -7,7 +7,6 @@ package io.opentelemetry.instrumentation.rocketmq import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest import io.opentelemetry.instrumentation.test.AgentTestTrait - class RocketMqClientTest extends AbstractRocketMqClientTest implements AgentTestTrait { diff --git a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle index b17a6c3038a7..3beae76dae0a 100644 --- a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle +++ b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle @@ -1,7 +1,6 @@ apply from: "$rootDir/gradle/instrumentation-library.gradle" dependencies { - implementation deps.opentelemetryTraceProps library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' testImplementation project(':instrumentation:rocketmq-client-4.8:testing') diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java new file mode 100644 index 000000000000..cbaf13f54cd3 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java @@ -0,0 +1,33 @@ +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.api.trace.Span; +import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; +import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageExt; +import java.util.List; + +public class TracingMessageInterceptor { + + public void producerIntercept(String addr, Message msg) { + Span span = RocketMqProducerTracer.tracer().startProducerSpan(addr, msg); + SendResult sendResult = new SendResult(); + sendResult.setSendStatus(SendStatus.SEND_OK); + RocketMqProducerTracer.tracer().onCallback(span, sendResult); + RocketMqProducerTracer.tracer().end(span); + } + + public void consumerConcurrentlyIntercept(List msgs) { + Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); + RocketMqConsumerTracer.tracer().endConcurrentlySpan(span, ConsumeConcurrentlyStatus.CONSUME_SUCCESS); + RocketMqConsumerTracer.tracer().tracer().end(span); + } + + public void consumerOrderlyIntercept(List msgs) { + Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); + RocketMqConsumerTracer.tracer().endOrderlySpan(span, ConsumeOrderlyStatus.SUCCESS); + RocketMqConsumerTracer.tracer().tracer().end(span); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 25e5e7483400..c9d586b21cb1 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -4,10 +4,25 @@ */ package io.opentelemetry.instrumentation.rocketmq - -import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest +import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientLibraryTest import io.opentelemetry.instrumentation.test.LibraryTestTrait +import org.apache.rocketmq.common.message.Message + +class RocketMqClientTest extends AbstractRocketMqClientLibraryTest implements LibraryTestTrait { -class RocketMqClientTest extends AbstractRocketMqClientTest implements LibraryTestTrait { + @Override + void producerIntercept(String addr,Message msg) { + TracingMessageInterceptor tracingProducerInterceptor= new TracingMessageInterceptor() + tracingProducerInterceptor.producerIntercept(addr,msg); + } + @Override + void consumerIntercept(List msg, String type) { + TracingMessageInterceptor tracingProducerInterceptor= new TracingMessageInterceptor() + if("concurrent".equals(type)){ + tracingProducerInterceptor.consumerConcurrentlyIntercept(msg); + } else { + tracingProducerInterceptor.consumerOrderlyIntercept(msg); + } + } } diff --git a/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle b/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle index ec6d24d3ce2a..6773527770ae 100644 --- a/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle +++ b/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle @@ -3,6 +3,8 @@ apply from: "$rootDir/gradle/java.gradle" dependencies { api project(':testing-common') api group: 'org.apache.rocketmq', name: 'rocketmq-test', version: '4.8.0' + api group: 'io.openmessaging', name: 'openmessaging-api', version: '0.3.1-alpha' + api group: 'org.apache.rocketmq', name: 'rocketmq-openmessaging', version: '4.8.0' implementation deps.guava implementation deps.groovy diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy new file mode 100644 index 000000000000..2a3ad3925190 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy @@ -0,0 +1,167 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetery.instrumentation.rocketmq + +import base.BaseConf +import io.opentelemetry.instrumentation.test.InstrumentationSpecification +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes +import org.apache.rocketmq.client.producer.DefaultMQProducer +import org.apache.rocketmq.common.message.Message +import org.apache.rocketmq.remoting.common.RemotingHelper +import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer +import org.apache.rocketmq.test.client.rmq.RMQNormalProducer +import org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener +import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener +import spock.lang.Shared +import spock.lang.Unroll + +import static io.opentelemetry.api.trace.SpanKind.CONSUMER +import static io.opentelemetry.api.trace.SpanKind.PRODUCER +import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan +import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace + +@Unroll +abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecification{ + + @Shared + RMQNormalConsumer consumer; + + @Shared + RMQNormalProducer producer; + + @Shared + DefaultMQProducer defaultMQProducer; + + @Shared + String sharedTopic; + + @Shared + String brokerAddr; + + @Shared + Message msg; + + @Shared + int consumeTime = 5000; + + @Shared + def baseConf =new BaseConf(); + + abstract void producerIntercept(String addr,Message msg) + + abstract void consumerIntercept(List msgs,String type) + + + def setup() { + sharedTopic =baseConf.initTopic(); + brokerAddr =baseConf.getBrokerAddr() + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)); + } + + def "test rocketmq produce callback"() { + setup: + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); + + when: + runUnderTrace("parent") { + producerIntercept(brokerAddr,msg); + producer.send(msg); + } + then: + assertTraces(1) { + trace(0, 2) { + basicSpan(it, 0, "parent") + span(1) { + name sharedTopic + " send" + kind PRODUCER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.callback_result" "SEND_OK" + } + } + } + cleanup: + defaultMQProducer.shutdown() + } + } + + def "test rocketmq concurrently consume"() { + setup: + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); + when: + producer.send(msg); + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) + consumerIntercept(consumer.getListener().getAllOriginMsg(),"concurrent") + then: + assertTraces(1) { + trace(0, 1) { + span(0) { + name sharedTopic + " process" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.consume_concurrently_status" "CONSUME_SUCCESS" + "messaging.rocketmq.queue_id" Long + "messaging.rocketmq.queue_offset" Long + + } + } + } + cleanup: + producer.shutdown() + consumer.shutdown() + + } + } + + def "test rocketmq orderly consume"() { + setup: + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()); + when: + producer.send(msg); + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + consumerIntercept(consumer.getListener().getAllOriginMsg(),"order") + then: + assertTraces(1) { + trace(0, 1) { + span(0) { + name sharedTopic + " process" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "messaging.rocketmq.tags" "TagA" + "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.consume_orderly_status" "SUCCESS" + "messaging.rocketmq.queue_id" Long + "messaging.rocketmq.queue_offset" Long + + } + } + } + cleanup: + producer.shutdown() + consumer.shutdown() + + } + } +} + diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 5e383e2a871a..2d70c5ef977a 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -12,6 +12,7 @@ import org.apache.rocketmq.client.producer.DefaultMQProducer import org.apache.rocketmq.client.producer.SendCallback import org.apache.rocketmq.client.producer.SendResult import org.apache.rocketmq.common.message.Message + import org.apache.rocketmq.remoting.common.RemotingHelper import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer import org.apache.rocketmq.test.client.rmq.RMQNormalProducer @@ -61,6 +62,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce callback"() { setup: defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr); + when: runUnderTrace("parent") { defaultMQProducer.send(msg, new SendCallback() { @@ -100,11 +102,13 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ setup: producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); + when: runUnderTrace("parent") { producer.send(msg); - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) } + then: assertTraces(1) { trace(0, 3) { @@ -145,7 +149,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ } } - def "test rocketmq produce and orderly consume"() { setup: producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); From 924b2e3158bdf303e3b104c509500c9e3502b171 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 12:18:54 +0800 Subject: [PATCH 04/44] modify tests --- .../rocketmq/TracingMessageInterceptor.java | 3 --- .../AbstractRocketMqClientLibraryTest.groovy | 16 ++++-------- .../AbstractRocketMqClientTest.groovy | 25 +++++++++---------- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java index cbaf13f54cd3..764e474576f1 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java @@ -13,9 +13,6 @@ public class TracingMessageInterceptor { public void producerIntercept(String addr, Message msg) { Span span = RocketMqProducerTracer.tracer().startProducerSpan(addr, msg); - SendResult sendResult = new SendResult(); - sendResult.setSendStatus(SendStatus.SEND_OK); - RocketMqProducerTracer.tracer().onCallback(span, sendResult); RocketMqProducerTracer.tracer().end(span); } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy index 2a3ad3925190..7fcadb870cba 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy @@ -8,7 +8,6 @@ package io.opentelemetery.instrumentation.rocketmq import base.BaseConf import io.opentelemetry.instrumentation.test.InstrumentationSpecification import io.opentelemetry.semconv.trace.attributes.SemanticAttributes -import org.apache.rocketmq.client.producer.DefaultMQProducer import org.apache.rocketmq.common.message.Message import org.apache.rocketmq.remoting.common.RemotingHelper import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer @@ -32,9 +31,6 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific @Shared RMQNormalProducer producer; - @Shared - DefaultMQProducer defaultMQProducer; - @Shared String sharedTopic; @@ -54,7 +50,6 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific abstract void consumerIntercept(List msgs,String type) - def setup() { sharedTopic =baseConf.initTopic(); brokerAddr =baseConf.getBrokerAddr() @@ -64,11 +59,10 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific def "test rocketmq produce callback"() { setup: producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); - when: runUnderTrace("parent") { - producerIntercept(brokerAddr,msg); - producer.send(msg); + producerIntercept(brokerAddr,msg) + producer.send(msg) } then: assertTraces(1) { @@ -83,12 +77,11 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" brokerAddr - "messaging.rocketmq.callback_result" "SEND_OK" } } } cleanup: - defaultMQProducer.shutdown() + producer.shutdown() } } @@ -97,7 +90,7 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); when: - producer.send(msg); + producer.send(msg) consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) consumerIntercept(consumer.getListener().getAllOriginMsg(),"concurrent") then: @@ -136,6 +129,7 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific producer.send(msg); consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); consumerIntercept(consumer.getListener().getAllOriginMsg(),"order") + then: assertTraces(1) { trace(0, 1) { diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 2d70c5ef977a..bb3d2bf0c8e6 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -12,7 +12,6 @@ import org.apache.rocketmq.client.producer.DefaultMQProducer import org.apache.rocketmq.client.producer.SendCallback import org.apache.rocketmq.client.producer.SendResult import org.apache.rocketmq.common.message.Message - import org.apache.rocketmq.remoting.common.RemotingHelper import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer import org.apache.rocketmq.test.client.rmq.RMQNormalProducer @@ -21,8 +20,8 @@ import org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener import spock.lang.Shared import spock.lang.Unroll -import static io.opentelemetry.api.trace.SpanKind.PRODUCER; -import static io.opentelemetry.api.trace.SpanKind.CONSUMER; +import static io.opentelemetry.api.trace.SpanKind.CONSUMER +import static io.opentelemetry.api.trace.SpanKind.PRODUCER import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace @@ -30,28 +29,28 @@ import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTra abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ @Shared - RMQNormalConsumer consumer; + private RMQNormalConsumer consumer; @Shared - RMQNormalProducer producer; + private RMQNormalProducer producer; @Shared DefaultMQProducer defaultMQProducer; @Shared - String sharedTopic; + private String sharedTopic; @Shared - String brokerAddr; + private String brokerAddr; @Shared Message msg; @Shared - int consumeTime = 5000; + int consumeTime = 1000; @Shared - def baseConf =new BaseConf(); + BaseConf baseConf =new BaseConf(); def setup() { sharedTopic =baseConf.initTopic(); @@ -62,7 +61,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce callback"() { setup: defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr); - when: runUnderTrace("parent") { defaultMQProducer.send(msg, new SendCallback() { @@ -93,8 +91,10 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ } } } + cleanup: defaultMQProducer.shutdown() + } } @@ -102,13 +102,11 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ setup: producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); - when: runUnderTrace("parent") { producer.send(msg); - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); } - then: assertTraces(1) { trace(0, 3) { @@ -149,6 +147,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ } } + def "test rocketmq produce and orderly consume"() { setup: producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); From fb411546229c05d03fc1bc77af63f2654cff0f63 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 14:53:59 +0800 Subject: [PATCH 05/44] modify tests --- .../rocketmq-client-4.8-library.gradle | 3 +- .../rocketmq/TracingMessageInterceptor.java | 7 ++-- .../AbstractRocketMqClientLibraryTest.groovy | 2 +- .../main/java/base/IntegrationTestBase.java | 35 ------------------- 4 files changed, 8 insertions(+), 39 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle index 3beae76dae0a..4357ed4f2d5f 100644 --- a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle +++ b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle @@ -1,7 +1,8 @@ apply from: "$rootDir/gradle/instrumentation-library.gradle" + dependencies { library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' testImplementation project(':instrumentation:rocketmq-client-4.8:testing') -} \ No newline at end of file +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java index 764e474576f1..0958fdb4886d 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java @@ -1,10 +1,13 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.instrumentation.rocketmq; import io.opentelemetry.api.trace.Span; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.client.producer.SendStatus; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.common.message.MessageExt; import java.util.List; diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy index 7fcadb870cba..8db435577258 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy @@ -56,7 +56,7 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)); } - def "test rocketmq produce callback"() { + def "test rocketmq produce"() { setup: producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); when: diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index 8080a3dbb121..e375876b50df 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -43,41 +43,6 @@ public static synchronized int nextPort() { } protected static Random random = new Random(); - static { - - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - try { - for (BrokerController brokerController : BROKER_CONTROLLERS) { - if (brokerController != null) { - brokerController.shutdown(); - } - } - - // should destroy message store, otherwise could not delete the temp files. - for (BrokerController brokerController : BROKER_CONTROLLERS) { - if (brokerController != null) { - brokerController.getMessageStore().destroy(); - } - } - - for (NamesrvController namesrvController : NAMESRV_CONTROLLERS) { - if (namesrvController != null) { - namesrvController.shutdown(); - } - } - for (File file : TMPE_FILES) { - UtilAll.deleteFile(file); - } - } catch (Exception e) { - logger.error("Shutdown error", e); - } - } - }); - - } - public static String createBaseDir() { String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); final File file = new File(baseDir); From 451d3a574bbf50d9aee854d579219a0de1acbcab Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 15:51:44 +0800 Subject: [PATCH 06/44] modify style --- .../RocketMqClientApiImplInstrumentation.java | 2 +- ...tMqConcurrentlyConsumeInstrumentation.java | 2 +- ...RocketMqOrderlyConsumeInstrumentation.java | 2 +- .../rocketmq/RocketMqClientConfig.java | 1 + .../rocketmq/RocketMqConsumerTracer.java | 1 + .../rocketmq/RocketMqProducerTracer.java | 2 + .../rocketmq/TracingMessageInterceptor.java | 2 +- .../testing/src/main/java/base/BaseConf.java | 213 +++++++++--------- .../main/java/base/IntegrationTestBase.java | 2 + .../dledger/DLedgerProduceAndConsumeIT.java | 55 +---- 10 files changed, 119 insertions(+), 163 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java index f0bf296e91fe..0f08c840ae6c 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java @@ -15,10 +15,10 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.rocketmq.SendCallbackWrapper; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.tooling.TypeInstrumentation; import java.util.Map; -import io.opentelemetry.instrumentation.rocketmq.SendCallbackWrapper; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java index 305eb42b38d2..3b731009e597 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java @@ -5,8 +5,8 @@ package io.opentelemetry.javaagent.instrumentation.rocketmq; -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java index 9b987ec1e28e..e2d3d455e55e 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java @@ -5,8 +5,8 @@ package io.opentelemetry.javaagent.instrumentation.rocketmq; -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; import static net.bytebuddy.matcher.ElementMatchers.named; diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java index 3b54c33902c0..ba0b631846bb 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java @@ -4,6 +4,7 @@ */ package io.opentelemetry.instrumentation.rocketmq; + import io.opentelemetry.instrumentation.api.config.Config; public final class RocketMqClientConfig { diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index 1933fd53f004..f53d83c16bf1 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -7,6 +7,7 @@ import static io.opentelemetry.api.trace.SpanKind.CONSUMER; import static io.opentelemetry.instrumentation.rocketmq.TextMapExtractAdapter.GETTER; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index 7ec664aeb10f..ad344db98dcd 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -4,7 +4,9 @@ */ package io.opentelemetry.instrumentation.rocketmq; + import static io.opentelemetry.api.trace.SpanKind.PRODUCER; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java index 0958fdb4886d..f4e6fcb88a8f 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java @@ -6,11 +6,11 @@ package io.opentelemetry.instrumentation.rocketmq; import io.opentelemetry.api.trace.Span; +import java.util.List; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.common.message.MessageExt; -import java.util.List; public class TracingMessageInterceptor { diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index b922a700f09e..a2b1d47a3052 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -25,137 +25,140 @@ import org.apache.rocketmq.test.util.MQRandomUtils; public class BaseConf { - public static String nsAddr; + public static String nsAddr; public static String broker1Addr; - protected static String broker1Name; - protected static String clusterName; - protected static int brokerNum; - protected static NamesrvController namesrvController; - protected static BrokerController brokerController1; - protected static List mqClients = new ArrayList(); - protected static boolean debug = false; - private static Logger log = Logger.getLogger(BaseConf.class); - - static { - System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION)); - namesrvController = IntegrationTestBase.createAndStartNamesrv(); - nsAddr = "127.0.0.1:" + namesrvController.getNettyServerConfig().getListenPort(); - brokerController1 = IntegrationTestBase.createAndStartBroker(nsAddr); - clusterName = brokerController1.getBrokerConfig().getBrokerClusterName(); - broker1Name = brokerController1.getBrokerConfig().getBrokerName(); - broker1Addr = "127.0.0.1:" + brokerController1.getNettyServerConfig().getListenPort(); - brokerNum = 2; - } + protected static String broker1Name; + protected static String clusterName; + protected static int brokerNum; + protected static NamesrvController namesrvController; + protected static BrokerController brokerController1; + protected static List mqClients = new ArrayList(); + protected static boolean debug = false; + private static Logger log = Logger.getLogger(BaseConf.class); + + static { + System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, + Integer.toString(MQVersion.CURRENT_VERSION)); + namesrvController = IntegrationTestBase.createAndStartNamesrv(); + nsAddr = "127.0.0.1:" + namesrvController.getNettyServerConfig().getListenPort(); + brokerController1 = IntegrationTestBase.createAndStartBroker(nsAddr); + clusterName = brokerController1.getBrokerConfig().getBrokerClusterName(); + broker1Name = brokerController1.getBrokerConfig().getBrokerName(); + broker1Addr = "127.0.0.1:" + brokerController1.getNettyServerConfig().getListenPort(); + brokerNum = 2; + } - public BaseConf() { + public BaseConf() { - } + } - public static String initTopic() { - String topic = MQRandomUtils.getRandomTopic(); - IntegrationTestBase.initTopic(topic, nsAddr, clusterName); + public static String initTopic() { + String topic = MQRandomUtils.getRandomTopic(); + IntegrationTestBase.initTopic(topic, nsAddr, clusterName); - return topic; - } + return topic; + } public static String getBrokerAddr() { return broker1Addr; } - public static String initConsumerGroup() { - String group = MQRandomUtils.getRandomConsumerGroup(); - return initConsumerGroup(group); - } + public static String initConsumerGroup() { + String group = MQRandomUtils.getRandomConsumerGroup(); + return initConsumerGroup(group); + } - public static String initConsumerGroup(String group) { - MQAdmin.createSub(nsAddr, clusterName, group); - return group; - } + public static String initConsumerGroup(String group) { + MQAdmin.createSub(nsAddr, clusterName, group); + return group; + } - public static RMQNormalProducer getProducer(String nsAddr, String topic) { - return getProducer(nsAddr, topic, false); - } + public static RMQNormalProducer getProducer(String nsAddr, String topic) { + return getProducer(nsAddr, topic, false); + } - public static RMQNormalProducer getProducer(String nsAddr, String topic, boolean useTLS) { - RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, useTLS); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; + public static RMQNormalProducer getProducer(String nsAddr, String topic, boolean useTls) { + RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, useTls); + if (debug) { + producer.setDebug(); } + mqClients.add(producer); + return producer; + } - public static RMQTransactionalProducer getTransactionalProducer(String nsAddr, String topic, TransactionListener transactionListener) { - RMQTransactionalProducer producer = new RMQTransactionalProducer(nsAddr, topic, false, transactionListener); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; + public static RMQNormalProducer getProducer(String nsAddr, String topic, String producerGoup, + String instanceName) { + RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, producerGoup, + instanceName); + if (debug) { + producer.setDebug(); } + mqClients.add(producer); + return producer; + } - public static RMQNormalProducer getProducer(String nsAddr, String topic, String producerGoup, - String instanceName) { - RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, producerGoup, - instanceName); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; + public static RMQTransactionalProducer getTransactionalProducer(String nsAddr, String topic, + TransactionListener transactionListener) { + RMQTransactionalProducer producer = new RMQTransactionalProducer(nsAddr, topic, false, + transactionListener); + if (debug) { + producer.setDebug(); } + mqClients.add(producer); + return producer; + } - public static RMQAsyncSendProducer getAsyncProducer(String nsAddr, String topic) { - RMQAsyncSendProducer producer = new RMQAsyncSendProducer(nsAddr, topic); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; + public static RMQAsyncSendProducer getAsyncProducer(String nsAddr, String topic) { + RMQAsyncSendProducer producer = new RMQAsyncSendProducer(nsAddr, topic); + if (debug) { + producer.setDebug(); } + mqClients.add(producer); + return producer; + } - public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, - AbstractListener listener) { - return getConsumer(nsAddr, topic, subExpression, listener, false); - } + public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, + AbstractListener listener) { + return getConsumer(nsAddr, topic, subExpression, listener, false); + } - public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, - AbstractListener listener, boolean useTLS) { - String consumerGroup = initConsumerGroup(); - return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, useTLS); - } + public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, + AbstractListener listener, boolean useTls) { + String consumerGroup = initConsumerGroup(); + return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, useTls); + } - public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, - String subExpression, AbstractListener listener) { - return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, false); - } + public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, + String subExpression, AbstractListener listener) { + return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, false); + } - public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, - String subExpression, AbstractListener listener, boolean useTLS) { - RMQNormalConsumer consumer = ConsumerFactory.getRMQNormalConsumer(nsAddr, consumerGroup, - topic, subExpression, listener, useTLS); - if (debug) { - consumer.setDebug(); - } - mqClients.add(consumer); - log.info(String.format("consumer[%s] start,topic[%s],subExpression[%s]", consumerGroup, - topic, subExpression)); - return consumer; + public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, + String subExpression, AbstractListener listener, boolean useTls) { + RMQNormalConsumer consumer = ConsumerFactory.getRMQNormalConsumer(nsAddr, consumerGroup, + topic, subExpression, listener, useTls); + if (debug) { + consumer.setDebug(); } + mqClients.add(consumer); + log.info(String.format("consumer[%s] start,topic[%s],subExpression[%s]", consumerGroup, + topic, subExpression)); + return consumer; + } - public static void shutdown() { - try { - for (Object mqClient : mqClients) { - if (mqClient instanceof AbstractMQProducer) { - ((AbstractMQProducer) mqClient).shutdown(); - - } else { - ((AbstractMQConsumer) mqClient).shutdown(); - } - } - } catch (Exception e) { - e.printStackTrace(); - } + public static void shutdown() { + try { + for (Object mqClient : mqClients) { + if (mqClient instanceof AbstractMQProducer) { + ((AbstractMQProducer) mqClient).shutdown(); + } else { + ((AbstractMQConsumer) mqClient).shutdown(); + } + } + } catch (Exception e) { + e.printStackTrace(); } + + } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index e375876b50df..4ffb8abc74ba 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -2,6 +2,7 @@ * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 */ + package base; import java.io.File; @@ -41,6 +42,7 @@ public class IntegrationTestBase { public static synchronized int nextPort() { return port.addAndGet(random.nextInt(10) + 10); } + protected static Random random = new Random(); public static String createBaseDir() { diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java index 9db1670cd258..0417c543a19b 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java @@ -7,10 +7,9 @@ import static base.IntegrationTestBase.nextPort; - -import java.util.UUID; import base.BaseConf; import base.IntegrationTestBase; +import java.util.UUID; import org.apache.rocketmq.broker.BrokerController; import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; import org.apache.rocketmq.client.consumer.PullResult; @@ -24,7 +23,6 @@ import org.apache.rocketmq.common.message.MessageQueue; import org.apache.rocketmq.store.config.BrokerRole; import org.apache.rocketmq.store.config.MessageStoreConfig; - import org.apache.rocketmq.test.factory.ConsumerFactory; import org.apache.rocketmq.test.factory.ProducerFactory; import org.junit.Assert; @@ -55,55 +53,4 @@ public MessageStoreConfig buildStoreConfig(String brokerName, String peers, Stri return storeConfig; } - @Test - public void testProduceAndConsume() throws Exception { - String cluster = UUID.randomUUID().toString(); - String brokerName = UUID.randomUUID().toString(); - String selfId = "n0"; - String peers = String.format("n0-localhost:%d", nextPort()); - BrokerConfig brokerConfig = buildBrokerConfig(cluster, brokerName); - MessageStoreConfig storeConfig = buildStoreConfig(brokerName, peers, selfId); - BrokerController brokerController = IntegrationTestBase.createAndStartBroker(storeConfig, brokerConfig); - Thread.sleep(3000); - - Assert.assertEquals(BrokerRole.SYNC_MASTER, storeConfig.getBrokerRole()); - - String topic = UUID.randomUUID().toString(); - String consumerGroup = UUID.randomUUID().toString(); - IntegrationTestBase.initTopic(topic, BaseConf.nsAddr, cluster, 1); - DefaultMQProducer producer = ProducerFactory.getRMQProducer(BaseConf.nsAddr); - DefaultMQPullConsumer consumer = ConsumerFactory.getRMQPullConsumer(BaseConf.nsAddr, consumerGroup); - - for (int i = 0; i < 10; i++) { - Message message = new Message(); - message.setTopic(topic); - message.setBody(("Hello" + i).getBytes()); - SendResult sendResult = producer.send(message); - Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus()); - Assert.assertEquals(0, sendResult.getMessageQueue().getQueueId()); - Assert.assertEquals(brokerName, sendResult.getMessageQueue().getBrokerName()); - Assert.assertEquals(i, sendResult.getQueueOffset()); - Assert.assertNotNull(sendResult.getMsgId()); - Assert.assertNotNull(sendResult.getOffsetMsgId()); - } - - Thread.sleep(500); - Assert.assertEquals(0, brokerController.getMessageStore().getMinOffsetInQueue(topic, 0)); - Assert.assertEquals(10, brokerController.getMessageStore().getMaxOffsetInQueue(topic, 0)); - - MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0); - PullResult pullResult= consumer.pull(messageQueue, "*", 0, 32); - Assert.assertEquals(PullStatus.FOUND, pullResult.getPullStatus()); - Assert.assertEquals(10, pullResult.getMsgFoundList().size()); - - for (int i = 0; i < 10; i++) { - MessageExt messageExt = pullResult.getMsgFoundList().get(i); - Assert.assertEquals(i, messageExt.getQueueOffset()); - Assert.assertArrayEquals(("Hello" + i).getBytes(), messageExt.getBody()); - } - - producer.shutdown(); - consumer.shutdown(); - brokerController.shutdown(); - } } From 24d1d4a57e29d207d87be72ecaf5e8e3aca52bf7 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 16:16:56 +0800 Subject: [PATCH 07/44] modify style --- .../rocketmq/RocketMqClientTest.groovy | 1 + .../rocketmq/TracingMessageInterceptor.java | 7 +- .../testing/src/main/java/base/BaseConf.java | 63 +++-- .../main/java/base/IntegrationTestBase.java | 231 +++++++++--------- .../dledger/DLedgerProduceAndConsumeIT.java | 59 ++--- 5 files changed, 183 insertions(+), 178 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 2e8fa369e144..4014df9ed698 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -2,6 +2,7 @@ * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 */ + package io.opentelemetry.instrumentation.rocketmq import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java index f4e6fcb88a8f..af1ac2df8d49 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java @@ -20,13 +20,14 @@ public void producerIntercept(String addr, Message msg) { } public void consumerConcurrentlyIntercept(List msgs) { - Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); - RocketMqConsumerTracer.tracer().endConcurrentlySpan(span, ConsumeConcurrentlyStatus.CONSUME_SUCCESS); + Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); + RocketMqConsumerTracer.tracer() + .endConcurrentlySpan(span, ConsumeConcurrentlyStatus.CONSUME_SUCCESS); RocketMqConsumerTracer.tracer().tracer().end(span); } public void consumerOrderlyIntercept(List msgs) { - Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); + Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); RocketMqConsumerTracer.tracer().endOrderlySpan(span, ConsumeOrderlyStatus.SUCCESS); RocketMqConsumerTracer.tracer().tracer().end(span); } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index a2b1d47a3052..c6482edf570a 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -37,8 +37,8 @@ public class BaseConf { private static Logger log = Logger.getLogger(BaseConf.class); static { - System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, - Integer.toString(MQVersion.CURRENT_VERSION)); + System.setProperty( + RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION)); namesrvController = IntegrationTestBase.createAndStartNamesrv(); nsAddr = "127.0.0.1:" + namesrvController.getNettyServerConfig().getListenPort(); brokerController1 = IntegrationTestBase.createAndStartBroker(nsAddr); @@ -48,9 +48,7 @@ public class BaseConf { brokerNum = 2; } - public BaseConf() { - - } + public BaseConf() {} public static String initTopic() { String topic = MQRandomUtils.getRandomTopic(); @@ -86,10 +84,9 @@ public static RMQNormalProducer getProducer(String nsAddr, String topic, boolean return producer; } - public static RMQNormalProducer getProducer(String nsAddr, String topic, String producerGoup, - String instanceName) { - RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, producerGoup, - instanceName); + public static RMQNormalProducer getProducer( + String nsAddr, String topic, String producerGoup, String instanceName) { + RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, producerGoup, instanceName); if (debug) { producer.setDebug(); } @@ -97,10 +94,10 @@ public static RMQNormalProducer getProducer(String nsAddr, String topic, String return producer; } - public static RMQTransactionalProducer getTransactionalProducer(String nsAddr, String topic, - TransactionListener transactionListener) { - RMQTransactionalProducer producer = new RMQTransactionalProducer(nsAddr, topic, false, - transactionListener); + public static RMQTransactionalProducer getTransactionalProducer( + String nsAddr, String topic, TransactionListener transactionListener) { + RMQTransactionalProducer producer = + new RMQTransactionalProducer(nsAddr, topic, false, transactionListener); if (debug) { producer.setDebug(); } @@ -117,32 +114,47 @@ public static RMQAsyncSendProducer getAsyncProducer(String nsAddr, String topic) return producer; } - public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, - AbstractListener listener) { + public static RMQNormalConsumer getConsumer( + String nsAddr, String topic, String subExpression, AbstractListener listener) { return getConsumer(nsAddr, topic, subExpression, listener, false); } - public static RMQNormalConsumer getConsumer(String nsAddr, String topic, String subExpression, - AbstractListener listener, boolean useTls) { + public static RMQNormalConsumer getConsumer( + String nsAddr, + String topic, + String subExpression, + AbstractListener listener, + boolean useTls) { String consumerGroup = initConsumerGroup(); return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, useTls); } - public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, - String subExpression, AbstractListener listener) { + public static RMQNormalConsumer getConsumer( + String nsAddr, + String consumerGroup, + String topic, + String subExpression, + AbstractListener listener) { return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, false); } - public static RMQNormalConsumer getConsumer(String nsAddr, String consumerGroup, String topic, - String subExpression, AbstractListener listener, boolean useTls) { - RMQNormalConsumer consumer = ConsumerFactory.getRMQNormalConsumer(nsAddr, consumerGroup, - topic, subExpression, listener, useTls); + public static RMQNormalConsumer getConsumer( + String nsAddr, + String consumerGroup, + String topic, + String subExpression, + AbstractListener listener, + boolean useTls) { + RMQNormalConsumer consumer = + ConsumerFactory.getRMQNormalConsumer( + nsAddr, consumerGroup, topic, subExpression, listener, useTls); if (debug) { consumer.setDebug(); } mqClients.add(consumer); - log.info(String.format("consumer[%s] start,topic[%s],subExpression[%s]", consumerGroup, - topic, subExpression)); + log.info( + String.format( + "consumer[%s] start,topic[%s],subExpression[%s]", consumerGroup, topic, subExpression)); return consumer; } @@ -159,6 +171,5 @@ public static void shutdown() { } catch (Exception e) { e.printStackTrace(); } - } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index 4ffb8abc74ba..678b003a6ffa 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -26,123 +26,132 @@ import org.junit.Assert; public class IntegrationTestBase { - public static InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); - - protected static final String SEP = File.separator; - protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; - protected static final AtomicInteger BROKER_INDEX = new AtomicInteger(0); - protected static final List TMPE_FILES = new ArrayList<>(); - protected static final List BROKER_CONTROLLERS = new ArrayList<>(); - protected static final List NAMESRV_CONTROLLERS = new ArrayList<>(); - protected static int topicCreateTime = 30 * 1000; - protected static final int COMMIT_LOG_SIZE = 1024 * 1024 * 100; - protected static final int INDEX_NUM = 1000; - private static final AtomicInteger port = new AtomicInteger(40000); - - public static synchronized int nextPort() { - return port.addAndGet(random.nextInt(10) + 10); + public static InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); + + protected static final String SEP = File.separator; + protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; + protected static final AtomicInteger BROKER_INDEX = new AtomicInteger(0); + protected static final List TMPE_FILES = new ArrayList<>(); + protected static final List BROKER_CONTROLLERS = new ArrayList<>(); + protected static final List NAMESRV_CONTROLLERS = new ArrayList<>(); + protected static int topicCreateTime = 30 * 1000; + protected static final int COMMIT_LOG_SIZE = 1024 * 1024 * 100; + protected static final int INDEX_NUM = 1000; + private static final AtomicInteger port = new AtomicInteger(40000); + + public static synchronized int nextPort() { + return port.addAndGet(random.nextInt(10) + 10); + } + + protected static Random random = new Random(); + + public static String createBaseDir() { + String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); + final File file = new File(baseDir); + if (file.exists()) { + logger.info( + String.format( + "[%s] has already existed, please back up and remove it for integration tests", + baseDir)); + System.exit(1); } - - protected static Random random = new Random(); - - public static String createBaseDir() { - String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); - final File file = new File(baseDir); - if (file.exists()) { - logger.info(String.format("[%s] has already existed, please back up and remove it for integration tests", baseDir)); - System.exit(1); - } - TMPE_FILES.add(file); - return baseDir; + TMPE_FILES.add(file); + return baseDir; + } + + public static NamesrvController createAndStartNamesrv() { + String baseDir = createBaseDir(); + NamesrvConfig namesrvConfig = new NamesrvConfig(); + NettyServerConfig nameServerNettyServerConfig = new NettyServerConfig(); + namesrvConfig.setKvConfigPath(baseDir + SEP + "namesrv" + SEP + "kvConfig.json"); + namesrvConfig.setConfigStorePath(baseDir + SEP + "namesrv" + SEP + "namesrv.properties"); + + nameServerNettyServerConfig.setListenPort(nextPort()); + NamesrvController namesrvController = + new NamesrvController(namesrvConfig, nameServerNettyServerConfig); + try { + Assert.assertTrue(namesrvController.initialize()); + logger.info("Name Server Start:{}", nameServerNettyServerConfig.getListenPort()); + namesrvController.start(); + } catch (Exception e) { + logger.info("Name Server start failed", e); + System.exit(1); } - - public static NamesrvController createAndStartNamesrv() { - String baseDir = createBaseDir(); - NamesrvConfig namesrvConfig = new NamesrvConfig(); - NettyServerConfig nameServerNettyServerConfig = new NettyServerConfig(); - namesrvConfig.setKvConfigPath(baseDir + SEP + "namesrv" + SEP + "kvConfig.json"); - namesrvConfig.setConfigStorePath(baseDir + SEP + "namesrv" + SEP + "namesrv.properties"); - - nameServerNettyServerConfig.setListenPort(nextPort()); - NamesrvController namesrvController = new NamesrvController(namesrvConfig, nameServerNettyServerConfig); - try { - Assert.assertTrue(namesrvController.initialize()); - logger.info("Name Server Start:{}", nameServerNettyServerConfig.getListenPort()); - namesrvController.start(); - } catch (Exception e) { - logger.info("Name Server start failed", e); - System.exit(1); - } - NAMESRV_CONTROLLERS.add(namesrvController); - return namesrvController; - - } - - public static BrokerController createAndStartBroker(String nsAddr) { - String baseDir = createBaseDir(); - BrokerConfig brokerConfig = new BrokerConfig(); - MessageStoreConfig storeConfig = new MessageStoreConfig(); - brokerConfig.setBrokerName(BROKER_NAME_PREFIX + BROKER_INDEX.getAndIncrement()); - brokerConfig.setBrokerIP1("127.0.0.1"); - brokerConfig.setNamesrvAddr(nsAddr); - brokerConfig.setEnablePropertyFilter(true); - storeConfig.setStorePathRootDir(baseDir); - storeConfig.setStorePathCommitLog(baseDir + SEP + "commitlog"); - storeConfig.setMappedFileSizeCommitLog(COMMIT_LOG_SIZE); - storeConfig.setMaxIndexNum(INDEX_NUM); - storeConfig.setMaxHashSlotNum(INDEX_NUM * 4); - return createAndStartBroker(storeConfig, brokerConfig); - + NAMESRV_CONTROLLERS.add(namesrvController); + return namesrvController; + } + + public static BrokerController createAndStartBroker(String nsAddr) { + String baseDir = createBaseDir(); + BrokerConfig brokerConfig = new BrokerConfig(); + MessageStoreConfig storeConfig = new MessageStoreConfig(); + brokerConfig.setBrokerName(BROKER_NAME_PREFIX + BROKER_INDEX.getAndIncrement()); + brokerConfig.setBrokerIP1("127.0.0.1"); + brokerConfig.setNamesrvAddr(nsAddr); + brokerConfig.setEnablePropertyFilter(true); + storeConfig.setStorePathRootDir(baseDir); + storeConfig.setStorePathCommitLog(baseDir + SEP + "commitlog"); + storeConfig.setMappedFileSizeCommitLog(COMMIT_LOG_SIZE); + storeConfig.setMaxIndexNum(INDEX_NUM); + storeConfig.setMaxHashSlotNum(INDEX_NUM * 4); + return createAndStartBroker(storeConfig, brokerConfig); + } + + public static BrokerController createAndStartBroker( + MessageStoreConfig storeConfig, BrokerConfig brokerConfig) { + NettyServerConfig nettyServerConfig = new NettyServerConfig(); + NettyClientConfig nettyClientConfig = new NettyClientConfig(); + nettyServerConfig.setListenPort(nextPort()); + storeConfig.setHaListenPort(nextPort()); + BrokerController brokerController = + new BrokerController(brokerConfig, nettyServerConfig, nettyClientConfig, storeConfig); + try { + Assert.assertTrue(brokerController.initialize()); + logger.info( + "Broker Start name:{} addr:{}", + brokerConfig.getBrokerName(), + brokerController.getBrokerAddr()); + brokerController.start(); + } catch (Throwable t) { + logger.error("Broker start failed, will exit", t); + System.exit(1); } - - public static BrokerController createAndStartBroker(MessageStoreConfig storeConfig, BrokerConfig brokerConfig) { - NettyServerConfig nettyServerConfig = new NettyServerConfig(); - NettyClientConfig nettyClientConfig = new NettyClientConfig(); - nettyServerConfig.setListenPort(nextPort()); - storeConfig.setHaListenPort(nextPort()); - BrokerController brokerController = new BrokerController(brokerConfig, nettyServerConfig, nettyClientConfig, storeConfig); - try { - Assert.assertTrue(brokerController.initialize()); - logger.info("Broker Start name:{} addr:{}", brokerConfig.getBrokerName(), brokerController.getBrokerAddr()); - brokerController.start(); - } catch (Throwable t) { - logger.error("Broker start failed, will exit", t); - System.exit(1); - } - BROKER_CONTROLLERS.add(brokerController); - return brokerController; + BROKER_CONTROLLERS.add(brokerController); + return brokerController; + } + + public static boolean initTopic( + String topic, String nsAddr, String clusterName, int queueNumbers) { + long startTime = System.currentTimeMillis(); + boolean createResult; + + while (true) { + createResult = MQAdmin.createTopic(nsAddr, clusterName, topic, queueNumbers); + if (createResult) { + break; + } else if (System.currentTimeMillis() - startTime > topicCreateTime) { + Assert.fail( + String.format( + "topic[%s] is created failed after:%d ms", + topic, System.currentTimeMillis() - startTime)); + break; + } else { + TestUtils.waitForMoment(500); + continue; + } } - public static boolean initTopic(String topic, String nsAddr, String clusterName, int queueNumbers) { - long startTime = System.currentTimeMillis(); - boolean createResult; - - while (true) { - createResult = MQAdmin.createTopic(nsAddr, clusterName, topic, queueNumbers); - if (createResult) { - break; - } else if (System.currentTimeMillis() - startTime > topicCreateTime) { - Assert.fail(String.format("topic[%s] is created failed after:%d ms", topic, - System.currentTimeMillis() - startTime)); - break; - } else { - TestUtils.waitForMoment(500); - continue; - } - } - - return createResult; - } + return createResult; + } - public static boolean initTopic(String topic, String nsAddr, String clusterName) { - return initTopic(topic, nsAddr, clusterName, 8); - } + public static boolean initTopic(String topic, String nsAddr, String clusterName) { + return initTopic(topic, nsAddr, clusterName, 8); + } - public static void deleteFile(File file) { - if (!file.exists()) { - return; - } - UtilAll.deleteFile(file); + public static void deleteFile(File file) { + if (!file.exists()) { + return; } - + UtilAll.deleteFile(file); + } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java index 0417c543a19b..25f133ac867d 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java @@ -9,48 +9,31 @@ import base.BaseConf; import base.IntegrationTestBase; -import java.util.UUID; -import org.apache.rocketmq.broker.BrokerController; -import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; -import org.apache.rocketmq.client.consumer.PullResult; -import org.apache.rocketmq.client.consumer.PullStatus; -import org.apache.rocketmq.client.producer.DefaultMQProducer; -import org.apache.rocketmq.client.producer.SendResult; -import org.apache.rocketmq.client.producer.SendStatus; import org.apache.rocketmq.common.BrokerConfig; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.message.MessageQueue; -import org.apache.rocketmq.store.config.BrokerRole; import org.apache.rocketmq.store.config.MessageStoreConfig; -import org.apache.rocketmq.test.factory.ConsumerFactory; -import org.apache.rocketmq.test.factory.ProducerFactory; -import org.junit.Assert; -import org.junit.Test; public class DLedgerProduceAndConsumeIT { - public BrokerConfig buildBrokerConfig(String cluster, String brokerName) { - BrokerConfig brokerConfig = new BrokerConfig(); - brokerConfig.setBrokerClusterName(cluster); - brokerConfig.setBrokerName(brokerName); - brokerConfig.setBrokerIP1("127.0.0.1"); - brokerConfig.setNamesrvAddr(BaseConf.nsAddr); - return brokerConfig; - } - - public MessageStoreConfig buildStoreConfig(String brokerName, String peers, String selfId) { - MessageStoreConfig storeConfig = new MessageStoreConfig(); - String baseDir = IntegrationTestBase.createBaseDir(); - storeConfig.setStorePathRootDir(baseDir); - storeConfig.setStorePathCommitLog(baseDir + "_" + "commitlog"); - storeConfig.setHaListenPort(nextPort()); - storeConfig.setMappedFileSizeCommitLog(10 * 1024 * 1024); - storeConfig.setEnableDLegerCommitLog(true); - storeConfig.setdLegerGroup(brokerName); - storeConfig.setdLegerSelfId(selfId); - storeConfig.setdLegerPeers(peers); - return storeConfig; - } + public BrokerConfig buildBrokerConfig(String cluster, String brokerName) { + BrokerConfig brokerConfig = new BrokerConfig(); + brokerConfig.setBrokerClusterName(cluster); + brokerConfig.setBrokerName(brokerName); + brokerConfig.setBrokerIP1("127.0.0.1"); + brokerConfig.setNamesrvAddr(BaseConf.nsAddr); + return brokerConfig; + } + public MessageStoreConfig buildStoreConfig(String brokerName, String peers, String selfId) { + MessageStoreConfig storeConfig = new MessageStoreConfig(); + String baseDir = IntegrationTestBase.createBaseDir(); + storeConfig.setStorePathRootDir(baseDir); + storeConfig.setStorePathCommitLog(baseDir + "_" + "commitlog"); + storeConfig.setHaListenPort(nextPort()); + storeConfig.setMappedFileSizeCommitLog(10 * 1024 * 1024); + storeConfig.setEnableDLegerCommitLog(true); + storeConfig.setdLegerGroup(brokerName); + storeConfig.setdLegerSelfId(selfId); + storeConfig.setdLegerPeers(peers); + return storeConfig; + } } From fd9f3a481a4c45a9c82125f2b0bf90efbaa9ec9d Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 16:34:14 +0800 Subject: [PATCH 08/44] modify style --- .../instrumentation/rocketmq/RocketMqClientTest.groovy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index c9d586b21cb1..bc08fa7306da 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -13,16 +13,16 @@ class RocketMqClientTest extends AbstractRocketMqClientLibraryTest implements Li @Override void producerIntercept(String addr,Message msg) { TracingMessageInterceptor tracingProducerInterceptor= new TracingMessageInterceptor() - tracingProducerInterceptor.producerIntercept(addr,msg); + tracingProducerInterceptor.producerIntercept(addr,msg) } @Override void consumerIntercept(List msg, String type) { TracingMessageInterceptor tracingProducerInterceptor= new TracingMessageInterceptor() if("concurrent".equals(type)){ - tracingProducerInterceptor.consumerConcurrentlyIntercept(msg); + tracingProducerInterceptor.consumerConcurrentlyIntercept(msg) } else { - tracingProducerInterceptor.consumerOrderlyIntercept(msg); + tracingProducerInterceptor.consumerOrderlyIntercept(msg) } } } From 06ea7bc1eb2a06ad10cd2c8871474df3f3b14a12 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 16:49:04 +0800 Subject: [PATCH 09/44] modify style --- .../AbstractRocketMqClientLibraryTest.groovy | 32 +++++++-------- .../AbstractRocketMqClientTest.groovy | 40 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy index 8db435577258..54d8d47d72a0 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy @@ -26,39 +26,39 @@ import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTra abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecification{ @Shared - RMQNormalConsumer consumer; + RMQNormalConsumer consumer @Shared - RMQNormalProducer producer; + RMQNormalProducer producer @Shared - String sharedTopic; + String sharedTopic @Shared - String brokerAddr; + String brokerAddr @Shared - Message msg; + Message msg @Shared - int consumeTime = 5000; + int consumeTime = 5000 @Shared - def baseConf =new BaseConf(); + def baseConf =new BaseConf() abstract void producerIntercept(String addr,Message msg) abstract void consumerIntercept(List msgs,String type) def setup() { - sharedTopic =baseConf.initTopic(); + sharedTopic =baseConf.initTopic() brokerAddr =baseConf.getBrokerAddr() - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)); + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) } def "test rocketmq produce"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) when: runUnderTrace("parent") { producerIntercept(brokerAddr,msg) @@ -87,8 +87,8 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific def "test rocketmq concurrently consume"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) when: producer.send(msg) consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) @@ -123,11 +123,11 @@ abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecific def "test rocketmq orderly consume"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()); + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) when: - producer.send(msg); - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + producer.send(msg) + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) consumerIntercept(consumer.getListener().getAllOriginMsg(),"order") then: diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index bb3d2bf0c8e6..60dcc1db2306 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -29,38 +29,38 @@ import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTra abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ @Shared - private RMQNormalConsumer consumer; + private RMQNormalConsumer consumer @Shared - private RMQNormalProducer producer; + private RMQNormalProducer producer @Shared - DefaultMQProducer defaultMQProducer; + DefaultMQProducer defaultMQProducer @Shared - private String sharedTopic; + private String sharedTopic @Shared - private String brokerAddr; + private String brokerAddr @Shared - Message msg; + Message msg @Shared - int consumeTime = 1000; + int consumeTime = 1000 @Shared - BaseConf baseConf =new BaseConf(); + BaseConf baseConf =new BaseConf() def setup() { - sharedTopic =baseConf.initTopic(); + sharedTopic =baseConf.initTopic() brokerAddr =baseConf.getBrokerAddr() - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)); + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) } def "test rocketmq produce callback"() { setup: - defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr); + defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr) when: runUnderTrace("parent") { defaultMQProducer.send(msg, new SendCallback() { @@ -71,7 +71,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ @Override void onException(Throwable throwable) { } - }); + }) } then: @@ -100,12 +100,12 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce and concurrently consume"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()); + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) when: runUnderTrace("parent") { - producer.send(msg); - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + producer.send(msg) + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) } then: assertTraces(1) { @@ -150,12 +150,12 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce and orderly consume"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic); - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()); + producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) + consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) when: runUnderTrace("parent") { - producer.send(msg); - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime); + producer.send(msg) + consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) } then: assertTraces(1) { From f40b086758f9663394d920185d54da199fecf4f1 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 17:07:01 +0800 Subject: [PATCH 10/44] modify tests --- .../testing/src/main/java/base/BaseConf.java | 14 +++++++------- .../src/main/java/base/IntegrationTestBase.java | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index c6482edf570a..7ab188d9fdac 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -25,16 +25,16 @@ import org.apache.rocketmq.test.util.MQRandomUtils; public class BaseConf { - public static String nsAddr; - public static String broker1Addr; + public final static String nsAddr; + public final static String broker1Addr; protected static String broker1Name; - protected static String clusterName; + protected final static String clusterName; protected static int brokerNum; - protected static NamesrvController namesrvController; - protected static BrokerController brokerController1; - protected static List mqClients = new ArrayList(); + protected final static NamesrvController namesrvController; + protected final static BrokerController brokerController1; + protected final static List mqClients = new ArrayList(); protected static boolean debug = false; - private static Logger log = Logger.getLogger(BaseConf.class); + private final static Logger log = Logger.getLogger(BaseConf.class); static { System.setProperty( diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index 678b003a6ffa..e1272630336e 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -26,7 +26,7 @@ import org.junit.Assert; public class IntegrationTestBase { - public static InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); + public final static InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); protected static final String SEP = File.separator; protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; @@ -43,7 +43,7 @@ public static synchronized int nextPort() { return port.addAndGet(random.nextInt(10) + 10); } - protected static Random random = new Random(); + protected final static Random random = new Random(); public static String createBaseDir() { String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); From bad5c16b232dcfd72ca527ad47002e4802f26597 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 17:23:43 +0800 Subject: [PATCH 11/44] modify tests --- .../testing/src/main/java/base/BaseConf.java | 14 +++++++------- .../src/main/java/base/IntegrationTestBase.java | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 7ab188d9fdac..58e40a612991 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -25,16 +25,16 @@ import org.apache.rocketmq.test.util.MQRandomUtils; public class BaseConf { - public final static String nsAddr; - public final static String broker1Addr; + public static final String nsAddr; + public static final String broker1Addr; protected static String broker1Name; - protected final static String clusterName; + protected static final String clusterName; protected static int brokerNum; - protected final static NamesrvController namesrvController; - protected final static BrokerController brokerController1; - protected final static List mqClients = new ArrayList(); + protected static final NamesrvController namesrvController; + protected static final BrokerController brokerController1; + protected static final List mqClients = new ArrayList(); protected static boolean debug = false; - private final static Logger log = Logger.getLogger(BaseConf.class); + private static final Logger log = Logger.getLogger(BaseConf.class); static { System.setProperty( diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index e1272630336e..9985a8e20dab 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -26,7 +26,7 @@ import org.junit.Assert; public class IntegrationTestBase { - public final static InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); + public static final InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); protected static final String SEP = File.separator; protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; @@ -43,7 +43,7 @@ public static synchronized int nextPort() { return port.addAndGet(random.nextInt(10) + 10); } - protected final static Random random = new Random(); + protected static final Random random = new Random(); public static String createBaseDir() { String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); From 73bbefba32d4e8a79092d667f5cacfb586c3850e Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 17:35:47 +0800 Subject: [PATCH 12/44] modify tests --- .../testing/src/main/java/base/BaseConf.java | 2 +- .../testing/src/main/java/base/IntegrationTestBase.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 58e40a612991..6f6e5e61dad6 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -28,7 +28,7 @@ public class BaseConf { public static final String nsAddr; public static final String broker1Addr; protected static String broker1Name; - protected static final String clusterName; + protected static final String clusterName; protected static int brokerNum; protected static final NamesrvController namesrvController; protected static final BrokerController brokerController1; diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index 9985a8e20dab..fbbbfd22468b 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -26,7 +26,8 @@ import org.junit.Assert; public class IntegrationTestBase { - public static final InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); + public static final InternalLogger logger = + InternalLoggerFactory.getLogger(IntegrationTestBase.class); protected static final String SEP = File.separator; protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; From 06175b23e306418cccacd14124c6a20db731ff00 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 16 Feb 2021 17:59:36 +0800 Subject: [PATCH 13/44] modify tests --- .../rocketmq/AbstractRocketMqClientTest.groovy | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 60dcc1db2306..f241b2f6ab74 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -58,7 +58,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) } - def "test rocketmq produce callback"() { + def "test rocketmq produce callback1"() { setup: defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr) when: @@ -91,10 +91,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ } } } - - cleanup: - defaultMQProducer.shutdown() - } } From 4094f4a92597a3c1251331562fe004dbc772d98c Mon Sep 17 00:00:00 2001 From: addname Date: Sun, 28 Feb 2021 18:10:26 +0800 Subject: [PATCH 14/44] Use hooks to register in the iavaagent instrumentation --- .../rocketmq-client-4.8-javaagent.gradle | 5 + .../RocketMqClientApiImplInstrumentation.java | 104 ----------- ...tMqConcurrentlyConsumeInstrumentation.java | 70 -------- .../RocketMqConsumerInstrumentation.java | 50 ++++++ .../RocketMqInstrumentationModule.java | 8 +- ...RocketMqOrderlyConsumeInstrumentation.java | 70 -------- .../RocketMqProducerInstrumentation.java | 50 ++++++ .../rocketmq/RocketMqClientTest.groovy | 1 - .../rocketmq-client-4.8-library.gradle | 5 +- .../rocketmq/RocketMqClientConfig.java | 4 + .../rocketmq/RocketMqConsumerTracer.java | 82 +++++---- .../rocketmq/RocketMqProducerTracer.java | 40 +++-- .../rocketmq/SendCallbackWrapper.java | 35 ---- .../rocketmq/TextMapExtractAdapter.java | 7 +- .../rocketmq/TextMapInjectAdapter.java | 15 +- .../TracingConsumeMessageHookImpl.java | 29 ++++ .../rocketmq/TracingMessageInterceptor.java | 34 ---- .../rocketmq/TracingSendMessageHookImpl.java | 40 +++++ .../rocketmq/RocketMqClientTest.groovy | 91 ++++++++-- .../rocketmq-client-4.8-testing.gradle | 2 - .../AbstractRocketMqClientLibraryTest.groovy | 161 ------------------ .../AbstractRocketMqClientTest.groovy | 60 +++---- .../testing/src/main/java/base/BaseConf.java | 135 +++------------ .../main/java/base/IntegrationTestBase.java | 59 ++++--- .../dledger/DLedgerProduceAndConsumeIT.java | 39 ----- 25 files changed, 434 insertions(+), 762 deletions(-) delete mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java delete mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java delete mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java delete mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java delete mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java delete mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy delete mode 100644 instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java diff --git a/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle b/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle index b3f29e425e7f..d4cd5efb6903 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle +++ b/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle @@ -5,8 +5,10 @@ muzzle { group = "org.apache.rocketmq" module = 'rocketmq-client' versions = "[4.8.0,)" + assertInverse = true } } + dependencies { library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' implementation project(':instrumentation:rocketmq-client-4.8:library') @@ -14,3 +16,6 @@ dependencies { } +tasks.withType(Test) { + jvmArgs "-Dotel.instrumentation.rocketmq.client.experimental-span-attributes=true" +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java deleted file mode 100644 index 0f08c840ae6c..000000000000 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientApiImplInstrumentation.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.rocketmq; - -import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; -import static io.opentelemetry.instrumentation.rocketmq.TextMapInjectAdapter.SETTER; -import static java.util.Collections.singletonMap; -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.named; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; - -import io.opentelemetry.api.trace.Span; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.instrumentation.rocketmq.SendCallbackWrapper; -import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; -import io.opentelemetry.javaagent.tooling.TypeInstrumentation; -import java.util.Map; -import net.bytebuddy.asm.Advice; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader; - -public class RocketMqClientApiImplInstrumentation implements TypeInstrumentation { - - @Override - public ElementMatcher typeMatcher() { - return named("org.apache.rocketmq.client.impl.MQClientAPIImpl"); - } - - @Override - public Map, String> transformers() { - return singletonMap( - isMethod().and(named("sendMessage")).and(takesArguments(12)), - RocketMqClientApiImplInstrumentation.class.getName() + "$SendMessageAdvice"); - } - - public static class SendMessageAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.Argument(value = 0, readOnly = false) String addr, - @Advice.Argument(value = 2, readOnly = false) Message msg, - @Advice.Argument(value = 3, readOnly = false) SendMessageRequestHeader requestHeader, - @Advice.Argument(value = 6, readOnly = false) SendCallback sendCallback, - @Advice.Local("otelSpan") Span span, - @Advice.Local("otelScope") Scope scope) { - - Context parent = Java8BytecodeBridge.currentContext(); - span = tracer().startProducerSpan(addr, msg); - Context newContext = parent.with(span); - try { - Java8BytecodeBridge.getGlobalPropagators() - .getTextMapPropagator() - .inject(newContext, requestHeader, SETTER); - } catch (IllegalStateException e) { - requestHeader = new SendMessageRequestHeader(); - requestHeader.getBornTimestamp(); - requestHeader.getDefaultTopic(); - requestHeader.getDefaultTopicQueueNums(); - requestHeader.getFlag(); - requestHeader.getProducerGroup(); - requestHeader.getMaxReconsumeTimes(); - requestHeader.getProperties(); - requestHeader.getSysFlag(); - requestHeader.getTopic(); - requestHeader.getQueueId(); - requestHeader.getReconsumeTimes(); - Java8BytecodeBridge.getGlobalPropagators() - .getTextMapPropagator() - .inject(newContext, requestHeader, SETTER); - } - - scope = newContext.makeCurrent(); - if (sendCallback != null) { - sendCallback = new SendCallbackWrapper(sendCallback, span); - } - } - - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static void stopSpan( - @Advice.Thrown Throwable throwable, - @Advice.Argument(value = 6, readOnly = false) SendCallback sendCallback, - @Advice.Local("otelSpan") Span span, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; - } - scope.close(); - if (sendCallback == null) { - if (throwable == null) { - tracer().end(span); - } else { - tracer().endExceptionally(span, throwable); - } - } - } - } -} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java deleted file mode 100644 index 3b731009e597..000000000000 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConcurrentlyConsumeInstrumentation.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.rocketmq; - -import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; -import static java.util.Collections.singletonMap; -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.named; - -import io.opentelemetry.api.trace.Span; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.tooling.TypeInstrumentation; -import java.util.List; -import java.util.Map; -import net.bytebuddy.asm.Advice; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; -import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; -import org.apache.rocketmq.common.message.MessageExt; - -public class RocketMqConcurrentlyConsumeInstrumentation implements TypeInstrumentation { - - @Override - public ElementMatcher typeMatcher() { - return implementsInterface( - named("org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently")); - } - - @Override - public Map, String> transformers() { - return singletonMap( - isMethod().and(named("consumeMessage")), - RocketMqConcurrentlyConsumeInstrumentation.class.getName() + "$ConcurrentlyConsumeAdvice"); - } - - public static class ConcurrentlyConsumeAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.Argument(0) List msgs, - @Advice.Local("otelSpan") Span span, - @Advice.Local("otelScope") Scope scope) { - - span = tracer().startSpan(msgs); - scope = span.makeCurrent(); - } - - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static void stopSpan( - @Advice.Return ConsumeConcurrentlyStatus status, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelSpan") Span span, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; - } - tracer().endConcurrentlySpan(span, status); - scope.close(); - if (throwable == null) { - tracer().end(span); - } else { - tracer().endExceptionally(span, throwable); - } - } - } -} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java new file mode 100644 index 000000000000..9d0ec6376934 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java @@ -0,0 +1,50 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import io.opentelemetry.instrumentation.rocketmq.TracingConsumeMessageHookImpl; +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; +import org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl; +import java.util.Map; + +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; +import static java.util.Collections.singletonMap; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class RocketMqConsumerInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher classLoaderOptimization() { + return hasClassesNamed("org.apache.rocketmq.client.consumer.DefaultMQPushConsumer"); + } + + @Override + public ElementMatcher typeMatcher() { + return named("org.apache.rocketmq.client.consumer.DefaultMQPushConsumer"); + } + + @Override + public Map, String> transformers() { + return singletonMap( + isMethod().and(named("start")).and(takesArguments(0)), + RocketMqConsumerInstrumentation.class.getName() + "$AdviceStart"); + } + + public static class AdviceStart { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void onEnter( + @Advice.FieldValue(value = "defaultMQPushConsumerImpl",declaringType = DefaultMQPushConsumer.class) DefaultMQPushConsumerImpl defaultMQPushConsumerImpl){ + defaultMQPushConsumerImpl.registerConsumeMessageHook(new TracingConsumeMessageHookImpl()); + } + } +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java index 6f9332e74fd5..dc532b79850d 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java @@ -15,14 +15,14 @@ @AutoService(InstrumentationModule.class) public class RocketMqInstrumentationModule extends InstrumentationModule { public RocketMqInstrumentationModule() { - super("rocketmq", "rockemq-client-4.3"); + super("rocketmq-client", "rocketmq-client-4.8"); } @Override public List typeInstrumentations() { return asList( - new RocketMqClientApiImplInstrumentation(), - new RocketMqConcurrentlyConsumeInstrumentation(), - new RocketMqOrderlyConsumeInstrumentation()); + new RocketMqProducerInstrumentation(), + new RocketMqConsumerInstrumentation() + ); } } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java deleted file mode 100644 index e2d3d455e55e..000000000000 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqOrderlyConsumeInstrumentation.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.rocketmq; - -import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; -import static java.util.Collections.singletonMap; -import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; -import static net.bytebuddy.matcher.ElementMatchers.named; - -import io.opentelemetry.api.trace.Span; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.tooling.TypeInstrumentation; -import java.util.List; -import java.util.Map; -import net.bytebuddy.asm.Advice; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; -import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; -import org.apache.rocketmq.common.message.MessageExt; - -public class RocketMqOrderlyConsumeInstrumentation implements TypeInstrumentation { - - @Override - public ElementMatcher typeMatcher() { - return implementsInterface( - named("org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly")); - } - - @Override - public Map, String> transformers() { - return singletonMap( - nameStartsWith("consumeMessage"), - RocketMqOrderlyConsumeInstrumentation.class.getName() + "$OrderlyConsumeAdvice"); - } - - public static class OrderlyConsumeAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.Argument(0) List msgs, - @Advice.Local("otelSpan") Span span, - @Advice.Local("otelScope") Scope scope) { - - span = tracer().startSpan(msgs); - scope = span.makeCurrent(); - } - - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static void stopSpan( - @Advice.Return ConsumeOrderlyStatus status, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelSpan") Span span, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; - } - tracer().endOrderlySpan(span, status); - scope.close(); - if (throwable == null) { - tracer().end(span); - } else { - tracer().endExceptionally(span, throwable); - } - } - } -} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java new file mode 100644 index 000000000000..90cbe3632af1 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java @@ -0,0 +1,50 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; +import static java.util.Collections.singletonMap; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +import io.opentelemetry.instrumentation.rocketmq.TracingSendMessageHookImpl; +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import java.util.Map; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl; +import org.apache.rocketmq.client.producer.DefaultMQProducer; + +public class RocketMqProducerInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher classLoaderOptimization() { + return hasClassesNamed("org.apache.rocketmq.client.producer.DefaultMQProducer"); + } + + @Override + public ElementMatcher typeMatcher() { + return named("org.apache.rocketmq.client.producer.DefaultMQProducer"); + } + + @Override + public Map, String> transformers() { + return singletonMap( + isMethod().and(named("start")).and(takesArguments(0)), + RocketMqProducerInstrumentation.class.getName() + "$AdviceStart"); + } + + public static class AdviceStart { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void onEnter( + @Advice.FieldValue(value = "defaultMQProducerImpl",declaringType = DefaultMQProducer.class) DefaultMQProducerImpl defaultMQProducerImpl){ + defaultMQProducerImpl.registerSendMessageHook(new TracingSendMessageHookImpl()); + } + } +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 4014df9ed698..2dc2e21addc3 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -10,5 +10,4 @@ import io.opentelemetry.instrumentation.test.AgentTestTrait class RocketMqClientTest extends AbstractRocketMqClientTest implements AgentTestTrait { - } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle index 4357ed4f2d5f..e2ac77c73d4f 100644 --- a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle +++ b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle @@ -1,8 +1,11 @@ apply from: "$rootDir/gradle/instrumentation-library.gradle" - dependencies { library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' testImplementation project(':instrumentation:rocketmq-client-4.8:testing') } + +tasks.withType(Test) { + jvmArgs "-Dotel.instrumentation.rocketmq.client.experimental-span-attributes=true" +} \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java index ba0b631846bb..c9ba3789d799 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java @@ -14,5 +14,9 @@ public static boolean isPropagationEnabled() { .getBooleanProperty("otel.instrumentation.rocketmq.client-propagation", true); } + public static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = + Config.get() + .getBooleanProperty("otel.instrumentation.rocketmq.client.experimental-span-attributes", false); + private RocketMqClientConfig() {} } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index f53d83c16bf1..696cde11628f 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -9,12 +9,11 @@ import static io.opentelemetry.instrumentation.rocketmq.TextMapExtractAdapter.GETTER; import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.List; -import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; -import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; import org.apache.rocketmq.common.message.MessageExt; public class RocketMqConsumerTracer extends BaseTracer { @@ -30,22 +29,46 @@ protected String getInstrumentationName() { return "io.opentelemetry.javaagent.rocketmq-client"; } - public Span startSpan(List msgs) { + public Context startSpan(Context parentContext, List msgs) { MessageExt msg = msgs.get(0); - Span span = - tracer - .spanBuilder(spanNameOnConsume(msg)) - .setSpanKind(CONSUMER) - .setParent(extractParent(msg)) - .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") - .setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()) - .setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic") - .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "process") - .setAttribute( - SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, getStoreSize(msgs)) - .startSpan(); - onConsume(span, msg); - return span; + if (msgs.size() == 1) { + SpanBuilder spanBuilder = startSpanBuilder(msg) + .setParent(extractParent(msg)); + return withClientSpan(parentContext, spanBuilder.startSpan()); + } else { + SpanBuilder spanBuilder = + tracer + .spanBuilder(msg.getTopic() + " receive") + .setSpanKind(CONSUMER) + .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") + .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "receive"); + Context rootContext = withClientSpan(parentContext, spanBuilder.startSpan()); + for (MessageExt message : msgs) { + createChildSpan(rootContext, message); + } + return rootContext; + } + } + + public void createChildSpan(Context parentContext, MessageExt msg) { + SpanBuilder childSpanBuilder = startSpanBuilder(msg) + .setParent(parentContext) + .addLink(Span.fromContext(extractParent(msg)).getSpanContext()); + end(withClientSpan(parentContext, childSpanBuilder.startSpan())); + } + + public SpanBuilder startSpanBuilder(MessageExt msg) { + SpanBuilder spanBuilder = tracer.spanBuilder(spanNameOnConsume(msg)) + .setSpanKind(CONSUMER) + .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") + .setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()) + .setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic") + .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "process") + .setAttribute(SemanticAttributes.MESSAGING_MESSAGE_ID, msg.getMsgId()) + .setAttribute( + SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, (long) msg.getBody().length); + onConsume(spanBuilder, msg); + return spanBuilder; } private Context extractParent(MessageExt msg) { @@ -56,16 +79,13 @@ private Context extractParent(MessageExt msg) { } } - void onConsume(Span span, MessageExt msg) { - span.setAttribute("messaging.rocketmq.tags", msg.getTags()); - span.setAttribute("messaging.rocketmq.queue_id", msg.getQueueId()); - span.setAttribute("messaging.rocketmq.queue_offset", msg.getQueueOffset()); - span.setAttribute("messaging.rocketmq.broker_address", getBrokerHost(msg)); - } - - long getStoreSize(List msgs) { - long storeSize = msgs.stream().mapToInt(item -> item.getStoreSize()).sum(); - return storeSize; + void onConsume(SpanBuilder spanBuilder, MessageExt msg) { + if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + spanBuilder.setAttribute("messaging.rocketmq.tags", msg.getTags()); + spanBuilder.setAttribute("messaging.rocketmq.queue_id", msg.getQueueId()); + spanBuilder.setAttribute("messaging.rocketmq.queue_offset", msg.getQueueOffset()); + spanBuilder.setAttribute("messaging.rocketmq.broker_address", getBrokerHost(msg)); + } } String spanNameOnConsume(MessageExt msg) { @@ -79,12 +99,4 @@ String getBrokerHost(MessageExt msg) { return null; } } - - public void endConcurrentlySpan(Span span, ConsumeConcurrentlyStatus status) { - span.setAttribute("messaging.rocketmq.consume_concurrently_status", status.name()); - } - - public void endOrderlySpan(Span span, ConsumeOrderlyStatus status) { - span.setAttribute("messaging.rocketmq.consume_orderly_status", status.name()); - } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index ad344db98dcd..c12b2e7bd223 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -9,6 +9,7 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import org.apache.rocketmq.client.producer.SendResult; @@ -27,25 +28,38 @@ protected String getInstrumentationName() { return "io.opentelemetry.javaagent.rocketmq-client"; } - public Span startProducerSpan(String addr, Message msg) { - SpanBuilder span = spanBuilder(spanNameOnProduce(msg), PRODUCER); - onProduce(span, msg, addr); - return span.startSpan(); + public Context startProducerSpan(String addr, Message msg, Context parentContext) { + SpanBuilder spanBuilder = spanBuilder(spanNameOnProduce(msg), PRODUCER); + onProduce(spanBuilder, msg, addr); + return withClientSpan(parentContext, spanBuilder.startSpan()); } - public void onCallback(Span span, SendResult sendResult) { - span.setAttribute("messaging.rocketmq.callback_result", sendResult.getSendStatus().name()); + public void onCallback(Context context, SendResult sendResult) { + if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + Span span = Span.fromContext(context); + span.setAttribute("messaging.rocketmq.callback_result", sendResult.getSendStatus().name()); + } } - public void onProduce(SpanBuilder span, Message msg, String addr) { - span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq"); - span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic"); - span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()); - span.setAttribute("messaging.rocketmq.tags", msg.getTags()); - span.setAttribute("messaging.rocketmq.broker_address", addr); + private void onProduce(SpanBuilder spanBuilder, Message msg, String addr) { + spanBuilder.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq"); + spanBuilder.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic"); + spanBuilder.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()); + if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + spanBuilder.setAttribute("messaging.rocketmq.tags", msg.getTags()); + spanBuilder.setAttribute("messaging.rocketmq.broker_address", addr); + } } - public String spanNameOnProduce(Message msg) { + public void afterProduce(Context context, SendResult sendResult) { + Span span = Span.fromContext(context); + span.setAttribute(SemanticAttributes.MESSAGING_MESSAGE_ID, sendResult.getMsgId()); + if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + span.setAttribute("messaging.rocketmq.send_result", sendResult.getSendStatus().name()); + } + } + + private String spanNameOnProduce(Message msg) { return msg.getTopic() + " send"; } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java deleted file mode 100644 index fb3bd39bf8df..000000000000 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/SendCallbackWrapper.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.rocketmq; - -import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; - -import io.opentelemetry.api.trace.Span; -import org.apache.rocketmq.client.producer.SendCallback; -import org.apache.rocketmq.client.producer.SendResult; - -public class SendCallbackWrapper implements SendCallback { - private final SendCallback sendCallback; - private final Span span; - - public SendCallbackWrapper(SendCallback sendCallback, Span span) { - this.sendCallback = sendCallback; - this.span = span; - } - - @Override - public void onSuccess(SendResult sendResult) { - tracer().onCallback(span, sendResult); - tracer().end(span); - sendCallback.onSuccess(sendResult); - } - - @Override - public void onException(Throwable e) { - tracer().endExceptionally(span, e); - sendCallback.onException(e); - } -} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java index c343e84b7ab8..94ae5beef546 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java @@ -5,10 +5,10 @@ package io.opentelemetry.instrumentation.rocketmq; -import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.context.propagation.TextMapGetter; import java.util.Map; -public class TextMapExtractAdapter implements TextMapPropagator.Getter> { +public class TextMapExtractAdapter implements TextMapGetter> { public static final TextMapExtractAdapter GETTER = new TextMapExtractAdapter(); @@ -19,7 +19,6 @@ public Iterable keys(Map carrier) { @Override public String get(Map carrier, String key) { - String obj = carrier.get(key); - return obj; + return carrier.get(key); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java index 22ec6733bd81..40ba41a351e6 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java @@ -5,20 +5,15 @@ package io.opentelemetry.instrumentation.rocketmq; -import io.opentelemetry.context.propagation.TextMapPropagator; -import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader; +import io.opentelemetry.context.propagation.TextMapSetter; +import java.util.Map; -public class TextMapInjectAdapter implements TextMapPropagator.Setter { +public class TextMapInjectAdapter implements TextMapSetter> { public static final TextMapInjectAdapter SETTER = new TextMapInjectAdapter(); @Override - public void set(SendMessageRequestHeader header, String key, String value) { - StringBuilder properties = new StringBuilder(header.getProperties()); - properties.append(key); - properties.append('\u0001'); - properties.append(value); - properties.append('\u0002'); - header.setProperties(properties.toString()); + public void set(Map carrier, String key, String value) { + carrier.put(key,value); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java new file mode 100644 index 000000000000..ca079db21f62 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java @@ -0,0 +1,29 @@ +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.context.Context; +import org.apache.rocketmq.client.hook.ConsumeMessageContext; +import org.apache.rocketmq.client.hook.ConsumeMessageHook; + +import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; + +public class TracingConsumeMessageHookImpl implements ConsumeMessageHook { + + @Override + public String hookName() { + return "OpenTelemetryConsumeMessageTraceHook"; + } + + @Override + public void consumeMessageBefore(ConsumeMessageContext context) { + } + + @Override + public void consumeMessageAfter(ConsumeMessageContext context) { + if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { + return; + } + Context traceContext =tracer().startSpan(Context.current(),context.getMsgList()); + tracer().end(traceContext); + } + +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java deleted file mode 100644 index af1ac2df8d49..000000000000 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingMessageInterceptor.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.rocketmq; - -import io.opentelemetry.api.trace.Span; -import java.util.List; -import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; -import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; -import org.apache.rocketmq.common.message.Message; -import org.apache.rocketmq.common.message.MessageExt; - -public class TracingMessageInterceptor { - - public void producerIntercept(String addr, Message msg) { - Span span = RocketMqProducerTracer.tracer().startProducerSpan(addr, msg); - RocketMqProducerTracer.tracer().end(span); - } - - public void consumerConcurrentlyIntercept(List msgs) { - Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); - RocketMqConsumerTracer.tracer() - .endConcurrentlySpan(span, ConsumeConcurrentlyStatus.CONSUME_SUCCESS); - RocketMqConsumerTracer.tracer().tracer().end(span); - } - - public void consumerOrderlyIntercept(List msgs) { - Span span = RocketMqConsumerTracer.tracer().startSpan(msgs); - RocketMqConsumerTracer.tracer().endOrderlySpan(span, ConsumeOrderlyStatus.SUCCESS); - RocketMqConsumerTracer.tracer().tracer().end(span); - } -} 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 new file mode 100644 index 000000000000..5177d69c78fe --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingSendMessageHookImpl.java @@ -0,0 +1,40 @@ +package io.opentelemetry.instrumentation.rocketmq; + +import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; +import static io.opentelemetry.instrumentation.rocketmq.TextMapInjectAdapter.SETTER; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.context.Context; +import org.apache.rocketmq.client.hook.SendMessageContext; +import org.apache.rocketmq.client.hook.SendMessageHook; + +public class TracingSendMessageHookImpl implements SendMessageHook { + + @Override + public String hookName() { + return "OpenTelemetrySendMessageTraceHook"; + } + + @Override + public void sendMessageBefore(SendMessageContext context) { + if (context == null) { + return; + } + Context traceContext = tracer().startProducerSpan(context.getBrokerAddr(), context.getMessage(), Context.current()); + if (RocketMqClientConfig.isPropagationEnabled()){ + GlobalOpenTelemetry.getPropagators() + .getTextMapPropagator() + .inject(traceContext, context.getMessage().getProperties(), SETTER); + } + context.setMqTraceContext(traceContext); + } + + @Override + public void sendMessageAfter(SendMessageContext context) { + if (context == null || context.getMqTraceContext() == null || context.getSendResult() == null) { + return; + } + tracer().afterProduce((Context)context.getMqTraceContext(), context.getSendResult()); + tracer().end((Context)context.getMqTraceContext()); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index bc08fa7306da..989b80c3750f 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -4,25 +4,90 @@ */ package io.opentelemetry.instrumentation.rocketmq -import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientLibraryTest + +import base.BaseConf +import io.opentelemetry.instrumentation.test.InstrumentationSpecification import io.opentelemetry.instrumentation.test.LibraryTestTrait +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer +import org.apache.rocketmq.client.producer.DefaultMQProducer import org.apache.rocketmq.common.message.Message +import org.apache.rocketmq.remoting.common.RemotingHelper +import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener +import spock.lang.Shared + +import static io.opentelemetry.api.trace.SpanKind.CONSUMER +import static io.opentelemetry.api.trace.SpanKind.PRODUCER +import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan +import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace + +class RocketMqClientTest extends InstrumentationSpecification implements LibraryTestTrait { + + @Shared + DefaultMQPushConsumer consumer + + @Shared + DefaultMQProducer producer + + @Shared + String sharedTopic -class RocketMqClientTest extends AbstractRocketMqClientLibraryTest implements LibraryTestTrait { + @Shared + String brokerAddr - @Override - void producerIntercept(String addr,Message msg) { - TracingMessageInterceptor tracingProducerInterceptor= new TracingMessageInterceptor() - tracingProducerInterceptor.producerIntercept(addr,msg) + @Shared + Message msg + + def setup() { + sharedTopic = BaseConf.initTopic() + brokerAddr = BaseConf.getBrokerAddr() + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) } - @Override - void consumerIntercept(List msg, String type) { - TracingMessageInterceptor tracingProducerInterceptor= new TracingMessageInterceptor() - if("concurrent".equals(type)){ - tracingProducerInterceptor.consumerConcurrentlyIntercept(msg) - } else { - tracingProducerInterceptor.consumerOrderlyIntercept(msg) + def "test rocketmq produce and consume"() { + setup: + producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + producer.getDefaultMQProducerImpl().registerSendMessageHook(new TracingSendMessageHookImpl()) + + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(new TracingConsumeMessageHookImpl()) + when: + runUnderTrace("parent") { + producer.send(msg) + } + then: + assertTraces(1) { + trace(0, 3) { + basicSpan(it, 0, "parent") + span(1) { + name sharedTopic + " send" + kind PRODUCER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String + } + } + span(2) { + name sharedTopic + " process" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String + } + } + } + cleanup: + producer.shutdown() + consumer.shutdown() + BaseConf.deleteTempDir() } } + } + diff --git a/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle b/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle index 6773527770ae..ec6d24d3ce2a 100644 --- a/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle +++ b/instrumentation/rocketmq-client-4.8/testing/rocketmq-client-4.8-testing.gradle @@ -3,8 +3,6 @@ apply from: "$rootDir/gradle/java.gradle" dependencies { api project(':testing-common') api group: 'org.apache.rocketmq', name: 'rocketmq-test', version: '4.8.0' - api group: 'io.openmessaging', name: 'openmessaging-api', version: '0.3.1-alpha' - api group: 'org.apache.rocketmq', name: 'rocketmq-openmessaging', version: '4.8.0' implementation deps.guava implementation deps.groovy diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy deleted file mode 100644 index 54d8d47d72a0..000000000000 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientLibraryTest.groovy +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetery.instrumentation.rocketmq - -import base.BaseConf -import io.opentelemetry.instrumentation.test.InstrumentationSpecification -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes -import org.apache.rocketmq.common.message.Message -import org.apache.rocketmq.remoting.common.RemotingHelper -import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer -import org.apache.rocketmq.test.client.rmq.RMQNormalProducer -import org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener -import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener -import spock.lang.Shared -import spock.lang.Unroll - -import static io.opentelemetry.api.trace.SpanKind.CONSUMER -import static io.opentelemetry.api.trace.SpanKind.PRODUCER -import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan -import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace - -@Unroll -abstract class AbstractRocketMqClientLibraryTest extends InstrumentationSpecification{ - - @Shared - RMQNormalConsumer consumer - - @Shared - RMQNormalProducer producer - - @Shared - String sharedTopic - - @Shared - String brokerAddr - - @Shared - Message msg - - @Shared - int consumeTime = 5000 - - @Shared - def baseConf =new BaseConf() - - abstract void producerIntercept(String addr,Message msg) - - abstract void consumerIntercept(List msgs,String type) - - def setup() { - sharedTopic =baseConf.initTopic() - brokerAddr =baseConf.getBrokerAddr() - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) - } - - def "test rocketmq produce"() { - setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) - when: - runUnderTrace("parent") { - producerIntercept(brokerAddr,msg) - producer.send(msg) - } - then: - assertTraces(1) { - trace(0, 2) { - basicSpan(it, 0, "parent") - span(1) { - name sharedTopic + " send" - kind PRODUCER - attributes { - "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" - "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic - "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" - "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr - } - } - } - cleanup: - producer.shutdown() - } - } - - def "test rocketmq concurrently consume"() { - setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) - when: - producer.send(msg) - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) - consumerIntercept(consumer.getListener().getAllOriginMsg(),"concurrent") - then: - assertTraces(1) { - trace(0, 1) { - span(0) { - name sharedTopic + " process" - kind CONSUMER - attributes { - "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" - "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic - "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" - "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" - "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long - "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr - "messaging.rocketmq.consume_concurrently_status" "CONSUME_SUCCESS" - "messaging.rocketmq.queue_id" Long - "messaging.rocketmq.queue_offset" Long - - } - } - } - cleanup: - producer.shutdown() - consumer.shutdown() - - } - } - - def "test rocketmq orderly consume"() { - setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) - when: - producer.send(msg) - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) - consumerIntercept(consumer.getListener().getAllOriginMsg(),"order") - - then: - assertTraces(1) { - trace(0, 1) { - span(0) { - name sharedTopic + " process" - kind CONSUMER - attributes { - "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" - "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic - "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" - "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" - "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long - "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr - "messaging.rocketmq.consume_orderly_status" "SUCCESS" - "messaging.rocketmq.queue_id" Long - "messaging.rocketmq.queue_offset" Long - - } - } - } - cleanup: - producer.shutdown() - consumer.shutdown() - - } - } -} - diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index f241b2f6ab74..efeaa38aed2b 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -8,14 +8,12 @@ package io.opentelemetery.instrumentation.rocketmq import base.BaseConf import io.opentelemetry.instrumentation.test.InstrumentationSpecification import io.opentelemetry.semconv.trace.attributes.SemanticAttributes +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer import org.apache.rocketmq.client.producer.DefaultMQProducer import org.apache.rocketmq.client.producer.SendCallback import org.apache.rocketmq.client.producer.SendResult import org.apache.rocketmq.common.message.Message import org.apache.rocketmq.remoting.common.RemotingHelper -import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer -import org.apache.rocketmq.test.client.rmq.RMQNormalProducer -import org.apache.rocketmq.test.factory.ProducerFactory import org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener import spock.lang.Shared @@ -29,41 +27,32 @@ import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTra abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ @Shared - private RMQNormalConsumer consumer + DefaultMQPushConsumer consumer @Shared - private RMQNormalProducer producer + DefaultMQProducer producer @Shared - DefaultMQProducer defaultMQProducer + String sharedTopic @Shared - private String sharedTopic - - @Shared - private String brokerAddr + String brokerAddr @Shared Message msg - @Shared - int consumeTime = 1000 - - @Shared - BaseConf baseConf =new BaseConf() - def setup() { - sharedTopic =baseConf.initTopic() - brokerAddr =baseConf.getBrokerAddr() + sharedTopic = BaseConf.initTopic() + brokerAddr = BaseConf.getBrokerAddr() msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) } - def "test rocketmq produce callback1"() { + def "test rocketmq produce callback"() { setup: - defaultMQProducer = ProducerFactory.getRMQProducer(baseConf.nsAddr) + producer = BaseConf.getRMQProducer(BaseConf.nsAddr) when: runUnderTrace("parent") { - defaultMQProducer.send(msg, new SendCallback() { + producer.send(msg, new SendCallback() { @Override void onSuccess(SendResult sendResult) { } @@ -85,23 +74,26 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" brokerAddr - "messaging.rocketmq.callback_result" "SEND_OK" + "messaging.rocketmq.send_result" "SEND_OK" + } } } + cleanup: + BaseConf.deleteTempDir() } } def "test rocketmq produce and concurrently consume"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) + producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) when: runUnderTrace("parent") { producer.send(msg) - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) } then: assertTraces(1) { @@ -114,8 +106,10 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.send_result" "SEND_OK" } } span(2) { @@ -127,9 +121,9 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" brokerAddr - "messaging.rocketmq.consume_concurrently_status" "CONSUME_SUCCESS" "messaging.rocketmq.queue_id" Long "messaging.rocketmq.queue_offset" Long @@ -139,19 +133,18 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ cleanup: producer.shutdown() consumer.shutdown() - + BaseConf.deleteTempDir() } } def "test rocketmq produce and orderly consume"() { setup: - producer = baseConf.getProducer(baseConf.nsAddr, sharedTopic) - consumer = baseConf.getConsumer(baseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) when: runUnderTrace("parent") { producer.send(msg) - consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime) } then: assertTraces(1) { @@ -164,8 +157,10 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.send_result" "SEND_OK" } } span(2) { @@ -177,19 +172,18 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" "messaging.rocketmq.broker_address" brokerAddr - "messaging.rocketmq.consume_orderly_status" "SUCCESS" "messaging.rocketmq.queue_id" Long "messaging.rocketmq.queue_offset" Long - } } } cleanup: producer.shutdown() consumer.shutdown() - + BaseConf.deleteTempDir() } } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 6f6e5e61dad6..296e8c070780 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -7,24 +7,21 @@ import java.util.ArrayList; import java.util.List; +import java.util.UUID; import org.apache.log4j.Logger; import org.apache.rocketmq.broker.BrokerController; -import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.common.MQVersion; import org.apache.rocketmq.namesrv.NamesrvController; import org.apache.rocketmq.remoting.protocol.RemotingCommand; -import org.apache.rocketmq.test.client.rmq.RMQAsyncSendProducer; -import org.apache.rocketmq.test.client.rmq.RMQNormalConsumer; -import org.apache.rocketmq.test.client.rmq.RMQNormalProducer; -import org.apache.rocketmq.test.client.rmq.RMQTransactionalProducer; -import org.apache.rocketmq.test.clientinterface.AbstractMQConsumer; -import org.apache.rocketmq.test.clientinterface.AbstractMQProducer; -import org.apache.rocketmq.test.factory.ConsumerFactory; import org.apache.rocketmq.test.listener.AbstractListener; import org.apache.rocketmq.test.util.MQAdmin; import org.apache.rocketmq.test.util.MQRandomUtils; +import org.apache.rocketmq.test.util.RandomUtil; -public class BaseConf { +final class BaseConf { public static final String nsAddr; public static final String broker1Addr; protected static String broker1Name; @@ -48,12 +45,13 @@ public class BaseConf { brokerNum = 2; } - public BaseConf() {} + private BaseConf() {} public static String initTopic() { String topic = MQRandomUtils.getRandomTopic(); - IntegrationTestBase.initTopic(topic, nsAddr, clusterName); - + if(!IntegrationTestBase.initTopic(topic, nsAddr, clusterName)){ + log.error("Topic init failed"); + } return topic; } @@ -71,105 +69,28 @@ public static String initConsumerGroup(String group) { return group; } - public static RMQNormalProducer getProducer(String nsAddr, String topic) { - return getProducer(nsAddr, topic, false); - } - - public static RMQNormalProducer getProducer(String nsAddr, String topic, boolean useTls) { - RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, useTls); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; - } - - public static RMQNormalProducer getProducer( - String nsAddr, String topic, String producerGoup, String instanceName) { - RMQNormalProducer producer = new RMQNormalProducer(nsAddr, topic, producerGoup, instanceName); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; - } - - public static RMQTransactionalProducer getTransactionalProducer( - String nsAddr, String topic, TransactionListener transactionListener) { - RMQTransactionalProducer producer = - new RMQTransactionalProducer(nsAddr, topic, false, transactionListener); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; - } - - public static RMQAsyncSendProducer getAsyncProducer(String nsAddr, String topic) { - RMQAsyncSendProducer producer = new RMQAsyncSendProducer(nsAddr, topic); - if (debug) { - producer.setDebug(); - } - mqClients.add(producer); - return producer; - } - - public static RMQNormalConsumer getConsumer( - String nsAddr, String topic, String subExpression, AbstractListener listener) { - return getConsumer(nsAddr, topic, subExpression, listener, false); - } - - public static RMQNormalConsumer getConsumer( - String nsAddr, - String topic, - String subExpression, - AbstractListener listener, - boolean useTls) { + public static DefaultMQPushConsumer getConsumer(String nsAddr, String topic, String subExpression, + AbstractListener listener) + throws MQClientException { String consumerGroup = initConsumerGroup(); - return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, useTls); - } - - public static RMQNormalConsumer getConsumer( - String nsAddr, - String consumerGroup, - String topic, - String subExpression, - AbstractListener listener) { - return getConsumer(nsAddr, consumerGroup, topic, subExpression, listener, false); - } - - public static RMQNormalConsumer getConsumer( - String nsAddr, - String consumerGroup, - String topic, - String subExpression, - AbstractListener listener, - boolean useTls) { - RMQNormalConsumer consumer = - ConsumerFactory.getRMQNormalConsumer( - nsAddr, consumerGroup, topic, subExpression, listener, useTls); - if (debug) { - consumer.setDebug(); - } - mqClients.add(consumer); - log.info( - String.format( - "consumer[%s] start,topic[%s],subExpression[%s]", consumerGroup, topic, subExpression)); + DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup); + consumer.setInstanceName(RandomUtil.getStringByUUID()); + consumer.setNamesrvAddr(nsAddr); + consumer.subscribe(topic, subExpression); + consumer.setMessageListener(listener); + consumer.start(); return consumer; } - public static void shutdown() { - try { - for (Object mqClient : mqClients) { - if (mqClient instanceof AbstractMQProducer) { - ((AbstractMQProducer) mqClient).shutdown(); + public static DefaultMQProducer getRMQProducer(String ns) throws MQClientException { + DefaultMQProducer producer = new DefaultMQProducer(RandomUtil.getStringByUUID()); + producer.setInstanceName(UUID.randomUUID().toString()); + producer.setNamesrvAddr(ns); + producer.start(); + return producer; + } - } else { - ((AbstractMQConsumer) mqClient).shutdown(); - } - } - } catch (Exception e) { - e.printStackTrace(); - } + private static void deleteTempDir() { + IntegrationTestBase.deleteTempDir(); } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index fbbbfd22468b..e8055d7bbc06 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -6,14 +6,16 @@ package base; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Random; -import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import org.apache.rocketmq.broker.BrokerController; import org.apache.rocketmq.common.BrokerConfig; -import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.namesrv.NamesrvConfig; import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLoggerFactory; @@ -29,7 +31,6 @@ public class IntegrationTestBase { public static final InternalLogger logger = InternalLoggerFactory.getLogger(IntegrationTestBase.class); - protected static final String SEP = File.separator; protected static final String BROKER_NAME_PREFIX = "TestBrokerName_"; protected static final AtomicInteger BROKER_INDEX = new AtomicInteger(0); protected static final List TMPE_FILES = new ArrayList<>(); @@ -46,26 +47,37 @@ public static synchronized int nextPort() { protected static final Random random = new Random(); - public static String createBaseDir() { - String baseDir = System.getProperty("user.home") + SEP + "unitteststore-" + UUID.randomUUID(); - final File file = new File(baseDir); - if (file.exists()) { - logger.info( - String.format( - "[%s] has already existed, please back up and remove it for integration tests", - baseDir)); - System.exit(1); + private static String createTempDir() { + String path = null; + try { + File file = Files.createTempDirectory("opentelemetry-rocketmq-client-temp").toFile(); + TMPE_FILES.add(file); + path= file.getCanonicalPath(); + } catch (IOException e) { + e.printStackTrace(); + } + return path; + } + + public static void deleteTempDir() { + for(File file:TMPE_FILES){ + boolean deleted = file.delete(); + if (!deleted) { + file.deleteOnExit(); + } } - TMPE_FILES.add(file); - return baseDir; } public static NamesrvController createAndStartNamesrv() { - String baseDir = createBaseDir(); + String baseDir = createTempDir(); + Path kvConfigPath = Paths.get(baseDir,"namesrv","kvConfig.json"); + Path namesrvPath = Paths.get(baseDir,"namesrv","namesrv.properties"); + NamesrvConfig namesrvConfig = new NamesrvConfig(); NettyServerConfig nameServerNettyServerConfig = new NettyServerConfig(); - namesrvConfig.setKvConfigPath(baseDir + SEP + "namesrv" + SEP + "kvConfig.json"); - namesrvConfig.setConfigStorePath(baseDir + SEP + "namesrv" + SEP + "namesrv.properties"); + + namesrvConfig.setKvConfigPath(kvConfigPath.toString()); + namesrvConfig.setConfigStorePath(namesrvPath.toString()); nameServerNettyServerConfig.setListenPort(nextPort()); NamesrvController namesrvController = @@ -76,14 +88,15 @@ public static NamesrvController createAndStartNamesrv() { namesrvController.start(); } catch (Exception e) { logger.info("Name Server start failed", e); - System.exit(1); } NAMESRV_CONTROLLERS.add(namesrvController); return namesrvController; } public static BrokerController createAndStartBroker(String nsAddr) { - String baseDir = createBaseDir(); + String baseDir = createTempDir(); + Path commitLogPath = Paths.get(baseDir,"commitlog"); + BrokerConfig brokerConfig = new BrokerConfig(); MessageStoreConfig storeConfig = new MessageStoreConfig(); brokerConfig.setBrokerName(BROKER_NAME_PREFIX + BROKER_INDEX.getAndIncrement()); @@ -91,7 +104,7 @@ public static BrokerController createAndStartBroker(String nsAddr) { brokerConfig.setNamesrvAddr(nsAddr); brokerConfig.setEnablePropertyFilter(true); storeConfig.setStorePathRootDir(baseDir); - storeConfig.setStorePathCommitLog(baseDir + SEP + "commitlog"); + storeConfig.setStorePathCommitLog(commitLogPath.toString()); storeConfig.setMappedFileSizeCommitLog(COMMIT_LOG_SIZE); storeConfig.setMaxIndexNum(INDEX_NUM); storeConfig.setMaxHashSlotNum(INDEX_NUM * 4); @@ -149,10 +162,4 @@ public static boolean initTopic(String topic, String nsAddr, String clusterName) return initTopic(topic, nsAddr, clusterName, 8); } - public static void deleteFile(File file) { - if (!file.exists()) { - return; - } - UtilAll.deleteFile(file); - } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java deleted file mode 100644 index 25f133ac867d..000000000000 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/dledger/DLedgerProduceAndConsumeIT.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package base.dledger; - -import static base.IntegrationTestBase.nextPort; - -import base.BaseConf; -import base.IntegrationTestBase; -import org.apache.rocketmq.common.BrokerConfig; -import org.apache.rocketmq.store.config.MessageStoreConfig; - -public class DLedgerProduceAndConsumeIT { - - public BrokerConfig buildBrokerConfig(String cluster, String brokerName) { - BrokerConfig brokerConfig = new BrokerConfig(); - brokerConfig.setBrokerClusterName(cluster); - brokerConfig.setBrokerName(brokerName); - brokerConfig.setBrokerIP1("127.0.0.1"); - brokerConfig.setNamesrvAddr(BaseConf.nsAddr); - return brokerConfig; - } - - public MessageStoreConfig buildStoreConfig(String brokerName, String peers, String selfId) { - MessageStoreConfig storeConfig = new MessageStoreConfig(); - String baseDir = IntegrationTestBase.createBaseDir(); - storeConfig.setStorePathRootDir(baseDir); - storeConfig.setStorePathCommitLog(baseDir + "_" + "commitlog"); - storeConfig.setHaListenPort(nextPort()); - storeConfig.setMappedFileSizeCommitLog(10 * 1024 * 1024); - storeConfig.setEnableDLegerCommitLog(true); - storeConfig.setdLegerGroup(brokerName); - storeConfig.setdLegerSelfId(selfId); - storeConfig.setdLegerPeers(peers); - return storeConfig; - } -} From 558acf769b0eaf953dc510b1e06feba02063a5f8 Mon Sep 17 00:00:00 2001 From: addname Date: Sun, 28 Feb 2021 19:49:24 +0800 Subject: [PATCH 15/44] Use hooks to register in the iavaagent instrumentation --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ab2d9aab4255..0d3119f60bc8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -81,4 +81,4 @@ jobs: env: S3_BUILD_CACHE_ACCESS_KEY_ID: ${{ secrets.S3_BUILD_CACHE_ACCESS_KEY_ID }} S3_BUILD_CACHE_SECRET_KEY: ${{ secrets.S3_BUILD_CACHE_SECRET_KEY }} - run: ./gradlew test -PtestJavaVersion=${{ matrix.java }} --stacktrace -Porg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }} -Porg.gradle.java.installations.auto-download=false + run: ./gradlew test -PtestJavaVersion=${{ matrix.java }} --stacktrace -Porg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }} -Porg.gradle.java.installations.auto-download=false \ No newline at end of file From 310c2684a1bc5b48ed665ea3713d15e163b2a795 Mon Sep 17 00:00:00 2001 From: addname Date: Sun, 28 Feb 2021 20:27:14 +0800 Subject: [PATCH 16/44] Use hooks to register in the iavaagent instrumentation --- .../RocketMqConsumerInstrumentation.java | 18 +++++++++--------- .../RocketMqProducerInstrumentation.java | 4 ++-- .../rocketmq/TextMapInjectAdapter.java | 2 +- .../TracingConsumeMessageHookImpl.java | 6 +++--- .../rocketmq/TracingSendMessageHookImpl.java | 6 +++--- .../rocketmq/RocketMqClientTest.groovy | 2 +- .../rocketmq/AbstractRocketMqClientTest.groovy | 6 +++--- .../testing/src/main/java/base/BaseConf.java | 4 ++-- .../main/java/base/IntegrationTestBase.java | 10 +++++----- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java index 9d0ec6376934..54be9b07f08d 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java @@ -5,21 +5,21 @@ package io.opentelemetry.javaagent.instrumentation.rocketmq; +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; +import static java.util.Collections.singletonMap; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + import io.opentelemetry.instrumentation.rocketmq.TracingConsumeMessageHookImpl; import io.opentelemetry.javaagent.tooling.TypeInstrumentation; +import java.util.Map; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl; -import java.util.Map; - -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; -import static java.util.Collections.singletonMap; -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.named; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; public class RocketMqConsumerInstrumentation implements TypeInstrumentation { @@ -43,8 +43,8 @@ public Map, String> transfor public static class AdviceStart { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter( - @Advice.FieldValue(value = "defaultMQPushConsumerImpl",declaringType = DefaultMQPushConsumer.class) DefaultMQPushConsumerImpl defaultMQPushConsumerImpl){ - defaultMQPushConsumerImpl.registerConsumeMessageHook(new TracingConsumeMessageHookImpl()); + @Advice.FieldValue(value = "defaultMQPushConsumerImpl", declaringType = DefaultMQPushConsumer.class) DefaultMQPushConsumerImpl defaultMqPushConsumerImpl) { + defaultMqPushConsumerImpl.registerConsumeMessageHook(new TracingConsumeMessageHookImpl()); } } } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java index 90cbe3632af1..3e3b01fb30d8 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java @@ -43,8 +43,8 @@ public Map, String> transfor public static class AdviceStart { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter( - @Advice.FieldValue(value = "defaultMQProducerImpl",declaringType = DefaultMQProducer.class) DefaultMQProducerImpl defaultMQProducerImpl){ - defaultMQProducerImpl.registerSendMessageHook(new TracingSendMessageHookImpl()); + @Advice.FieldValue(value = "defaultMQProducerImpl", declaringType = DefaultMQProducer.class) DefaultMQProducerImpl defaultMqProducerImpl) { + defaultMqProducerImpl.registerSendMessageHook(new TracingSendMessageHookImpl()); } } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java index 40ba41a351e6..28ab0a80b7d7 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java @@ -14,6 +14,6 @@ public class TextMapInjectAdapter implements TextMapSetter> @Override public void set(Map carrier, String key, String value) { - carrier.put(key,value); + carrier.put(key, value); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java index ca079db21f62..9591aa54573f 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java @@ -1,11 +1,11 @@ package io.opentelemetry.instrumentation.rocketmq; +import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; + import io.opentelemetry.context.Context; import org.apache.rocketmq.client.hook.ConsumeMessageContext; import org.apache.rocketmq.client.hook.ConsumeMessageHook; -import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; - public class TracingConsumeMessageHookImpl implements ConsumeMessageHook { @Override @@ -22,7 +22,7 @@ public void consumeMessageAfter(ConsumeMessageContext context) { if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { return; } - Context traceContext =tracer().startSpan(Context.current(),context.getMsgList()); + Context traceContext = tracer().startSpan(Context.current(), context.getMsgList()); tracer().end(traceContext); } 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 5177d69c78fe..d4850abb25e5 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 @@ -21,7 +21,7 @@ public void sendMessageBefore(SendMessageContext context) { return; } Context traceContext = tracer().startProducerSpan(context.getBrokerAddr(), context.getMessage(), Context.current()); - if (RocketMqClientConfig.isPropagationEnabled()){ + if (RocketMqClientConfig.isPropagationEnabled()) { GlobalOpenTelemetry.getPropagators() .getTextMapPropagator() .inject(traceContext, context.getMessage().getProperties(), SETTER); @@ -34,7 +34,7 @@ public void sendMessageAfter(SendMessageContext context) { if (context == null || context.getMqTraceContext() == null || context.getSendResult() == null) { return; } - tracer().afterProduce((Context)context.getMqTraceContext(), context.getSendResult()); - tracer().end((Context)context.getMqTraceContext()); + tracer().afterProduce((Context) context.getMqTraceContext(), context.getSendResult()); + tracer().end((Context) context.getMqTraceContext()); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 989b80c3750f..79c85d0450ed 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -46,7 +46,7 @@ class RocketMqClientTest extends InstrumentationSpecification implements Library def "test rocketmq produce and consume"() { setup: - producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + producer = BaseConf.getProducer(BaseConf.nsAddr) producer.getDefaultMQProducerImpl().registerSendMessageHook(new TracingSendMessageHookImpl()) consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index efeaa38aed2b..8e5898c37cf0 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -49,7 +49,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce callback"() { setup: - producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + producer = BaseConf.getProducer(BaseConf.nsAddr) when: runUnderTrace("parent") { producer.send(msg, new SendCallback() { @@ -89,7 +89,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce and concurrently consume"() { setup: - producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + producer = BaseConf.getProducer(BaseConf.nsAddr) consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) when: runUnderTrace("parent") { @@ -140,7 +140,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ def "test rocketmq produce and orderly consume"() { setup: - producer = BaseConf.getRMQProducer(BaseConf.nsAddr) + producer = BaseConf.getProducer(BaseConf.nsAddr) consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) when: runUnderTrace("parent") { diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 296e8c070780..0e60ace96a65 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -49,7 +49,7 @@ private BaseConf() {} public static String initTopic() { String topic = MQRandomUtils.getRandomTopic(); - if(!IntegrationTestBase.initTopic(topic, nsAddr, clusterName)){ + if (!IntegrationTestBase.initTopic(topic, nsAddr, clusterName)) { log.error("Topic init failed"); } return topic; @@ -82,7 +82,7 @@ public static DefaultMQPushConsumer getConsumer(String nsAddr, String topic, Str return consumer; } - public static DefaultMQProducer getRMQProducer(String ns) throws MQClientException { + public static DefaultMQProducer getProducer(String ns) throws MQClientException { DefaultMQProducer producer = new DefaultMQProducer(RandomUtil.getStringByUUID()); producer.setInstanceName(UUID.randomUUID().toString()); producer.setNamesrvAddr(ns); diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index e8055d7bbc06..44ad8b37c305 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -52,7 +52,7 @@ private static String createTempDir() { try { File file = Files.createTempDirectory("opentelemetry-rocketmq-client-temp").toFile(); TMPE_FILES.add(file); - path= file.getCanonicalPath(); + path = file.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); } @@ -60,7 +60,7 @@ private static String createTempDir() { } public static void deleteTempDir() { - for(File file:TMPE_FILES){ + for (File file : TMPE_FILES) { boolean deleted = file.delete(); if (!deleted) { file.deleteOnExit(); @@ -70,8 +70,8 @@ public static void deleteTempDir() { public static NamesrvController createAndStartNamesrv() { String baseDir = createTempDir(); - Path kvConfigPath = Paths.get(baseDir,"namesrv","kvConfig.json"); - Path namesrvPath = Paths.get(baseDir,"namesrv","namesrv.properties"); + Path kvConfigPath = Paths.get(baseDir, "namesrv", "kvConfig.json"); + Path namesrvPath = Paths.get(baseDir, "namesrv", "namesrv.properties"); NamesrvConfig namesrvConfig = new NamesrvConfig(); NettyServerConfig nameServerNettyServerConfig = new NettyServerConfig(); @@ -95,7 +95,7 @@ public static NamesrvController createAndStartNamesrv() { public static BrokerController createAndStartBroker(String nsAddr) { String baseDir = createTempDir(); - Path commitLogPath = Paths.get(baseDir,"commitlog"); + Path commitLogPath = Paths.get(baseDir, "commitlog"); BrokerConfig brokerConfig = new BrokerConfig(); MessageStoreConfig storeConfig = new MessageStoreConfig(); From c98b82823725ed7a2700a873f4b6021f2512fece Mon Sep 17 00:00:00 2001 From: addname Date: Sun, 28 Feb 2021 20:41:07 +0800 Subject: [PATCH 17/44] Use hooks to register in the iavaagent instrumentation --- .../RocketMqConsumerInstrumentation.java | 5 ++- .../RocketMqInstrumentationModule.java | 5 +-- .../RocketMqProducerInstrumentation.java | 3 +- .../rocketmq/RocketMqClientConfig.java | 3 +- .../rocketmq/RocketMqConsumerTracer.java | 31 ++++++++++--------- .../TracingConsumeMessageHookImpl.java | 9 ++++-- .../rocketmq/TracingSendMessageHookImpl.java | 9 +++++- .../testing/src/main/java/base/BaseConf.java | 4 +-- .../main/java/base/IntegrationTestBase.java | 3 +- 9 files changed, 43 insertions(+), 29 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java index 54be9b07f08d..900ae1f8efce 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java @@ -43,7 +43,10 @@ public Map, String> transfor public static class AdviceStart { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter( - @Advice.FieldValue(value = "defaultMQPushConsumerImpl", declaringType = DefaultMQPushConsumer.class) DefaultMQPushConsumerImpl defaultMqPushConsumerImpl) { + @Advice.FieldValue( + value = "defaultMQPushConsumerImpl", + declaringType = DefaultMQPushConsumer.class) + DefaultMQPushConsumerImpl defaultMqPushConsumerImpl) { defaultMqPushConsumerImpl.registerConsumeMessageHook(new TracingConsumeMessageHookImpl()); } } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java index dc532b79850d..9b8aeead195a 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqInstrumentationModule.java @@ -20,9 +20,6 @@ public RocketMqInstrumentationModule() { @Override public List typeInstrumentations() { - return asList( - new RocketMqProducerInstrumentation(), - new RocketMqConsumerInstrumentation() - ); + return asList(new RocketMqProducerInstrumentation(), new RocketMqConsumerInstrumentation()); } } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java index 3e3b01fb30d8..2517f206bdf5 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java @@ -43,7 +43,8 @@ public Map, String> transfor public static class AdviceStart { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter( - @Advice.FieldValue(value = "defaultMQProducerImpl", declaringType = DefaultMQProducer.class) DefaultMQProducerImpl defaultMqProducerImpl) { + @Advice.FieldValue(value = "defaultMQProducerImpl", declaringType = DefaultMQProducer.class) + DefaultMQProducerImpl defaultMqProducerImpl) { defaultMqProducerImpl.registerSendMessageHook(new TracingSendMessageHookImpl()); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java index c9ba3789d799..ba9f08d6d793 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java @@ -16,7 +16,8 @@ public static boolean isPropagationEnabled() { public static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = Config.get() - .getBooleanProperty("otel.instrumentation.rocketmq.client.experimental-span-attributes", false); + .getBooleanProperty( + "otel.instrumentation.rocketmq.client.experimental-span-attributes", false); private RocketMqClientConfig() {} } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index 696cde11628f..db5383120f19 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -32,8 +32,7 @@ protected String getInstrumentationName() { public Context startSpan(Context parentContext, List msgs) { MessageExt msg = msgs.get(0); if (msgs.size() == 1) { - SpanBuilder spanBuilder = startSpanBuilder(msg) - .setParent(extractParent(msg)); + SpanBuilder spanBuilder = startSpanBuilder(msg).setParent(extractParent(msg)); return withClientSpan(parentContext, spanBuilder.startSpan()); } else { SpanBuilder spanBuilder = @@ -51,22 +50,26 @@ public Context startSpan(Context parentContext, List msgs) { } public void createChildSpan(Context parentContext, MessageExt msg) { - SpanBuilder childSpanBuilder = startSpanBuilder(msg) - .setParent(parentContext) - .addLink(Span.fromContext(extractParent(msg)).getSpanContext()); + SpanBuilder childSpanBuilder = + startSpanBuilder(msg) + .setParent(parentContext) + .addLink(Span.fromContext(extractParent(msg)).getSpanContext()); end(withClientSpan(parentContext, childSpanBuilder.startSpan())); } public SpanBuilder startSpanBuilder(MessageExt msg) { - SpanBuilder spanBuilder = tracer.spanBuilder(spanNameOnConsume(msg)) - .setSpanKind(CONSUMER) - .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") - .setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()) - .setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic") - .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "process") - .setAttribute(SemanticAttributes.MESSAGING_MESSAGE_ID, msg.getMsgId()) - .setAttribute( - SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, (long) msg.getBody().length); + SpanBuilder spanBuilder = + tracer + .spanBuilder(spanNameOnConsume(msg)) + .setSpanKind(CONSUMER) + .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") + .setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()) + .setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic") + .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "process") + .setAttribute(SemanticAttributes.MESSAGING_MESSAGE_ID, msg.getMsgId()) + .setAttribute( + SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, + (long) msg.getBody().length); onConsume(spanBuilder, msg); return spanBuilder; } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java index 9591aa54573f..21d8b3476b87 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.instrumentation.rocketmq; import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; @@ -14,8 +19,7 @@ public String hookName() { } @Override - public void consumeMessageBefore(ConsumeMessageContext context) { - } + public void consumeMessageBefore(ConsumeMessageContext context) {} @Override public void consumeMessageAfter(ConsumeMessageContext context) { @@ -25,5 +29,4 @@ public void consumeMessageAfter(ConsumeMessageContext context) { Context traceContext = tracer().startSpan(Context.current(), context.getMsgList()); tracer().end(traceContext); } - } 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 d4850abb25e5..a11625ca0651 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 @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.instrumentation.rocketmq; import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; @@ -20,7 +25,9 @@ public void sendMessageBefore(SendMessageContext context) { if (context == null) { return; } - Context traceContext = tracer().startProducerSpan(context.getBrokerAddr(), context.getMessage(), Context.current()); + Context traceContext = + tracer() + .startProducerSpan(context.getBrokerAddr(), context.getMessage(), Context.current()); if (RocketMqClientConfig.isPropagationEnabled()) { GlobalOpenTelemetry.getPropagators() .getTextMapPropagator() diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 0e60ace96a65..dfdf9343e3ef 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -69,8 +69,8 @@ public static String initConsumerGroup(String group) { return group; } - public static DefaultMQPushConsumer getConsumer(String nsAddr, String topic, String subExpression, - AbstractListener listener) + public static DefaultMQPushConsumer getConsumer( + String nsAddr, String topic, String subExpression, AbstractListener listener) throws MQClientException { String consumerGroup = initConsumerGroup(); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup); diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index 44ad8b37c305..def8779a09a6 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -47,7 +47,7 @@ public static synchronized int nextPort() { protected static final Random random = new Random(); - private static String createTempDir() { + private static String createTempDir() { String path = null; try { File file = Files.createTempDirectory("opentelemetry-rocketmq-client-temp").toFile(); @@ -161,5 +161,4 @@ public static boolean initTopic( public static boolean initTopic(String topic, String nsAddr, String clusterName) { return initTopic(topic, nsAddr, clusterName, 8); } - } From 1ea63af94b37629501667916a8ad7e0df06f1525 Mon Sep 17 00:00:00 2001 From: addname Date: Sun, 28 Feb 2021 21:11:06 +0800 Subject: [PATCH 18/44] Use hooks to register in the iavaagent instrumentation --- .../rocketmq/RocketMqClientTest.groovy | 2 -- .../testing/src/main/java/base/BaseConf.java | 4 +-- .../main/java/base/IntegrationTestBase.java | 28 ++----------------- 3 files changed, 3 insertions(+), 31 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 79c85d0450ed..48d8b9ae1ddb 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -83,8 +83,6 @@ class RocketMqClientTest extends InstrumentationSpecification implements Library } } cleanup: - producer.shutdown() - consumer.shutdown() BaseConf.deleteTempDir() } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index dfdf9343e3ef..381797f64d86 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -49,9 +49,7 @@ private BaseConf() {} public static String initTopic() { String topic = MQRandomUtils.getRandomTopic(); - if (!IntegrationTestBase.initTopic(topic, nsAddr, clusterName)) { - log.error("Topic init failed"); - } + IntegrationTestBase.initTopic(topic, nsAddr, clusterName); return topic; } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index def8779a09a6..8fd7529cb7ff 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -134,31 +134,7 @@ public static BrokerController createAndStartBroker( return brokerController; } - public static boolean initTopic( - String topic, String nsAddr, String clusterName, int queueNumbers) { - long startTime = System.currentTimeMillis(); - boolean createResult; - - while (true) { - createResult = MQAdmin.createTopic(nsAddr, clusterName, topic, queueNumbers); - if (createResult) { - break; - } else if (System.currentTimeMillis() - startTime > topicCreateTime) { - Assert.fail( - String.format( - "topic[%s] is created failed after:%d ms", - topic, System.currentTimeMillis() - startTime)); - break; - } else { - TestUtils.waitForMoment(500); - continue; - } - } - - return createResult; - } - - public static boolean initTopic(String topic, String nsAddr, String clusterName) { - return initTopic(topic, nsAddr, clusterName, 8); + public static void initTopic(String topic, String nsAddr, String clusterName) { + MQAdmin.createTopic(nsAddr, clusterName, topic, 20); } } From 449b8e222de0b0f008c787d6868a40759d63f097 Mon Sep 17 00:00:00 2001 From: addname Date: Sun, 28 Feb 2021 21:46:29 +0800 Subject: [PATCH 19/44] Use hooks to register in the iavaagent instrumentation --- .../testing/src/main/java/base/BaseConf.java | 6 ------ .../testing/src/main/java/base/IntegrationTestBase.java | 2 -- 2 files changed, 8 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 381797f64d86..11794e434d98 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -5,10 +5,7 @@ package base; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; -import org.apache.log4j.Logger; import org.apache.rocketmq.broker.BrokerController; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.exception.MQClientException; @@ -29,9 +26,6 @@ final class BaseConf { protected static int brokerNum; protected static final NamesrvController namesrvController; protected static final BrokerController brokerController1; - protected static final List mqClients = new ArrayList(); - protected static boolean debug = false; - private static final Logger log = Logger.getLogger(BaseConf.class); static { System.setProperty( diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java index 8fd7529cb7ff..f58e58125789 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/IntegrationTestBase.java @@ -24,7 +24,6 @@ import org.apache.rocketmq.remoting.netty.NettyServerConfig; import org.apache.rocketmq.store.config.MessageStoreConfig; import org.apache.rocketmq.test.util.MQAdmin; -import org.apache.rocketmq.test.util.TestUtils; import org.junit.Assert; public class IntegrationTestBase { @@ -36,7 +35,6 @@ public class IntegrationTestBase { protected static final List TMPE_FILES = new ArrayList<>(); protected static final List BROKER_CONTROLLERS = new ArrayList<>(); protected static final List NAMESRV_CONTROLLERS = new ArrayList<>(); - protected static int topicCreateTime = 30 * 1000; protected static final int COMMIT_LOG_SIZE = 1024 * 1024 * 100; protected static final int INDEX_NUM = 1000; private static final AtomicInteger port = new AtomicInteger(40000); From a92c9b3f1e53b093bd97cb816e57258c3ce87027 Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 11:21:26 +0800 Subject: [PATCH 20/44] Fix --- .../rocketmq-client-4.8-javaagent.gradle | 2 +- .../RocketMqConsumerInstrumentation.java | 5 - .../RocketMqProducerInstrumentation.java | 5 - .../rocketmq/RocketMqClientTest.groovy | 13 ++ .../rocketmq-client-4.8-library.gradle | 3 +- .../rocketmq/ContextAndScope.java | 27 ++++ .../rocketmq/RocketMqClientConfig.java | 4 +- .../rocketmq/RocketMqConsumerTracer.java | 19 +-- .../rocketmq/RocketMqProducerTracer.java | 14 +- .../TracingConsumeMessageHookImpl.java | 16 ++- .../rocketmq/TracingSendMessageHookImpl.java | 13 +- .../rocketmq/RocketMqClientTest.groovy | 84 ++---------- .../AbstractRocketMqClientTest.groovy | 123 +++++++++++------- .../testing/src/main/java/base/BaseConf.java | 20 +-- 14 files changed, 168 insertions(+), 180 deletions(-) create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java diff --git a/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle b/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle index d4cd5efb6903..b0b2ed33f6ba 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle +++ b/instrumentation/rocketmq-client-4.8/javaagent/rocketmq-client-4.8-javaagent.gradle @@ -17,5 +17,5 @@ dependencies { } tasks.withType(Test) { - jvmArgs "-Dotel.instrumentation.rocketmq.client.experimental-span-attributes=true" + jvmArgs "-Dotel.instrumentation.rocketmq-client.experimental-span-attributes=true" } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java index 900ae1f8efce..06fc0e19dcc2 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java @@ -23,11 +23,6 @@ public class RocketMqConsumerInstrumentation implements TypeInstrumentation { - @Override - public ElementMatcher classLoaderOptimization() { - return hasClassesNamed("org.apache.rocketmq.client.consumer.DefaultMQPushConsumer"); - } - @Override public ElementMatcher typeMatcher() { return named("org.apache.rocketmq.client.consumer.DefaultMQPushConsumer"); diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java index 2517f206bdf5..6d55b95354d3 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java @@ -23,11 +23,6 @@ public class RocketMqProducerInstrumentation implements TypeInstrumentation { - @Override - public ElementMatcher classLoaderOptimization() { - return hasClassesNamed("org.apache.rocketmq.client.producer.DefaultMQProducer"); - } - @Override public ElementMatcher typeMatcher() { return named("org.apache.rocketmq.client.producer.DefaultMQProducer"); diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 2dc2e21addc3..e8af256cf021 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -5,9 +5,22 @@ package io.opentelemetry.instrumentation.rocketmq +import base.BaseConf import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest import io.opentelemetry.instrumentation.test.AgentTestTrait +import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener class RocketMqClientTest extends AbstractRocketMqClientTest implements AgentTestTrait { + @Override + void configureMQProducer() { + producer=BaseConf.getProducer(BaseConf.nsAddr) + } + + @Override + void configureMQPushConsumer() { + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer.setConsumeMessageBatchMaxSize(2); + } + } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle index e2ac77c73d4f..f1dd2887b63c 100644 --- a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle +++ b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle @@ -3,9 +3,8 @@ apply from: "$rootDir/gradle/instrumentation-library.gradle" dependencies { library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' testImplementation project(':instrumentation:rocketmq-client-4.8:testing') - } tasks.withType(Test) { - jvmArgs "-Dotel.instrumentation.rocketmq.client.experimental-span-attributes=true" + jvmArgs "-Dotel.instrumentation.rocketmq-client.experimental-span-attributes=true" } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java new file mode 100644 index 000000000000..d4bea5246e30 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java @@ -0,0 +1,27 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; + +public final class ContextAndScope { + private final Context context; + private final Scope scope; + + public ContextAndScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; + } + + public Context getContext() { + return context; + } + + public void closeScope() { + scope.close(); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java index ba9f08d6d793..193679855b62 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java @@ -11,13 +11,13 @@ public final class RocketMqClientConfig { public static boolean isPropagationEnabled() { return Config.get() - .getBooleanProperty("otel.instrumentation.rocketmq.client-propagation", true); + .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true); } public static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = Config.get() .getBooleanProperty( - "otel.instrumentation.rocketmq.client.experimental-span-attributes", false); + "otel.instrumentation.rocketmq-client.experimental-span-attributes", false); private RocketMqClientConfig() {} } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index db5383120f19..36c411b24d8c 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -15,6 +15,7 @@ import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.List; import org.apache.rocketmq.common.message.MessageExt; +import org.checkerframework.checker.nullness.qual.Nullable; public class RocketMqConsumerTracer extends BaseTracer { @@ -30,14 +31,13 @@ protected String getInstrumentationName() { } public Context startSpan(Context parentContext, List msgs) { - MessageExt msg = msgs.get(0); if (msgs.size() == 1) { - SpanBuilder spanBuilder = startSpanBuilder(msg).setParent(extractParent(msg)); - return withClientSpan(parentContext, spanBuilder.startSpan()); + SpanBuilder spanBuilder = startSpanBuilder(msgs.get(0)).setParent(extractParent(msgs.get(0))); + return parentContext.with(spanBuilder.startSpan()); } else { SpanBuilder spanBuilder = tracer - .spanBuilder(msg.getTopic() + " receive") + .spanBuilder("multiple_sources receive") .setSpanKind(CONSUMER) .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "receive"); @@ -49,7 +49,7 @@ public Context startSpan(Context parentContext, List msgs) { } } - public void createChildSpan(Context parentContext, MessageExt msg) { + private void createChildSpan(Context parentContext, MessageExt msg) { SpanBuilder childSpanBuilder = startSpanBuilder(msg) .setParent(parentContext) @@ -57,7 +57,7 @@ public void createChildSpan(Context parentContext, MessageExt msg) { end(withClientSpan(parentContext, childSpanBuilder.startSpan())); } - public SpanBuilder startSpanBuilder(MessageExt msg) { + private SpanBuilder startSpanBuilder(MessageExt msg) { SpanBuilder spanBuilder = tracer .spanBuilder(spanNameOnConsume(msg)) @@ -82,7 +82,7 @@ private Context extractParent(MessageExt msg) { } } - void onConsume(SpanBuilder spanBuilder, MessageExt msg) { + private void onConsume(SpanBuilder spanBuilder, MessageExt msg) { if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { spanBuilder.setAttribute("messaging.rocketmq.tags", msg.getTags()); spanBuilder.setAttribute("messaging.rocketmq.queue_id", msg.getQueueId()); @@ -91,11 +91,12 @@ void onConsume(SpanBuilder spanBuilder, MessageExt msg) { } } - String spanNameOnConsume(MessageExt msg) { + private String spanNameOnConsume(MessageExt msg) { return msg.getTopic() + " process"; } - String getBrokerHost(MessageExt msg) { + @Nullable + private String getBrokerHost(MessageExt msg) { if (msg.getStoreHost() != null) { return msg.getStoreHost().toString().replace("/", ""); } else { diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index c12b2e7bd223..73e015ca20e1 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -28,17 +28,11 @@ protected String getInstrumentationName() { return "io.opentelemetry.javaagent.rocketmq-client"; } - public Context startProducerSpan(String addr, Message msg, Context parentContext) { - SpanBuilder spanBuilder = spanBuilder(spanNameOnProduce(msg), PRODUCER); + public Context startProducerSpan(Context parentContext,String addr, Message msg) { + SpanBuilder spanBuilder = + spanBuilder(spanNameOnProduce(msg), PRODUCER).setParent(parentContext); onProduce(spanBuilder, msg, addr); - return withClientSpan(parentContext, spanBuilder.startSpan()); - } - - public void onCallback(Context context, SendResult sendResult) { - if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { - Span span = Span.fromContext(context); - span.setAttribute("messaging.rocketmq.callback_result", sendResult.getSendStatus().name()); - } + return parentContext.with(spanBuilder.startSpan()); } private void onProduce(SpanBuilder spanBuilder, Message msg, String addr) { diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java index 21d8b3476b87..6616b474fc9d 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java @@ -19,14 +19,24 @@ public String hookName() { } @Override - public void consumeMessageBefore(ConsumeMessageContext context) {} + public void consumeMessageBefore(ConsumeMessageContext context) { + if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { + return; + } + Context traceContext = tracer().startSpan(Context.current(), context.getMsgList()); + ContextAndScope contextAndScope = new ContextAndScope(traceContext, traceContext.makeCurrent()); + context.setMqTraceContext(contextAndScope); + } @Override public void consumeMessageAfter(ConsumeMessageContext context) { if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { return; } - Context traceContext = tracer().startSpan(Context.current(), context.getMsgList()); - tracer().end(traceContext); + if (context.getMqTraceContext() instanceof ContextAndScope) { + ContextAndScope contextAndScope = (ContextAndScope) context.getMqTraceContext(); + contextAndScope.closeScope(); + tracer().end(contextAndScope.getContext()); + } } } 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 a11625ca0651..f9ec3bfcedc0 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 @@ -27,13 +27,14 @@ public void sendMessageBefore(SendMessageContext context) { } Context traceContext = tracer() - .startProducerSpan(context.getBrokerAddr(), context.getMessage(), Context.current()); + .startProducerSpan(Context.current(), context.getBrokerAddr(), context.getMessage()); if (RocketMqClientConfig.isPropagationEnabled()) { GlobalOpenTelemetry.getPropagators() .getTextMapPropagator() .inject(traceContext, context.getMessage().getProperties(), SETTER); } - context.setMqTraceContext(traceContext); + ContextAndScope contextAndScope = new ContextAndScope(traceContext, traceContext.makeCurrent()); + context.setMqTraceContext(contextAndScope); } @Override @@ -41,7 +42,11 @@ public void sendMessageAfter(SendMessageContext context) { if (context == null || context.getMqTraceContext() == null || context.getSendResult() == null) { return; } - tracer().afterProduce((Context) context.getMqTraceContext(), context.getSendResult()); - tracer().end((Context) context.getMqTraceContext()); + if (context.getMqTraceContext() instanceof ContextAndScope) { + ContextAndScope contextAndScope = (ContextAndScope) context.getMqTraceContext(); + tracer().afterProduce(contextAndScope.getContext(), context.getSendResult()); + contextAndScope.closeScope(); + tracer().end(contextAndScope.getContext()); + } } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 48d8b9ae1ddb..d8e9db3c114d 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -6,86 +6,22 @@ package io.opentelemetry.instrumentation.rocketmq import base.BaseConf -import io.opentelemetry.instrumentation.test.InstrumentationSpecification +import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest import io.opentelemetry.instrumentation.test.LibraryTestTrait -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes -import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer -import org.apache.rocketmq.client.producer.DefaultMQProducer -import org.apache.rocketmq.common.message.Message -import org.apache.rocketmq.remoting.common.RemotingHelper import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener -import spock.lang.Shared -import static io.opentelemetry.api.trace.SpanKind.CONSUMER -import static io.opentelemetry.api.trace.SpanKind.PRODUCER -import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan -import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace +class RocketMqClientTest extends AbstractRocketMqClientTest implements LibraryTestTrait { -class RocketMqClientTest extends InstrumentationSpecification implements LibraryTestTrait { - - @Shared - DefaultMQPushConsumer consumer - - @Shared - DefaultMQProducer producer - - @Shared - String sharedTopic - - @Shared - String brokerAddr - - @Shared - Message msg - - def setup() { - sharedTopic = BaseConf.initTopic() - brokerAddr = BaseConf.getBrokerAddr() - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) - } - - def "test rocketmq produce and consume"() { - setup: - producer = BaseConf.getProducer(BaseConf.nsAddr) + @Override + void configureMQProducer() { + producer=BaseConf.getProducer(BaseConf.nsAddr) producer.getDefaultMQProducerImpl().registerSendMessageHook(new TracingSendMessageHookImpl()) + } + @Override + void configureMQPushConsumer() { consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer.setConsumeMessageBatchMaxSize(2); consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(new TracingConsumeMessageHookImpl()) - when: - runUnderTrace("parent") { - producer.send(msg) - } - then: - assertTraces(1) { - trace(0, 3) { - basicSpan(it, 0, "parent") - span(1) { - name sharedTopic + " send" - kind PRODUCER - attributes { - "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" - "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic - "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" - "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String - } - } - span(2) { - name sharedTopic + " process" - kind CONSUMER - attributes { - "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" - "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic - "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" - "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" - "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long - "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String - } - } - } - cleanup: - BaseConf.deleteTempDir() - } } - -} - +} \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 8e5898c37cf0..c89cc849da83 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -14,8 +14,6 @@ import org.apache.rocketmq.client.producer.SendCallback import org.apache.rocketmq.client.producer.SendResult import org.apache.rocketmq.common.message.Message import org.apache.rocketmq.remoting.common.RemotingHelper -import org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener -import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener import spock.lang.Shared import spock.lang.Unroll import static io.opentelemetry.api.trace.SpanKind.CONSUMER @@ -24,7 +22,7 @@ import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace @Unroll -abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ +abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared DefaultMQPushConsumer consumer @@ -33,41 +31,36 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ DefaultMQProducer producer @Shared - String sharedTopic + def sharedTopic = BaseConf.initTopic() @Shared - String brokerAddr + Message msg @Shared - Message msg + def msgs = new ArrayList(); - def setup() { - sharedTopic = BaseConf.initTopic() - brokerAddr = BaseConf.getBrokerAddr() - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) - } + abstract void configureMQProducer() + + abstract void configureMQPushConsumer() def "test rocketmq produce callback"() { setup: - producer = BaseConf.getProducer(BaseConf.nsAddr) + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) + configureMQProducer() when: - runUnderTrace("parent") { - producer.send(msg, new SendCallback() { - @Override - void onSuccess(SendResult sendResult) { - } - - @Override - void onException(Throwable throwable) { - } - }) + producer.send(msg, new SendCallback() { + @Override + void onSuccess(SendResult sendResult) { + } - } + @Override + void onException(Throwable throwable) { + } + }) then: assertTraces(1) { - trace(0, 2) { - basicSpan(it, 0, "parent") - span(1) { + trace(0, 1) { + span(0) { name sharedTopic + " send" kind PRODUCER attributes { @@ -76,21 +69,22 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.broker_address" String "messaging.rocketmq.send_result" "SEND_OK" - } } } cleanup: + producer.shutdown() BaseConf.deleteTempDir() } } - def "test rocketmq produce and concurrently consume"() { + def "test rocketmq produce and consume"() { setup: - producer = BaseConf.getProducer(BaseConf.nsAddr) - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQNormalListener()) + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) + configureMQProducer() + configureMQPushConsumer() when: runUnderTrace("parent") { producer.send(msg) @@ -108,7 +102,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.broker_address" String "messaging.rocketmq.send_result" "SEND_OK" } } @@ -123,32 +117,39 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.broker_address" String "messaging.rocketmq.queue_id" Long "messaging.rocketmq.queue_offset" Long - } } } cleanup: + Thread.sleep(1000) producer.shutdown() consumer.shutdown() BaseConf.deleteTempDir() } } - - def "test rocketmq produce and orderly consume"() { + def "test rocketmq produce and batch consume"() { setup: - producer = BaseConf.getProducer(BaseConf.nsAddr) - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + Message msg1 = new Message(sharedTopic, "TagA", ("hello world a").getBytes()); + Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()); + msgs.add(msg1); + msgs.add(msg2); + configureMQProducer() + configureMQPushConsumer() when: runUnderTrace("parent") { - producer.send(msg) + producer.send(msgs) } then: - assertTraces(1) { - trace(0, 3) { + assertTraces(2) { + def itemStepSpan = null + + trace(0, 2) { + itemStepSpan = span(1) + basicSpan(it, 0, "parent") span(1) { name sharedTopic + " send" @@ -158,12 +159,22 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String - "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.broker_address" String "messaging.rocketmq.send_result" "SEND_OK" } } - span(2) { + } + + trace(1, 3) { + span(0) { + name "multiple_sources receive" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "receive" + } + } + span(1) { name sharedTopic + " process" kind CONSUMER attributes { @@ -174,17 +185,37 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification{ "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String "messaging.rocketmq.tags" "TagA" - "messaging.rocketmq.broker_address" brokerAddr + "messaging.rocketmq.broker_address" String + "messaging.rocketmq.queue_id" Long + "messaging.rocketmq.queue_offset" Long + } + childOf span(0) + hasLink itemStepSpan + } + span(2) { + name sharedTopic + " process" + kind CONSUMER + attributes { + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "rocketmq" + "${SemanticAttributes.MESSAGING_DESTINATION.key}" sharedTopic + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long + "${SemanticAttributes.MESSAGING_MESSAGE_ID.key}" String + "messaging.rocketmq.tags" "TagB" + "messaging.rocketmq.broker_address" String "messaging.rocketmq.queue_id" Long "messaging.rocketmq.queue_offset" Long } + childOf span(0) + hasLink itemStepSpan } } cleanup: + Thread.sleep(1000) producer.shutdown() consumer.shutdown() BaseConf.deleteTempDir() } } } - diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 11794e434d98..9b95296fca98 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -14,7 +14,6 @@ import org.apache.rocketmq.namesrv.NamesrvController; import org.apache.rocketmq.remoting.protocol.RemotingCommand; import org.apache.rocketmq.test.listener.AbstractListener; -import org.apache.rocketmq.test.util.MQAdmin; import org.apache.rocketmq.test.util.MQRandomUtils; import org.apache.rocketmq.test.util.RandomUtil; @@ -23,7 +22,6 @@ final class BaseConf { public static final String broker1Addr; protected static String broker1Name; protected static final String clusterName; - protected static int brokerNum; protected static final NamesrvController namesrvController; protected static final BrokerController brokerController1; @@ -36,7 +34,6 @@ final class BaseConf { clusterName = brokerController1.getBrokerConfig().getBrokerClusterName(); broker1Name = brokerController1.getBrokerConfig().getBrokerName(); broker1Addr = "127.0.0.1:" + brokerController1.getNettyServerConfig().getListenPort(); - brokerNum = 2; } private BaseConf() {} @@ -47,25 +44,10 @@ public static String initTopic() { return topic; } - public static String getBrokerAddr() { - return broker1Addr; - } - - public static String initConsumerGroup() { - String group = MQRandomUtils.getRandomConsumerGroup(); - return initConsumerGroup(group); - } - - public static String initConsumerGroup(String group) { - MQAdmin.createSub(nsAddr, clusterName, group); - return group; - } - public static DefaultMQPushConsumer getConsumer( String nsAddr, String topic, String subExpression, AbstractListener listener) throws MQClientException { - String consumerGroup = initConsumerGroup(); - DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup); + DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumerGroup"); consumer.setInstanceName(RandomUtil.getStringByUUID()); consumer.setNamesrvAddr(nsAddr); consumer.subscribe(topic, subExpression); From b5ba2ca0fefe732870507f2f253728c0ee4bdb78 Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 11:45:00 +0800 Subject: [PATCH 21/44] Revert "Use hooks to register in the iavaagent instrumentation" This reverts commit 558acf76 --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0d3119f60bc8..ab2d9aab4255 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -81,4 +81,4 @@ jobs: env: S3_BUILD_CACHE_ACCESS_KEY_ID: ${{ secrets.S3_BUILD_CACHE_ACCESS_KEY_ID }} S3_BUILD_CACHE_SECRET_KEY: ${{ secrets.S3_BUILD_CACHE_SECRET_KEY }} - run: ./gradlew test -PtestJavaVersion=${{ matrix.java }} --stacktrace -Porg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }} -Porg.gradle.java.installations.auto-download=false \ No newline at end of file + run: ./gradlew test -PtestJavaVersion=${{ matrix.java }} --stacktrace -Porg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }} -Porg.gradle.java.installations.auto-download=false From 48de0f7bad6b85878a6c4176d8c9f6fb274bf86d Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 12:42:54 +0800 Subject: [PATCH 22/44] Fix --- .../rocketmq/RocketMqConsumerTracer.java | 15 +++++---------- .../rocketmq/RocketMqProducerTracer.java | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index 36c411b24d8c..7bb50df25d8c 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -32,13 +32,11 @@ protected String getInstrumentationName() { public Context startSpan(Context parentContext, List msgs) { if (msgs.size() == 1) { - SpanBuilder spanBuilder = startSpanBuilder(msgs.get(0)).setParent(extractParent(msgs.get(0))); + SpanBuilder spanBuilder = startSpanBuilder(extractParent(msgs.get(0)), msgs.get(0)); return parentContext.with(spanBuilder.startSpan()); } else { SpanBuilder spanBuilder = - tracer - .spanBuilder("multiple_sources receive") - .setSpanKind(CONSUMER) + spanBuilder(parentContext, "multiple_sources receive", CONSUMER) .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "receive"); Context rootContext = withClientSpan(parentContext, spanBuilder.startSpan()); @@ -51,17 +49,14 @@ public Context startSpan(Context parentContext, List msgs) { private void createChildSpan(Context parentContext, MessageExt msg) { SpanBuilder childSpanBuilder = - startSpanBuilder(msg) - .setParent(parentContext) + startSpanBuilder(parentContext, msg) .addLink(Span.fromContext(extractParent(msg)).getSpanContext()); end(withClientSpan(parentContext, childSpanBuilder.startSpan())); } - private SpanBuilder startSpanBuilder(MessageExt msg) { + private SpanBuilder startSpanBuilder(Context parentContext, MessageExt msg) { SpanBuilder spanBuilder = - tracer - .spanBuilder(spanNameOnConsume(msg)) - .setSpanKind(CONSUMER) + spanBuilder(parentContext, spanNameOnConsume(msg), CONSUMER) .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") .setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()) .setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic") diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index 73e015ca20e1..6a6ed17a2b35 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -30,7 +30,7 @@ protected String getInstrumentationName() { public Context startProducerSpan(Context parentContext,String addr, Message msg) { SpanBuilder spanBuilder = - spanBuilder(spanNameOnProduce(msg), PRODUCER).setParent(parentContext); + spanBuilder(parentContext,spanNameOnProduce(msg), PRODUCER); onProduce(spanBuilder, msg, addr); return parentContext.with(spanBuilder.startSpan()); } From ad8cbc74dc8b578668f8a614744502e1081a99cd Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 13:13:47 +0800 Subject: [PATCH 23/44] Fix --- .../instrumentation/rocketmq/AbstractRocketMqClientTest.groovy | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index c89cc849da83..9d6eef94cfe4 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -124,7 +124,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } cleanup: - Thread.sleep(1000) producer.shutdown() consumer.shutdown() BaseConf.deleteTempDir() @@ -212,7 +211,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } cleanup: - Thread.sleep(1000) producer.shutdown() consumer.shutdown() BaseConf.deleteTempDir() From f28eee62eb6d48213517b6cea43b9c91eaa4d8ce Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 14:54:31 +0800 Subject: [PATCH 24/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 9d6eef94cfe4..eed7ab4ce47f 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -43,6 +43,10 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { abstract void configureMQPushConsumer() + def cleanupSpec() { + BaseConf.deleteTempDir() + } + def "test rocketmq produce callback"() { setup: msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) @@ -74,9 +78,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } - cleanup: - producer.shutdown() - BaseConf.deleteTempDir() } } @@ -123,10 +124,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } - cleanup: - producer.shutdown() - consumer.shutdown() - BaseConf.deleteTempDir() } } @@ -210,10 +207,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { hasLink itemStepSpan } } - cleanup: - producer.shutdown() - consumer.shutdown() - BaseConf.deleteTempDir() } } } From 81c372069f66cc9bb66100fc08e09420bf9d425d Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 15:12:04 +0800 Subject: [PATCH 25/44] Fix --- .../instrumentation/rocketmq/RocketMqProducerTracer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index 6a6ed17a2b35..7d9a5b5396b3 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -28,9 +28,9 @@ protected String getInstrumentationName() { return "io.opentelemetry.javaagent.rocketmq-client"; } - public Context startProducerSpan(Context parentContext,String addr, Message msg) { + public Context startProducerSpan(Context parentContext, String addr, Message msg) { SpanBuilder spanBuilder = - spanBuilder(parentContext,spanNameOnProduce(msg), PRODUCER); + spanBuilder(parentContext, spanNameOnProduce(msg), PRODUCER); onProduce(spanBuilder, msg, addr); return parentContext.with(spanBuilder.startSpan()); } From 1c2c71f96fd2e9e02b1a4d8f51f9bd4338624d91 Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 15:28:55 +0800 Subject: [PATCH 26/44] Fix --- .../instrumentation/rocketmq/RocketMqClientTest.groovy | 2 +- .../instrumentation/rocketmq/RocketMqClientTest.groovy | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index e8af256cf021..e361e25b9e76 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -20,7 +20,7 @@ class RocketMqClientTest extends AbstractRocketMqClientTest implements AgentTest @Override void configureMQPushConsumer() { consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) - consumer.setConsumeMessageBatchMaxSize(2); + consumer.setConsumeMessageBatchMaxSize(2) } } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index d8e9db3c114d..8f52fb2986c4 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -21,7 +21,7 @@ class RocketMqClientTest extends AbstractRocketMqClientTest implements LibraryTe @Override void configureMQPushConsumer() { consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) - consumer.setConsumeMessageBatchMaxSize(2); + consumer.setConsumeMessageBatchMaxSize(2) consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(new TracingConsumeMessageHookImpl()) } } \ No newline at end of file From 0ed11a0aa3e231fd30f1f2168ede8a996476e076 Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 15:42:14 +0800 Subject: [PATCH 27/44] Fix --- .../rocketmq/RocketMqConsumerInstrumentation.java | 1 - .../rocketmq/RocketMqProducerInstrumentation.java | 1 - .../instrumentation/rocketmq/RocketMqProducerTracer.java | 3 +-- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java index 06fc0e19dcc2..fe2eaa37c0bc 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java @@ -5,7 +5,6 @@ package io.opentelemetry.javaagent.instrumentation.rocketmq; -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java index 6d55b95354d3..c7b7f2edc35a 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java @@ -5,7 +5,6 @@ package io.opentelemetry.javaagent.instrumentation.rocketmq; -import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index 7d9a5b5396b3..fdacfe7617b1 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -29,8 +29,7 @@ protected String getInstrumentationName() { } public Context startProducerSpan(Context parentContext, String addr, Message msg) { - SpanBuilder spanBuilder = - spanBuilder(parentContext, spanNameOnProduce(msg), PRODUCER); + SpanBuilder spanBuilder = spanBuilder(parentContext, spanNameOnProduce(msg), PRODUCER); onProduce(spanBuilder, msg, addr); return parentContext.with(spanBuilder.startSpan()); } From c32bc400f838d041ef7674b4f021bff142fe4ffa Mon Sep 17 00:00:00 2001 From: addname Date: Sat, 6 Mar 2021 15:55:33 +0800 Subject: [PATCH 28/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index eed7ab4ce47f..08feee3d6aaa 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -37,7 +37,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { Message msg @Shared - def msgs = new ArrayList(); + def msgs = new ArrayList() abstract void configureMQProducer() @@ -129,10 +129,10 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and batch consume"() { setup: - Message msg1 = new Message(sharedTopic, "TagA", ("hello world a").getBytes()); - Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()); - msgs.add(msg1); - msgs.add(msg2); + Message msg1 = new Message(sharedTopic, "TagA", ("hello world a").getBytes()) + Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) + msgs.add(msg1) + msgs.add(msg2) configureMQProducer() configureMQPushConsumer() when: From 0489ef93a2aafac0e88bd0aab617d4ef92eb5df6 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 9 Mar 2021 10:20:33 +0800 Subject: [PATCH 29/44] Fix --- .../rocketmq/RocketMqClientHooks.java | 30 +++++++++++ .../RocketMqConsumerInstrumentation.java | 4 +- .../RocketMqProducerInstrumentation.java | 3 +- .../rocketmq/RocketMqClientTest.groovy | 12 ++--- .../rocketmq/RocketMqClientConfig.java | 23 -------- .../rocketmq/RocketMqConsumerTracer.java | 18 ++++--- .../rocketmq/RocketMqProducerTracer.java | 16 +++--- .../rocketmq/RocketMqTracing.java | 52 +++++++++++++++++++ .../rocketmq/RocketMqTracingBuilder.java | 36 +++++++++++++ .../rocketmq/TextMapExtractAdapter.java | 2 +- .../rocketmq/TextMapInjectAdapter.java | 2 +- .../TracingConsumeMessageHookImpl.java | 14 +++-- .../rocketmq/TracingSendMessageHookImpl.java | 20 ++++--- .../rocketmq/RocketMqClientTest.groovy | 15 +++--- .../AbstractRocketMqClientTest.groovy | 31 ++++++----- 15 files changed, 193 insertions(+), 85 deletions(-) create mode 100644 instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientHooks.java delete mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java create mode 100644 instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientHooks.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientHooks.java new file mode 100644 index 000000000000..eaebd89e0340 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqClientHooks.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.rocketmq; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.instrumentation.api.config.Config; +import io.opentelemetry.instrumentation.rocketmq.RocketMqTracing; +import org.apache.rocketmq.client.hook.ConsumeMessageHook; +import org.apache.rocketmq.client.hook.SendMessageHook; + +public final class RocketMqClientHooks { + private static final RocketMqTracing TRACING = + RocketMqTracing.newBuilder(GlobalOpenTelemetry.get()) + .setPropagationEnabled( + Config.get() + .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true)) + .setCaptureExperimentalSpanAttributes( + Config.get() + .getBooleanProperty( + "otel.instrumentation.rocketmq-client.experimental-span-attributes", false)) + .build(); + + public static final ConsumeMessageHook CONSUME_MESSAGE_HOOK = + TRACING.newTracingConsumeMessageHook(); + + public static final SendMessageHook SEND_MESSAGE_HOOK = TRACING.newTracingSendMessageHook(); +} diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java index fe2eaa37c0bc..69b8fea08fac 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqConsumerInstrumentation.java @@ -10,7 +10,6 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; -import io.opentelemetry.instrumentation.rocketmq.TracingConsumeMessageHookImpl; import io.opentelemetry.javaagent.tooling.TypeInstrumentation; import java.util.Map; import net.bytebuddy.asm.Advice; @@ -41,7 +40,8 @@ public static void onEnter( value = "defaultMQPushConsumerImpl", declaringType = DefaultMQPushConsumer.class) DefaultMQPushConsumerImpl defaultMqPushConsumerImpl) { - defaultMqPushConsumerImpl.registerConsumeMessageHook(new TracingConsumeMessageHookImpl()); + defaultMqPushConsumerImpl.registerConsumeMessageHook( + RocketMqClientHooks.CONSUME_MESSAGE_HOOK); } } } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java index c7b7f2edc35a..153ef6d2a836 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmq/RocketMqProducerInstrumentation.java @@ -10,7 +10,6 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; -import io.opentelemetry.instrumentation.rocketmq.TracingSendMessageHookImpl; import io.opentelemetry.javaagent.tooling.TypeInstrumentation; import java.util.Map; import net.bytebuddy.asm.Advice; @@ -39,7 +38,7 @@ public static class AdviceStart { public static void onEnter( @Advice.FieldValue(value = "defaultMQProducerImpl", declaringType = DefaultMQProducer.class) DefaultMQProducerImpl defaultMqProducerImpl) { - defaultMqProducerImpl.registerSendMessageHook(new TracingSendMessageHookImpl()); + defaultMqProducerImpl.registerSendMessageHook(RocketMqClientHooks.SEND_MESSAGE_HOOK); } } } diff --git a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index e361e25b9e76..478b03d22566 100644 --- a/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/javaagent/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -5,22 +5,18 @@ package io.opentelemetry.instrumentation.rocketmq -import base.BaseConf import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest import io.opentelemetry.instrumentation.test.AgentTestTrait -import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer +import org.apache.rocketmq.client.producer.DefaultMQProducer class RocketMqClientTest extends AbstractRocketMqClientTest implements AgentTestTrait { @Override - void configureMQProducer() { - producer=BaseConf.getProducer(BaseConf.nsAddr) + void configureMQProducer(DefaultMQProducer producer) { } @Override - void configureMQPushConsumer() { - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) - consumer.setConsumeMessageBatchMaxSize(2) + void configureMQPushConsumer(DefaultMQPushConsumer consumer) { } - } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java deleted file mode 100644 index 193679855b62..000000000000 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqClientConfig.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.rocketmq; - -import io.opentelemetry.instrumentation.api.config.Config; - -public final class RocketMqClientConfig { - - public static boolean isPropagationEnabled() { - return Config.get() - .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true); - } - - public static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = - Config.get() - .getBooleanProperty( - "otel.instrumentation.rocketmq-client.experimental-span-attributes", false); - - private RocketMqClientConfig() {} -} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index 7bb50df25d8c..51c521c90938 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -8,6 +8,7 @@ import static io.opentelemetry.api.trace.SpanKind.CONSUMER; import static io.opentelemetry.instrumentation.rocketmq.TextMapExtractAdapter.GETTER; +import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.context.Context; @@ -17,12 +18,15 @@ import org.apache.rocketmq.common.message.MessageExt; import org.checkerframework.checker.nullness.qual.Nullable; -public class RocketMqConsumerTracer extends BaseTracer { +final class RocketMqConsumerTracer extends BaseTracer { - private static final RocketMqConsumerTracer TRACER = new RocketMqConsumerTracer(); + private boolean captureExperimentalSpanAttributes; + private boolean propagationEnabled; - public static RocketMqConsumerTracer tracer() { - return TRACER; + RocketMqConsumerTracer(OpenTelemetry openTelemetry, boolean captureExperimentalSpanAttributes, boolean propagationEnabled) { + super(openTelemetry); + this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; + this.propagationEnabled = propagationEnabled; } @Override @@ -30,7 +34,7 @@ protected String getInstrumentationName() { return "io.opentelemetry.javaagent.rocketmq-client"; } - public Context startSpan(Context parentContext, List msgs) { + Context startSpan(Context parentContext, List msgs) { if (msgs.size() == 1) { SpanBuilder spanBuilder = startSpanBuilder(extractParent(msgs.get(0)), msgs.get(0)); return parentContext.with(spanBuilder.startSpan()); @@ -70,7 +74,7 @@ private SpanBuilder startSpanBuilder(Context parentContext, MessageExt msg) { } private Context extractParent(MessageExt msg) { - if (RocketMqClientConfig.isPropagationEnabled()) { + if (propagationEnabled) { return extract(msg.getProperties(), GETTER); } else { return Context.current(); @@ -78,7 +82,7 @@ private Context extractParent(MessageExt msg) { } private void onConsume(SpanBuilder spanBuilder, MessageExt msg) { - if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + if (captureExperimentalSpanAttributes) { spanBuilder.setAttribute("messaging.rocketmq.tags", msg.getTags()); spanBuilder.setAttribute("messaging.rocketmq.queue_id", msg.getQueueId()); spanBuilder.setAttribute("messaging.rocketmq.queue_offset", msg.getQueueOffset()); diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java index fdacfe7617b1..607367f84773 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqProducerTracer.java @@ -7,6 +7,7 @@ import static io.opentelemetry.api.trace.SpanKind.PRODUCER; +import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.context.Context; @@ -15,12 +16,13 @@ import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.common.message.Message; -public class RocketMqProducerTracer extends BaseTracer { +final class RocketMqProducerTracer extends BaseTracer { - private static final RocketMqProducerTracer TRACER = new RocketMqProducerTracer(); + private boolean captureExperimentalSpanAttributes; - public static RocketMqProducerTracer tracer() { - return TRACER; + RocketMqProducerTracer(OpenTelemetry openTelemetry, boolean captureExperimentalSpanAttributes) { + super(openTelemetry); + this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; } @Override @@ -28,7 +30,7 @@ protected String getInstrumentationName() { return "io.opentelemetry.javaagent.rocketmq-client"; } - public Context startProducerSpan(Context parentContext, String addr, Message msg) { + Context startProducerSpan(Context parentContext, String addr, Message msg) { SpanBuilder spanBuilder = spanBuilder(parentContext, spanNameOnProduce(msg), PRODUCER); onProduce(spanBuilder, msg, addr); return parentContext.with(spanBuilder.startSpan()); @@ -38,7 +40,7 @@ private void onProduce(SpanBuilder spanBuilder, Message msg, String addr) { spanBuilder.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq"); spanBuilder.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic"); spanBuilder.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, msg.getTopic()); - if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + if (captureExperimentalSpanAttributes) { spanBuilder.setAttribute("messaging.rocketmq.tags", msg.getTags()); spanBuilder.setAttribute("messaging.rocketmq.broker_address", addr); } @@ -47,7 +49,7 @@ private void onProduce(SpanBuilder spanBuilder, Message msg, String addr) { public void afterProduce(Context context, SendResult sendResult) { Span span = Span.fromContext(context); span.setAttribute(SemanticAttributes.MESSAGING_MESSAGE_ID, sendResult.getMsgId()); - if (RocketMqClientConfig.CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + if (captureExperimentalSpanAttributes) { span.setAttribute("messaging.rocketmq.send_result", sendResult.getSendStatus().name()); } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java new file mode 100644 index 000000000000..96bdd6651d89 --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java @@ -0,0 +1,52 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.api.config.Config; +import org.apache.rocketmq.client.hook.ConsumeMessageHook; +import org.apache.rocketmq.client.hook.SendMessageHook; + +public final class RocketMqTracing { + public static RocketMqTracing create(OpenTelemetry openTelemetry) { + return newBuilder(openTelemetry).setPropagationEnabled( + Config.get() + .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true)) + .setCaptureExperimentalSpanAttributes( + Config.get() + .getBooleanProperty( + "otel.instrumentation.rocketmq-client.experimental-span-attributes", true)).build(); + } + + public static RocketMqTracingBuilder newBuilder(OpenTelemetry openTelemetry) { + return new RocketMqTracingBuilder(openTelemetry); + } + + private final boolean captureExperimentalSpanAttributes; + private final boolean propagationEnabled; + + private final RocketMqConsumerTracer rocketMqConsumerTracer; + private final RocketMqProducerTracer rocketMqProducerTracer; + + RocketMqTracing( + OpenTelemetry openTelemetry, + boolean captureExperimentalSpanAttributes, + boolean propagationEnabled) { + this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; + this.propagationEnabled = propagationEnabled; + rocketMqConsumerTracer = new RocketMqConsumerTracer(openTelemetry, captureExperimentalSpanAttributes, propagationEnabled); + rocketMqProducerTracer = new RocketMqProducerTracer(openTelemetry, captureExperimentalSpanAttributes); + } + + public ConsumeMessageHook newTracingConsumeMessageHook() { + return new TracingConsumeMessageHookImpl(rocketMqConsumerTracer); + } + + public SendMessageHook newTracingSendMessageHook() { + return new TracingSendMessageHookImpl(rocketMqProducerTracer, propagationEnabled); + } + +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java new file mode 100644 index 000000000000..b5ccd26ab17c --- /dev/null +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.rocketmq; + +import io.opentelemetry.api.OpenTelemetry; + +public final class RocketMqTracingBuilder { + + private final OpenTelemetry openTelemetry; + + private boolean captureExperimentalSpanAttributes; + private boolean propagationEnabled; + + RocketMqTracingBuilder(OpenTelemetry openTelemetry) { + this.openTelemetry = openTelemetry; + } + + public RocketMqTracingBuilder setCaptureExperimentalSpanAttributes( + boolean captureExperimentalSpanAttributes) { + this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; + return this; + } + + public RocketMqTracingBuilder setPropagationEnabled(boolean propagationEnabled) { + this.propagationEnabled = propagationEnabled; + return this; + } + + public RocketMqTracing build() { + return new RocketMqTracing( + openTelemetry, captureExperimentalSpanAttributes, propagationEnabled); + } +} diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java index 94ae5beef546..c7fd9ed0a623 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapExtractAdapter.java @@ -8,7 +8,7 @@ import io.opentelemetry.context.propagation.TextMapGetter; import java.util.Map; -public class TextMapExtractAdapter implements TextMapGetter> { +final class TextMapExtractAdapter implements TextMapGetter> { public static final TextMapExtractAdapter GETTER = new TextMapExtractAdapter(); diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java index 28ab0a80b7d7..556f60f32800 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TextMapInjectAdapter.java @@ -8,7 +8,7 @@ import io.opentelemetry.context.propagation.TextMapSetter; import java.util.Map; -public class TextMapInjectAdapter implements TextMapSetter> { +final class TextMapInjectAdapter implements TextMapSetter> { public static final TextMapInjectAdapter SETTER = new TextMapInjectAdapter(); diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java index 6616b474fc9d..11b0775a02cc 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/TracingConsumeMessageHookImpl.java @@ -5,13 +5,17 @@ package io.opentelemetry.instrumentation.rocketmq; -import static io.opentelemetry.instrumentation.rocketmq.RocketMqConsumerTracer.tracer; - import io.opentelemetry.context.Context; import org.apache.rocketmq.client.hook.ConsumeMessageContext; import org.apache.rocketmq.client.hook.ConsumeMessageHook; -public class TracingConsumeMessageHookImpl implements ConsumeMessageHook { +final class TracingConsumeMessageHookImpl implements ConsumeMessageHook { + + private final RocketMqConsumerTracer tracer; + + TracingConsumeMessageHookImpl(RocketMqConsumerTracer tracer) { + this.tracer = tracer; + } @Override public String hookName() { @@ -23,7 +27,7 @@ public void consumeMessageBefore(ConsumeMessageContext context) { if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) { return; } - Context traceContext = tracer().startSpan(Context.current(), context.getMsgList()); + Context traceContext = tracer.startSpan(Context.current(), context.getMsgList()); ContextAndScope contextAndScope = new ContextAndScope(traceContext, traceContext.makeCurrent()); context.setMqTraceContext(contextAndScope); } @@ -36,7 +40,7 @@ public void consumeMessageAfter(ConsumeMessageContext context) { if (context.getMqTraceContext() instanceof ContextAndScope) { ContextAndScope contextAndScope = (ContextAndScope) context.getMqTraceContext(); contextAndScope.closeScope(); - tracer().end(contextAndScope.getContext()); + tracer.end(contextAndScope.getContext()); } } } 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 f9ec3bfcedc0..8665c3c9f36a 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 @@ -5,7 +5,6 @@ package io.opentelemetry.instrumentation.rocketmq; -import static io.opentelemetry.instrumentation.rocketmq.RocketMqProducerTracer.tracer; import static io.opentelemetry.instrumentation.rocketmq.TextMapInjectAdapter.SETTER; import io.opentelemetry.api.GlobalOpenTelemetry; @@ -13,7 +12,15 @@ import org.apache.rocketmq.client.hook.SendMessageContext; import org.apache.rocketmq.client.hook.SendMessageHook; -public class TracingSendMessageHookImpl implements SendMessageHook { +final class TracingSendMessageHookImpl implements SendMessageHook { + + private final RocketMqProducerTracer tracer; + private boolean propagationEnabled; + + TracingSendMessageHookImpl(RocketMqProducerTracer tracer, boolean propagationEnabled) { + this.tracer = tracer; + this.propagationEnabled = propagationEnabled; + } @Override public String hookName() { @@ -26,9 +33,8 @@ public void sendMessageBefore(SendMessageContext context) { return; } Context traceContext = - tracer() - .startProducerSpan(Context.current(), context.getBrokerAddr(), context.getMessage()); - if (RocketMqClientConfig.isPropagationEnabled()) { + tracer.startProducerSpan(Context.current(), context.getBrokerAddr(), context.getMessage()); + if (propagationEnabled) { GlobalOpenTelemetry.getPropagators() .getTextMapPropagator() .inject(traceContext, context.getMessage().getProperties(), SETTER); @@ -44,9 +50,9 @@ public void sendMessageAfter(SendMessageContext context) { } if (context.getMqTraceContext() instanceof ContextAndScope) { ContextAndScope contextAndScope = (ContextAndScope) context.getMqTraceContext(); - tracer().afterProduce(contextAndScope.getContext(), context.getSendResult()); + tracer.afterProduce(contextAndScope.getContext(), context.getSendResult()); contextAndScope.closeScope(); - tracer().end(contextAndScope.getContext()); + tracer.end(contextAndScope.getContext()); } } } diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 8f52fb2986c4..5ac344ff0f27 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -5,23 +5,20 @@ package io.opentelemetry.instrumentation.rocketmq -import base.BaseConf import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest import io.opentelemetry.instrumentation.test.LibraryTestTrait -import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer +import org.apache.rocketmq.client.producer.DefaultMQProducer class RocketMqClientTest extends AbstractRocketMqClientTest implements LibraryTestTrait { @Override - void configureMQProducer() { - producer=BaseConf.getProducer(BaseConf.nsAddr) - producer.getDefaultMQProducerImpl().registerSendMessageHook(new TracingSendMessageHookImpl()) + void configureMQProducer(DefaultMQProducer producer) { + producer.getDefaultMQProducerImpl().registerSendMessageHook(RocketMqTracing.create(getOpenTelemetry()).newTracingSendMessageHook()) } @Override - void configureMQPushConsumer() { - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) - consumer.setConsumeMessageBatchMaxSize(2) - consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(new TracingConsumeMessageHookImpl()) + void configureMQPushConsumer(DefaultMQPushConsumer consumer) { + consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(RocketMqTracing.create(getOpenTelemetry()).newTracingConsumeMessageHook()) } } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 08feee3d6aaa..0a067072c2d8 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -14,6 +14,7 @@ import org.apache.rocketmq.client.producer.SendCallback import org.apache.rocketmq.client.producer.SendResult import org.apache.rocketmq.common.message.Message import org.apache.rocketmq.remoting.common.RemotingHelper +import org.apache.rocketmq.test.listener.rmq.order.RMQOrderListener import spock.lang.Shared import spock.lang.Unroll import static io.opentelemetry.api.trace.SpanKind.CONSUMER @@ -25,13 +26,13 @@ import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTra abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared - DefaultMQPushConsumer consumer + DefaultMQProducer producer @Shared - DefaultMQProducer producer + DefaultMQPushConsumer consumer @Shared - def sharedTopic = BaseConf.initTopic() + def sharedTopic @Shared Message msg @@ -39,18 +40,22 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared def msgs = new ArrayList() - abstract void configureMQProducer() + abstract void configureMQProducer(DefaultMQProducer producer) + + abstract void configureMQPushConsumer(DefaultMQPushConsumer consumer) - abstract void configureMQPushConsumer() + def setupSpec() { + sharedTopic = BaseConf.initTopic() + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) + producer = BaseConf.getProducer(BaseConf.nsAddr) + configureMQProducer(producer) + } def cleanupSpec() { BaseConf.deleteTempDir() } def "test rocketmq produce callback"() { - setup: - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) - configureMQProducer() when: producer.send(msg, new SendCallback() { @Override @@ -83,9 +88,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and consume"() { setup: - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) - configureMQProducer() - configureMQPushConsumer() + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + configureMQPushConsumer(consumer) when: runUnderTrace("parent") { producer.send(msg) @@ -133,8 +137,9 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) msgs.add(msg1) msgs.add(msg2) - configureMQProducer() - configureMQPushConsumer() + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer.setConsumeMessageBatchMaxSize(2) + consumer = configureMQPushConsumer(consumer) when: runUnderTrace("parent") { producer.send(msgs) From 963e012f76d32deacb757f50dda6821cced611e1 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 9 Mar 2021 10:51:46 +0800 Subject: [PATCH 30/44] Fix --- .../rocketmq/RocketMqConsumerTracer.java | 5 ++++- .../rocketmq/RocketMqTracing.java | 18 +++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index 51c521c90938..fbb2ea8376bd 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -23,7 +23,10 @@ final class RocketMqConsumerTracer extends BaseTracer { private boolean captureExperimentalSpanAttributes; private boolean propagationEnabled; - RocketMqConsumerTracer(OpenTelemetry openTelemetry, boolean captureExperimentalSpanAttributes, boolean propagationEnabled) { + RocketMqConsumerTracer( + OpenTelemetry openTelemetry, + boolean captureExperimentalSpanAttributes, + boolean propagationEnabled) { super(openTelemetry); this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; this.propagationEnabled = propagationEnabled; diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java index 96bdd6651d89..ef3f00cfab51 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java @@ -12,13 +12,15 @@ public final class RocketMqTracing { public static RocketMqTracing create(OpenTelemetry openTelemetry) { - return newBuilder(openTelemetry).setPropagationEnabled( - Config.get() - .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true)) + return newBuilder(openTelemetry) + .setPropagationEnabled( + Config.get() + .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true)) .setCaptureExperimentalSpanAttributes( Config.get() .getBooleanProperty( - "otel.instrumentation.rocketmq-client.experimental-span-attributes", true)).build(); + "otel.instrumentation.rocketmq-client.experimental-span-attributes", true)) + .build(); } public static RocketMqTracingBuilder newBuilder(OpenTelemetry openTelemetry) { @@ -37,8 +39,11 @@ public static RocketMqTracingBuilder newBuilder(OpenTelemetry openTelemetry) { boolean propagationEnabled) { this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; this.propagationEnabled = propagationEnabled; - rocketMqConsumerTracer = new RocketMqConsumerTracer(openTelemetry, captureExperimentalSpanAttributes, propagationEnabled); - rocketMqProducerTracer = new RocketMqProducerTracer(openTelemetry, captureExperimentalSpanAttributes); + rocketMqConsumerTracer = + new RocketMqConsumerTracer( + openTelemetry, captureExperimentalSpanAttributes, propagationEnabled); + rocketMqProducerTracer = + new RocketMqProducerTracer(openTelemetry, captureExperimentalSpanAttributes); } public ConsumeMessageHook newTracingConsumeMessageHook() { @@ -48,5 +53,4 @@ public ConsumeMessageHook newTracingConsumeMessageHook() { public SendMessageHook newTracingSendMessageHook() { return new TracingSendMessageHookImpl(rocketMqProducerTracer, propagationEnabled); } - } From 00aa1fa8b69acc4c96d2b211d15bf49df0e22825 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 9 Mar 2021 13:24:55 +0800 Subject: [PATCH 31/44] Fix --- .../instrumentation/rocketmq/RocketMqTracing.java | 12 ++++++++++++ .../rocketmq/RocketMqTracingBuilder.java | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java index ef3f00cfab51..504a28ffcdf9 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java @@ -10,7 +10,10 @@ import org.apache.rocketmq.client.hook.ConsumeMessageHook; import org.apache.rocketmq.client.hook.SendMessageHook; +/** Entrypoint for tracing RocketMq producers or consumers. */ public final class RocketMqTracing { + + /** Returns a new {@link RocketMqTracing} configured with the given {@link OpenTelemetry}. */ public static RocketMqTracing create(OpenTelemetry openTelemetry) { return newBuilder(openTelemetry) .setPropagationEnabled( @@ -23,6 +26,7 @@ public static RocketMqTracing create(OpenTelemetry openTelemetry) { .build(); } + /** Returns a new {@link RocketMqTracingBuilder} configured with the given {@link OpenTelemetry}. */ public static RocketMqTracingBuilder newBuilder(OpenTelemetry openTelemetry) { return new RocketMqTracingBuilder(openTelemetry); } @@ -46,10 +50,18 @@ public static RocketMqTracingBuilder newBuilder(OpenTelemetry openTelemetry) { new RocketMqProducerTracer(openTelemetry, captureExperimentalSpanAttributes); } + /** + * Returns a new {@link ConsumeMessageHook} for use with methods like {@link + * org.apache.rocketmq.client.impl.consumer.DefaultMQPullConsumerImpl#registerConsumeMessageHook(ConsumeMessageHook)}. + */ public ConsumeMessageHook newTracingConsumeMessageHook() { return new TracingConsumeMessageHookImpl(rocketMqConsumerTracer); } + /** + * Returns a new {@link SendMessageHook} for use with methods like {@link + * org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl#registerSendMessageHook(SendMessageHook)}. + */ public SendMessageHook newTracingSendMessageHook() { return new TracingSendMessageHookImpl(rocketMqProducerTracer, propagationEnabled); } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java index b5ccd26ab17c..4581a33e8983 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java @@ -7,6 +7,7 @@ import io.opentelemetry.api.OpenTelemetry; +/** A builder of {@link RocketMqTracing}. */ public final class RocketMqTracingBuilder { private final OpenTelemetry openTelemetry; @@ -18,6 +19,11 @@ public final class RocketMqTracingBuilder { this.openTelemetry = openTelemetry; } + /** + * Sets whether experimental attributes should be set to spans. These attributes may be changed or + * removed in the future, so only enable this if you know you do not require attributes filled by + * this instrumentation to be stable across versions + */ public RocketMqTracingBuilder setCaptureExperimentalSpanAttributes( boolean captureExperimentalSpanAttributes) { this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; @@ -29,6 +35,7 @@ public RocketMqTracingBuilder setPropagationEnabled(boolean propagationEnabled) return this; } + /** Returns a new {@link RocketMqTracing} with the settings of this {@link RocketMqTracingBuilder}. */ public RocketMqTracing build() { return new RocketMqTracing( openTelemetry, captureExperimentalSpanAttributes, propagationEnabled); From 54190818a297b40441a3b49b852d2eb232a9b417 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 9 Mar 2021 13:40:46 +0800 Subject: [PATCH 32/44] Fix --- .../instrumentation/rocketmq/RocketMqTracing.java | 4 +++- .../instrumentation/rocketmq/RocketMqTracingBuilder.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java index 504a28ffcdf9..b8b8b9d3b314 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java @@ -26,7 +26,9 @@ public static RocketMqTracing create(OpenTelemetry openTelemetry) { .build(); } - /** Returns a new {@link RocketMqTracingBuilder} configured with the given {@link OpenTelemetry}. */ + /** + * Returns a new {@link RocketMqTracingBuilder} configured with the given {@link OpenTelemetry}. + */ public static RocketMqTracingBuilder newBuilder(OpenTelemetry openTelemetry) { return new RocketMqTracingBuilder(openTelemetry); } diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java index 4581a33e8983..dc8bc6f69b98 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java @@ -35,7 +35,9 @@ public RocketMqTracingBuilder setPropagationEnabled(boolean propagationEnabled) return this; } - /** Returns a new {@link RocketMqTracing} with the settings of this {@link RocketMqTracingBuilder}. */ + /** + * Returns a new {@link RocketMqTracing} with the settings of this {@link RocketMqTracingBuilder}. + */ public RocketMqTracing build() { return new RocketMqTracing( openTelemetry, captureExperimentalSpanAttributes, propagationEnabled); From 8419db8d80355571c6311dc2fa8afd59e270a8f9 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 9 Mar 2021 14:36:11 +0800 Subject: [PATCH 33/44] Fix --- .../rocketmq/RocketMqTracingBuilder.java | 4 ++++ .../rocketmq/AbstractRocketMqClientTest.groovy | 14 +++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java index dc8bc6f69b98..863fe7a76588 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java @@ -30,6 +30,10 @@ public RocketMqTracingBuilder setCaptureExperimentalSpanAttributes( return this; } + /** + * Sets whether the trace context should be written from producers / read from consumers for + * propagating through messaging. + */ public RocketMqTracingBuilder setPropagationEnabled(boolean propagationEnabled) { this.propagationEnabled = propagationEnabled; return this; diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 0a067072c2d8..e3ea16d79c1e 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -28,8 +28,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared DefaultMQProducer producer - @Shared - DefaultMQPushConsumer consumer +// @Shared +// DefaultMQPushConsumer consumer @Shared def sharedTopic @@ -51,10 +51,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { configureMQProducer(producer) } - def cleanupSpec() { - BaseConf.deleteTempDir() - } - def "test rocketmq produce callback"() { when: producer.send(msg, new SendCallback() { @@ -88,7 +84,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and consume"() { setup: - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + def consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) configureMQPushConsumer(consumer) when: runUnderTrace("parent") { @@ -137,9 +133,9 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) msgs.add(msg1) msgs.add(msg2) - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + def consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) consumer.setConsumeMessageBatchMaxSize(2) - consumer = configureMQPushConsumer(consumer) + configureMQPushConsumer(consumer) when: runUnderTrace("parent") { producer.send(msgs) From aa2c7b5659d28837c34ed4f2a9e37f0d47463189 Mon Sep 17 00:00:00 2001 From: addname Date: Tue, 9 Mar 2021 14:50:37 +0800 Subject: [PATCH 34/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index e3ea16d79c1e..606e712a7601 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -28,8 +28,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared DefaultMQProducer producer -// @Shared -// DefaultMQPushConsumer consumer + @Shared + DefaultMQPushConsumer consumer @Shared def sharedTopic @@ -84,7 +84,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and consume"() { setup: - def consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) configureMQPushConsumer(consumer) when: runUnderTrace("parent") { @@ -124,6 +124,9 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } + cleanup:{ + consumer.shutdown() + } } } @@ -133,7 +136,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) msgs.add(msg1) msgs.add(msg2) - def consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) consumer.setConsumeMessageBatchMaxSize(2) configureMQPushConsumer(consumer) when: From 8c606f69b44ec1da45e2a1facc3198dcf045e464 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Tue, 9 Mar 2021 15:53:06 +0900 Subject: [PATCH 35/44] Update settings.gradle Co-authored-by: Mateusz Rzeszutek --- settings.gradle | 2 -- 1 file changed, 2 deletions(-) diff --git a/settings.gradle b/settings.gradle index 0c73d3895622..2474ec60f8c4 100644 --- a/settings.gradle +++ b/settings.gradle @@ -273,5 +273,3 @@ def setBuildFile(project) { rootProject.children.each { setBuildFile(it) } - - From 11aa52037d7cfb1f4247614994cf1efe55bcf073 Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 11:05:27 +0800 Subject: [PATCH 36/44] Fix --- .../library/rocketmq-client-4.8-library.gradle | 4 ---- .../instrumentation/rocketmq/ContextAndScope.java | 2 +- .../rocketmq/RocketMqConsumerTracer.java | 4 ++-- .../instrumentation/rocketmq/RocketMqTracing.java | 11 +---------- .../rocketmq/RocketMqTracingBuilder.java | 2 +- .../rocketmq/RocketMqClientTest.groovy | 15 +++++++++++++-- .../testing/src/main/java/base/BaseConf.java | 2 +- 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle index f1dd2887b63c..e9ab4deda882 100644 --- a/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle +++ b/instrumentation/rocketmq-client-4.8/library/rocketmq-client-4.8-library.gradle @@ -4,7 +4,3 @@ dependencies { library group: 'org.apache.rocketmq', name: 'rocketmq-client', version: '4.8.0' testImplementation project(':instrumentation:rocketmq-client-4.8:testing') } - -tasks.withType(Test) { - jvmArgs "-Dotel.instrumentation.rocketmq-client.experimental-span-attributes=true" -} \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java index d4bea5246e30..aa67e1f4cde1 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/ContextAndScope.java @@ -8,7 +8,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; -public final class ContextAndScope { +final class ContextAndScope { private final Context context; private final Scope scope; diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java index fbb2ea8376bd..cad16785156d 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqConsumerTracer.java @@ -46,7 +46,7 @@ Context startSpan(Context parentContext, List msgs) { spanBuilder(parentContext, "multiple_sources receive", CONSUMER) .setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "rocketmq") .setAttribute(SemanticAttributes.MESSAGING_OPERATION, "receive"); - Context rootContext = withClientSpan(parentContext, spanBuilder.startSpan()); + Context rootContext = parentContext.with(spanBuilder.startSpan()); for (MessageExt message : msgs) { createChildSpan(rootContext, message); } @@ -58,7 +58,7 @@ private void createChildSpan(Context parentContext, MessageExt msg) { SpanBuilder childSpanBuilder = startSpanBuilder(parentContext, msg) .addLink(Span.fromContext(extractParent(msg)).getSpanContext()); - end(withClientSpan(parentContext, childSpanBuilder.startSpan())); + end(parentContext.with(childSpanBuilder.startSpan())); } private SpanBuilder startSpanBuilder(Context parentContext, MessageExt msg) { diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java index b8b8b9d3b314..c29f7a20132e 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracing.java @@ -6,7 +6,6 @@ package io.opentelemetry.instrumentation.rocketmq; import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.instrumentation.api.config.Config; import org.apache.rocketmq.client.hook.ConsumeMessageHook; import org.apache.rocketmq.client.hook.SendMessageHook; @@ -15,15 +14,7 @@ public final class RocketMqTracing { /** Returns a new {@link RocketMqTracing} configured with the given {@link OpenTelemetry}. */ public static RocketMqTracing create(OpenTelemetry openTelemetry) { - return newBuilder(openTelemetry) - .setPropagationEnabled( - Config.get() - .getBooleanProperty("otel.instrumentation.rocketmq-client.propagation", true)) - .setCaptureExperimentalSpanAttributes( - Config.get() - .getBooleanProperty( - "otel.instrumentation.rocketmq-client.experimental-span-attributes", true)) - .build(); + return newBuilder(openTelemetry).build(); } /** diff --git a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java index 863fe7a76588..faaa2407d4b8 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java +++ b/instrumentation/rocketmq-client-4.8/library/src/main/java/io/opentelemetry/instrumentation/rocketmq/RocketMqTracingBuilder.java @@ -13,7 +13,7 @@ public final class RocketMqTracingBuilder { private final OpenTelemetry openTelemetry; private boolean captureExperimentalSpanAttributes; - private boolean propagationEnabled; + private boolean propagationEnabled = true; RocketMqTracingBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; diff --git a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy index 5ac344ff0f27..994c865fc161 100644 --- a/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/library/src/test/groovy/io/opentelemetry/instrumentation/rocketmq/RocketMqClientTest.groovy @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.rocketmq import io.opentelemetery.instrumentation.rocketmq.AbstractRocketMqClientTest +import io.opentelemetry.instrumentation.api.config.Config import io.opentelemetry.instrumentation.test.LibraryTestTrait import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer import org.apache.rocketmq.client.producer.DefaultMQProducer @@ -14,11 +15,21 @@ class RocketMqClientTest extends AbstractRocketMqClientTest implements LibraryTe @Override void configureMQProducer(DefaultMQProducer producer) { - producer.getDefaultMQProducerImpl().registerSendMessageHook(RocketMqTracing.create(getOpenTelemetry()).newTracingSendMessageHook()) + producer.getDefaultMQProducerImpl().registerSendMessageHook(RocketMqTracing.newBuilder(openTelemetry) + .setCaptureExperimentalSpanAttributes( + Config.get() + .getBooleanProperty( + "otel.instrumentation.rocketmq-client.experimental-span-attributes", true)) + .build().newTracingSendMessageHook()) } @Override void configureMQPushConsumer(DefaultMQPushConsumer consumer) { - consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(RocketMqTracing.create(getOpenTelemetry()).newTracingConsumeMessageHook()) + consumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(RocketMqTracing.newBuilder(openTelemetry) + .setCaptureExperimentalSpanAttributes( + Config.get() + .getBooleanProperty( + "otel.instrumentation.rocketmq-client.experimental-span-attributes", true)) + .build().newTracingConsumeMessageHook()) } } \ No newline at end of file diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index 9b95296fca98..cdf95f0c3f2d 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -17,7 +17,7 @@ import org.apache.rocketmq.test.util.MQRandomUtils; import org.apache.rocketmq.test.util.RandomUtil; -final class BaseConf { +public final class BaseConf { public static final String nsAddr; public static final String broker1Addr; protected static String broker1Name; From 6ad79ebe806d570c64ae94e15d69634c48e226d9 Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 12:02:55 +0800 Subject: [PATCH 37/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 606e712a7601..ec6f8ef3e1b0 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -44,7 +44,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { abstract void configureMQPushConsumer(DefaultMQPushConsumer consumer) - def setupSpec() { + def setup() { sharedTopic = BaseConf.initTopic() msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) producer = BaseConf.getProducer(BaseConf.nsAddr) @@ -79,6 +79,9 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } + cleanup:{ + producer.shutdown() + } } } @@ -125,6 +128,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } cleanup:{ + producer.shutdown() consumer.shutdown() } } From 24de0ca267410f22217004282167aef695eeb51f Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 13:02:09 +0800 Subject: [PATCH 38/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 7 +------ .../testing/src/main/java/base/BaseConf.java | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index ec6f8ef3e1b0..cdc30d99a24b 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -32,7 +32,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { DefaultMQPushConsumer consumer @Shared - def sharedTopic + def sharedTopic = BaseConf.initTopic() @Shared Message msg @@ -45,7 +45,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { abstract void configureMQPushConsumer(DefaultMQPushConsumer consumer) def setup() { - sharedTopic = BaseConf.initTopic() msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) producer = BaseConf.getProducer(BaseConf.nsAddr) configureMQProducer(producer) @@ -79,9 +78,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } - cleanup:{ - producer.shutdown() - } } } @@ -128,7 +124,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } cleanup:{ - producer.shutdown() consumer.shutdown() } } diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java index cdf95f0c3f2d..c84669a19eb2 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/java/base/BaseConf.java @@ -29,11 +29,11 @@ public final class BaseConf { System.setProperty( RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION)); namesrvController = IntegrationTestBase.createAndStartNamesrv(); - nsAddr = "127.0.0.1:" + namesrvController.getNettyServerConfig().getListenPort(); + nsAddr = "localhost:" + namesrvController.getNettyServerConfig().getListenPort(); brokerController1 = IntegrationTestBase.createAndStartBroker(nsAddr); clusterName = brokerController1.getBrokerConfig().getBrokerClusterName(); broker1Name = brokerController1.getBrokerConfig().getBrokerName(); - broker1Addr = "127.0.0.1:" + brokerController1.getNettyServerConfig().getListenPort(); + broker1Addr = "localhost:" + brokerController1.getNettyServerConfig().getListenPort(); } private BaseConf() {} From b4eb7c359fd711e9428d9b40c450f50ab34012df Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 13:24:20 +0800 Subject: [PATCH 39/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index cdc30d99a24b..b035a15b5e31 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -28,9 +28,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared DefaultMQProducer producer - @Shared - DefaultMQPushConsumer consumer - @Shared def sharedTopic = BaseConf.initTopic() @@ -83,7 +80,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and consume"() { setup: - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + def consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) configureMQPushConsumer(consumer) when: runUnderTrace("parent") { @@ -123,9 +120,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } - cleanup:{ - consumer.shutdown() - } + cleanup: + consumer.shutdown() } } @@ -135,9 +131,9 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) msgs.add(msg1) msgs.add(msg2) - consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) - consumer.setConsumeMessageBatchMaxSize(2) - configureMQPushConsumer(consumer) + def batchConsumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + batchConsumer.setConsumeMessageBatchMaxSize(2) + configureMQPushConsumer(batchConsumer) when: runUnderTrace("parent") { producer.send(msgs) From 58801edee6d72de3e7b8fe2059abcebb4e4b088e Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 13:47:26 +0800 Subject: [PATCH 40/44] Fix --- .../instrumentation/rocketmq/AbstractRocketMqClientTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index b035a15b5e31..bbbb37a818a9 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -41,7 +41,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { abstract void configureMQPushConsumer(DefaultMQPushConsumer consumer) - def setup() { + def setupSpec() { msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) producer = BaseConf.getProducer(BaseConf.nsAddr) configureMQProducer(producer) From 398246b170a776e5a58fd9b0ee54c9b3354cbfef Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 14:11:02 +0800 Subject: [PATCH 41/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index bbbb37a818a9..7f297b5b1338 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -28,6 +28,12 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { @Shared DefaultMQProducer producer + @Shared + DefaultMQPushConsumer consumer + + @Shared + DefaultMQPushConsumer batchConsumer + @Shared def sharedTopic = BaseConf.initTopic() @@ -80,7 +86,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and consume"() { setup: - def consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) configureMQPushConsumer(consumer) when: runUnderTrace("parent") { @@ -120,8 +126,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } - cleanup: - consumer.shutdown() } } @@ -131,7 +135,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) msgs.add(msg1) msgs.add(msg2) - def batchConsumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) + batchConsumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) batchConsumer.setConsumeMessageBatchMaxSize(2) configureMQPushConsumer(batchConsumer) when: From 5b45a6a61377911caffd464036d6a9c412732b55 Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 14:33:07 +0800 Subject: [PATCH 42/44] Fix --- .../rocketmq/AbstractRocketMqClientTest.groovy | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 7f297b5b1338..e260c597e659 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -35,7 +35,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { DefaultMQPushConsumer batchConsumer @Shared - def sharedTopic = BaseConf.initTopic() + def sharedTopic @Shared Message msg @@ -48,12 +48,14 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { abstract void configureMQPushConsumer(DefaultMQPushConsumer consumer) def setupSpec() { - msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) producer = BaseConf.getProducer(BaseConf.nsAddr) configureMQProducer(producer) } def "test rocketmq produce callback"() { + setup: + sharedTopic = BaseConf.initTopic() + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) when: producer.send(msg, new SendCallback() { @Override @@ -86,6 +88,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and consume"() { setup: + sharedTopic = BaseConf.initTopic() + msg = new Message(sharedTopic, "TagA", ("Hello RocketMQ").getBytes(RemotingHelper.DEFAULT_CHARSET)) consumer = BaseConf.getConsumer(BaseConf.nsAddr, sharedTopic, "*", new RMQOrderListener()) configureMQPushConsumer(consumer) when: @@ -131,6 +135,7 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { def "test rocketmq produce and batch consume"() { setup: + sharedTopic = BaseConf.initTopic() Message msg1 = new Message(sharedTopic, "TagA", ("hello world a").getBytes()) Message msg2 = new Message(sharedTopic, "TagB", ("hello world b").getBytes()) msgs.add(msg1) From f873db791b201ab8f07617ab70b90482cf83a0da Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 15:09:47 +0800 Subject: [PATCH 43/44] Fix --- .../instrumentation/rocketmq/AbstractRocketMqClientTest.groovy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index e260c597e659..8180b0111f29 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -130,6 +130,8 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } + cleanup: + consumer.shutdown() } } From 28f30fcb3425c2cd537715d0d32ecf0c2265b6fe Mon Sep 17 00:00:00 2001 From: addname Date: Wed, 10 Mar 2021 15:42:06 +0800 Subject: [PATCH 44/44] Fix --- .../instrumentation/rocketmq/AbstractRocketMqClientTest.groovy | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy index 8180b0111f29..e260c597e659 100644 --- a/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy +++ b/instrumentation/rocketmq-client-4.8/testing/src/main/groovy/io/opentelemetery/instrumentation/rocketmq/AbstractRocketMqClientTest.groovy @@ -130,8 +130,6 @@ abstract class AbstractRocketMqClientTest extends InstrumentationSpecification { } } } - cleanup: - consumer.shutdown() } }