Skip to content

Commit 8d1e883

Browse files
authored
Merge branch 'main' into feat/grpc-keepalive-new
2 parents 32fd222 + e43a802 commit 8d1e883

File tree

32 files changed

+1103
-19
lines changed

32 files changed

+1103
-19
lines changed

.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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.release>11</maven.compiler.release>
36+
</properties>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<artifactId>maven-deploy-plugin</artifactId>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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.HashMap;
36+
import java.util.Map;
37+
import java.util.concurrent.CompletableFuture;
38+
39+
public class HttpClientSendAsyncInterceptor implements InstanceMethodsAroundInterceptor {
40+
41+
@Override
42+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
43+
HttpRequest request = (HttpRequest) allArguments[0];
44+
URI uri = request.uri();
45+
46+
ContextCarrier contextCarrier = new ContextCarrier();
47+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri));
48+
49+
if (request instanceof EnhancedInstance) {
50+
((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier);
51+
}
52+
53+
span.setComponent(ComponentsDefine.JDK_HTTP);
54+
Tags.HTTP.METHOD.set(span, request.method());
55+
Tags.URL.set(span, String.valueOf(uri));
56+
SpanLayer.asHttp(span);
57+
}
58+
59+
@Override
60+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
61+
62+
if (ContextManager.isActive()) {
63+
AbstractSpan span = ContextManager.activeSpan();
64+
span.prepareForAsync();
65+
66+
if (ret != null) {
67+
CompletableFuture<?> future = (CompletableFuture<?>) ret;
68+
ret = future.whenComplete((response, throwable) -> {
69+
try {
70+
if (throwable != null) {
71+
span.errorOccurred();
72+
span.log(throwable);
73+
return;
74+
}
75+
if (response instanceof HttpResponse) {
76+
HttpResponse<?> httpResponse = (HttpResponse<?>) response;
77+
int statusCode = httpResponse.statusCode();
78+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, statusCode);
79+
if (statusCode >= 400) {
80+
span.errorOccurred();
81+
}
82+
}
83+
} finally {
84+
span.asyncFinish();
85+
}
86+
});
87+
} else {
88+
Map<String, String> eventMap = new HashMap<String, String>();
89+
eventMap.put("error", "No response");
90+
span.log(System.currentTimeMillis(), eventMap);
91+
span.errorOccurred();
92+
}
93+
ContextManager.stopSpan();
94+
}
95+
return ret;
96+
}
97+
98+
@Override
99+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
100+
if (ContextManager.isActive()) {
101+
ContextManager.activeSpan().log(t);
102+
}
103+
}
104+
105+
private String buildOperationName(String method, URI uri) {
106+
String path = uri.getPath();
107+
if (path == null || path.isEmpty()) {
108+
path = "/";
109+
}
110+
return method + ":" + path;
111+
}
112+
113+
private String getPeer(URI uri) {
114+
String host = uri.getHost();
115+
int port = uri.getPort();
116+
117+
if (port == -1) {
118+
port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
119+
}
120+
121+
return host + ":" + port;
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.HashMap;
36+
import java.util.Map;
37+
38+
public class HttpClientSendInterceptor implements InstanceMethodsAroundInterceptor {
39+
40+
@Override
41+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
42+
HttpRequest request = (HttpRequest) allArguments[0];
43+
URI uri = request.uri();
44+
45+
ContextCarrier contextCarrier = new ContextCarrier();
46+
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri));
47+
48+
if (request instanceof EnhancedInstance) {
49+
((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier);
50+
}
51+
52+
span.setComponent(ComponentsDefine.JDK_HTTP);
53+
Tags.HTTP.METHOD.set(span, request.method());
54+
Tags.URL.set(span, String.valueOf(uri));
55+
SpanLayer.asHttp(span);
56+
}
57+
58+
@Override
59+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
60+
61+
if (ContextManager.isActive()) {
62+
AbstractSpan span = ContextManager.activeSpan();
63+
if (ret != null) {
64+
HttpResponse<?> response = (HttpResponse<?>) ret;
65+
int statusCode = response.statusCode();
66+
67+
Tags.HTTP_RESPONSE_STATUS_CODE.set(span, response.statusCode());
68+
if (statusCode >= 400) {
69+
span.errorOccurred();
70+
}
71+
} else {
72+
Map<String, String> eventMap = new HashMap<String, String>();
73+
eventMap.put("error", "No response");
74+
span.log(System.currentTimeMillis(), eventMap);
75+
span.errorOccurred();
76+
}
77+
78+
ContextManager.stopSpan();
79+
}
80+
return ret;
81+
}
82+
83+
@Override
84+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
85+
if (ContextManager.isActive()) {
86+
ContextManager.activeSpan().log(t);
87+
}
88+
}
89+
90+
private String buildOperationName(String method, URI uri) {
91+
String path = uri.getPath();
92+
if (path == null || path.isEmpty()) {
93+
path = "/";
94+
}
95+
return method + ":" + path;
96+
}
97+
98+
private String getPeer(URI uri) {
99+
String host = uri.getHost();
100+
int port = uri.getPort();
101+
102+
if (port == -1) {
103+
port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
104+
}
105+
106+
return host + ":" + port;
107+
}
108+
}
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)