-
Notifications
You must be signed in to change notification settings - Fork 24
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
1 parent
6c93275
commit 6a62e1f
Showing
5 changed files
with
231 additions
and
10 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...va/com/jd/live/agent/plugin/transmission/jdkhttp/definition/JavaHttpClientDefinition.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,68 @@ | ||
/* | ||
* Copyright © ${year} ${owner} (${email}) | ||
* | ||
* 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.jd.live.agent.plugin.transmission.jdkhttp.definition; | ||
|
||
import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder; | ||
import com.jd.live.agent.core.extension.annotation.ConditionalOnClass; | ||
import com.jd.live.agent.core.extension.annotation.ConditionalOnProperty; | ||
import com.jd.live.agent.core.extension.annotation.Extension; | ||
import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter; | ||
import com.jd.live.agent.core.plugin.definition.PluginDefinition; | ||
import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter; | ||
import com.jd.live.agent.governance.config.GovernanceConfig; | ||
import com.jd.live.agent.plugin.transmission.jdkhttp.interceptor.JavaHttpClientInterceptor; | ||
|
||
/** | ||
* Defines the instrumentation for the Java HTTP Client's HttpRequestBuilderImpl class. | ||
* This class specifies the conditions under which the {@link JavaHttpClientInterceptor} | ||
* is applied to modify or monitor HTTP requests during their construction. | ||
* | ||
* <p>The interceptor is conditionally applied based on the presence of the HttpRequestBuilderImpl | ||
* class and the configuration specified by {@link GovernanceConfig#CONFIG_TRANSMISSION_ENABLED}. | ||
* This allows for dynamic enabling or disabling of this instrumentation based on runtime configuration.</p> | ||
* | ||
* <p>Annotations used:</p> | ||
* <ul> | ||
* <li>{@link Extension} - Marks this class as an extension with a specific purpose within the framework, | ||
* allowing it to be automatically discovered and applied.</li> | ||
* <li>{@link ConditionalOnProperty} - Controls whether this plugin is active based on the | ||
* {@code CONFIG_TRANSMISSION_ENABLED} configuration property.</li> | ||
* <li>{@link ConditionalOnClass} - Ensures that this plugin is only loaded if HttpRequestBuilderImpl | ||
* class is available in the runtime environment, preventing class loading issues in environments | ||
* with different Java versions or configurations.</li> | ||
* </ul> | ||
* | ||
* @see PluginDefinitionAdapter | ||
* @see JavaHttpClientInterceptor | ||
*/ | ||
@Extension(value = "JavaHttpClientDefinition", order = PluginDefinition.ORDER_TRANSMISSION) | ||
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_TRANSMISSION_ENABLED, matchIfMissing = true) | ||
@ConditionalOnClass(JavaHttpClientDefinition.TYPE_HTTP_REQUEST_BUILDER_IMPL) | ||
public class JavaHttpClientDefinition extends PluginDefinitionAdapter { | ||
|
||
protected static final String TYPE_HTTP_REQUEST_BUILDER_IMPL = "jdk.internal.net.http.HttpRequestBuilderImpl"; | ||
|
||
private static final String METHOD_BUILD = "build"; | ||
|
||
private static final String METHOD_BUILD_FOR_WEBSOCKET = "buildForWebSocket"; | ||
|
||
public JavaHttpClientDefinition() { | ||
super(MatcherBuilder.named(TYPE_HTTP_REQUEST_BUILDER_IMPL), | ||
new InterceptorDefinitionAdapter( | ||
MatcherBuilder.in(METHOD_BUILD, METHOD_BUILD_FOR_WEBSOCKET), | ||
new JavaHttpClientInterceptor())); | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
.../com/jd/live/agent/plugin/transmission/jdkhttp/interceptor/JavaHttpClientInterceptor.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,91 @@ | ||
/* | ||
* Copyright © ${year} ${owner} (${email}) | ||
* | ||
* 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.jd.live.agent.plugin.transmission.jdkhttp.interceptor; | ||
|
||
import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext; | ||
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; | ||
import com.jd.live.agent.governance.context.RequestContext; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
import static com.jd.live.agent.core.util.type.ClassUtils.describe; | ||
|
||
/** | ||
* Interceptor for the Java HTTP Client's request builder implementation. | ||
* This class uses reflection to access and modify the headers of HTTP requests | ||
* constructed by the internal {@code HttpRequestBuilderImpl} class. | ||
* | ||
* <p>Due to module encapsulation in Java 9 and above, direct access to the internal | ||
* HTTP request builder is not possible. This interceptor bypasses these restrictions | ||
* to allow header manipulation for outgoing HTTP requests.</p> | ||
* | ||
* <p>Note: This interceptor is designed to work with specific versions of the JDK | ||
* where {@code HttpRequestBuilderImpl} and its method {@code setHeader} are present.</p> | ||
*/ | ||
public class JavaHttpClientInterceptor extends InterceptorAdaptor { | ||
|
||
private static final String TYPE_HTTP_REQUEST_BUILDER_IMPL = "jdk.internal.net.http.HttpRequestBuilderImpl"; | ||
|
||
private static final String METHOD_SET_HEADER = "setHeader"; | ||
|
||
private final Method method; | ||
|
||
public JavaHttpClientInterceptor() { | ||
// use reflect to avoid module error in java 17. | ||
Method method = null; | ||
try { | ||
// Use reflection to get the setHeader method from HttpRequestBuilderImpl | ||
List<Method> methods = describe(Class.forName(TYPE_HTTP_REQUEST_BUILDER_IMPL)) | ||
.getMethodList() | ||
.getMethods(METHOD_SET_HEADER); | ||
method = methods == null || methods.isEmpty() ? null : methods.get(0); | ||
} catch (ClassNotFoundException ignore) { | ||
} | ||
this.method = method; | ||
} | ||
|
||
@Override | ||
public void onEnter(ExecutableContext ctx) { | ||
if (method != null) { | ||
attachTag(ctx.getTarget()); | ||
} | ||
} | ||
|
||
/** | ||
* Attaches tags to the HTTP header using the {@code setHeader} method. | ||
* | ||
* @param header The HTTP request header object to which the tags will be attached. | ||
*/ | ||
private void attachTag(Object header) { | ||
RequestContext.traverse((key, value) -> addHeader(header, key, value)); | ||
} | ||
|
||
/** | ||
* Adds a single header to the HTTP request. | ||
* | ||
* @param header The HTTP request header object. | ||
* @param key The header key. | ||
* @param value The header value. | ||
*/ | ||
private void addHeader(Object header, String key, String value) { | ||
try { | ||
method.invoke(header, key, value); | ||
} catch (Throwable ignore) { | ||
} | ||
} | ||
|
||
} |
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
3 changes: 2 additions & 1 deletion
3
...ain/resources/META-INF/services/com.jd.live.agent.core.plugin.definition.PluginDefinition
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 +1,2 @@ | ||
com.jd.live.agent.plugin.transmission.jdkhttp.definition.JdkHttpClientDefinition | ||
com.jd.live.agent.plugin.transmission.jdkhttp.definition.SunHttpClientDefinition | ||
com.jd.live.agent.plugin.transmission.jdkhttp.definition.JavaHttpClientDefinition |