-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sermant流量标签透传插件支持grpc框架,grpc框架版本为1.13+
- Loading branch information
Showing
16 changed files
with
545 additions
and
50 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
46 changes: 46 additions & 0 deletions
46
...a/com/huaweicloud/sermant/tag/transmission/declarers/rpc/grpc/ClientCallImplDeclarer.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,46 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.tag.transmission.declarers.rpc.grpc; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; | ||
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; | ||
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; | ||
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; | ||
import com.huaweicloud.sermant.tag.transmission.interceptors.rpc.grpc.ClientCallImplInterceptor; | ||
|
||
/** | ||
* grpc client端Declarer,拦截header参数注入流量标签,支持grpc 1.13+版本 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-21 | ||
**/ | ||
public class ClientCallImplDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "io.grpc.internal.ClientCallImpl"; | ||
|
||
private static final String METHOD_NAMES = "start"; | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.nameEquals(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAMES), | ||
new ClientCallImplInterceptor())}; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
.../java/com/huaweicloud/sermant/tag/transmission/declarers/rpc/grpc/GrpcServerDeclarer.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,47 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.tag.transmission.declarers.rpc.grpc; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; | ||
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; | ||
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; | ||
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; | ||
import com.huaweicloud.sermant.tag.transmission.interceptors.rpc.grpc.GrpcServerInterceptor; | ||
|
||
/** | ||
* grpc server端declare,支持grpc 1.13+版本 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-15 | ||
**/ | ||
public class GrpcServerDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "io.grpc.internal.AbstractServerImplBuilder"; | ||
|
||
private static final String METHOD_NAME = "build"; | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.nameEquals(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{ | ||
InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME), new GrpcServerInterceptor()) | ||
}; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...n/src/main/java/com/huaweicloud/sermant/tag/transmission/enumeration/SpecialExecutor.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,68 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.tag.transmission.enumeration; | ||
|
||
/** | ||
* 特殊的线程池枚举类,该类线程池不会使用新线程执行方法 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-09-04 | ||
**/ | ||
public enum SpecialExecutor { | ||
/** | ||
* grpc的ThreadlessExecutor线程池。 | ||
* 对于该线程池,主线程会创建子线程提交线程任务,然后主线程执行。 | ||
* ThreadlessExecutor协调两个线程的任务,形成生产消费的模式 | ||
*/ | ||
THREAD_LESS_EXECUTOR("ThreadlessExecutor"), | ||
|
||
/** | ||
* grpc的SynchronizationContext线程池。 | ||
* 对于该线程池,并不会使用新的线程执行线程任务,而是在调用该线程池的线程中依次执行线程池队列的任务。 | ||
*/ | ||
SYNCHRONIZATION_CONTEXT("SynchronizationContext"), | ||
|
||
/** | ||
* 用于getSpecialExecutorByName方法寻找不到合适的线程池枚举对象时返回返回值 | ||
*/ | ||
OTHER_EXECUTORS("otherExecutors"); | ||
|
||
private final String executorName; | ||
|
||
SpecialExecutor(String executorName) { | ||
this.executorName = executorName; | ||
} | ||
|
||
public String getExecutorName() { | ||
return this.executorName; | ||
} | ||
|
||
/** | ||
* 用于根据线程池名称获取线程池枚举对象 | ||
* | ||
* @param name 线程池名称 | ||
* @return SpecialExecutor对象 | ||
*/ | ||
public static SpecialExecutor getSpecialExecutorByName(String name) { | ||
for (SpecialExecutor value : values()) { | ||
if (value.getExecutorName().equals(name)) { | ||
return value; | ||
} | ||
} | ||
return OTHER_EXECUTORS; | ||
} | ||
} |
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
...huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientCallImplInterceptor.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 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.tag.transmission.interceptors.rpc.grpc; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.core.utils.CollectionUtils; | ||
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; | ||
import com.huaweicloud.sermant.tag.transmission.interceptors.AbstractClientInterceptor; | ||
|
||
import io.grpc.Metadata; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* grpc使用DynamicMessage调用服务端,拦截header参数注入流量标签 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-21 | ||
**/ | ||
public class ClientCallImplInterceptor extends AbstractClientInterceptor<Metadata> { | ||
@Override | ||
protected ExecuteContext doBefore(ExecuteContext context) { | ||
Object[] arguments = context.getArguments(); | ||
|
||
// 被拦截方法的入参数量为2 | ||
if (arguments == null || arguments.length <= 1) { | ||
return context; | ||
} | ||
Object metadataObject = arguments[1]; | ||
if (metadataObject instanceof Metadata) { | ||
injectTrafficTag2Carrier((Metadata) metadataObject); | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
protected ExecuteContext doAfter(ExecuteContext context) { | ||
return context; | ||
} | ||
|
||
@Override | ||
protected void injectTrafficTag2Carrier(Metadata header) { | ||
for (String key : tagTransmissionConfig.getTagKeys()) { | ||
List<String> values = TrafficUtils.getTrafficTag().getTag().get(key); | ||
if (CollectionUtils.isEmpty(values)) { | ||
continue; | ||
} | ||
header.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), values.get(0)); | ||
} | ||
} | ||
} |
Oops, something went wrong.