Skip to content

Commit 3f285f3

Browse files
HttpClient sync and async usage supprort SOFATracer (#54)
* Add Zipkin Test Cases * Slf4j and slf4j adapter dependency optimization to 1.7.21 * Delete log4j and add slf4j-log4j dependency instead * Change SOFATracer version from 2.1.2.SNAPSHOT to 2.1.2 for release * Change version to 2.2.0-SNAPSHOT * SOFATracer support httpclient log disk and reported to Zipkin. Through HttpResponseInterceptor and HttpRequestInterceptor extension support * Http client 4.5.X supprot SOFATracer. Construct httpclient request and server response * Http client encoder and custom output * Code format for HttpClient Tracer * HttpClient post tracer test cases * Code format * Http Client Tracer Builder and Interceptor bettor code and convenience extension * No need import package * Http client build provide extension * Format code and delete unnecessary * Http Client B3 format httprequest carrier * Http client interceptor code format * Http async client interceptor support and provide test cases * Test cases strong and add async test cases * Change unnecessary version and statistics tracer log when success print true and fail print false * Http client tracer review and delete unnecessary code * Fix codereview and http/mvc success code
1 parent be7a77f commit 3f285f3

28 files changed

+1628
-25
lines changed

pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>tracer-core</module>
2020
<module>tracer-extensions</module>
2121
<module>sofa-tracer-plugins/sofa-tracer-springmvc-plugin</module>
22+
<module>sofa-tracer-plugins/sofa-tracer-httpclient-plugin</module>
2223
<module>tracer-all</module>
2324
<module>tracer-sofa-boot-starter</module>
2425
<module>tracer-samples</module>
@@ -88,6 +89,11 @@
8889
<artifactId>sofa-tracer-springmvc-plugin</artifactId>
8990
<version>${sofa.tracer.version}</version>
9091
</dependency>
92+
<dependency>
93+
<groupId>com.alipay.sofa</groupId>
94+
<artifactId>sofa-tracer-httpclient-plugin</artifactId>
95+
<version>${sofa.tracer.version}</version>
96+
</dependency>
9197
<dependency>
9298
<groupId>com.alipay.sofa</groupId>
9399
<artifactId>tracer-all</artifactId>
@@ -127,6 +133,19 @@
127133
<version>3.1.0</version>
128134
<scope>provided</scope>
129135
</dependency>
136+
<!-- HttpClient Dependency -->
137+
<dependency>
138+
<groupId>org.apache.httpcomponents</groupId>
139+
<artifactId>httpclient</artifactId>
140+
<version>4.5.3</version>
141+
<scope>provided</scope>
142+
</dependency>
143+
<dependency>
144+
<groupId>org.apache.httpcomponents</groupId>
145+
<artifactId>httpasyncclient</artifactId>
146+
<version>4.1.3</version>
147+
<scope>provided</scope>
148+
</dependency>
130149
<!-- test 依赖 -->
131150
<dependency>
132151
<groupId>org.jmockit</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>tracer-all-parent</artifactId>
7+
<groupId>com.alipay.sofa</groupId>
8+
<version>2.2.0-SNAPSHOT</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>sofa-tracer-httpclient-plugin</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.alipay.sofa</groupId>
18+
<artifactId>tracer-core</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.alipay.sofa</groupId>
22+
<artifactId>tracer-extensions</artifactId>
23+
</dependency>
24+
<!-- HttpClient sync and async dependency -->
25+
<dependency>
26+
<groupId>org.apache.httpcomponents</groupId>
27+
<artifactId>httpclient</artifactId>
28+
<optional>true</optional>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.apache.httpcomponents</groupId>
32+
<artifactId>httpasyncclient</artifactId>
33+
<scope>provided</scope>
34+
</dependency>
35+
36+
<!-- test dependency -->
37+
<dependency>
38+
<groupId>org.jmockit</groupId>
39+
<artifactId>jmockit</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-core</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.jmockit</groupId>
49+
<artifactId>jmockit-coverage</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>commons-io</groupId>
59+
<artifactId>commons-io</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-web</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.alibaba</groupId>
69+
<artifactId>fastjson</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-test</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
</dependencies>
78+
79+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
package com.alipay.sofa.tracer.plugins.httpclient;
18+
19+
import com.alipay.common.tracer.core.appender.builder.JsonStringBuilder;
20+
import com.alipay.common.tracer.core.appender.self.Timestamp;
21+
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
22+
import com.alipay.common.tracer.core.middleware.parent.AbstractDigestSpanEncoder;
23+
import com.alipay.common.tracer.core.span.CommonSpanTags;
24+
import com.alipay.common.tracer.core.span.SofaTracerSpan;
25+
26+
import java.io.IOException;
27+
import java.util.Map;
28+
29+
/**
30+
* HttpClientDigestJsonEncoder
31+
*
32+
* @author yangguanchao
33+
* @since 2018/08/07
34+
*/
35+
public class HttpClientDigestJsonEncoder extends AbstractDigestSpanEncoder {
36+
37+
@Override
38+
public String encode(SofaTracerSpan span) throws IOException {
39+
JsonStringBuilder jsonStringBuilder = new JsonStringBuilder();
40+
//span end time
41+
jsonStringBuilder.appendBegin("time", Timestamp.format(span.getEndTime()));
42+
appendSlot(jsonStringBuilder, span);
43+
return jsonStringBuilder.toString();
44+
}
45+
46+
private void appendSlot(JsonStringBuilder jsonStringBuilder, SofaTracerSpan sofaTracerSpan) {
47+
SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext();
48+
Map<String, String> tagWithStr = sofaTracerSpan.getTagsWithStr();
49+
Map<String, Number> tagWithNumber = sofaTracerSpan.getTagsWithNumber();
50+
//app
51+
jsonStringBuilder
52+
.append(CommonSpanTags.LOCAL_APP, tagWithStr.get(CommonSpanTags.LOCAL_APP));
53+
//TraceId
54+
jsonStringBuilder.append("traceId", context.getTraceId());
55+
//SpanId
56+
jsonStringBuilder.append("spanId", context.getSpanId());
57+
//URL
58+
jsonStringBuilder.append(CommonSpanTags.REQUEST_URL,
59+
tagWithStr.get(CommonSpanTags.REQUEST_URL));
60+
//POST/GET
61+
jsonStringBuilder.append(CommonSpanTags.METHOD, tagWithStr.get(CommonSpanTags.METHOD));
62+
//Http status code
63+
jsonStringBuilder.append(CommonSpanTags.RESULT_CODE,
64+
tagWithStr.get(CommonSpanTags.RESULT_CODE));
65+
Number requestSize = tagWithNumber.get(CommonSpanTags.REQ_SIZE);
66+
//Request Body bytes length
67+
jsonStringBuilder.append(CommonSpanTags.REQ_SIZE,
68+
(requestSize == null ? 0L : requestSize.longValue()));
69+
Number responseSize = tagWithNumber.get(CommonSpanTags.RESP_SIZE);
70+
//Response Body bytes length
71+
jsonStringBuilder.append(CommonSpanTags.RESP_SIZE, (responseSize == null ? 0L
72+
: responseSize.longValue()));
73+
//time-consuming ms
74+
jsonStringBuilder.append("time.cost.milliseconds",
75+
(sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()));
76+
jsonStringBuilder.append(CommonSpanTags.CURRENT_THREAD_NAME,
77+
tagWithStr.get(CommonSpanTags.CURRENT_THREAD_NAME));
78+
//target appName
79+
jsonStringBuilder.append(CommonSpanTags.REMOTE_APP,
80+
tagWithStr.get(CommonSpanTags.REMOTE_APP));
81+
this.appendBaggage(jsonStringBuilder, context);
82+
}
83+
84+
private void appendBaggage(JsonStringBuilder jsonStringBuilder,
85+
SofaTracerSpanContext sofaTracerSpanContext) {
86+
//baggage
87+
jsonStringBuilder.appendEnd("baggage", baggageSerialized(sofaTracerSpanContext));
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
package com.alipay.sofa.tracer.plugins.httpclient;
18+
19+
/**
20+
* HttpClientLogEnum
21+
*
22+
* @author yangguanchao
23+
* @since 2018/08/07
24+
*/
25+
public enum HttpClientLogEnum {
26+
27+
// Http Client Digest Log
28+
HTTP_CLIENT_DIGEST("httpclient_digest_log_name", "httpclient-digest.log",
29+
"httpclient_digest_rolling"),
30+
// Http Client Stat Log
31+
HTTP_CLIENT_STAT("httpclient_stat_log_name", "httpclient-stat.log", "httpclient_stat_rolling"), ;
32+
33+
private String logNameKey;
34+
private String defaultLogName;
35+
private String rollingKey;
36+
37+
HttpClientLogEnum(String logNameKey, String defaultLogName, String rollingKey) {
38+
this.logNameKey = logNameKey;
39+
this.defaultLogName = defaultLogName;
40+
this.rollingKey = rollingKey;
41+
}
42+
43+
public String getLogNameKey() {
44+
//log reserve config key
45+
return logNameKey;
46+
}
47+
48+
public String getDefaultLogName() {
49+
return defaultLogName;
50+
}
51+
52+
public String getRollingKey() {
53+
return rollingKey;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
package com.alipay.sofa.tracer.plugins.httpclient;
18+
19+
import io.opentracing.propagation.TextMap;
20+
import org.apache.http.HttpRequest;
21+
import org.apache.http.message.BasicHeader;
22+
23+
import java.util.Iterator;
24+
import java.util.Map;
25+
26+
/**
27+
* HttpClientRequestCarrier
28+
*
29+
* @author yangguanchao
30+
* @since 2018/08/11
31+
*/
32+
public class HttpClientRequestCarrier implements TextMap {
33+
34+
private final HttpRequest request;
35+
36+
public HttpClientRequestCarrier(HttpRequest request) {
37+
this.request = request;
38+
}
39+
40+
@Override
41+
public Iterator<Map.Entry<String, String>> iterator() {
42+
// no operation
43+
throw new UnsupportedOperationException();
44+
}
45+
46+
@Override
47+
public void put(String key, String value) {
48+
request.addHeader(new BasicHeader(key, value));
49+
}
50+
}

0 commit comments

Comments
 (0)