diff --git a/.github/workflows/Codecov.yml b/.github/workflows/Codecov.yml index be73e7663d..6d11710095 100644 --- a/.github/workflows/Codecov.yml +++ b/.github/workflows/Codecov.yml @@ -38,9 +38,9 @@ jobs: sh apache-servicecomb-service-center-2.1.0-linux-amd64/start-service-center.sh - name: download zookeeper run: | - curl -o apache-zookeeper-3.8.0-bin.tar.gz -L https://dlcdn.apache.org/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz - tar -zxf apache-zookeeper-3.8.0-bin.tar.gz - bash apache-zookeeper-3.8.0-bin/bin/zkServer.sh start apache-zookeeper-3.8.0-bin/conf/zoo_sample.cfg + curl -o apache-zookeeper-3.6.3-bin.tar.gz -L https://archive.apache.org/dist/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz + tar -zxf apache-zookeeper-3.6.3-bin.tar.gz + bash apache-zookeeper-3.6.3-bin/bin/zkServer.sh start apache-zookeeper-3.6.3-bin/conf/zoo_sample.cfg - name: Build with Maven run: mvn test - name: Generate code coverage report diff --git a/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml b/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml index e6c64e0c87..c2204a096a 100644 --- a/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml +++ b/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml @@ -10,6 +10,7 @@ plugins: - mq-consume-deny - service-removal - service-visibility + - tag-transmission profiles: cse: - flowcontrol diff --git a/sermant-agentcore/sermant-agentcore-config/config/test/plugins.yaml b/sermant-agentcore/sermant-agentcore-config/config/test/plugins.yaml index e6c64e0c87..c2204a096a 100644 --- a/sermant-agentcore/sermant-agentcore-config/config/test/plugins.yaml +++ b/sermant-agentcore/sermant-agentcore-config/config/test/plugins.yaml @@ -10,6 +10,7 @@ plugins: - mq-consume-deny - service-removal - service-visibility + - tag-transmission profiles: cse: - flowcontrol diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/CollectionUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/CollectionUtils.java new file mode 100644 index 0000000000..907cf9d08b --- /dev/null +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/CollectionUtils.java @@ -0,0 +1,40 @@ +/* + * 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 C¬ONDITIONS 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; + +import java.util.Collection; + +/** + * 集合工具类 + * + * @author lilai + * @since 2023-07-17 + */ +public class CollectionUtils { + private CollectionUtils() { + } + + /** + * 判断集合是否为null或没有元素 + * + * @param collection 集合 + * @return 是否为空 + */ + public static boolean isEmpty(Collection collection) { + return collection == null || collection.isEmpty(); + } +} \ No newline at end of file diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/MapUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/MapUtils.java index 155cf1b3fa..07d6bd7f9c 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/MapUtils.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/MapUtils.java @@ -60,4 +60,14 @@ public static void resolveNestMap(Map result, Map map) { + return map == null || map.isEmpty(); + } } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficTag.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficTag.java new file mode 100644 index 0000000000..511081e5e1 --- /dev/null +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficTag.java @@ -0,0 +1,55 @@ +/* + * 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 C¬ONDITIONS 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 com.huaweicloud.sermant.core.utils.MapUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 流量标签 + * + * @author lilai + * @since 2023-07-17 + */ +public class TrafficTag { + private final Map> tag; + + /** + * 构造方法 + * + * @param tag 流量标签 http请求的header/dubbo请求的attachment/消息队列header或properties + */ + public TrafficTag(Map> tag) { + this.tag = MapUtils.isEmpty(tag) ? new HashMap<>() : tag; + } + + public Map> getTag() { + return tag; + } + + /** + * 更新流量标签 + * + * @param map 流量标签 + */ + public void updateTag(Map> map) { + this.tag.putAll(map); + } +} diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficUtils.java new file mode 100644 index 0000000000..e04c2c9984 --- /dev/null +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/tag/TrafficUtils.java @@ -0,0 +1,80 @@ +/* + * 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 C¬ONDITIONS 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 com.huaweicloud.sermant.core.utils.MapUtils; + +import java.util.List; +import java.util.Map; + +/** + * 流量中包含的请求信息 + * + * @author lilai + * @since 2023-07-17 + */ +public class TrafficUtils { + private static final ThreadLocal TAG = new ThreadLocal<>(); + + private TrafficUtils() { + } + + /** + * 获取线程中的流量标签 + * + * @return 流量标签 + */ + public static TrafficTag getTrafficTag() { + return TAG.get(); + } + + /** + * 更新线程中的流量标签 + * + * @param tag 流量标签map + */ + public static void updateTrafficTag(Map> tag) { + if (MapUtils.isEmpty(tag)) { + return; + } + TrafficTag trafficTag = TAG.get(); + if (trafficTag == null) { + TAG.set(new TrafficTag(tag)); + return; + } + trafficTag.updateTag(tag); + } + + /** + * 重设线程中的流量标签 + * + * @param trafficTag 流量标签 + */ + public static void setTrafficTag(TrafficTag trafficTag) { + if (trafficTag == null) { + return; + } + TAG.set(trafficTag); + } + + /** + * 删除线程变量 + */ + public static void removeTrafficTag() { + TAG.remove(); + } +} diff --git a/sermant-common/src/main/java/com/huawei/common/Application.java b/sermant-common/src/main/java/com/huaweicloud/common/Application.java similarity index 95% rename from sermant-common/src/main/java/com/huawei/common/Application.java rename to sermant-common/src/main/java/com/huaweicloud/common/Application.java index 97ad12659b..bef745a1d5 100644 --- a/sermant-common/src/main/java/com/huawei/common/Application.java +++ b/sermant-common/src/main/java/com/huaweicloud/common/Application.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huawei.common; +package com.huaweicloud.common; /** * JavaDoc启动类 diff --git a/sermant-plugins/pom.xml b/sermant-plugins/pom.xml index f2d4e19b3c..b72f9041fa 100644 --- a/sermant-plugins/pom.xml +++ b/sermant-plugins/pom.xml @@ -41,6 +41,7 @@ sermant-service-visibility sermant-service-removal sermant-spring-beans-deal + sermant-tag-transmission @@ -72,6 +73,7 @@ sermant-service-visibility sermant-service-removal sermant-spring-beans-deal + sermant-tag-transmission @@ -103,6 +105,7 @@ sermant-service-visibility sermant-service-removal sermant-spring-beans-deal + sermant-tag-transmission diff --git a/sermant-plugins/sermant-tag-transmission/config/config.yaml b/sermant-plugins/sermant-tag-transmission/config/config.yaml new file mode 100644 index 0000000000..3e0f5d5022 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/config/config.yaml @@ -0,0 +1,3 @@ +tag.transmission.plugin: + enabled: true + tagKeys: [id,name] \ No newline at end of file diff --git a/sermant-plugins/sermant-tag-transmission/pom.xml b/sermant-plugins/sermant-tag-transmission/pom.xml new file mode 100644 index 0000000000..8c5c66968f --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/pom.xml @@ -0,0 +1,50 @@ + + + + sermant-plugins + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + sermant-tag-transmission + pom + + + tag-transmission-plugin + + + + 8 + 8 + ${pom.basedir}/../../.. + tag-transmission + + + + + agent + + true + + + tag-transmission-plugin + + + + test + + tag-transmission-plugin + + + + release + + tag-transmission-plugin + + + + + \ No newline at end of file diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml new file mode 100644 index 0000000000..df1c0f85ff --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml @@ -0,0 +1,50 @@ + + + + sermant-tag-transmission + com.huaweicloud.sermant + 1.0.0 + + 4.0.0 + + tag-transmission-plugin + + + 8 + 8 + false + plugin + 4.3 + + + + + com.huaweicloud.sermant + sermant-agentcore-core + provided + + + com.huaweicloud.sermant + sermant-common + provided + + + org.apache.httpcomponents + httpclient + ${apache-httpclient.version} + provided + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + + + + \ No newline at end of file diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java new file mode 100644 index 0000000000..edde96f0be --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/config/TagTransmissionConfig.java @@ -0,0 +1,58 @@ +/* + * 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.ConfigTypeKey; +import com.huaweicloud.sermant.core.plugin.config.PluginConfig; + +import java.util.ArrayList; +import java.util.List; + +/** + * 流量标签透传插件配置 + * + * @author lilai + * @since 2023-07-17 + */ +@ConfigTypeKey("tag.transmission.plugin") +public class TagTransmissionConfig implements PluginConfig { + /** + * 是否开启适配 + */ + private boolean enabled; + + /** + * 需要透传的标签的key + */ + private List tagKeys = new ArrayList<>(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public List getTagKeys() { + return tagKeys; + } + + public void setTagKeys(List tagKeys) { + this.tagKeys = tagKeys; + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/declarers/HttpClient4xDeclarer.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/declarers/HttpClient4xDeclarer.java new file mode 100644 index 0000000000..c1ef760f48 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/declarers/HttpClient4xDeclarer.java @@ -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; + +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.HttpClient4xInterceptor; + +/** + * HttpClient 流量标签透传的增强声明, 仅针对4.x版本 + * + * @author lilai + * @since 2023-07-17 + */ +public class HttpClient4xDeclarer extends AbstractPluginDeclarer { + /** + * 增强类的全限定名 http请求 + */ + private static final String[] ENHANCE_CLASSES = { + "org.apache.http.impl.client.AbstractHttpClient", + "org.apache.http.impl.client.DefaultRequestDirector", + "org.apache.http.impl.client.InternalHttpClient", + "org.apache.http.impl.client.MinimalHttpClient" + }; + + /** + * 拦截类的全限定名 + */ + private static final String INTERCEPT_CLASS = HttpClient4xInterceptor.class.getCanonicalName(); + + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameContains(ENHANCE_CLASSES); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.nameContains("doExecute", "execute") + .and(MethodMatcher.paramTypesEqual( + "org.apache.http.HttpHost", + "org.apache.http.HttpRequest", + "org.apache.http.protocol.HttpContext")), + INTERCEPT_CLASS) + }; + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpClient4xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpClient4xInterceptor.java new file mode 100644 index 0000000000..ddc69e9081 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpClient4xInterceptor.java @@ -0,0 +1,78 @@ +/* + * 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; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import com.huaweicloud.sermant.core.utils.CollectionUtils; +import com.huaweicloud.sermant.core.utils.tag.TrafficTag; +import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.config.TagTransmissionConfig; + +import org.apache.http.HttpRequest; + +import java.util.List; + +/** + * HttpClient 流量标签透传的拦截器, 仅针对4.x版本 + * + * @author lilai + * @since 2023-07-17 + */ +public class HttpClient4xInterceptor extends AbstractInterceptor { + private final TagTransmissionConfig tagTransmissionConfig; + + /** + * 构造器 + */ + public HttpClient4xInterceptor() { + tagTransmissionConfig = PluginConfigManager.getPluginConfig(TagTransmissionConfig.class); + } + + @Override + public ExecuteContext before(ExecuteContext context) { + if (!tagTransmissionConfig.isEnabled()) { + return context; + } + + TrafficTag trafficTag = TrafficUtils.getTrafficTag(); + if (trafficTag == null) { + return context; + } + + Object httpRequestObject = context.getArguments()[1]; + if (httpRequestObject instanceof HttpRequest) { + final HttpRequest httpRequest = (HttpRequest) httpRequestObject; + for (String key : tagTransmissionConfig.getTagKeys()) { + List values = trafficTag.getTag().get(key); + if (CollectionUtils.isEmpty(values)) { + continue; + } + for (String value : values) { + httpRequest.addHeader(key, value); + } + } + } + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + return context; + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer new file mode 100644 index 0000000000..d31f054de7 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -0,0 +1,18 @@ +# +# 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. +# +# +com.huaweicloud.sermant.tag.transmission.declarers.HttpClient4xDeclarer + diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig new file mode 100644 index 0000000000..01cfa4275b --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig @@ -0,0 +1,17 @@ +# +# 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. +# + +com.huaweicloud.sermant.tag.transmission.config.TagTransmissionConfig \ No newline at end of file