|
| 1 | +package datadog.trace.instrumentation.ibmmq; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 5 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 6 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 7 | + |
| 8 | +import com.ibm.mq.MQDestination; |
| 9 | +import com.ibm.mq.MQMessage; |
| 10 | +import datadog.trace.agent.tooling.Instrumenter; |
| 11 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 12 | +import net.bytebuddy.asm.Advice; |
| 13 | +import net.bytebuddy.description.type.TypeDescription; |
| 14 | +import net.bytebuddy.matcher.ElementMatcher; |
| 15 | + |
| 16 | +public class QueueInstrumentation |
| 17 | + implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { |
| 18 | + public QueueInstrumentation(String namespace) {} |
| 19 | + |
| 20 | + @Override |
| 21 | + public String hierarchyMarkerType() { |
| 22 | + return "com.ibm.mq.MQDestination"; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public ElementMatcher<TypeDescription> hierarchyMatcher() { |
| 27 | + return implementsInterface(named(hierarchyMarkerType())); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void methodAdvice(MethodTransformer transformer) { |
| 32 | + transformer.applyAdvice( |
| 33 | + named("send").and(takesArgument(0, named("com.ibm.mq.MQMessage"))).and(isPublic()), |
| 34 | + QueueInstrumentation.class.getName() + "$ProducerAdvice"); |
| 35 | + } |
| 36 | + |
| 37 | + public static class ProducerAdvice { |
| 38 | + |
| 39 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 40 | + public static void beforeSend( |
| 41 | + @Advice.Argument(0) final MQMessage message, @Advice.This final MQDestination destination) { |
| 42 | + // final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(MQDestination.class); |
| 43 | + // if (callDepth > 0) { |
| 44 | + // return null; |
| 45 | + // } |
| 46 | + |
| 47 | + // MessageProducerState producerState = |
| 48 | + // InstrumentationContext.get(MessageProducer.class, MessageProducerState.class) |
| 49 | + // .get(producer); |
| 50 | + |
| 51 | + // CharSequence resourceName; |
| 52 | + // String destinationName; |
| 53 | + // try { |
| 54 | + // // fall-back when producer wasn't created via standard Session.createProducer API |
| 55 | + // if (null != producerState) { |
| 56 | + // resourceName = producerState.getResourceName(); |
| 57 | + // Destination destination = producer.getDestination(); |
| 58 | + // destinationName = PRODUCER_DECORATE.getDestinationName(destination); |
| 59 | + // } else { |
| 60 | + // Destination destination = producer.getDestination(); |
| 61 | + // destinationName = PRODUCER_DECORATE.getDestinationName(destination); |
| 62 | + // boolean isQueue = PRODUCER_DECORATE.isQueue(destination); |
| 63 | + // resourceName = PRODUCER_DECORATE.toResourceName(destinationName, isQueue); |
| 64 | + // } |
| 65 | + // } catch (Exception ignored) { |
| 66 | + // resourceName = "Unknown Destination"; |
| 67 | + // destinationName = ""; |
| 68 | + // } |
| 69 | + |
| 70 | + // final AgentSpan span = startSpan(JMS_PRODUCE); |
| 71 | + // PRODUCER_DECORATE.afterStart(span); |
| 72 | + // PRODUCER_DECORATE.onProduce(span, resourceName); |
| 73 | + |
| 74 | + // if (null != destinationName |
| 75 | + // && !destinationName.isEmpty() |
| 76 | + // && Config.get().isDataStreamsEnabled()) { |
| 77 | + // final String tech = messageTechnology(message); |
| 78 | + // if ("ibmmq".equals(tech)) { // Initial release only supports DSM in JMS for IBM MQ |
| 79 | + // DataStreamsTags tags = create(tech, OUTBOUND, destinationName); |
| 80 | + // DataStreamsContext dsmContext = DataStreamsContext.fromTags(tags); |
| 81 | + // AgentTracer.get().getDataStreamsMonitoring().setCheckpoint(span, dsmContext); |
| 82 | + // } |
| 83 | + // } |
| 84 | + |
| 85 | + // if (JMSDecorator.canInject(message)) { |
| 86 | + // if (Config.get().isJmsPropagationEnabled() |
| 87 | + // && (null == producerState || !producerState.isPropagationDisabled())) { |
| 88 | + // defaultPropagator().inject(span, message, SETTER); |
| 89 | + // } |
| 90 | + // if (TIME_IN_QUEUE_ENABLED) { |
| 91 | + // if (null != producerState) { |
| 92 | + // SETTER.injectTimeInQueue(message, producerState); |
| 93 | + // } |
| 94 | + // } |
| 95 | + // } |
| 96 | + // return activateSpan(span); |
| 97 | + } |
| 98 | + |
| 99 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 100 | + public static void afterSend( |
| 101 | + @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) { |
| 102 | + // if (scope == null) { |
| 103 | + // return; |
| 104 | + // } |
| 105 | + // PRODUCER_DECORATE.onError(scope, throwable); |
| 106 | + // PRODUCER_DECORATE.beforeFinish(scope); |
| 107 | + // scope.close(); |
| 108 | + // scope.span().finish(); |
| 109 | + // CallDepthThreadLocalMap.reset(MessageProducer.class); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments