nbrendah commented on a change in pull request #3895: URL: https://github.com/apache/activemq-artemis/pull/3895#discussion_r790434947
########## File path: examples/features/standard/broker-plugin/src/main/java/org/apache/activemq/artemis/jms/example/OpenTracingBrokerPlugin.java ########## @@ -0,0 +1,94 @@ +/* + * 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.StatusCode; +import io.opentelemetry.api.trace.Tracer; +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 static final ThreadLocal<Span> threadLocal = new ThreadLocal(); + private static final String OPERATION_NAME = "/TracingPlugin/"; + private static OpenTelemetrySdk sdk = initOpenTracing(); + private static final Tracer TRACER = GlobalOpenTelemetry.getTracer(OpenTracingBrokerPlugin.class.getName()); + + 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 = TRACER.spanBuilder(OPERATION_NAME).setAttribute("message", message.toString()).setSpanKind(SpanKind.SERVER); + Span span = spanBuilder.startSpan(); + threadLocal.set(span); + } + + @Override + public void afterSend(Transaction tx, + Message message, + boolean direct, + boolean noAutoCreateQueue, + RoutingStatus result) throws ActiveMQException { + Span span = threadLocal.get(); + span.end(); + threadLocal.remove(); + } + + @Override + public void onSendException(ServerSession session, + Transaction tx, + Message message, + boolean direct, + boolean noAutoCreateQueue, + Exception e) throws ActiveMQException { + threadLocal.get().setStatus(StatusCode.ERROR).recordException(e); + threadLocal.remove(); Review comment: @franz1981 does this guarantee that the object is gonna be removed in case of any exception? -- 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]
