-
Notifications
You must be signed in to change notification settings - Fork 672
Add the jdk httpclient plugin #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
a55f143
fix
peachisai d79fb55
fix
peachisai 711963c
fix
peachisai 6207a80
Fix
peachisai eba26a2
Fix
peachisai 0e30316
Fix
peachisai 508f01b
Fix
peachisai c214ede
Fix
peachisai 3a218aa
Fix
peachisai 9d5205f
Fix
peachisai b7076cc
Fix
peachisai 20eb38a
Fix
peachisai 80e9003
Fix
peachisai 626a629
Fix
peachisai f9411ff
Fix
peachisai 9a8f830
Fix
peachisai 45b14f2
Fix
peachisai 1853b42
Fix
peachisai c091b25
Fix
peachisai 4e415f4
Fix
peachisai 2392140
Fix
peachisai bfd2f4c
Fix
peachisai 4431e59
Fix
peachisai 70ce00c
Fix
peachisai 4aba780
Fix
peachisai 91f3eec
Fix
peachisai f4ed10b
Fix
peachisai 461e90c
Fix
peachisai d8b3072
Fix
peachisai 87aeeab
Fix
peachisai e01f6ad
Merge pull request #15 from peachisai/uat
peachisai 1b72de9
Fix
peachisai b394856
Fix
peachisai 2a45fe2
Fix
peachisai 8932f85
Merge pull request #16 from peachisai/uat
peachisai 8387a6c
Fix
peachisai a976175
Fix
peachisai 88e0982
Merge pull request #17 from peachisai/uat
peachisai e29f1d0
Fix
peachisai 64377df
Fix
peachisai e62c17c
Fix
peachisai dcf1013
Fix
peachisai b58fa70
Merge pull request #18 from peachisai/uat
peachisai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
46 changes: 46 additions & 0 deletions
46
apm-sniffer/bootstrap-plugins/jdk-httpclient-plugin/pom.xml
This file contains hidden or 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,46 @@ | ||
| <!-- | ||
| ~ Licensed to the Apache Software Foundation (ASF) under one or more | ||
| ~ contributor license agreements. See the NOTICE file distributed with | ||
| ~ this work for additional information regarding copyright ownership. | ||
| ~ The ASF licenses this file to You 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. | ||
| ~ | ||
| --> | ||
|
|
||
| <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>bootstrap-plugins</artifactId> | ||
| <groupId>org.apache.skywalking</groupId> | ||
| <version>9.6.0-SNAPSHOT</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>apm-jdk-httpclient-plugin</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>apm-jdk-httpclient-plugin</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <maven.compiler.release>11</maven.compiler.release> | ||
| </properties> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-deploy-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
123 changes: 123 additions & 0 deletions
123
...plugin/src/main/java/org/apache/skywalking/apm/plugin/HttpClientSendAsyncInterceptor.java
This file contains hidden or 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,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin; | ||
|
|
||
| import org.apache.skywalking.apm.agent.core.context.ContextCarrier; | ||
| import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
| import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
| import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
| import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
| import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.net.URI; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| public class HttpClientSendAsyncInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
|
||
| @Override | ||
| public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { | ||
| HttpRequest request = (HttpRequest) allArguments[0]; | ||
| URI uri = request.uri(); | ||
|
|
||
| ContextCarrier contextCarrier = new ContextCarrier(); | ||
| AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri)); | ||
|
|
||
| if (request instanceof EnhancedInstance) { | ||
| ((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier); | ||
| } | ||
|
|
||
| span.setComponent(ComponentsDefine.JDK_HTTP); | ||
| Tags.HTTP.METHOD.set(span, request.method()); | ||
| Tags.URL.set(span, String.valueOf(uri)); | ||
| SpanLayer.asHttp(span); | ||
| } | ||
|
|
||
| @Override | ||
| public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { | ||
|
|
||
| if (ContextManager.isActive()) { | ||
| AbstractSpan span = ContextManager.activeSpan(); | ||
| span.prepareForAsync(); | ||
|
|
||
| if (ret != null) { | ||
| CompletableFuture<?> future = (CompletableFuture<?>) ret; | ||
| ret = future.whenComplete((response, throwable) -> { | ||
| try { | ||
| if (throwable != null) { | ||
| span.errorOccurred(); | ||
| span.log(throwable); | ||
| return; | ||
| } | ||
| if (response instanceof HttpResponse) { | ||
| HttpResponse<?> httpResponse = (HttpResponse<?>) response; | ||
| int statusCode = httpResponse.statusCode(); | ||
| Tags.HTTP_RESPONSE_STATUS_CODE.set(span, statusCode); | ||
| if (statusCode >= 400) { | ||
| span.errorOccurred(); | ||
| } | ||
| } | ||
| } finally { | ||
| span.asyncFinish(); | ||
| } | ||
| }); | ||
| } else { | ||
| Map<String, String> eventMap = new HashMap<String, String>(); | ||
| eventMap.put("error", "No response"); | ||
| span.log(System.currentTimeMillis(), eventMap); | ||
| span.errorOccurred(); | ||
| } | ||
| ContextManager.stopSpan(); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { | ||
| if (ContextManager.isActive()) { | ||
| ContextManager.activeSpan().log(t); | ||
| } | ||
| } | ||
|
|
||
| private String buildOperationName(String method, URI uri) { | ||
| String path = uri.getPath(); | ||
| if (path == null || path.isEmpty()) { | ||
| path = "/"; | ||
| } | ||
| return method + ":" + path; | ||
| } | ||
|
|
||
| private String getPeer(URI uri) { | ||
| String host = uri.getHost(); | ||
| int port = uri.getPort(); | ||
|
|
||
| if (port == -1) { | ||
| port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; | ||
| } | ||
|
|
||
| return host + ":" + port; | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
...ient-plugin/src/main/java/org/apache/skywalking/apm/plugin/HttpClientSendInterceptor.java
This file contains hidden or 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,108 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin; | ||
|
|
||
| import org.apache.skywalking.apm.agent.core.context.ContextCarrier; | ||
| import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
| import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
| import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
| import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
| import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.net.URI; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class HttpClientSendInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
|
||
| @Override | ||
| public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { | ||
| HttpRequest request = (HttpRequest) allArguments[0]; | ||
| URI uri = request.uri(); | ||
|
|
||
| ContextCarrier contextCarrier = new ContextCarrier(); | ||
| AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri)); | ||
|
|
||
| if (request instanceof EnhancedInstance) { | ||
| ((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier); | ||
| } | ||
|
|
||
| span.setComponent(ComponentsDefine.JDK_HTTP); | ||
| Tags.HTTP.METHOD.set(span, request.method()); | ||
| Tags.URL.set(span, String.valueOf(uri)); | ||
| SpanLayer.asHttp(span); | ||
| } | ||
|
|
||
| @Override | ||
| public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { | ||
|
|
||
| if (ContextManager.isActive()) { | ||
| AbstractSpan span = ContextManager.activeSpan(); | ||
| if (ret != null) { | ||
| HttpResponse<?> response = (HttpResponse<?>) ret; | ||
| int statusCode = response.statusCode(); | ||
|
|
||
| Tags.HTTP_RESPONSE_STATUS_CODE.set(span, response.statusCode()); | ||
| if (statusCode >= 400) { | ||
| span.errorOccurred(); | ||
| } | ||
| } else { | ||
| Map<String, String> eventMap = new HashMap<String, String>(); | ||
| eventMap.put("error", "No response"); | ||
| span.log(System.currentTimeMillis(), eventMap); | ||
| span.errorOccurred(); | ||
| } | ||
|
|
||
| ContextManager.stopSpan(); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { | ||
| if (ContextManager.isActive()) { | ||
| ContextManager.activeSpan().log(t); | ||
| } | ||
| } | ||
|
|
||
| private String buildOperationName(String method, URI uri) { | ||
| String path = uri.getPath(); | ||
| if (path == null || path.isEmpty()) { | ||
| path = "/"; | ||
| } | ||
| return method + ":" + path; | ||
| } | ||
|
|
||
| private String getPeer(URI uri) { | ||
| String host = uri.getHost(); | ||
| int port = uri.getPort(); | ||
|
|
||
| if (port == -1) { | ||
| port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; | ||
| } | ||
|
|
||
| return host + ":" + port; | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
...-plugin/src/main/java/org/apache/skywalking/apm/plugin/HttpRequestHeadersInterceptor.java
This file contains hidden or 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,62 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin; | ||
|
|
||
| import org.apache.skywalking.apm.agent.core.context.CarrierItem; | ||
| import org.apache.skywalking.apm.agent.core.context.ContextCarrier; | ||
| import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.net.http.HttpHeaders; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class HttpRequestHeadersInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
|
||
| @Override | ||
| public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { | ||
|
|
||
| ContextCarrier contextCarrier = (ContextCarrier) objInst.getSkyWalkingDynamicField(); | ||
| HttpHeaders originalHeaders = (HttpHeaders) ret; | ||
| final Map<String, List<String>> headerMap = new HashMap<>(originalHeaders.map()); | ||
| CarrierItem next = contextCarrier.items(); | ||
| while (next.hasNext()) { | ||
| next = next.next(); | ||
| headerMap.put(next.getHeadKey(), List.of(next.getHeadValue())); | ||
| } | ||
|
|
||
| return HttpHeaders.of(headerMap, (k, v) -> true); | ||
| } | ||
|
|
||
| @Override | ||
| public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { | ||
| if (ContextManager.isActive()) { | ||
| ContextManager.activeSpan().log(t); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same concern here, if there is no return, should not response code. And if there is a null expected, we should log the reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be not resolved? No reponse still maps to 404 response code. Is this correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sry, had done