Skip to content
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

Resttemplate plugin #117

Merged
merged 24 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.5.2</version>
<version>2.5.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个版本号为什么降下来了? 2.5.2 里面修复tracer日志配置问题

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前是从2.4.9 升上来的,2.3.0 会依赖 sofaboot 2.5.2以上版本

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,所以升级到最新版的2.5.2吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 PR 里面统一进行了版本的升级 Upgrade to 2.3.0

</parent>

<artifactId>tracer-all-parent</artifactId>
<version>2.2.3</version>
<version>2.3.0</version>
<packaging>pom</packaging>

<name>tracer-all-parent</name>
Expand All @@ -21,6 +21,7 @@
<module>sofa-tracer-plugins/sofa-tracer-springmvc-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-httpclient-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-datasource-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-resttmplate-plugin</module>
<module>tracer-all</module>
<module>tracer-sofa-boot-starter</module>
<module>tracer-samples</module>
Expand All @@ -32,7 +33,7 @@

<properties>
<opentracing.version>0.22.0</opentracing.version>
<sofa.tracer.version>2.2.3</sofa.tracer.version>
<sofa.tracer.version>2.3.0</sofa.tracer.version>
<java.compiler.source.version>1.6</java.compiler.source.version>
<java.compiler.target.version>1.6</java.compiler.target.version>
<jmh.version>1.9.3</jmh.version>
Expand Down Expand Up @@ -95,6 +96,11 @@
<artifactId>sofa-tracer-httpclient-plugin</artifactId>
<version>${sofa.tracer.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-resttmplate-plugin</artifactId>
<version>${sofa.tracer.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-datasource-plugin</artifactId>
Expand Down Expand Up @@ -324,6 +330,7 @@
<!-- 这里需要根据自己的需要指定要跑的单元测试 -->
<include>**/*Test.java</include>
</includes>
<forkMode>once</forkMode>
</configuration>
</plugin>

Expand Down
2 changes: 1 addition & 1 deletion sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>tracer-all-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>2.2.3</version>
<version>2.3.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>tracer-all-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>2.2.3</version>
<version>2.3.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
75 changes: 75 additions & 0 deletions sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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.3.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sofa-tracer-resttmplate-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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- needed for AsyncClientHttpRequestInterceptor -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</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,84 @@
/*
* 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.sofa.alipay.tracer.plugins.rest;

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;

/**
* RestTemplateDigestJsonEncoder
* @author: guolei.sgl
* @since: v2.3.0
*/
public class RestTemplateDigestJsonEncoder 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 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.sofa.alipay.tracer.plugins.rest;

/**
* RestTemplateLogEnum
* @author: guolei.sgl
* @since: v2.3.0
*/
public enum RestTemplateLogEnum {

// RestTemplate Client Digest Log
REST_TEMPLATE_DIGEST("resttemplate_digest_log_name", "resttemplate-digest.log",
"resttemplate_digest_rolling"),
// RestTemplate Stat Log
REST_TEMPLATE_STAT("resttemplate_stat_log_name", "resttemplate-stat.log",
"resttemplate_stat_rolling");

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

RestTemplateLogEnum(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,47 @@
/*
* 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.sofa.alipay.tracer.plugins.rest;

import io.opentracing.propagation.TextMap;
import org.springframework.http.HttpRequest;

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

/**
* RestTemplateRequestCarrier
* @author: guolei.sgl
* @since: v2.3.0
*/
public class RestTemplateRequestCarrier implements TextMap {

private final HttpRequest request;

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

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

@Override
public void put(String key, String value) {
request.getHeaders().add(key, value);
}
}
Loading