franz1981 commented on a change in pull request #3895: URL: https://github.com/apache/activemq-artemis/pull/3895#discussion_r789479101
########## File path: examples/features/standard/broker-plugin/src/main/java/org/apache/activemq/artemis/jms/example/OpenTracingBrokerPlugin.java ########## @@ -0,0 +1,90 @@ +/* + * 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.artemis.jms.example; + +import java.io.InputStream; +import java.util.Properties; + +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.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import org.apache.activemq.artemis.api.core.ActiveMQException; +import org.apache.activemq.artemis.api.core.Message; +import org.apache.activemq.artemis.core.postoffice.RoutingStatus; +import org.apache.activemq.artemis.core.server.ServerSession; +import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin; +import org.apache.activemq.artemis.core.transaction.Transaction; + +public class OpenTracingBrokerPlugin implements ActiveMQServerPlugin { + private ThreadLocal threadLocal = new ThreadLocal(); + private static final String OPERATION_NAME = "/TracingPlugin/rootInvoke/"; + private static OpenTelemetrySdk sdk = initOpenTracing(); + + public static OpenTelemetrySdk initOpenTracing() { + try { + InputStream input = OpenTracingBrokerPlugin.class.getClassLoader().getResourceAsStream("tracing.properties"); + if (input == null) { + throw new NullPointerException("Unable to find tracing.properties file"); + } + Properties prop = new Properties(System.getProperties()); + prop.load(input); + System.setProperties(prop); + + sdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + + } catch (Throwable t) { + t.printStackTrace(); + } + return sdk; + } + + @Override + public void beforeSend(ServerSession session, + Transaction tx, + Message message, + boolean direct, + boolean noAutoCreateQueue) throws ActiveMQException { + SpanBuilder spanBuilder = GlobalOpenTelemetry.getTracer(OpenTracingBrokerPlugin.class.getName()) Review comment: ```suggestion SpanBuilder spanBuilder = TRACER ``` `GlobalOpenTelemetry.getTracer(OpenTracingBrokerPlugin.class.getName())` can be declared in the plugin as ```java private static final Tracer TRACER = GlobalOpenTelemetry.getTracer(OpenTracingBrokerPlugin.class.getName()); ``` ########## File path: examples/features/standard/broker-plugin/src/main/java/org/apache/activemq/artemis/jms/example/OpenTracingBrokerPlugin.java ########## @@ -0,0 +1,90 @@ +/* + * 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.artemis.jms.example; + +import java.io.InputStream; +import java.util.Properties; + +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.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import org.apache.activemq.artemis.api.core.ActiveMQException; +import org.apache.activemq.artemis.api.core.Message; +import org.apache.activemq.artemis.core.postoffice.RoutingStatus; +import org.apache.activemq.artemis.core.server.ServerSession; +import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin; +import org.apache.activemq.artemis.core.transaction.Transaction; + +public class OpenTracingBrokerPlugin implements ActiveMQServerPlugin { + private ThreadLocal threadLocal = new ThreadLocal(); + private static final String OPERATION_NAME = "/TracingPlugin/rootInvoke/"; + private static OpenTelemetrySdk sdk = initOpenTracing(); + + public static OpenTelemetrySdk initOpenTracing() { + try { + InputStream input = OpenTracingBrokerPlugin.class.getClassLoader().getResourceAsStream("tracing.properties"); + if (input == null) { + throw new NullPointerException("Unable to find tracing.properties file"); + } + Properties prop = new Properties(System.getProperties()); + prop.load(input); + System.setProperties(prop); + + sdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + + } catch (Throwable t) { + t.printStackTrace(); + } + return sdk; + } + + @Override + public void beforeSend(ServerSession session, + Transaction tx, + Message message, + boolean direct, + boolean noAutoCreateQueue) throws ActiveMQException { + SpanBuilder spanBuilder = GlobalOpenTelemetry.getTracer(OpenTracingBrokerPlugin.class.getName()) + .spanBuilder(OPERATION_NAME) + .setAttribute("message", message.toString()) + .setSpanKind(SpanKind.SERVER); + Span span = spanBuilder.startSpan(); + threadLocal.set(span); + + /** + span = tracer.spanBuilder(String.format("message: %s", message.getStringProperty("count"))).setSpanKind(SpanKind.PRODUCER).startSpan(); + span.addEvent(String.format("message: %s", message.getStringProperty("count"))); + **/ + } + + @Override + public void afterSend(Transaction tx, Review comment: I suggest to override `public void onSendException` as well or we risk to leak `Span` in case of exceptions ########## File path: examples/features/standard/broker-plugin/src/main/java/org/apache/activemq/artemis/jms/example/OpenTracingBrokerPlugin.java ########## @@ -0,0 +1,90 @@ +/* + * 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.artemis.jms.example; + +import java.io.InputStream; +import java.util.Properties; + +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.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import org.apache.activemq.artemis.api.core.ActiveMQException; +import org.apache.activemq.artemis.api.core.Message; +import org.apache.activemq.artemis.core.postoffice.RoutingStatus; +import org.apache.activemq.artemis.core.server.ServerSession; +import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin; +import org.apache.activemq.artemis.core.transaction.Transaction; + +public class OpenTracingBrokerPlugin implements ActiveMQServerPlugin { + private ThreadLocal threadLocal = new ThreadLocal(); Review comment: I think we have several options here: - `ThreadLocal` must be declared as `private static final ThreadLocal` to benefit from important optimizations by the JVM - `ThreadLocal::remove` can be quite expensive and subsequent `ThreadLocal::set` would create new `ThreadLocalMap::Entry`: saving a leak to happen seems important, but I would search a different way to solve this problem For the latter @clebertsuconic maybe we can change the plugin API in order to bring context `Object` all around a-la C language (with function pointers) eg ```java default Object beforeSend(ServerSession session, Transaction tx, Message message, boolean direct, boolean noAutoCreateQueue, Object context) throws ActiveMQException { this.beforeSend(session, tx, message, direct, noAutoCreateQueue); return context; } default void afterSend(ServerSession session, Transaction tx, Message message, boolean direct, boolean noAutoCreateQueue, RoutingStatus result, Object context) throws ActiveMQException { this.afterSend(tx, message, direct, noAutoCreateQueue, result); } default void onSendException(ServerSession session, Transaction tx, Message message, boolean direct, boolean noAutoCreateQueue, Exception e, Object context) throws ActiveMQException { this.onSendException(session, tx, message, direct, noAutoCreateQueue, e); } ``` This would allows using the `Span` as the `context` (ie state) to be propagated around without needing any extra map/thread local/.... ########## File path: examples/features/standard/broker-plugin/src/main/java/org/apache/activemq/artemis/jms/example/OpenTracingBrokerPlugin.java ########## @@ -0,0 +1,90 @@ +/* + * 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.artemis.jms.example; + +import java.io.InputStream; +import java.util.Properties; + +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.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import org.apache.activemq.artemis.api.core.ActiveMQException; +import org.apache.activemq.artemis.api.core.Message; +import org.apache.activemq.artemis.core.postoffice.RoutingStatus; +import org.apache.activemq.artemis.core.server.ServerSession; +import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin; +import org.apache.activemq.artemis.core.transaction.Transaction; + +public class OpenTracingBrokerPlugin implements ActiveMQServerPlugin { + private ThreadLocal threadLocal = new ThreadLocal(); + private static final String OPERATION_NAME = "/TracingPlugin/rootInvoke/"; + private static OpenTelemetrySdk sdk = initOpenTracing(); + + public static OpenTelemetrySdk initOpenTracing() { + try { + InputStream input = OpenTracingBrokerPlugin.class.getClassLoader().getResourceAsStream("tracing.properties"); + if (input == null) { + throw new NullPointerException("Unable to find tracing.properties file"); + } + Properties prop = new Properties(System.getProperties()); + prop.load(input); + System.setProperties(prop); + + sdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + + } catch (Throwable t) { + t.printStackTrace(); + } + return sdk; + } + + @Override + public void beforeSend(ServerSession session, + Transaction tx, + Message message, + boolean direct, + boolean noAutoCreateQueue) throws ActiveMQException { + SpanBuilder spanBuilder = GlobalOpenTelemetry.getTracer(OpenTracingBrokerPlugin.class.getName()) + .spanBuilder(OPERATION_NAME) + .setAttribute("message", message.toString()) + .setSpanKind(SpanKind.SERVER); + Span span = spanBuilder.startSpan(); + threadLocal.set(span); Review comment: This is correct, because messages are not supposed to contain things that cannot be encoded and sent to client(s), and `ThreadLocal` although temporary isn't an object instance that we wish to send to client(s). -- 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]
