-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 增加对spring-kafka的生产-消费增强trace支持
- Loading branch information
Showing
5 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/io/github/artlibs/autotrace4j/transformer/impl/KafkaProducerTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.artlibs.autotrace4j.transformer.impl; | ||
|
||
import io.github.artlibs.autotrace4j.context.MethodWrapper; | ||
import io.github.artlibs.autotrace4j.context.ReflectUtils; | ||
import io.github.artlibs.autotrace4j.context.TraceContext; | ||
import io.github.artlibs.autotrace4j.transformer.abs.AbsVisitorTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
import java.util.Objects; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.*; | ||
|
||
/** | ||
* Kafka 生产者增强转换器 | ||
* <p> | ||
* @author Fury | ||
* @since 2024-03-30 | ||
* <p> | ||
* All rights Reserved. | ||
*/ | ||
public class KafkaProducerTransformer extends AbsVisitorTransformer { | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return hasSuperType(named("org.apache.kafka.clients.producer.KafkaProducer")); | ||
} | ||
|
||
@Override | ||
protected MethodMatcherHolder methodMatchers() { | ||
return ofMatcher(isPrivate().and(named("doSend")) | ||
.and(takesArgument(0, hasSuperType(named("org.apache.kafka.clients.producer.ProducerRecord")))) | ||
.and(takesArgument(1, hasSuperType(named("org.apache.kafka.clients.producer.Callback")))) | ||
); | ||
} | ||
|
||
/** | ||
* 发送消息时将当前上下文trace信息放入消息头 | ||
* @param producerRecord - | ||
*/ | ||
@Advice.OnMethodEnter | ||
public static void adviceOnMethodEnter( | ||
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC | ||
, readOnly = false) Object producerRecord) { | ||
if (Objects.isNull(producerRecord) || Objects.isNull(TraceContext.getTraceId())) { | ||
return; | ||
} | ||
Object headers = ReflectUtils.getMethodWrapper(producerRecord | ||
, "headers").invoke(); | ||
if (Objects.isNull(headers)) { | ||
return; | ||
} | ||
MethodWrapper method = ReflectUtils.getMethodWrapper(headers | ||
, "add", String.class, byte[].class); | ||
method.invoke(TraceContext.TRACE_KEY, TraceContext.getTraceId()); | ||
if (Objects.nonNull(TraceContext.getSpanId())) { | ||
method.invoke(TraceContext.SPAN_KEY, TraceContext.getSpanId()); | ||
} | ||
if (Objects.nonNull(TraceContext.getParentSpanId())) { | ||
method.invoke(TraceContext.PARENT_SPAN_KEY, TraceContext.getParentSpanId()); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...n/java/io/github/artlibs/autotrace4j/transformer/impl/KafkaSpringConsumerTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.artlibs.autotrace4j.transformer.impl; | ||
|
||
import io.github.artlibs.autotrace4j.context.MethodWrapper; | ||
import io.github.artlibs.autotrace4j.context.ReflectUtils; | ||
import io.github.artlibs.autotrace4j.context.TraceContext; | ||
import io.github.artlibs.autotrace4j.transformer.abs.AbsVisitorTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
import java.util.Iterator; | ||
import java.util.Objects; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.*; | ||
|
||
/** | ||
* Spring Kafka 消费增强转换器, 参见类 | ||
* org.springframework.kafka.listener.KafkaMessageListenerContainer:doStart() | ||
* org.springframework.kafka.listener.KafkaMessageListenerContainer | ||
* .ListenerConsumer:run(),invokeListener(), invokeBatchOnMessageWithRecordsOrList() & doInvokeOnMessage() | ||
* org.apache.kafka.clients.consumer.Consumer:poll() | ||
* <p> | ||
* @author Fury | ||
* @since 2024-03-30 | ||
* <p> | ||
* All rights Reserved. | ||
*/ | ||
public class KafkaSpringConsumerTransformer extends AbsVisitorTransformer { | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return hasSuperType(named("org.springframework.kafka.listener." + | ||
"KafkaMessageListenerContainer.ListenerConsumer")); | ||
} | ||
|
||
@Override | ||
protected MethodMatcherHolder methodMatchers() { | ||
return ofMatcher(isPrivate().and( | ||
// private void invokeBatchOnMessageWithRecordsOrList(final ConsumerRecords<K, V> recordsArg, | ||
// @Nullable List<ConsumerRecord<K, V>> recordListArg) | ||
named("invokeBatchOnMessageWithRecordsOrList") | ||
// private void doInvokeOnMessage(final ConsumerRecord<K, V> recordArg) | ||
.or(named("doInvokeOnMessage")) | ||
)); | ||
} | ||
|
||
/** | ||
* 消费消息时将消息头的trace信息放入上下文, 或者生成新的上下文 | ||
* @param recOrRecs ConsumerRecord<K, V> or ConsumerRecords<K, V> | ||
*/ | ||
@Advice.OnMethodEnter | ||
public static void adviceOnMethodEnter(@Advice.Argument(value = 0) Object recOrRecs) { | ||
Object consumerRecord = recOrRecs; | ||
if (Iterable.class.isAssignableFrom(recOrRecs.getClass())) { | ||
Iterator<?> iterator = ReflectUtils.getMethodWrapper(recOrRecs | ||
, "iterator").invoke(); | ||
consumerRecord = ReflectUtils.getMethodWrapper(iterator | ||
, "next").invoke(); | ||
} | ||
Object headers = ReflectUtils.getMethodWrapper(consumerRecord | ||
, "headers").invoke(); | ||
MethodWrapper method = ReflectUtils.getMethodWrapper(headers | ||
, "lastHeader", String.class); | ||
|
||
String traceId = method.invoke(TraceContext.TRACE_KEY); | ||
if (Objects.isNull(traceId)) { | ||
traceId = TraceContext.generate(); | ||
} | ||
TraceContext.setTraceId(traceId); | ||
TraceContext.setSpanId(TraceContext.generate()); | ||
String spanId = method.invoke(TraceContext.SPAN_KEY); | ||
if (Objects.nonNull(spanId)) { | ||
TraceContext.setParentSpanId(spanId); | ||
} | ||
} | ||
|
||
/** | ||
* 方法结束时清空上下文 | ||
*/ | ||
@Advice.OnMethodExit | ||
public static void adviceOnMethodExit() { | ||
TraceContext.removeAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters