nbrendah commented on a change in pull request #3895:
URL: https://github.com/apache/activemq-artemis/pull/3895#discussion_r789414835



##########
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:
       @franz1981 I thought we can add the ThreadLocal or span instance to the 
messages itself like `message.putObjectProperty("SpanThread", threadLocal);`  
But i get an exception `java.lang.ThreadLocal is not a valid property type: 
org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException` 
showing that `ThreadLocal` is not accepted as an object.
   
   and then in the afterMethod we get it like` (ThreadLocal) 
message.getObjectProperty();` and finally get the span 

##########
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:
       @franz1981 I thought we can add the ThreadLocal or span instance to the 
messages itself like `message.putObjectProperty("SpanThread", threadLocal);`  
But i get an exception `java.lang.ThreadLocal is not a valid property type: 
org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException` 
showing that `ThreadLocal` is not accepted as an object.
   
   and then in the afterMethod we get it like` (ThreadLocal) 
message.getObjectProperty();` and finally get the span 




-- 
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]


Reply via email to