Skip to content

Commit e01f6ad

Browse files
authored
Merge pull request #15 from peachisai/uat
fix
2 parents 606c563 + 87aeeab commit e01f6ad

25 files changed

Lines changed: 1086 additions & 9 deletions

File tree

.github/workflows/plugins-jdk11-test.3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
matrix:
5656
case:
5757
- jdk11-forkjoinpool-scenario
58+
- jdk-httpclient-scenario
5859
steps:
5960
- uses: actions/checkout@v2
6061
with:

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Release Notes.
1818
* Fix ClassLoader cache OOM issue with WeakHashMap.
1919
* Fix Jetty client cannot receive the HTTP response body.
2020
* Eliminate repeated code with HttpServletRequestWrapper in mvc-annotation-commons.
21+
* Add the jdk httpclient plugin.
2122

2223
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/242?closed=1)
2324

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
~ Licensed to the Apache Software Foundation (ASF) under one or more
3+
~ contributor license agreements. See the NOTICE file distributed with
4+
~ this work for additional information regarding copyright ownership.
5+
~ The ASF licenses this file to You under the Apache License, Version 2.0
6+
~ (the "License"); you may not use this file except in compliance with
7+
~ the License. You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<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">
20+
<parent>
21+
<artifactId>bootstrap-plugins</artifactId>
22+
<groupId>org.apache.skywalking</groupId>
23+
<version>9.6.0-SNAPSHOT</version>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<artifactId>apm-jdk-httpclient-plugin</artifactId>
28+
<packaging>jar</packaging>
29+
30+
<name>apm-jdk-httpclient-plugin</name>
31+
<url>http://maven.apache.org</url>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
<maven.compiler.target>11</maven.compiler.target>
37+
<maven.compiler.release>11</maven.compiler.release>
38+
<compiler.version>11</compiler.version>
39+
</properties>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<artifactId>maven-deploy-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
22+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
23+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
24+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
25+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
30+
31+
import java.lang.reflect.Method;
32+
import java.net.URI;
33+
import java.net.http.HttpRequest;
34+
import java.net.http.HttpResponse;
35+
import java.util.concurrent.CompletableFuture;
36+
37+
public class HttpClientSendAsyncInterceptor implements InstanceMethodsAroundInterceptor {
38+
39+
@Override
40+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
41+
HttpRequest request = (HttpRequest) allArguments[0];
42+
URI uri = request.uri();
43+
44+
ContextCarrier contextCarrier = new ContextCarrier();
45+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri));
46+
47+
if (request instanceof EnhancedInstance) {
48+
((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier);
49+
}
50+
51+
span.setComponent(ComponentsDefine.JDK_HTTP);
52+
Tags.HTTP.METHOD.set(span, request.method());
53+
Tags.URL.set(span, String.valueOf(uri));
54+
SpanLayer.asHttp(span);
55+
}
56+
57+
@Override
58+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
59+
60+
if (ContextManager.isActive()) {
61+
AbstractSpan span = ContextManager.activeSpan();
62+
span.prepareForAsync();
63+
64+
if (ret != null) {
65+
CompletableFuture<?> future = (CompletableFuture<?>) ret;
66+
future.whenComplete((response, throwable) -> {
67+
try {
68+
if (throwable != null) {
69+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, 500);
70+
span.errorOccurred();
71+
span.log(throwable);
72+
} else if (response instanceof HttpResponse) {
73+
HttpResponse<?> httpResponse = (HttpResponse<?>) response;
74+
int statusCode = httpResponse.statusCode();
75+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, statusCode);
76+
if (statusCode >= 400) {
77+
span.errorOccurred();
78+
}
79+
}
80+
} finally {
81+
span.asyncFinish();
82+
}
83+
});
84+
} else {
85+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, 404);
86+
span.errorOccurred();
87+
}
88+
ContextManager.stopSpan();
89+
}
90+
return ret;
91+
}
92+
93+
@Override
94+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
95+
if (ContextManager.isActive()) {
96+
ContextManager.activeSpan().log(t);
97+
}
98+
}
99+
100+
private String buildOperationName(String method, URI uri) {
101+
String path = uri.getPath();
102+
if (path == null || path.isEmpty()) {
103+
path = "/";
104+
}
105+
return method + ":" + path;
106+
}
107+
108+
private String getPeer(URI uri) {
109+
String host = uri.getHost();
110+
int port = uri.getPort();
111+
112+
if (port == -1) {
113+
port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
114+
}
115+
116+
return host + ":" + port;
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
22+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
23+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
24+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
25+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
30+
31+
import java.lang.reflect.Method;
32+
import java.net.URI;
33+
import java.net.http.HttpRequest;
34+
import java.net.http.HttpResponse;
35+
36+
public class HttpClientSendInterceptor implements InstanceMethodsAroundInterceptor {
37+
38+
@Override
39+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
40+
HttpRequest request = (HttpRequest) allArguments[0];
41+
URI uri = request.uri();
42+
43+
ContextCarrier contextCarrier = new ContextCarrier();
44+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri));
45+
46+
if (request instanceof EnhancedInstance) {
47+
((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier);
48+
}
49+
50+
span.setComponent(ComponentsDefine.JDK_HTTP);
51+
Tags.HTTP.METHOD.set(span, request.method());
52+
Tags.URL.set(span, String.valueOf(uri));
53+
SpanLayer.asHttp(span);
54+
}
55+
56+
@Override
57+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
58+
59+
if (ContextManager.isActive()) {
60+
AbstractSpan span = ContextManager.activeSpan();
61+
if (ret != null) {
62+
HttpResponse<?> response = (HttpResponse<?>) ret;
63+
int statusCode = response.statusCode();
64+
65+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, response.statusCode());
66+
if (statusCode >= 400) {
67+
span.errorOccurred();
68+
}
69+
} else {
70+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, 404);
71+
span.errorOccurred();
72+
}
73+
74+
ContextManager.stopSpan();
75+
}
76+
return ret;
77+
}
78+
79+
@Override
80+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
81+
if (ContextManager.isActive()) {
82+
ContextManager.activeSpan().log(t);
83+
}
84+
}
85+
86+
private String buildOperationName(String method, URI uri) {
87+
String path = uri.getPath();
88+
if (path == null || path.isEmpty()) {
89+
path = "/";
90+
}
91+
return method + ":" + path;
92+
}
93+
94+
private String getPeer(URI uri) {
95+
String host = uri.getHost();
96+
int port = uri.getPort();
97+
98+
if (port == -1) {
99+
port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
100+
}
101+
102+
return host + ":" + port;
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin;
20+
21+
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
22+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
23+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
27+
28+
import java.lang.reflect.Method;
29+
import java.net.http.HttpHeaders;
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class HttpRequestHeadersInterceptor implements InstanceMethodsAroundInterceptor {
35+
36+
@Override
37+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
38+
39+
}
40+
41+
@Override
42+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
43+
44+
ContextCarrier contextCarrier = (ContextCarrier) objInst.getSkyWalkingDynamicField();
45+
HttpHeaders originalHeaders = (HttpHeaders) ret;
46+
final Map<String, List<String>> headerMap = new HashMap<>(originalHeaders.map());
47+
CarrierItem next = contextCarrier.items();
48+
while (next.hasNext()) {
49+
next = next.next();
50+
headerMap.put(next.getHeadKey(), List.of(next.getHeadValue()));
51+
}
52+
53+
return HttpHeaders.of(headerMap, (k, v) -> true);
54+
}
55+
56+
@Override
57+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
58+
if (ContextManager.isActive()) {
59+
ContextManager.activeSpan().log(t);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)