-
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.
add FeignClient/RestTemplate invoke tag transmission plugin
Signed-off-by: chengyouling <spadgerlin888@163.com>
- Loading branch information
1 parent
abfc06b
commit 74a0bf8
Showing
11 changed files
with
637 additions
and
1 deletion.
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
75 changes: 75 additions & 0 deletions
75
sermant-plugins/sermant-tag-transmission/tag-transmission-rpc-plugin/pom.xml
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,75 @@ | ||
<?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> | ||
<groupId>io.sermant</groupId> | ||
<artifactId>sermant-tag-transmission</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>tag-transmission-rpc-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> | ||
<spring-web.version>5.3.30</spring-web.version> | ||
<feign-core.version>9.5.0</feign-core.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.sermant</groupId> | ||
<artifactId>sermant-agentcore-core</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.sermant</groupId> | ||
<artifactId>tag-transmission-common</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>${spring-web.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.github.openfeign</groupId> | ||
<artifactId>feign-core</artifactId> | ||
<version>${feign-core.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-inline</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
48 changes: 48 additions & 0 deletions
48
...in/src/main/java/io/sermant/tag/transmission/rpc/declarers/ClientHttpRequestDeclarer.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,48 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.tag.transmission.rpc.declarers; | ||
|
||
import io.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; | ||
import io.sermant.core.plugin.agent.declarer.InterceptDeclarer; | ||
import io.sermant.core.plugin.agent.matcher.ClassMatcher; | ||
import io.sermant.core.plugin.agent.matcher.MethodMatcher; | ||
import io.sermant.tag.transmission.rpc.interceptors.ClientHttpRequestTagTransmissionInterceptor; | ||
|
||
/** | ||
* ClientHttpRequest enhancement class, initiate restTemplate request method | ||
* | ||
* @author chengyouling | ||
* @since 2024-12-30 | ||
*/ | ||
public class ClientHttpRequestDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "org.springframework.http.client.ClientHttpRequest"; | ||
|
||
private static final String METHOD_NAME = "execute"; | ||
|
||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.isExtendedFrom(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{ | ||
InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME), | ||
new ClientHttpRequestTagTransmissionInterceptor()) | ||
}; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...c-plugin/src/main/java/io/sermant/tag/transmission/rpc/declarers/FeignClientDeclarer.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,51 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.tag.transmission.rpc.declarers; | ||
|
||
import io.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; | ||
import io.sermant.core.plugin.agent.declarer.InterceptDeclarer; | ||
import io.sermant.core.plugin.agent.matcher.ClassMatcher; | ||
import io.sermant.core.plugin.agent.matcher.MethodMatcher; | ||
import io.sermant.tag.transmission.rpc.interceptors.FeignClientTagTransmissionInterceptor; | ||
|
||
/** | ||
* The client enhancement class initiates the feign request method | ||
* | ||
* @author chengyouling | ||
* @since 2024-12-30 | ||
*/ | ||
public class FeignClientDeclarer extends AbstractPluginDeclarer { | ||
private static final String ENHANCE_CLASS = "feign.Client"; | ||
|
||
private static final String METHOD_NAME = "execute"; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return ClassMatcher.isExtendedFrom(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{ | ||
InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME), | ||
new FeignClientTagTransmissionInterceptor()) | ||
}; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...ermant/tag/transmission/rpc/interceptors/ClientHttpRequestTagTransmissionInterceptor.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,89 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.tag.transmission.rpc.interceptors; | ||
|
||
import io.sermant.core.common.LoggerFactory; | ||
import io.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import io.sermant.core.plugin.agent.interceptor.AbstractInterceptor; | ||
import io.sermant.core.utils.CollectionUtils; | ||
import io.sermant.core.utils.tag.TrafficUtils; | ||
import io.sermant.tag.transmission.config.strategy.TagKeyMatcher; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpRequest; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* ClientHttpRequestInterceptor enhancement class, initiate restTemplate request method | ||
* | ||
* @author chengyouling | ||
* @since 2024-12-30 | ||
*/ | ||
public class ClientHttpRequestTagTransmissionInterceptor extends AbstractInterceptor { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
@Override | ||
public ExecuteContext before(ExecuteContext context) { | ||
if (TrafficUtils.getTrafficTag() == null || TrafficUtils.getTrafficTag().getTag() == null) { | ||
return context; | ||
} | ||
Object obj = context.getObject(); | ||
if (obj instanceof HttpRequest) { | ||
HttpRequest request = (HttpRequest) obj; | ||
HttpHeaders headers = request.getHeaders(); | ||
for (Map.Entry<String, List<String>> entry : TrafficUtils.getTrafficTag().getTag().entrySet()) { | ||
String key = entry.getKey(); | ||
if (!TagKeyMatcher.isMatch(key)) { | ||
continue; | ||
} | ||
List<String> values = entry.getValue(); | ||
|
||
// The server side converts the label value to list storage when it is not null. If it is null, | ||
// it directly puts null. Therefore, if the client side values are empty, they must be null. | ||
if (CollectionUtils.isEmpty(values)) { | ||
headers.set(key, null); | ||
LOGGER.log(Level.FINE, "Traffic tag {0} have been injected to httpclient.", entry); | ||
continue; | ||
} | ||
for (String value : values) { | ||
if (!CollectionUtils.isEmpty(headers.get(key))) { | ||
headers.get(key).add(value); | ||
} else { | ||
headers.set(key, value); | ||
} | ||
} | ||
LOGGER.log(Level.FINE, "Traffic tag {0}={1} have been injected to httpclient.", new Object[]{key, | ||
values}); | ||
} | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
public ExecuteContext after(ExecuteContext context) { | ||
return context; | ||
} | ||
|
||
@Override | ||
public ExecuteContext onThrow(ExecuteContext context) { | ||
return context; | ||
} | ||
} |
Oops, something went wrong.