Skip to content

Commit

Permalink
Dynamically evaluate service name for message consumers (#8088)
Browse files Browse the repository at this point in the history
* Dynamically evaluate service name for message consumers

* Do complex logic only when the FF is enabled
  • Loading branch information
amarziali authored Dec 18, 2024
1 parent 53cf809 commit a19f73a
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.function.Supplier;

public class SqsDecorator extends MessagingClientDecorator {
static final CharSequence COMPONENT_NAME = UTF8BytesString.create("java-aws-sdk");
Expand All @@ -25,7 +26,7 @@ public class SqsDecorator extends MessagingClientDecorator {
Config.get().isTimeInQueueEnabled(!SQS_LEGACY_TRACING, "sqs");
private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

public static final SqsDecorator CONSUMER_DECORATE =
new SqsDecorator(
Expand All @@ -42,10 +43,10 @@ public class SqsDecorator extends MessagingClientDecorator {
MESSAGE_BROKER,
SpanNaming.instance().namingSchema().messaging().timeInQueueService("sqs"));

protected SqsDecorator(String spanKind, CharSequence spanType, String serviceName) {
protected SqsDecorator(String spanKind, CharSequence spanType, Supplier<String> serviceName) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceName;
}

@Override
Expand All @@ -60,7 +61,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ abstract class SqsClientTest extends VersionedNamingTestBase {
}
trace(1) {
span {
serviceName SpanNaming.instance().namingSchema().messaging().inboundService("jms", Config.get().isJmsLegacyTracingEnabled()) ?: Config.get().getServiceName()
serviceName SpanNaming.instance().namingSchema().messaging().inboundService("jms", Config.get().isJmsLegacyTracingEnabled()).get() ?: Config.get().getServiceName()
operationName SpanNaming.instance().namingSchema().messaging().inboundOperation("jms")
resourceName "Consumed from Queue somequeue"
spanType DDSpanTypes.MESSAGE_CONSUMER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.function.Supplier;

public class SqsDecorator extends MessagingClientDecorator {
static final CharSequence COMPONENT_NAME = UTF8BytesString.create("java-aws-sdk");
Expand All @@ -26,7 +27,7 @@ public class SqsDecorator extends MessagingClientDecorator {
Config.get().isTimeInQueueEnabled(!SQS_LEGACY_TRACING, "sqs");
private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

public static final SqsDecorator CONSUMER_DECORATE =
new SqsDecorator(
Expand All @@ -43,10 +44,11 @@ public class SqsDecorator extends MessagingClientDecorator {
MESSAGE_BROKER,
SpanNaming.instance().namingSchema().messaging().timeInQueueService("sqs"));

protected SqsDecorator(String spanKind, CharSequence spanType, String serviceName) {
protected SqsDecorator(
String spanKind, CharSequence spanType, Supplier<String> serviceNameSupplier) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -61,7 +63,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ abstract class SqsClientTest extends VersionedNamingTestBase {
}
trace(1) {
span {
serviceName SpanNaming.instance().namingSchema().messaging().inboundService("jms", Config.get().isJmsLegacyTracingEnabled()) ?: Config.get().getServiceName()
serviceName SpanNaming.instance().namingSchema().messaging().inboundService("jms", Config.get().isJmsLegacyTracingEnabled()).get() ?: Config.get().getServiceName()
operationName SpanNaming.instance().namingSchema().messaging().inboundOperation("jms")
resourceName "Consumed from Queue somequeue"
spanType DDSpanTypes.MESSAGE_CONSUMER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -91,12 +92,13 @@ public CharSequence apply(CharSequence input) {
.inboundService(PUBSUB, Config.get().isGooglePubSubLegacyTracingEnabled()));
private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

protected PubSubDecorator(String spanKind, CharSequence spanType, String serviceName) {
protected PubSubDecorator(
String spanKind, CharSequence spanType, Supplier<String> serviceNameSupplier) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -111,7 +113,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.jms.Topic;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -60,7 +61,7 @@ public final class JMSDecorator extends MessagingClientDecorator {

private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

public static final JMSDecorator PRODUCER_DECORATE =
new JMSDecorator(
Expand Down Expand Up @@ -89,7 +90,10 @@ public final class JMSDecorator extends MessagingClientDecorator {
SpanNaming.instance().namingSchema().messaging().timeInQueueService(JMS.toString()));

public JMSDecorator(
String resourcePrefix, String spanKind, CharSequence spanType, String serviceName) {
String resourcePrefix,
String spanKind,
CharSequence spanType,
Supplier<String> serviceNameSupplier) {
this.resourcePrefix = resourcePrefix;

this.queueTempResourceName = UTF8BytesString.create(resourcePrefix + "Temporary Queue");
Expand All @@ -100,7 +104,7 @@ public JMSDecorator(

this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -115,7 +119,7 @@ protected CharSequence spanType() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
Expand Down Expand Up @@ -60,7 +61,7 @@ public final class JMSDecorator extends MessagingClientDecorator {

private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

public static final JMSDecorator PRODUCER_DECORATE =
new JMSDecorator(
Expand Down Expand Up @@ -90,7 +91,10 @@ public final class JMSDecorator extends MessagingClientDecorator {
SpanNaming.instance().namingSchema().messaging().timeInQueueService(JMS.toString()));

public JMSDecorator(
String resourcePrefix, String spanKind, CharSequence spanType, String serviceName) {
String resourcePrefix,
String spanKind,
CharSequence spanType,
Supplier<String> serviceNameSupplier) {
this.resourcePrefix = resourcePrefix;

this.queueTempResourceName = UTF8BytesString.create(resourcePrefix + "Temporary Queue");
Expand All @@ -101,7 +105,7 @@ public JMSDecorator(

this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

public static void logJMSException(JMSException ex) {
Expand All @@ -122,7 +126,7 @@ protected CharSequence spanType() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
Expand All @@ -41,7 +42,7 @@ public class KafkaDecorator extends MessagingClientDecorator {
public static final String KAFKA_PRODUCED_KEY = "x_datadog_kafka_produced";
private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

private static final DDCache<CharSequence, CharSequence> PRODUCER_RESOURCE_NAME_CACHE =
DDCaches.newFixedSizeCache(32);
Expand Down Expand Up @@ -78,10 +79,11 @@ public class KafkaDecorator extends MessagingClientDecorator {
InternalSpanTypes.MESSAGE_BROKER,
SpanNaming.instance().namingSchema().messaging().timeInQueueService(KAFKA));

protected KafkaDecorator(String spanKind, CharSequence spanType, String serviceName) {
protected KafkaDecorator(
String spanKind, CharSequence spanType, Supplier<String> serviceNameSupplier) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -96,7 +98,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
Expand All @@ -41,7 +42,7 @@ public class KafkaDecorator extends MessagingClientDecorator {
public static final String KAFKA_PRODUCED_KEY = "x_datadog_kafka_produced";
private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

private static final DDCache<CharSequence, CharSequence> PRODUCER_RESOURCE_NAME_CACHE =
DDCaches.newFixedSizeCache(32);
Expand Down Expand Up @@ -78,10 +79,11 @@ public class KafkaDecorator extends MessagingClientDecorator {
InternalSpanTypes.MESSAGE_BROKER,
SpanNaming.instance().namingSchema().messaging().timeInQueueService(KAFKA));

protected KafkaDecorator(String spanKind, CharSequence spanType, String serviceName) {
protected KafkaDecorator(
String spanKind, CharSequence spanType, Supplier<String> serviceNameSupplier) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -96,7 +98,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.MessagingClientDecorator;
import java.util.function.Supplier;
import org.apache.kafka.streams.processor.internals.ProcessorNode;
import org.apache.kafka.streams.processor.internals.ProcessorRecordContext;
import org.apache.kafka.streams.processor.internals.StampedRecord;
Expand All @@ -33,7 +34,7 @@ public class KafkaStreamsDecorator extends MessagingClientDecorator {

private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

private static final DDCache<CharSequence, CharSequence> RESOURCE_NAME_CACHE =
DDCaches.newFixedSizeCache(32);
Expand All @@ -54,10 +55,11 @@ public class KafkaStreamsDecorator extends MessagingClientDecorator {
InternalSpanTypes.MESSAGE_BROKER,
SpanNaming.instance().namingSchema().messaging().timeInQueueService(KAFKA));

protected KafkaStreamsDecorator(String spanKind, CharSequence spanType, String serviceName) {
protected KafkaStreamsDecorator(
String spanKind, CharSequence spanType, Supplier<String> serviceNameSupplier) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -67,7 +69,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class RabbitDecorator extends MessagingClientDecorator {

Expand Down Expand Up @@ -84,12 +85,13 @@ public class RabbitDecorator extends MessagingClientDecorator {

private final String spanKind;
private final CharSequence spanType;
private final String serviceName;
private final Supplier<String> serviceNameSupplier;

public RabbitDecorator(String spanKind, CharSequence spanType, String serviceName) {
public RabbitDecorator(
String spanKind, CharSequence spanType, Supplier<String> serviceNameSupplier) {
this.spanKind = spanKind;
this.spanType = spanType;
this.serviceName = serviceName;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand All @@ -99,7 +101,7 @@ protected String[] instrumentationNames() {

@Override
protected String service() {
return serviceName;
return serviceNameSupplier.get();
}

@Override
Expand Down
Loading

0 comments on commit a19f73a

Please sign in to comment.