|
| 1 | +package datadog.trace.instrumentation.confluentschemaregistry; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 5 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 6 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 7 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 8 | + |
| 9 | +import com.google.auto.service.AutoService; |
| 10 | +import datadog.trace.agent.tooling.Instrumenter; |
| 11 | +import datadog.trace.agent.tooling.Instrumenter.MethodTransformer; |
| 12 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 13 | +import datadog.trace.bootstrap.InstrumentationContext; |
| 14 | +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
| 15 | +import datadog.trace.instrumentation.kafka_common.ClusterIdHolder; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import org.apache.kafka.common.serialization.Deserializer; |
| 20 | + |
| 21 | +/** |
| 22 | + * Instruments Confluent Schema Registry deserializers (Avro, Protobuf, and JSON) to capture |
| 23 | + * deserialization operations. |
| 24 | + */ |
| 25 | +@AutoService(InstrumenterModule.class) |
| 26 | +public class KafkaDeserializerInstrumentation extends InstrumenterModule.Tracing |
| 27 | + implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { |
| 28 | + |
| 29 | + public KafkaDeserializerInstrumentation() { |
| 30 | + super("confluent-schema-registry", "kafka"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String[] knownMatchingTypes() { |
| 35 | + return new String[] { |
| 36 | + "io.confluent.kafka.serializers.KafkaAvroDeserializer", |
| 37 | + "io.confluent.kafka.serializers.json.KafkaJsonSchemaDeserializer", |
| 38 | + "io.confluent.kafka.serializers.protobuf.KafkaProtobufDeserializer" |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String[] helperClassNames() { |
| 44 | + return new String[] { |
| 45 | + "datadog.trace.instrumentation.kafka_common.ClusterIdHolder", |
| 46 | + packageName + ".SchemaIdExtractor" |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Map<String, String> contextStore() { |
| 52 | + Map<String, String> contextStores = new HashMap<>(); |
| 53 | + contextStores.put("org.apache.kafka.common.serialization.Deserializer", "java.lang.Boolean"); |
| 54 | + return contextStores; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void methodAdvice(MethodTransformer transformer) { |
| 59 | + // Instrument configure to capture isKey value |
| 60 | + transformer.applyAdvice( |
| 61 | + isMethod() |
| 62 | + .and(named("configure")) |
| 63 | + .and(isPublic()) |
| 64 | + .and(takesArguments(2)) |
| 65 | + .and(takesArgument(1, boolean.class)), |
| 66 | + getClass().getName() + "$ConfigureAdvice"); |
| 67 | + |
| 68 | + // Instrument deserialize(String topic, Headers headers, byte[] data) |
| 69 | + // The 2-arg version calls this one, so we only need to instrument this to avoid duplicates |
| 70 | + transformer.applyAdvice( |
| 71 | + isMethod() |
| 72 | + .and(named("deserialize")) |
| 73 | + .and(isPublic()) |
| 74 | + .and(takesArguments(3)) |
| 75 | + .and(takesArgument(0, String.class)) |
| 76 | + .and(takesArgument(2, byte[].class)), |
| 77 | + getClass().getName() + "$DeserializeAdvice"); |
| 78 | + } |
| 79 | + |
| 80 | + public static class ConfigureAdvice { |
| 81 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 82 | + public static void onExit( |
| 83 | + @Advice.This Deserializer deserializer, @Advice.Argument(1) boolean isKey) { |
| 84 | + // Store the isKey value in InstrumentationContext for later use |
| 85 | + InstrumentationContext.get(Deserializer.class, Boolean.class).put(deserializer, isKey); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static class DeserializeAdvice { |
| 90 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 91 | + public static void onExit( |
| 92 | + @Advice.This Deserializer deserializer, |
| 93 | + @Advice.Argument(0) String topic, |
| 94 | + @Advice.Argument(2) byte[] data, |
| 95 | + @Advice.Return Object result, |
| 96 | + @Advice.Thrown Throwable throwable) { |
| 97 | + |
| 98 | + // Get isKey from InstrumentationContext (stored during configure) |
| 99 | + Boolean isKeyObj = |
| 100 | + InstrumentationContext.get(Deserializer.class, Boolean.class).get(deserializer); |
| 101 | + boolean isKey = isKeyObj != null && isKeyObj; |
| 102 | + |
| 103 | + // Get cluster ID from thread-local (set by Kafka consumer instrumentation) |
| 104 | + String clusterId = ClusterIdHolder.get(); |
| 105 | + |
| 106 | + boolean isSuccess = throwable == null; |
| 107 | + int schemaId = isSuccess ? SchemaIdExtractor.extractSchemaId(data) : -1; |
| 108 | + |
| 109 | + // Record the schema registry usage |
| 110 | + AgentTracer.get() |
| 111 | + .getDataStreamsMonitoring() |
| 112 | + .reportSchemaRegistryUsage(topic, clusterId, schemaId, isSuccess, isKey, "deserialize"); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments