Skip to content

Commit e3a705d

Browse files
committed
[#12218] Backport: Update spring kafka container entry point of kafka plugin
1 parent 0695c1d commit e3a705d

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

agent-module/agent/src/main/resources/profiles/local/pinpoint.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,11 @@ profiler.kafka.producer.enable=false
13461346
profiler.kafka.consumer.enable=false
13471347
# Setting when using spring-kafka (In this case, you can leave profiler.kafka.consumer.entryPoint option to empty.)
13481348
profiler.springkafka.consumer.enable=false
1349+
# Use spring-kafka's KafkaMessageListenerContainer entry point.
1350+
# Used to widen tracking
1351+
profiler.springkafka.container.enable=false
1352+
# Set whether to treat exceptions as errors when using the KafkaMessageListenerContainer entry point.
1353+
profiler.springkafka.container.mark.error=false
13491354
# you must set target that handles ConsumerRecord or ConsumerRecords(Remote Trace feature is not enabled.) as a argument for remote trace
13501355
# ex) profiler.kafka.consumer.entryPoint=clazzName.methodName
13511356
profiler.kafka.consumer.entryPoint=

agent-module/agent/src/main/resources/profiles/release/pinpoint.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,11 @@ profiler.kafka.producer.enable=false
13691369
profiler.kafka.consumer.enable=false
13701370
# Setting when using spring-kafka (In this case, you can leave profiler.kafka.consumer.entryPoint option to empty.)
13711371
profiler.springkafka.consumer.enable=false
1372+
# Use spring-kafka's KafkaMessageListenerContainer entry point.
1373+
# Used to widen tracking
1374+
profiler.springkafka.container.enable=false
1375+
# Set whether to treat exceptions as errors when using the KafkaMessageListenerContainer entry point.
1376+
profiler.springkafka.container.mark.error=false
13721377
# you must set target that handles ConsumerRecord or ConsumerRecords(Remote Trace feature is not enabled.) as a argument for remote trace
13731378
# ex) profiler.kafka.consumer.entryPoint=clazzName.methodName
13741379
profiler.kafka.consumer.entryPoint=

agent-module/plugins/kafka/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ To enable Kafka Consumer, set the following option in *pinpoint.config*:
1515
```
1616
# Setting when using spring-kafka (In this case, you can leave profiler.kafka.consumer.entryPoint option to empty.)
1717
profiler.springkafka.consumer.enable=true
18+
19+
# Use spring-kafka's KafkaMessageListenerContainer entry point.
20+
# Used to widen tracking
21+
profiler.springkafka.container.enable=false
22+
# Set whether to treat exceptions as errors when using the KafkaMessageListenerContainer entry point.
23+
profiler.springkafka.container.mark.error=false
1824
```
1925

2026
#### to use consumer exclusively

agent-module/plugins/kafka/src/main/java/com/navercorp/pinpoint/plugin/kafka/KafkaConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class KafkaConfig {
4444
private final boolean headerEnable;
4545
private final boolean headerRecorded;
4646
private final String kafkaEntryPoint;
47+
private final boolean kafkaMessageListenerContainerEnable;
48+
private final boolean kafkaMessageListenerContainerMarkError;
4749

4850
public KafkaConfig(ProfilerConfig config) {
4951
this.enable = config.readBoolean(ENABLE, true);
@@ -54,6 +56,8 @@ public KafkaConfig(ProfilerConfig config) {
5456
this.headerEnable = config.readBoolean(HEADER_ENABLE, true);
5557
this.headerRecorded = config.readBoolean(HEADER_RECORD, true);
5658
this.kafkaEntryPoint = config.readString(CONSUMER_ENTRY_POINT, "");
59+
this.kafkaMessageListenerContainerEnable = config.readBoolean("profiler.springkafka.container.enable", false);
60+
this.kafkaMessageListenerContainerMarkError = config.readBoolean("profiler.springkafka.container.mark.error", false);
5761
}
5862

5963
public boolean isEnable() {
@@ -88,6 +92,14 @@ public String getKafkaEntryPoint() {
8892
return kafkaEntryPoint;
8993
}
9094

95+
public boolean isKafkaMessageListenerContainerEnable() {
96+
return kafkaMessageListenerContainerEnable;
97+
}
98+
99+
public boolean isKafkaMessageListenerContainerMarkError() {
100+
return kafkaMessageListenerContainerMarkError;
101+
}
102+
91103
@Override
92104
public String toString() {
93105
return "KafkaConfig{" +
@@ -99,6 +111,8 @@ public String toString() {
99111
", headerEnable=" + headerEnable +
100112
", headerRecorded=" + headerRecorded +
101113
", kafkaEntryPoint='" + kafkaEntryPoint + '\'' +
114+
", kafkaMessageListenerContainerEnable=" + kafkaMessageListenerContainerEnable +
115+
", kafkaMessageListenerContainerMarkError=" + kafkaMessageListenerContainerMarkError +
102116
'}';
103117
}
104118
}

agent-module/plugins/kafka/src/main/java/com/navercorp/pinpoint/plugin/kafka/KafkaPlugin.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.navercorp.pinpoint.plugin.kafka.interceptor.ConsumerRecordEntryPointInterceptor;
4848
import com.navercorp.pinpoint.plugin.kafka.interceptor.ConsumerRecordsInterceptor;
4949
import com.navercorp.pinpoint.plugin.kafka.interceptor.FetchResponseInterceptor;
50+
import com.navercorp.pinpoint.plugin.kafka.interceptor.ListenerConsumerInvokeErrorHandlerInterceptor;
5051
import com.navercorp.pinpoint.plugin.kafka.interceptor.NetworkClientPollInterceptor;
5152
import com.navercorp.pinpoint.plugin.kafka.interceptor.ProcessInterceptor;
5253
import com.navercorp.pinpoint.plugin.kafka.interceptor.ProducerAddHeaderInterceptor;
@@ -104,6 +105,11 @@ public void setup(ProfilerPluginSetupContext context) {
104105
transformTemplate.transform("org.apache.kafka.common.requests.FetchResponse", FetchResponseTransform.class);
105106

106107
if (config.isSpringConsumerEnable()) {
108+
if(config.isKafkaMessageListenerContainerEnable()) {
109+
// KafkaMessageListenerContainer$ListenerConsumer
110+
transformTemplate.transform("org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer", ListenerConsumerTransform.class);
111+
}
112+
107113
transformTemplate.transform("org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter", AcknowledgingConsumerAwareMessageListenerTransform.class);
108114
transformTemplate.transform("org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter", BatchMessagingMessageListenerAdapterTransform.class);
109115

@@ -348,6 +354,34 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader,
348354

349355
}
350356

357+
public static class ListenerConsumerTransform implements TransformCallback {
358+
359+
@Override
360+
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
361+
final InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classfileBuffer);
362+
363+
final InstrumentMethod doInvokeRecordListenerMethod = target.getDeclaredMethod("doInvokeRecordListener", "org.apache.kafka.clients.consumer.ConsumerRecord", "java.util.Iterator");
364+
if (doInvokeRecordListenerMethod != null) {
365+
doInvokeRecordListenerMethod.addScopedInterceptor(ConsumerRecordEntryPointInterceptor.class, va(0), KafkaConstants.SCOPE, ExecutionPolicy.BOUNDARY);
366+
}
367+
final InstrumentMethod invokeErrorHandlerMethod = target.getDeclaredMethod("invokeErrorHandler", "org.apache.kafka.clients.consumer.ConsumerRecord", "java.util.Iterator", "java.lang.RuntimeException");
368+
if (invokeErrorHandlerMethod != null) {
369+
invokeErrorHandlerMethod.addInterceptor(ListenerConsumerInvokeErrorHandlerInterceptor.class);
370+
}
371+
final InstrumentMethod doInvokeBatchListenerMethod = target.getDeclaredMethod("doInvokeBatchListener", "org.apache.kafka.clients.consumer.ConsumerRecord", "java.util.List");
372+
if (doInvokeBatchListenerMethod != null) {
373+
doInvokeBatchListenerMethod.addScopedInterceptor(ConsumerMultiRecordEntryPointInterceptor.class, va(1), KafkaConstants.SCOPE, ExecutionPolicy.BOUNDARY);
374+
}
375+
final InstrumentMethod invokeBatchErrorHandlerMethod = target.getDeclaredMethod("invokeBatchErrorHandler", "org.apache.kafka.clients.consumer.ConsumerRecord", "java.util.List", "java.lang.RuntimeException");
376+
if (invokeBatchErrorHandlerMethod != null) {
377+
invokeBatchErrorHandlerMethod.addInterceptor(ListenerConsumerInvokeErrorHandlerInterceptor.class);
378+
}
379+
380+
return target.toBytecode();
381+
}
382+
}
383+
384+
351385
public static class AcknowledgingConsumerAwareMessageListenerTransform implements TransformCallback {
352386

353387
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.plugin.kafka.interceptor;
18+
19+
import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
20+
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
21+
import com.navercorp.pinpoint.bootstrap.context.Trace;
22+
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
23+
import com.navercorp.pinpoint.bootstrap.interceptor.SpanEventSimpleAroundInterceptorForPlugin;
24+
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
25+
import com.navercorp.pinpoint.plugin.kafka.KafkaConfig;
26+
import com.navercorp.pinpoint.plugin.kafka.KafkaConstants;
27+
28+
public class ListenerConsumerInvokeErrorHandlerInterceptor extends SpanEventSimpleAroundInterceptorForPlugin {
29+
final boolean markError;
30+
31+
public ListenerConsumerInvokeErrorHandlerInterceptor(TraceContext traceContext, MethodDescriptor methodDescriptor) {
32+
super(traceContext, methodDescriptor);
33+
final KafkaConfig config = new KafkaConfig(traceContext.getProfilerConfig());
34+
this.markError = config.isKafkaMessageListenerContainerMarkError();
35+
}
36+
37+
@Override
38+
public Trace currentTrace() {
39+
if (markError) {
40+
return traceContext.currentTraceObject();
41+
}
42+
return null;
43+
}
44+
45+
@Override
46+
public void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[] args) throws Exception {
47+
}
48+
49+
@Override
50+
public void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) throws Exception {
51+
recorder.recordApi(methodDescriptor);
52+
recorder.recordServiceType(KafkaConstants.KAFKA_CLIENT_INTERNAL);
53+
final RuntimeException runtimeException = ArrayArgumentUtils.getArgument(args, 2, RuntimeException.class);
54+
if (runtimeException != null) {
55+
recorder.recordException(runtimeException);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)