Skip to content

Commit

Permalink
add FeignClient/RestTemplate invoke tag transmission plugin
Browse files Browse the repository at this point in the history
Signed-off-by: chengyouling <spadgerlin888@163.com>
  • Loading branch information
chengyouling committed Jan 10, 2025
1 parent abfc06b commit 74a0bf8
Show file tree
Hide file tree
Showing 11 changed files with 637 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# 'plugins' is used to configure static plugins. The plugins take effect when Sermant starts by premain. Plugins that support static installation are configured and cannot be uninstalled
plugins:
- flowcontrol
- tag-transmission
- service-router
- service-registry
- loadbalancer
Expand All @@ -16,7 +17,6 @@ plugins:
- mq-consume-prohibition
- service-removal
- service-visibility
- tag-transmission
- database-write-prohibition
- mq-grayscale
# 'dynamicPlugins' is used to configure plugins that support dynamic installation. The plugins take effect when Sermant starts by agentmain. allows uninstallation:
Expand Down
3 changes: 3 additions & 0 deletions sermant-plugins/sermant-tag-transmission/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<module>tag-transmission-dubbo2.7.x-plugin</module>
<module>tag-transmission-dubbo3.x-plugin</module>
<module>tag-transmission-service</module>
<module>tag-transmission-rpc-plugin</module>
</modules>
</profile>
<profile>
Expand All @@ -75,6 +76,7 @@
<module>tag-transmission-dubbo2.7.x-plugin</module>
<module>tag-transmission-dubbo3.x-plugin</module>
<module>tag-transmission-service</module>
<module>tag-transmission-rpc-plugin</module>
</modules>
</profile>
<profile>
Expand All @@ -97,6 +99,7 @@
<module>tag-transmission-dubbo2.7.x-plugin</module>
<module>tag-transmission-dubbo3.x-plugin</module>
<module>tag-transmission-service</module>
<module>tag-transmission-rpc-plugin</module>
</modules>
</profile>
</profiles>
Expand Down
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>
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())
};
}
}
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())
};
}
}
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;
}
}
Loading

0 comments on commit 74a0bf8

Please sign in to comment.