Skip to content

Commit

Permalink
Merge pull request #1251 from lilai23/tag_trans
Browse files Browse the repository at this point in the history
【feature】流量标签透传特性:支持httpclient4.x
  • Loading branch information
Sherlockhan authored Jul 18, 2023
2 parents 3fb63d0 + 8de452d commit 8cdb26b
Show file tree
Hide file tree
Showing 17 changed files with 531 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/Codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins:
- mq-consume-deny
- service-removal
- service-visibility
- tag-transmission
profiles:
cse:
- flowcontrol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins:
- mq-consume-deny
- service-removal
- service-visibility
- tag-transmission
profiles:
cse:
- flowcontrol
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public static void resolveNestMap(Map<String, Object> result, Map<String, Object
}
}
}

/**
* 判断Map是否为null或没有键值对
*
* @param map map
* @return 是否为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> tag;

/**
* 构造方法
*
* @param tag 流量标签 http请求的header/dubbo请求的attachment/消息队列header或properties
*/
public TrafficTag(Map<String, List<String>> tag) {
this.tag = MapUtils.isEmpty(tag) ? new HashMap<>() : tag;
}

public Map<String, List<String>> getTag() {
return tag;
}

/**
* 更新流量标签
*
* @param map 流量标签
*/
public void updateTag(Map<String, List<String>> map) {
this.tag.putAll(map);
}
}
Original file line number Diff line number Diff line change
@@ -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<TrafficTag> TAG = new ThreadLocal<>();

private TrafficUtils() {
}

/**
* 获取线程中的流量标签
*
* @return 流量标签
*/
public static TrafficTag getTrafficTag() {
return TAG.get();
}

/**
* 更新线程中的流量标签
*
* @param tag 流量标签map
*/
public static void updateTrafficTag(Map<String, List<String>> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.huawei.common;
package com.huaweicloud.common;

/**
* JavaDoc启动类
Expand Down
3 changes: 3 additions & 0 deletions sermant-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<module>sermant-service-visibility</module>
<module>sermant-service-removal</module>
<module>sermant-spring-beans-deal</module>
<module>sermant-tag-transmission</module>
</modules>
<build>
<pluginManagement>
Expand Down Expand Up @@ -72,6 +73,7 @@
<module>sermant-service-visibility</module>
<module>sermant-service-removal</module>
<module>sermant-spring-beans-deal</module>
<module>sermant-tag-transmission</module>
</modules>
<build>
<pluginManagement>
Expand Down Expand Up @@ -103,6 +105,7 @@
<module>sermant-service-visibility</module>
<module>sermant-service-removal</module>
<module>sermant-spring-beans-deal</module>
<module>sermant-tag-transmission</module>
</modules>
</profile>
</profiles>
Expand Down
3 changes: 3 additions & 0 deletions sermant-plugins/sermant-tag-transmission/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tag.transmission.plugin:
enabled: true
tagKeys: [id,name]
50 changes: 50 additions & 0 deletions sermant-plugins/sermant-tag-transmission/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sermant-plugins</artifactId>
<groupId>com.huaweicloud.sermant</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sermant-tag-transmission</artifactId>
<packaging>pom</packaging>

<modules>
<module>tag-transmission-plugin</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<sermant.basedir>${pom.basedir}/../../..</sermant.basedir>
<package.plugin.name>tag-transmission</package.plugin.name>
</properties>

<profiles>
<profile>
<id>agent</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>tag-transmission-plugin</module>
</modules>
</profile>
<profile>
<id>test</id>
<modules>
<module>tag-transmission-plugin</module>
</modules>
</profile>
<profile>
<id>release</id>
<modules>
<module>tag-transmission-plugin</module>
</modules>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sermant-tag-transmission</artifactId>
<groupId>com.huaweicloud.sermant</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>tag-transmission-plugin</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<config.skip.flag>false</config.skip.flag>
<package.plugin.type>plugin</package.plugin.type>
<apache-httpclient.version>4.3</apache-httpclient.version>
</properties>

<dependencies>
<dependency>
<groupId>com.huaweicloud.sermant</groupId>
<artifactId>sermant-agentcore-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud.sermant</groupId>
<artifactId>sermant-common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache-httpclient.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 8cdb26b

Please sign in to comment.