-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
支持跨线程传递标签,该能力可单独使用,可与路由等其他插件配合传递请求信息。
- Loading branch information
Showing
26 changed files
with
1,082 additions
and
20 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
62 changes: 62 additions & 0 deletions
62
...mant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficData.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,62 @@ | ||
/* | ||
* 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.core.utils.tag; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* 流量相关新信息 | ||
* | ||
* @author lilai | ||
* @since 2023-07-26 | ||
*/ | ||
public class TrafficData extends TrafficTag { | ||
private final String path; | ||
|
||
private final String httpMethod; | ||
|
||
/** | ||
* 构造方法 | ||
* | ||
* @param header 请求头/attachments | ||
* @param path 请求路径 | ||
* @param httpMethod 请求方法 | ||
*/ | ||
public TrafficData(Map<String, List<String>> header, String path, String httpMethod) { | ||
super(header); | ||
this.path = path; | ||
this.httpMethod = httpMethod; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public String getHttpMethod() { | ||
return httpMethod; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "{" | ||
+ "path='" + path + '\'' | ||
+ ", httpMethod='" + httpMethod + '\'' | ||
+ ", tag='" + getTag() + '\'' | ||
+ '}'; | ||
} | ||
} |
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
16 changes: 14 additions & 2 deletions
16
sermant-plugins/sermant-tag-transmission/config/config.yaml
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 |
---|---|---|
@@ -1,3 +1,15 @@ | ||
tag.transmission.plugin: | ||
# 流量标签在各种通道间(http/rpc/消息队列等)传递的配置 | ||
tag.transmission.config: | ||
# 是否开启流量标签透传 | ||
enabled: true | ||
tagKeys: [id,name] | ||
# 需要透传的流量标签的key | ||
tagKeys: [id,name] | ||
|
||
# 跨线程传递标签的配置,该能力可单独使用 | ||
crossthread.config: | ||
# 是否在直接new Thread时传递标签 | ||
enabled-thread: true | ||
# 是否在非定时线程池中传递标签 | ||
enabled-thread-pool: true | ||
# 是否在定时线程池的schedule/scheduleAtFixedRate/scheduleWithFixedDelay方法中传递标签 | ||
enabled-scheduler: true |
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
72 changes: 72 additions & 0 deletions
72
...ugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/CrossThreadConfig.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,72 @@ | ||
/* | ||
* 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.config; | ||
|
||
import com.huaweicloud.sermant.core.config.common.ConfigFieldKey; | ||
import com.huaweicloud.sermant.core.config.common.ConfigTypeKey; | ||
import com.huaweicloud.sermant.core.plugin.config.PluginConfig; | ||
|
||
/** | ||
* 跨线程传递开关配置 | ||
* | ||
* @author lilai | ||
* @since 2023-08-02 | ||
*/ | ||
@ConfigTypeKey("crossthread.config") | ||
public class CrossThreadConfig implements PluginConfig { | ||
/** | ||
* 是否在非定时线程池中传递标签 | ||
*/ | ||
@ConfigFieldKey("enabled-thread-pool") | ||
private boolean enabledThreadPool; | ||
|
||
/** | ||
* 是否在定时线程池的schedule/scheduleAtFixedRate/scheduleWithFixedDelay方法中传递标签 | ||
*/ | ||
@ConfigFieldKey("enabled-scheduler") | ||
private boolean enabledScheduler; | ||
|
||
/** | ||
* 是否在直接new Thread时传递标签 | ||
*/ | ||
@ConfigFieldKey("enabled-thread") | ||
private boolean enabledThread; | ||
|
||
public boolean isEnabledThread() { | ||
return enabledThread; | ||
} | ||
|
||
public void setEnabledThread(boolean enabledThread) { | ||
this.enabledThread = enabledThread; | ||
} | ||
|
||
public boolean isEnabledThreadPool() { | ||
return enabledThreadPool; | ||
} | ||
|
||
public void setEnabledThreadPool(boolean enabledThreadPool) { | ||
this.enabledThreadPool = enabledThreadPool; | ||
} | ||
|
||
public boolean isEnabledScheduler() { | ||
return enabledScheduler; | ||
} | ||
|
||
public void setEnabledScheduler(boolean enabledScheduler) { | ||
this.enabledScheduler = enabledScheduler; | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...java/com/huaweicloud/sermant/tag/transmission/declarers/crossthread/ExecutorDeclarer.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,63 @@ | ||
/* | ||
* 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.crossthread; | ||
|
||
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.core.plugin.config.PluginConfigManager; | ||
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; | ||
import com.huaweicloud.sermant.tag.transmission.config.CrossThreadConfig; | ||
import com.huaweicloud.sermant.tag.transmission.interceptors.crossthread.ExecutorInterceptor; | ||
|
||
/** | ||
* 拦截Executor | ||
* | ||
* @author provenceee | ||
* @since 2023-04-20 | ||
*/ | ||
public class ExecutorDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "java.util.concurrent.Executor"; | ||
|
||
private static final String INTERCEPT_CLASS = ExecutorInterceptor.class.getCanonicalName(); | ||
|
||
private static final String[] METHOD_NAME = {"execute", "submit"}; | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.isExtendedFrom(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{ | ||
InterceptDeclarer.build(MethodMatcher.nameContains(METHOD_NAME).and(MethodMatcher.isPublicMethod()), | ||
INTERCEPT_CLASS) | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
CrossThreadConfig config = PluginConfigManager.getPluginConfig(CrossThreadConfig.class); | ||
if (config.isEnabledThread()) { | ||
TrafficUtils.setInheritableThreadLocal(); | ||
return true; | ||
} | ||
return config.isEnabledThreadPool(); | ||
} | ||
} |
Oops, something went wrong.