-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/repairkafka3.7 #707
Changes from 2 commits
3feef5a
2971c2b
7db4f51
1cbc3db
6220bd4
1e2b82a
d875bd3
ef2193b
e20ef76
3543204
526abbb
abd3848
0cb524c
f31fb6f
8297b83
73c85a0
1cc9438
960be6c
14ebf37
39353e8
3f51753
1a5f39c
b876765
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.kafka; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.common.header.Header; | ||
import org.apache.skywalking.apm.agent.core.context.CarrierItem; | ||
import org.apache.skywalking.apm.agent.core.context.ContextCarrier; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
|
||
import java.lang.reflect.Method; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Iterator; | ||
|
||
public class Kafka37ConsumerInterceptor extends KafkaConsumerInterceptor { | ||
|
||
@Override | ||
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { | ||
if (ret instanceof ConsumerRecords) { | ||
ConsumerEnhanceRequiredInfo requiredInfo = (ConsumerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField(); | ||
ConsumerRecords<?, ?> consumerRecords = (ConsumerRecords<?, ?>) ret; | ||
if (consumerRecords.count() == 0) { | ||
return ret; | ||
} | ||
for (ConsumerRecord<?, ?> consumerRecord : consumerRecords) { | ||
if (consumerRecord == null) { | ||
continue; | ||
} | ||
ContextCarrier contextCarrier = new ContextCarrier(); | ||
CarrierItem next = contextCarrier.items(); | ||
while (next.hasNext()) { | ||
next = next.next(); | ||
Iterator<Header> iterator = consumerRecord.headers().headers(next.getHeadKey()).iterator(); | ||
if (iterator.hasNext()) { | ||
next.setHeadValue(new String(iterator.next().value(), StandardCharsets.UTF_8)); | ||
} | ||
} | ||
String operationName = OPERATE_NAME_PREFIX + requiredInfo.getTopics() + CONSUMER_OPERATE_NAME + requiredInfo.getGroupId(); | ||
AbstractSpan activeSpan = ContextManager.createEntrySpan(operationName, contextCarrier).start(requiredInfo.getStartTime()); | ||
activeSpan.setComponent(ComponentsDefine.KAFKA_CONSUMER); | ||
SpanLayer.asMQ(activeSpan); | ||
Tags.MQ_BROKER.set(activeSpan, requiredInfo.getBrokerServers()); | ||
Tags.MQ_TOPIC.set(activeSpan, requiredInfo.getTopics()); | ||
activeSpan.setPeer(requiredInfo.getBrokerServers()); | ||
ContextManager.stopSpan(); | ||
} | ||
} | ||
return ret; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,8 @@ public class KafkaConsumerInstrumentation extends AbstractKafkaInstrumentation { | |
public static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.kafka.KafkaConsumerInterceptor"; | ||
public static final String INTERCEPTOR_CLASS_KAFKA3_2 = "org.apache.skywalking.apm.plugin.kafka.Kafka3ConsumerInterceptor"; | ||
public static final String ENHANCE_METHOD = "pollOnce"; | ||
public static final String ENHANCE_METHOD_KAFKA3_7 = "poll"; | ||
public static final String INTERCEPTOR_CLASS_KAFKA3_7 = "org.apache.skywalking.apm.plugin.kafka.Kafka37ConsumerInterceptor"; | ||
public static final String ENHANCE_COMPATIBLE_METHOD = "pollForFetches"; | ||
public static final String ENHANCE_CLASS = "org.apache.kafka.clients.consumer.KafkaConsumer"; | ||
public static final String SUBSCRIBE_METHOD = "subscribe"; | ||
|
@@ -62,104 +64,121 @@ public class KafkaConsumerInstrumentation extends AbstractKafkaInstrumentation { | |
|
||
@Override | ||
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { | ||
return new ConstructorInterceptPoint[] { | ||
new ConstructorInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getConstructorMatcher() { | ||
return takesArgumentWithType(0, CONSTRUCTOR_INTERCEPT_TYPE); | ||
} | ||
return new ConstructorInterceptPoint[]{ | ||
new ConstructorInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getConstructorMatcher() { | ||
return takesArgumentWithType(0, CONSTRUCTOR_INTERCEPT_TYPE); | ||
} | ||
|
||
@Override | ||
public String getConstructorInterceptor() { | ||
return CONSUMER_CONFIG_CONSTRUCTOR_INTERCEPTOR_CLASS; | ||
} | ||
}, | ||
new ConstructorInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getConstructorMatcher() { | ||
return takesArgumentWithType(0, CONSTRUCTOR_INTERCEPT_MAP_TYPE); | ||
} | ||
|
||
@Override | ||
public String getConstructorInterceptor() { | ||
return MAP_CONSTRUCTOR_INTERCEPTOR_CLASS; | ||
} | ||
}, | ||
@Override | ||
public String getConstructorInterceptor() { | ||
return CONSUMER_CONFIG_CONSTRUCTOR_INTERCEPTOR_CLASS; | ||
} | ||
}, | ||
new ConstructorInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getConstructorMatcher() { | ||
return takesArgumentWithType(0, CONSTRUCTOR_INTERCEPT_MAP_TYPE); | ||
} | ||
|
||
@Override | ||
public String getConstructorInterceptor() { | ||
return MAP_CONSTRUCTOR_INTERCEPTOR_CLASS; | ||
} | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert re-format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I have already recerted it, this class dosen't change, I have already rollback it to the original version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, you didn't. Change is still there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Those changes are compare to the changes which are not correct I commit before... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I dont't know why.... I check out the original branch and copy the codes into the class file, but the IDE says that there is no difference..... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Resolved |
||
|
||
}; | ||
} | ||
|
||
@Override | ||
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { | ||
return new InstanceMethodsInterceptPoint[] { | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
// targeting Kafka Client < 3.2 | ||
return named(ENHANCE_METHOD).or(named(ENHANCE_COMPATIBLE_METHOD).and(returns(Map.class))); | ||
} | ||
return new InstanceMethodsInterceptPoint[]{ | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
// targeting Kafka Client < 3.2 | ||
return named(ENHANCE_METHOD).or(named(ENHANCE_COMPATIBLE_METHOD).and(returns(Map.class))); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return INTERCEPTOR_CLASS; | ||
} | ||
@Override | ||
public String getMethodsInterceptor() { | ||
return INTERCEPTOR_CLASS; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
// targeting Kafka Client >= 3.2 | ||
return named(ENHANCE_COMPATIBLE_METHOD).and(returns(named("org.apache.kafka.clients.consumer.internals.Fetch"))); | ||
} | ||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
// targeting Kafka Client >= 3.2 | ||
return named(ENHANCE_COMPATIBLE_METHOD).and(returns(named("org.apache.kafka.clients.consumer.internals.Fetch"))); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return INTERCEPTOR_CLASS_KAFKA3_2; | ||
} | ||
@Override | ||
public String getMethodsInterceptor() { | ||
return INTERCEPTOR_CLASS_KAFKA3_2; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named(SUBSCRIBE_METHOD) | ||
.and(takesArgumentWithType(0, SUBSCRIBE_INTERCEPT_TYPE_NAME)); | ||
} | ||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
// targeting Kafka Client >= 3.7 | ||
return named(ENHANCE_METHOD_KAFKA3_7).and(returns(named("org.apache.kafka.clients.consumer.ConsumerRecords"))); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return SUBSCRIBE_INTERCEPT_CLASS; | ||
} | ||
@Override | ||
public String getMethodsInterceptor() { | ||
return INTERCEPTOR_CLASS_KAFKA3_7; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named(SUBSCRIBE_METHOD) | ||
.and(takesArgumentWithType(0, SUBSCRIBE_INTERCEPT_TYPE_PATTERN)); | ||
} | ||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named(SUBSCRIBE_METHOD) | ||
.and(takesArgumentWithType(0, SUBSCRIBE_INTERCEPT_TYPE_NAME)); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return SUBSCRIBE_INTERCEPT_CLASS; | ||
} | ||
@Override | ||
public String getMethodsInterceptor() { | ||
return SUBSCRIBE_INTERCEPT_CLASS; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named(SUBSCRIBE_METHOD) | ||
.and(takesArgumentWithType(0, SUBSCRIBE_INTERCEPT_TYPE_PATTERN)); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return SUBSCRIBE_INTERCEPT_CLASS; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not reformat, thr code style file is in the source codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reset the java file which I reformat