Dynamic value for metricName in @Trace annotation?

I am sending the data of messages consumed by my JmsListeners to New Relic. I tried the below code

@Trace(dispatcher = true, metricName = "${jms.bulkUseruploadEventQueue.name}")
@JmsListener(destination = "${jms.bulkUseruploadEventQueue.name}", concurrency = "${jms.bulkUseruploadEventQueue.concurrency}")
public void processMessage(Message<BulkUserUploadEvent> message) {
   log.info("event:{} processed successfully", message.getPayload());
}

Here I can see the data in New Relic, but I can see the metric names as ${jms.bulkUseruploadEventQueue.name} instead of the exact value of the queue which is to be fetched from the config.
How can I set the exact queue name as a metricName in this case?

The @Trace annotation is handled at class loading time, so it is does not support SpEL or other expressions. Only plain old Strings will work there.

You could use the API inside the method to set the metric name.

@Value("${jms.bulkUseruploadEventQueue.name}")
private String metricName;

@Trace(dispatcher = true)
@JmsListener(destination = "${jms.bulkUseruploadEventQueue.name}", concurrency = "${jms.bulkUseruploadEventQueue.concurrency}")
public void processMessage(Message<BulkUserUploadEvent> message) {
    NewRelic.getAgent().getTracedMethod().setMetricName(metricName);
    NewRelic.getAgent().getTransaction().setTransactionName(TransactionNamePriority.CUSTOM_HIGH, true, metricName);

You probably don’t need the 2 lines starting with NewRelic, most likely the 2nd one is the one you want.

Leave a Comment