jeanouii commented on code in PR #1769: URL: https://github.com/apache/activemq/pull/1769#discussion_r3063162431
########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPlugin.java: ########## @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.util.concurrent.ConcurrentHashMap; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConsumerBrokerExchange; +import org.apache.activemq.broker.ProducerBrokerExchange; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.activemq.command.MessageDispatch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A broker plugin that provides OpenTelemetry distributed tracing support. + * Traces message send, dispatch, and acknowledge operations with proper + * context propagation via W3C TraceContext. + * + * @org.apache.xbean.XBean element="openTelemetryPlugin" + */ +public class OpenTelemetryBrokerPlugin extends BrokerPluginSupport { + + private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryBrokerPlugin.class); + private static final String INSTRUMENTATION_NAME = "org.apache.activemq"; + + private boolean traceProducer = true; + private boolean traceConsumer = true; + private boolean traceAcknowledge = true; + + private final ConcurrentHashMap<MessageDispatch, Span> dispatchSpans = new ConcurrentHashMap<>(); + + public boolean isTraceProducer() { + return traceProducer; + } + + public void setTraceProducer(boolean traceProducer) { + this.traceProducer = traceProducer; + } + + public boolean isTraceConsumer() { + return traceConsumer; + } + + public void setTraceConsumer(boolean traceConsumer) { + this.traceConsumer = traceConsumer; + } + + public boolean isTraceAcknowledge() { + return traceAcknowledge; + } + + public void setTraceAcknowledge(boolean traceAcknowledge) { + this.traceAcknowledge = traceAcknowledge; + } + + @Override + public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { + if (!traceProducer) { + super.send(producerExchange, messageSend); + return; + } + + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + // Extract any existing context from the message (e.g., set by client) + Context parentContext = propagator.extract(Context.current(), messageSend, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = messageSend.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " publish") + .setSpanKind(SpanKind.PRODUCER) + .setParent(parentContext) + .setAttribute("messaging.system", "activemq") Review Comment: "messaging.system" --> "messaging.system.name"? ########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPlugin.java: ########## @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.util.concurrent.ConcurrentHashMap; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConsumerBrokerExchange; +import org.apache.activemq.broker.ProducerBrokerExchange; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.activemq.command.MessageDispatch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A broker plugin that provides OpenTelemetry distributed tracing support. + * Traces message send, dispatch, and acknowledge operations with proper + * context propagation via W3C TraceContext. + * + * @org.apache.xbean.XBean element="openTelemetryPlugin" + */ +public class OpenTelemetryBrokerPlugin extends BrokerPluginSupport { + + private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryBrokerPlugin.class); + private static final String INSTRUMENTATION_NAME = "org.apache.activemq"; + + private boolean traceProducer = true; + private boolean traceConsumer = true; + private boolean traceAcknowledge = true; + + private final ConcurrentHashMap<MessageDispatch, Span> dispatchSpans = new ConcurrentHashMap<>(); + + public boolean isTraceProducer() { + return traceProducer; + } + + public void setTraceProducer(boolean traceProducer) { + this.traceProducer = traceProducer; + } + + public boolean isTraceConsumer() { + return traceConsumer; + } + + public void setTraceConsumer(boolean traceConsumer) { + this.traceConsumer = traceConsumer; + } + + public boolean isTraceAcknowledge() { + return traceAcknowledge; + } + + public void setTraceAcknowledge(boolean traceAcknowledge) { + this.traceAcknowledge = traceAcknowledge; + } + + @Override + public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { + if (!traceProducer) { + super.send(producerExchange, messageSend); + return; + } + + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + // Extract any existing context from the message (e.g., set by client) + Context parentContext = propagator.extract(Context.current(), messageSend, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = messageSend.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " publish") + .setSpanKind(SpanKind.PRODUCER) + .setParent(parentContext) + .setAttribute("messaging.system", "activemq") + .setAttribute("messaging.destination.name", destinationName) + .setAttribute("messaging.operation", "publish"); + + if (messageSend.getMessageId() != null) { + spanBuilder.setAttribute("messaging.message.id", messageSend.getMessageId().toString()); + } + if (producerExchange.getConnectionContext() != null + && producerExchange.getConnectionContext().getClientId() != null) { + spanBuilder.setAttribute("messaging.client_id", producerExchange.getConnectionContext().getClientId()); + } + + Span span = spanBuilder.startSpan(); + try { + // Inject trace context into the message for downstream propagation + Context contextWithSpan = parentContext.with(span); + propagator.inject(contextWithSpan, messageSend, ActiveMQMessageTextMapSetter.INSTANCE); + + super.send(producerExchange, messageSend); + } catch (Exception e) { + span.setStatus(StatusCode.ERROR, e.getMessage()); + span.recordException(e); + throw e; + } finally { + span.end(); + } + } + + @Override + public void preProcessDispatch(MessageDispatch messageDispatch) { + if (traceConsumer && messageDispatch != null && messageDispatch.getMessage() != null) { + try { + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + Message message = messageDispatch.getMessage(); + Context extractedContext = propagator.extract(Context.current(), message, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = message.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " deliver") + .setSpanKind(SpanKind.CONSUMER) + .setParent(extractedContext) + .setAttribute("messaging.system", "activemq") + .setAttribute("messaging.destination.name", destinationName) + .setAttribute("messaging.operation", "deliver"); + + if (message.getMessageId() != null) { + spanBuilder.setAttribute("messaging.message.id", message.getMessageId().toString()); + } + + Span span = spanBuilder.startSpan(); + dispatchSpans.put(messageDispatch, span); + } catch (Exception e) { + LOG.warn("Failed to create deliver span", e); + } + } + super.preProcessDispatch(messageDispatch); + } + + @Override + public void postProcessDispatch(MessageDispatch messageDispatch) { + if (messageDispatch != null) { + Span span = dispatchSpans.remove(messageDispatch); Review Comment: Because by nature, this can't be done in a try/finally block, some spans may leak in the map forever. The map needs to be either bound or we need to implement a TTL to remove old entries in my opinion. ########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPlugin.java: ########## @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.util.concurrent.ConcurrentHashMap; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConsumerBrokerExchange; +import org.apache.activemq.broker.ProducerBrokerExchange; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.activemq.command.MessageDispatch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A broker plugin that provides OpenTelemetry distributed tracing support. + * Traces message send, dispatch, and acknowledge operations with proper + * context propagation via W3C TraceContext. + * + * @org.apache.xbean.XBean element="openTelemetryPlugin" + */ +public class OpenTelemetryBrokerPlugin extends BrokerPluginSupport { + + private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryBrokerPlugin.class); + private static final String INSTRUMENTATION_NAME = "org.apache.activemq"; + + private boolean traceProducer = true; + private boolean traceConsumer = true; + private boolean traceAcknowledge = true; + + private final ConcurrentHashMap<MessageDispatch, Span> dispatchSpans = new ConcurrentHashMap<>(); + + public boolean isTraceProducer() { + return traceProducer; + } + + public void setTraceProducer(boolean traceProducer) { + this.traceProducer = traceProducer; + } + + public boolean isTraceConsumer() { + return traceConsumer; + } + + public void setTraceConsumer(boolean traceConsumer) { + this.traceConsumer = traceConsumer; + } + + public boolean isTraceAcknowledge() { + return traceAcknowledge; + } + + public void setTraceAcknowledge(boolean traceAcknowledge) { + this.traceAcknowledge = traceAcknowledge; + } + + @Override + public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { + if (!traceProducer) { + super.send(producerExchange, messageSend); + return; + } + + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + // Extract any existing context from the message (e.g., set by client) + Context parentContext = propagator.extract(Context.current(), messageSend, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = messageSend.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " publish") + .setSpanKind(SpanKind.PRODUCER) + .setParent(parentContext) + .setAttribute("messaging.system", "activemq") + .setAttribute("messaging.destination.name", destinationName) + .setAttribute("messaging.operation", "publish"); Review Comment: "messaging.operation" --> "messaging.operation.type"? ########## activemq-opentelemetry/src/test/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPluginTest.java: ########## @@ -0,0 +1,244 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import jakarta.jms.Connection; +import jakarta.jms.DeliveryMode; +import jakarta.jms.MessageConsumer; +import jakarta.jms.MessageProducer; +import jakarta.jms.Session; +import jakarta.jms.TextMessage; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; +import io.opentelemetry.sdk.trace.SdkTracerProvider; +import io.opentelemetry.sdk.trace.data.SpanData; +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerPlugin; +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.broker.TransportConnector; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class OpenTelemetryBrokerPluginTest { + + private BrokerService broker; + private TransportConnector connector; + private InMemorySpanExporter spanExporter; + private Connection connection; + private Session session; + private final String queueName = "TEST.OTEL"; + + @Before + public void setUp() throws Exception { + GlobalOpenTelemetry.resetForTest(); + + spanExporter = InMemorySpanExporter.create(); + SdkTracerProvider tracerProvider = SdkTracerProvider.builder() + .addSpanProcessor(SimpleSpanProcessor.create(spanExporter)) + .build(); + + OpenTelemetrySdk.builder() + .setTracerProvider(tracerProvider) + .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) + .buildAndRegisterGlobal(); + + OpenTelemetryBrokerPlugin plugin = new OpenTelemetryBrokerPlugin(); + + broker = new BrokerService(); + broker.setBrokerName("otelTestBroker"); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.setPlugins(new BrokerPlugin[]{plugin}); + connector = broker.addConnector("tcp://localhost:0"); + broker.start(); + broker.waitUntilStarted(); + + connection = new ActiveMQConnectionFactory(connector.getConnectUri()).createConnection(); + connection.start(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + } + + @After + public void tearDown() throws Exception { + if (session != null) session.close(); + if (connection != null) connection.close(); + if (broker != null) { + broker.stop(); + broker.waitUntilStopped(); + } + GlobalOpenTelemetry.resetForTest(); + } + + @Test + public void testPublishSpanCreatedOnSend() throws Exception { + MessageProducer producer = session.createProducer(session.createQueue(queueName)); + producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + TextMessage message = session.createTextMessage("test"); + producer.send(message); + producer.close(); + + // Allow async broker processing to complete + Thread.sleep(500); Review Comment: Can you use the WaitFor pattern instead? ########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPlugin.java: ########## @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.util.concurrent.ConcurrentHashMap; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConsumerBrokerExchange; +import org.apache.activemq.broker.ProducerBrokerExchange; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.activemq.command.MessageDispatch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A broker plugin that provides OpenTelemetry distributed tracing support. + * Traces message send, dispatch, and acknowledge operations with proper + * context propagation via W3C TraceContext. + * + * @org.apache.xbean.XBean element="openTelemetryPlugin" + */ +public class OpenTelemetryBrokerPlugin extends BrokerPluginSupport { + + private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryBrokerPlugin.class); + private static final String INSTRUMENTATION_NAME = "org.apache.activemq"; + + private boolean traceProducer = true; + private boolean traceConsumer = true; + private boolean traceAcknowledge = true; + + private final ConcurrentHashMap<MessageDispatch, Span> dispatchSpans = new ConcurrentHashMap<>(); + + public boolean isTraceProducer() { + return traceProducer; + } + + public void setTraceProducer(boolean traceProducer) { + this.traceProducer = traceProducer; + } + + public boolean isTraceConsumer() { + return traceConsumer; + } + + public void setTraceConsumer(boolean traceConsumer) { + this.traceConsumer = traceConsumer; + } + + public boolean isTraceAcknowledge() { + return traceAcknowledge; + } + + public void setTraceAcknowledge(boolean traceAcknowledge) { + this.traceAcknowledge = traceAcknowledge; + } + + @Override + public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { + if (!traceProducer) { + super.send(producerExchange, messageSend); + return; + } + + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); Review Comment: Shouldn't these 2 lines be done in the constructor only and not for each invocation? ########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/ActiveMQMessageTextMapSetter.java: ########## @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.io.IOException; + +import io.opentelemetry.context.propagation.TextMapSetter; +import org.apache.activemq.command.Message; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ActiveMQMessageTextMapSetter implements TextMapSetter<Message> { + + private static final Logger LOG = LoggerFactory.getLogger(ActiveMQMessageTextMapSetter.class); + + public static final ActiveMQMessageTextMapSetter INSTANCE = new ActiveMQMessageTextMapSetter(); + + @Override + public void set(Message message, String key, String value) { + try { + message.setProperty(key, value); + message.setMarshalledProperties(null); Review Comment: This is to force the properties to be injected, correct? what's the performance impact? ########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPlugin.java: ########## @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.util.concurrent.ConcurrentHashMap; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConsumerBrokerExchange; +import org.apache.activemq.broker.ProducerBrokerExchange; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.activemq.command.MessageDispatch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A broker plugin that provides OpenTelemetry distributed tracing support. + * Traces message send, dispatch, and acknowledge operations with proper + * context propagation via W3C TraceContext. + * + * @org.apache.xbean.XBean element="openTelemetryPlugin" + */ +public class OpenTelemetryBrokerPlugin extends BrokerPluginSupport { + + private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryBrokerPlugin.class); + private static final String INSTRUMENTATION_NAME = "org.apache.activemq"; + + private boolean traceProducer = true; + private boolean traceConsumer = true; + private boolean traceAcknowledge = true; + + private final ConcurrentHashMap<MessageDispatch, Span> dispatchSpans = new ConcurrentHashMap<>(); + + public boolean isTraceProducer() { + return traceProducer; + } + + public void setTraceProducer(boolean traceProducer) { + this.traceProducer = traceProducer; + } + + public boolean isTraceConsumer() { + return traceConsumer; + } + + public void setTraceConsumer(boolean traceConsumer) { + this.traceConsumer = traceConsumer; + } + + public boolean isTraceAcknowledge() { + return traceAcknowledge; + } + + public void setTraceAcknowledge(boolean traceAcknowledge) { + this.traceAcknowledge = traceAcknowledge; + } + + @Override + public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { + if (!traceProducer) { + super.send(producerExchange, messageSend); + return; + } + + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + // Extract any existing context from the message (e.g., set by client) + Context parentContext = propagator.extract(Context.current(), messageSend, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = messageSend.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " publish") + .setSpanKind(SpanKind.PRODUCER) + .setParent(parentContext) + .setAttribute("messaging.system", "activemq") + .setAttribute("messaging.destination.name", destinationName) + .setAttribute("messaging.operation", "publish"); + + if (messageSend.getMessageId() != null) { + spanBuilder.setAttribute("messaging.message.id", messageSend.getMessageId().toString()); + } + if (producerExchange.getConnectionContext() != null + && producerExchange.getConnectionContext().getClientId() != null) { + spanBuilder.setAttribute("messaging.client_id", producerExchange.getConnectionContext().getClientId()); + } + + Span span = spanBuilder.startSpan(); + try { + // Inject trace context into the message for downstream propagation + Context contextWithSpan = parentContext.with(span); + propagator.inject(contextWithSpan, messageSend, ActiveMQMessageTextMapSetter.INSTANCE); + + super.send(producerExchange, messageSend); + } catch (Exception e) { + span.setStatus(StatusCode.ERROR, e.getMessage()); + span.recordException(e); + throw e; + } finally { + span.end(); + } + } + + @Override + public void preProcessDispatch(MessageDispatch messageDispatch) { + if (traceConsumer && messageDispatch != null && messageDispatch.getMessage() != null) { + try { + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + Message message = messageDispatch.getMessage(); + Context extractedContext = propagator.extract(Context.current(), message, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = message.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " deliver") Review Comment: How is this going to show up for topics? Many spans for the same message? ########## activemq-opentelemetry/src/main/java/org/apache/activemq/broker/util/opentelemetry/OpenTelemetryBrokerPlugin.java: ########## @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util.opentelemetry; + +import java.util.concurrent.ConcurrentHashMap; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapPropagator; +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConsumerBrokerExchange; +import org.apache.activemq.broker.ProducerBrokerExchange; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.activemq.command.MessageDispatch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A broker plugin that provides OpenTelemetry distributed tracing support. + * Traces message send, dispatch, and acknowledge operations with proper + * context propagation via W3C TraceContext. + * + * @org.apache.xbean.XBean element="openTelemetryPlugin" + */ +public class OpenTelemetryBrokerPlugin extends BrokerPluginSupport { + + private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryBrokerPlugin.class); + private static final String INSTRUMENTATION_NAME = "org.apache.activemq"; + + private boolean traceProducer = true; + private boolean traceConsumer = true; + private boolean traceAcknowledge = true; + + private final ConcurrentHashMap<MessageDispatch, Span> dispatchSpans = new ConcurrentHashMap<>(); + + public boolean isTraceProducer() { + return traceProducer; + } + + public void setTraceProducer(boolean traceProducer) { + this.traceProducer = traceProducer; + } + + public boolean isTraceConsumer() { + return traceConsumer; + } + + public void setTraceConsumer(boolean traceConsumer) { + this.traceConsumer = traceConsumer; + } + + public boolean isTraceAcknowledge() { + return traceAcknowledge; + } + + public void setTraceAcknowledge(boolean traceAcknowledge) { + this.traceAcknowledge = traceAcknowledge; + } + + @Override + public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { + if (!traceProducer) { + super.send(producerExchange, messageSend); + return; + } + + TextMapPropagator propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); + Tracer tracer = GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME); + + // Extract any existing context from the message (e.g., set by client) + Context parentContext = propagator.extract(Context.current(), messageSend, ActiveMQMessageTextMapGetter.INSTANCE); + + String destinationName = messageSend.getDestination().getPhysicalName(); + SpanBuilder spanBuilder = tracer.spanBuilder(destinationName + " publish") + .setSpanKind(SpanKind.PRODUCER) + .setParent(parentContext) + .setAttribute("messaging.system", "activemq") + .setAttribute("messaging.destination.name", destinationName) + .setAttribute("messaging.operation", "publish"); + + if (messageSend.getMessageId() != null) { + spanBuilder.setAttribute("messaging.message.id", messageSend.getMessageId().toString()); + } + if (producerExchange.getConnectionContext() != null + && producerExchange.getConnectionContext().getClientId() != null) { + spanBuilder.setAttribute("messaging.client_id", producerExchange.getConnectionContext().getClientId()); Review Comment: We use `client_id` a lot, but naming conventions require `.` (dot) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information, visit: https://activemq.apache.org/contact
