|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2025 New Relic Corporation. All rights reserved. |
| 4 | + * * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +package com.azure.messaging.servicebus; |
| 9 | + |
| 10 | +import com.azure.messaging.servicebus.implementation.MessagingEntityType; |
| 11 | +import com.newrelic.api.agent.MessageProduceParameters; |
| 12 | +import com.newrelic.api.agent.NewRelic; |
| 13 | +import com.newrelic.api.agent.Trace; |
| 14 | +import com.newrelic.api.agent.weaver.MatchType; |
| 15 | +import com.newrelic.api.agent.weaver.NewField; |
| 16 | +import com.newrelic.api.agent.weaver.Weave; |
| 17 | +import com.newrelic.api.agent.weaver.WeaveAllConstructors; |
| 18 | +import com.newrelic.api.agent.weaver.Weaver; |
| 19 | +import com.newrelic.utils.ServiceBusBatchRequestHeaders; |
| 20 | +import com.newrelic.utils.ServiceBusUtil; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | + |
| 23 | +import java.util.logging.Level; |
| 24 | + |
| 25 | +// Note: the ServiceBusSenderClient (non-Async) proxies to the Async client, so no need to instrument both |
| 26 | +// that is not true for the receiver |
| 27 | +@Weave(type = MatchType.ExactClass, originalName = "com.azure.messaging.servicebus.ServiceBusSenderAsyncClient") |
| 28 | +public final class ServiceBusSenderAsyncClient_Instrumentation { |
| 29 | + |
| 30 | + private final MessagingEntityType entityType = Weaver.callOriginal(); |
| 31 | + private final String entityName = Weaver.callOriginal(); |
| 32 | + private final String fullyQualifiedNamespace = Weaver.callOriginal(); |
| 33 | + @NewField |
| 34 | + public String nrEntityName = entityName; |
| 35 | + @NewField |
| 36 | + public String nrFullyQualifiedNamespace = fullyQualifiedNamespace; |
| 37 | + @NewField |
| 38 | + public MessagingEntityType nrEntityType = entityType; |
| 39 | + |
| 40 | + @WeaveAllConstructors |
| 41 | + ServiceBusSenderAsyncClient_Instrumentation() { |
| 42 | + // do nothing, this is just here to get passed the compiler and validator checks |
| 43 | + } |
| 44 | + |
| 45 | + // this is called by the non-async version with the same signature |
| 46 | + @Trace |
| 47 | + public Mono<Void> sendMessage(ServiceBusMessage message) { |
| 48 | + ServiceBusBatchRequestHeaders headers = new ServiceBusBatchRequestHeaders(message); |
| 49 | + NewRelic.getAgent().getTransaction().insertDistributedTraceHeaders(headers); |
| 50 | + message = headers.tryToUpdateHeaders(); |
| 51 | + |
| 52 | + MessageProduceParameters messageProduceParameters = ServiceBusUtil.generateExternalProduceMetrics(this); |
| 53 | + NewRelic.getAgent().getTracedMethod().reportAsExternal(messageProduceParameters); |
| 54 | + |
| 55 | + return Weaver.callOriginal(); |
| 56 | + } |
| 57 | + |
| 58 | + // this is called by the non-async version with the same signature |
| 59 | + @Trace |
| 60 | + public Mono<Void> sendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext) { |
| 61 | + ServiceBusBatchRequestHeaders headers = new ServiceBusBatchRequestHeaders(message); |
| 62 | + NewRelic.getAgent().getTransaction().insertDistributedTraceHeaders(headers); |
| 63 | + message = headers.tryToUpdateHeaders(); |
| 64 | + |
| 65 | + MessageProduceParameters messageProduceParameters = ServiceBusUtil.generateExternalProduceMetrics(this); |
| 66 | + NewRelic.getAgent().getTracedMethod().reportAsExternal(messageProduceParameters); |
| 67 | + return Weaver.callOriginal(); |
| 68 | + } |
| 69 | + |
| 70 | + // this is called by the non-async version with the same signature |
| 71 | + @Trace |
| 72 | + public Mono<Void> sendMessages(ServiceBusMessageBatch batch) { |
| 73 | + batch = tryToUpdateBatch(batch); |
| 74 | + |
| 75 | + MessageProduceParameters messageProduceParameters = ServiceBusUtil.generateExternalProduceMetrics(this); |
| 76 | + NewRelic.getAgent().getTracedMethod().reportAsExternal(messageProduceParameters); |
| 77 | + return Weaver.callOriginal(); |
| 78 | + } |
| 79 | + |
| 80 | + // this is called by the non-async version with the same signature |
| 81 | + @Trace |
| 82 | + public Mono<Void> sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext) { |
| 83 | + batch = tryToUpdateBatch(batch); |
| 84 | + |
| 85 | + MessageProduceParameters messageProduceParameters = ServiceBusUtil.generateExternalProduceMetrics(this); |
| 86 | + NewRelic.getAgent().getTracedMethod().reportAsExternal(messageProduceParameters); |
| 87 | + return Weaver.callOriginal(); |
| 88 | + } |
| 89 | + |
| 90 | + // if we are unable to add DT headers for even 1 message here, we won't add it for any in the batch |
| 91 | + private ServiceBusMessageBatch tryToUpdateBatch(ServiceBusMessageBatch batch) { |
| 92 | + // first let's check to make sure the batch can hold all the new headers |
| 93 | + // Note: this won't be exact, but we dramatically overestimate the NR_DT_HEADER_SIZE to account |
| 94 | + if (batch.getMaxSizeInBytes() - batch.getSizeInBytes() < ServiceBusUtil.NR_DT_HEADER_SIZE * batch.getCount()) { |
| 95 | + NewRelic.getAgent().getLogger().log(Level.FINE, "Unable to add DT headers to batch, not enough space in batch"); |
| 96 | + return batch; |
| 97 | + } |
| 98 | + |
| 99 | + // now let's check to make sure that each message is small enough to add headers to |
| 100 | + // Note: this won't be exact, but we dramatically overestimate the NR_DT_HEADER_SIZE to account |
| 101 | + for (ServiceBusMessage message : batch.getMessages()) { |
| 102 | + if (message.getBody().getLength() + ServiceBusUtil.NR_DT_HEADER_SIZE > ServiceBusBatchRequestHeaders.DEFAULT_MAX_MESSAGE_SIZE) { |
| 103 | + NewRelic.getAgent().getLogger().log(Level.FINE, "Unable to add DT headers to batch, not enough space in message: "+message.getMessageId()); |
| 104 | + return batch; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (ServiceBusMessage message : batch.getMessages()) { |
| 109 | + ServiceBusBatchRequestHeaders headers = new ServiceBusBatchRequestHeaders(message); |
| 110 | + NewRelic.getAgent().getTransaction().insertDistributedTraceHeaders(headers); |
| 111 | + // we've already checked for space, we can skip tryToUpdateHeaders(), it only adds the same check |
| 112 | + headers.addDTHeaders(); |
| 113 | + } |
| 114 | + |
| 115 | + return batch; |
| 116 | + } |
| 117 | +} |
0 commit comments