-
Notifications
You must be signed in to change notification settings - Fork 164
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
15 changed files
with
630 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
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
...weicloud/sermant/tag/transmission/declarers/rpc/grpc/ClientUseDynamicMessageDeclarer.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.ClientUseDynamicMessageInterceptor; | ||
|
||
/** | ||
* grpc使用DynamicMessage方式调用服务端declarer,支持grpc 1.13+版本 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-21 | ||
**/ | ||
public class ClientUseDynamicMessageDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "io.grpc.stub.ClientCalls"; | ||
|
||
private static final String[] METHOD_NAMES = {"asyncUnaryCall", "asyncServerStreamingCall", "futureUnaryCall"}; | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.nameEquals(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameContains(METHOD_NAMES), | ||
new ClientUseDynamicMessageInterceptor())}; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...loud/sermant/tag/transmission/declarers/rpc/grpc/ClientUseDynamicMessageImplDeclarer.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.ClientUseDynamicMessageImplInterceptor; | ||
|
||
/** | ||
* grpc使用DynamicMessage调用服务端,拦截header参数注入流量标签,支持grpc 1.13+版本 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-21 | ||
**/ | ||
public class ClientUseDynamicMessageImplDeclarer 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 ClientUseDynamicMessageImplInterceptor())}; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../java/com/huaweicloud/sermant/tag/transmission/declarers/rpc/grpc/GrpcClientDeclarer.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,49 @@ | ||
/* | ||
* 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.GrpcClientInterceptor; | ||
|
||
/** | ||
* grpc 客户端 declarer,支持grpc 1.13+版本 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-15 | ||
**/ | ||
public class GrpcClientDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "io.grpc.stub.AbstractStub"; | ||
|
||
private static final int CONSTRUCTOR_PARAM_COUNT = 2; | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.nameEquals(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{ | ||
InterceptDeclarer.build(MethodMatcher.isConstructor(). | ||
and(MethodMatcher.paramCountEquals(CONSTRUCTOR_PARAM_COUNT)), | ||
new GrpcClientInterceptor()) | ||
}; | ||
} | ||
} |
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()) | ||
}; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
...huaweicloud/sermant/tag/transmission/interceptors/rpc/grpc/ClientMetadataInterceptor.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,66 @@ | ||
/* | ||
* 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.utils.MapUtils; | ||
import com.huaweicloud.sermant.core.utils.tag.TrafficTag; | ||
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; | ||
|
||
import io.grpc.CallOptions; | ||
import io.grpc.Channel; | ||
import io.grpc.ClientCall; | ||
import io.grpc.ClientInterceptor; | ||
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; | ||
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; | ||
import io.grpc.Metadata; | ||
import io.grpc.MethodDescriptor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* grpc内部的client interceptor, 将流量标签注入grpc的header中 | ||
* | ||
* @author daizhenyu | ||
* @since 2023-08-15 | ||
**/ | ||
public class ClientMetadataInterceptor implements ClientInterceptor { | ||
@Override | ||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, | ||
CallOptions callOptions, Channel next) { | ||
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { | ||
@Override | ||
public void start(Listener<RespT> responseListener, Metadata headers) { | ||
// 注入流量标签 | ||
TrafficTag trafficTag = TrafficUtils.getTrafficTag(); | ||
if (headers != null || trafficTag == null || MapUtils.isEmpty(trafficTag.getTag())) { | ||
insertTag2Header(headers, TrafficUtils.getTrafficTag().getTag()); | ||
} | ||
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) { }, headers); | ||
} | ||
}; | ||
} | ||
|
||
private void insertTag2Header(Metadata header, Map<String, List<String>> tag) { | ||
for (Map.Entry<String, List<String>> entry : tag.entrySet()) { | ||
if (entry.getKey() == null || entry.getValue() == null) { | ||
continue; | ||
} | ||
header.put(Metadata.Key.of(entry.getKey(), Metadata.ASCII_STRING_MARSHALLER), entry.getValue().get(0)); | ||
} | ||
} | ||
} |
Oops, something went wrong.