@@ -78,11 +78,28 @@ public void methodAdvice(MethodTransformer transformer) {
7878 .and (takesArgument (2 , named ("org.apache.kafka.common.serialization.Deserializer" ))),
7979 KafkaConsumerInfoInstrumentation .class .getName () + "$ConstructorAdvice" );
8080
81+ transformer .applyAdvice (
82+ isConstructor ()
83+ .and (takesArgument (0 , Map .class ))
84+ .and (takesArgument (1 , named ("org.apache.kafka.common.serialization.Deserializer" )))
85+ .and (takesArgument (2 , named ("org.apache.kafka.common.serialization.Deserializer" ))),
86+ KafkaConsumerInfoInstrumentation .class .getName () + "$SecondConstructorAdvice" );
87+
88+ transformer .applyAdvice (
89+ isMethod ()
90+ .and (isPublic ())
91+ .and (named ("poll" ))
92+ .and (takesArguments (1 ))
93+ .and (takesArgument (0 , long .class ))
94+ .and (returns (named ("org.apache.kafka.clients.consumer.ConsumerRecords" ))),
95+ KafkaConsumerInfoInstrumentation .class .getName () + "$RecordsAdvice" );
96+
8197 transformer .applyAdvice (
8298 isMethod ()
8399 .and (isPublic ())
84100 .and (named ("poll" ))
85101 .and (takesArguments (1 ))
102+ .and (takesArgument (0 , named ("java.time.Duration" )))
86103 .and (returns (named ("org.apache.kafka.clients.consumer.ConsumerRecords" ))),
87104 KafkaConsumerInfoInstrumentation .class .getName () + "$RecordsAdvice" );
88105 }
@@ -94,6 +111,7 @@ public static void captureGroup(
94111 @ Advice .FieldValue ("metadata" ) Metadata metadata ,
95112 @ Advice .FieldValue ("coordinator" ) ConsumerCoordinator coordinator ,
96113 @ Advice .Argument (0 ) ConsumerConfig consumerConfig ) {
114+ System .out .println ("constructor advice!!!" );
97115 String consumerGroup = consumerConfig .getString (ConsumerConfig .GROUP_ID_CONFIG );
98116 String normalizedConsumerGroup =
99117 consumerGroup != null && !consumerGroup .isEmpty () ? consumerGroup : null ;
@@ -131,6 +149,66 @@ public static void muzzleCheck(ConsumerRecord record) {
131149 }
132150 }
133151
152+ public static class SecondConstructorAdvice {
153+ @ Advice .OnMethodExit (suppress = Throwable .class )
154+ public static void captureGroup (
155+ @ Advice .This KafkaConsumer consumer ,
156+ @ Advice .FieldValue ("metadata" ) Metadata metadata ,
157+ @ Advice .FieldValue ("coordinator" ) ConsumerCoordinator coordinator ,
158+ @ Advice .Argument (0 ) Map <String , Object > consumerConfig ) {
159+ System .out .println ("new constructor advice!!!" );
160+ Object groupID = consumerConfig .get ("group.id" );
161+ String consumerGroup = groupID instanceof String ? (String ) groupID : null ;
162+ String normalizedConsumerGroup =
163+ consumerGroup != null && !consumerGroup .isEmpty () ? consumerGroup : null ;
164+ System .out .println ("consume group " + normalizedConsumerGroup );
165+
166+ String bootstrapServers = null ;
167+ Object bootstrapServersObj = consumerConfig .get ("bootstrap.servers" );
168+ if (bootstrapServersObj instanceof String ) {
169+ bootstrapServers = (String ) bootstrapServersObj ;
170+ System .out .println ("bootstrap servers " + bootstrapServers );
171+ } else {
172+ System .out .println ("it s not a string" );
173+ }
174+ // if (bootstrapServersList != null && !bootstrapServersList.isEmpty()) {
175+ // bootstrapServers = String.join(",", bootstrapServersList);
176+ // }
177+ // Object bootstrapServersObj = consumerConfig.get("bootstrap.servers");
178+ // if (bootstrapServersObj instanceof List) {
179+ // List<?> tempList = (List<?>) bootstrapServersObj;
180+
181+ // // Verify each element is a String
182+ // if (!tempList.isEmpty() && tempList.stream().allMatch(element -> element instanceof String)) {
183+ // bootstrapServers = String.join(",", (List<String>) tempList);
184+ // }
185+ // }
186+ KafkaConsumerInfo kafkaConsumerInfo ;
187+ if (Config .get ().isDataStreamsEnabled ()) {
188+ kafkaConsumerInfo =
189+ new KafkaConsumerInfo (normalizedConsumerGroup , metadata , bootstrapServers );
190+ } else {
191+ kafkaConsumerInfo = new KafkaConsumerInfo (normalizedConsumerGroup , bootstrapServers );
192+ }
193+
194+ if (kafkaConsumerInfo .getConsumerGroup () != null
195+ || kafkaConsumerInfo .getClientMetadata () != null ) {
196+ InstrumentationContext .get (KafkaConsumer .class , KafkaConsumerInfo .class )
197+ .put (consumer , kafkaConsumerInfo );
198+ if (coordinator != null ) {
199+ InstrumentationContext .get (ConsumerCoordinator .class , KafkaConsumerInfo .class )
200+ .put (coordinator , kafkaConsumerInfo );
201+ }
202+ }
203+ }
204+
205+ public static void muzzleCheck (ConsumerRecord record ) {
206+ // KafkaConsumerInstrumentation only applies for kafka versions with headers
207+ // Make an explicit call so KafkaConsumerGroupInstrumentation does the same
208+ record .headers ();
209+ }
210+ }
211+
134212 /**
135213 * this method transfers the consumer group from the KafkaConsumer class key to the
136214 * ConsumerRecords key. This is necessary because in the poll method, we don't have access to the
@@ -157,6 +235,7 @@ public static void captureGroup(
157235 @ Advice .Enter final AgentScope scope ,
158236 @ Advice .This KafkaConsumer consumer ,
159237 @ Advice .Return ConsumerRecords records ) {
238+ System .out .println ("polling!" );
160239 int recordsCount = 0 ;
161240 if (records != null ) {
162241 KafkaConsumerInfo kafkaConsumerInfo =
0 commit comments