Skip to content

Commit

Permalink
HttpClient sync and async usage supprort SOFATracer (#54)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
guanchao-yang authored Aug 21, 2018
1 parent be7a77f commit 3f285f3
Show file tree
Hide file tree
Showing 28 changed files with 1,628 additions and 25 deletions.
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<module>tracer-core</module>
<module>tracer-extensions</module>
<module>sofa-tracer-plugins/sofa-tracer-springmvc-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-httpclient-plugin</module>
<module>tracer-all</module>
<module>tracer-sofa-boot-starter</module>
<module>tracer-samples</module>
Expand Down Expand Up @@ -88,6 +89,11 @@
<artifactId>sofa-tracer-springmvc-plugin</artifactId>
<version>${sofa.tracer.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-httpclient-plugin</artifactId>
<version>${sofa.tracer.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-all</artifactId>
Expand Down Expand Up @@ -127,6 +133,19 @@
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- HttpClient Dependency -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.3</version>
<scope>provided</scope>
</dependency>
<!-- test 依赖 -->
<dependency>
<groupId>org.jmockit</groupId>
Expand Down
79 changes: 79 additions & 0 deletions sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>tracer-all-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>2.2.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sofa-tracer-httpclient-plugin</artifactId>

<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-core</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-extensions</artifactId>
</dependency>
<!-- HttpClient sync and async dependency -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<scope>provided</scope>
</dependency>

<!-- test dependency -->
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit-coverage</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 com.alipay.sofa.tracer.plugins.httpclient;

import com.alipay.common.tracer.core.appender.builder.JsonStringBuilder;
import com.alipay.common.tracer.core.appender.self.Timestamp;
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.middleware.parent.AbstractDigestSpanEncoder;
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.SofaTracerSpan;

import java.io.IOException;
import java.util.Map;

/**
* HttpClientDigestJsonEncoder
*
* @author yangguanchao
* @since 2018/08/07
*/
public class HttpClientDigestJsonEncoder extends AbstractDigestSpanEncoder {

@Override
public String encode(SofaTracerSpan span) throws IOException {
JsonStringBuilder jsonStringBuilder = new JsonStringBuilder();
//span end time
jsonStringBuilder.appendBegin("time", Timestamp.format(span.getEndTime()));
appendSlot(jsonStringBuilder, span);
return jsonStringBuilder.toString();
}

private void appendSlot(JsonStringBuilder jsonStringBuilder, SofaTracerSpan sofaTracerSpan) {
SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext();
Map<String, String> tagWithStr = sofaTracerSpan.getTagsWithStr();
Map<String, Number> tagWithNumber = sofaTracerSpan.getTagsWithNumber();
//app
jsonStringBuilder
.append(CommonSpanTags.LOCAL_APP, tagWithStr.get(CommonSpanTags.LOCAL_APP));
//TraceId
jsonStringBuilder.append("traceId", context.getTraceId());
//SpanId
jsonStringBuilder.append("spanId", context.getSpanId());
//URL
jsonStringBuilder.append(CommonSpanTags.REQUEST_URL,
tagWithStr.get(CommonSpanTags.REQUEST_URL));
//POST/GET
jsonStringBuilder.append(CommonSpanTags.METHOD, tagWithStr.get(CommonSpanTags.METHOD));
//Http status code
jsonStringBuilder.append(CommonSpanTags.RESULT_CODE,
tagWithStr.get(CommonSpanTags.RESULT_CODE));
Number requestSize = tagWithNumber.get(CommonSpanTags.REQ_SIZE);
//Request Body bytes length
jsonStringBuilder.append(CommonSpanTags.REQ_SIZE,
(requestSize == null ? 0L : requestSize.longValue()));
Number responseSize = tagWithNumber.get(CommonSpanTags.RESP_SIZE);
//Response Body bytes length
jsonStringBuilder.append(CommonSpanTags.RESP_SIZE, (responseSize == null ? 0L
: responseSize.longValue()));
//time-consuming ms
jsonStringBuilder.append("time.cost.milliseconds",
(sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()));
jsonStringBuilder.append(CommonSpanTags.CURRENT_THREAD_NAME,
tagWithStr.get(CommonSpanTags.CURRENT_THREAD_NAME));
//target appName
jsonStringBuilder.append(CommonSpanTags.REMOTE_APP,
tagWithStr.get(CommonSpanTags.REMOTE_APP));
this.appendBaggage(jsonStringBuilder, context);
}

private void appendBaggage(JsonStringBuilder jsonStringBuilder,
SofaTracerSpanContext sofaTracerSpanContext) {
//baggage
jsonStringBuilder.appendEnd("baggage", baggageSerialized(sofaTracerSpanContext));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 com.alipay.sofa.tracer.plugins.httpclient;

/**
* HttpClientLogEnum
*
* @author yangguanchao
* @since 2018/08/07
*/
public enum HttpClientLogEnum {

// Http Client Digest Log
HTTP_CLIENT_DIGEST("httpclient_digest_log_name", "httpclient-digest.log",
"httpclient_digest_rolling"),
// Http Client Stat Log
HTTP_CLIENT_STAT("httpclient_stat_log_name", "httpclient-stat.log", "httpclient_stat_rolling"), ;

private String logNameKey;
private String defaultLogName;
private String rollingKey;

HttpClientLogEnum(String logNameKey, String defaultLogName, String rollingKey) {
this.logNameKey = logNameKey;
this.defaultLogName = defaultLogName;
this.rollingKey = rollingKey;
}

public String getLogNameKey() {
//log reserve config key
return logNameKey;
}

public String getDefaultLogName() {
return defaultLogName;
}

public String getRollingKey() {
return rollingKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 com.alipay.sofa.tracer.plugins.httpclient;

import io.opentracing.propagation.TextMap;
import org.apache.http.HttpRequest;
import org.apache.http.message.BasicHeader;

import java.util.Iterator;
import java.util.Map;

/**
* HttpClientRequestCarrier
*
* @author yangguanchao
* @since 2018/08/11
*/
public class HttpClientRequestCarrier implements TextMap {

private final HttpRequest request;

public HttpClientRequestCarrier(HttpRequest request) {
this.request = request;
}

@Override
public Iterator<Map.Entry<String, String>> iterator() {
// no operation
throw new UnsupportedOperationException();
}

@Override
public void put(String key, String value) {
request.addHeader(new BasicHeader(key, value));
}
}
Loading

0 comments on commit 3f285f3

Please sign in to comment.