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

Conversation

glmapper
Copy link
Contributor

Motivation:

Provide the RestTemplate plugin

Result:

Fixes #89

@glmapper glmapper self-assigned this Nov 12, 2018
@glmapper glmapper added the enhancement New feature or request label Nov 12, 2018
@glmapper glmapper added this to the 2.3.0 milestone Nov 12, 2018
@coveralls
Copy link

coveralls commented Nov 12, 2018

@glmapper glmapper mentioned this pull request Nov 12, 2018
@glmapper glmapper removed the request for review from guanchao-yang November 14, 2018 10:22
卫恒 and others added 4 commits November 19, 2018 14:45
# Conflicts:
#	pom.xml
#	sofa-tracer-plugins/sofa-tracer-datasource-plugin/pom.xml
#	sofa-tracer-plugins/sofa-tracer-httpclient-plugin/pom.xml
#	sofa-tracer-plugins/sofa-tracer-springmvc-plugin/pom.xml
#	tracer-all/pom.xml
#	tracer-core/pom.xml
#	tracer-extensions/pom.xml
#	tracer-samples/pom.xml
#	tracer-samples/tracer-sample-with-h2/pom.xml
#	tracer-samples/tracer-sample-with-httpclient/pom.xml
#	tracer-samples/tracer-sample-with-slf4j/pom.xml
#	tracer-samples/tracer-sample-with-sofarpc/pom.xml
#	tracer-samples/tracer-sample-with-springmvc/pom.xml
#	tracer-samples/tracer-sample-with-zipkin/pom.xml
#	tracer-sofa-boot-plugin/pom.xml
#	tracer-sofa-boot-starter/pom.xml
#	tracer-test/core-test/pom.xml
#	tracer-test/log4j-test/pom.xml
#	tracer-test/log4j2-test/pom.xml
#	tracer-test/logback-test/pom.xml
@QilongZhang
Copy link
Contributor

@glmapper
SofaTracerRestTemplateBuilder 尽量不要让用户直接使用我们的 API 写代码,这样侵入性太强了。在 Spring Boot 环境下,参考下这个 https://docs.spring.io/spring-boot/docs/1.5.15.RELEASE/reference/htmlsingle/#boot-features-restclient-customization 应该是可以通过依赖注入的形式。

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-sofa-boot-starter</artifactId>
<version>2.3.0</version>
Copy link
Contributor

Choose a reason for hiding this comment

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

前面提示引入parent,这里的version不用配置了。

/**
* RestTemplateDigestJsonEncoder
* @author: guolei.sgl
* @since: 18/10/15
Copy link
Contributor

@QilongZhang QilongZhang Nov 20, 2018

Choose a reason for hiding this comment

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

统一风格,@ since 后面不接日期,更换成版本号。

try {
return response = execution.execute(request, body);
} catch (Exception e) {
throw new RuntimeException(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的异常没必要包一层,直接抛出来。在 finally 中没有打印出现异常的请求,只过滤了 reponse != null 条件,而对于出现异常的请求 response = null。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@QilongZhang 这里将异常向上抛出是不想在catch块中在做一次return,并且这个返回值也不清楚返回什么,所以以异常的方式交给程序本身来处理或者在上层处理。

如果 response = null ,这种情况没有必要将信息输出到tracer日志中,因为 response = null 这种情况本身就没有什么意义,拿不到任何有用的数据。当然如果是异常导致的,catch 块中将会捕获帮助我们定位

Copy link
Contributor

Choose a reason for hiding this comment

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

我的意思是直接抛出来,不是包一层RuntimeException, 这个重载的方法默认是抛出IOException的

Copy link
Contributor Author

@glmapper glmapper Nov 20, 2018

Choose a reason for hiding this comment

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

这里我的理解有点偏差,应该将 IOException 直接抛出

sofaTracerSpan));
return result;
} catch (RuntimeException e) {
restTemplateTracer.clientReceiveTagFinish(sofaTracerSpan, String.valueOf(500));
Copy link
Contributor

Choose a reason for hiding this comment

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

这里 catch 的异常类型,把 RuntimeException 换成 Throwable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@QilongZhang 这里在外层还是抛出去吧,因为这里落的日志不会太详细。这里如果把异常吞掉的话不利于排查问题

Copy link
Contributor

Choose a reason for hiding this comment

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

@glmapper 不是说吞掉.

      try {
            ListenableFuture<ClientHttpResponse> result = execution.executeAsync(request, body);
            result.addCallback(new SofaTraceListenableFutureCallback(restTemplateTracer,
                sofaTracerSpan));
            return result;
        }  finally {
            restTemplateTracer.clientReceiveTagFinish(sofaTracerSpan, String.valueOf(500));
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@QilongZhang 这样应该是不行的,如果在finally打statueCode是String.valueOf(500)的的log,是否就意味着当前这个处理就是失败的 ?

pom.xml Outdated
@@ -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

@ConditionalOnWebApplication
public class RestTemplateAutoConfiguration {
@Bean
public RestTemplate restTemplateCustomizer() {
Copy link
Contributor

Choose a reason for hiding this comment

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

这里加个 Condition,判断Template插件是否引入,否则会报错?

Copy link
Contributor

Choose a reason for hiding this comment

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

不应该使用
ConditionalOnMissintBean
而是使用
@ConditionalOnClass(name = "com.sofa.alipay.tracer.plugins.rest.interceptor.RestTemplateInterceptor")

* @since: v2.3.0
**/
@ConfigurationProperties("com.alipay.sofa.tracer.resttemplate")
public class RestTemplateProperties {
Copy link
Contributor

Choose a reason for hiding this comment

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

这个类为什么是空的?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里补充一个enable,如果com.alipay.sofa.tracer.resttemplate.enable设置成false,将不会收集tracer日志

Copy link
Contributor

Choose a reason for hiding this comment

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

这样写默认值是 false, 即引入依赖情况下,注入的方式默认也是失败的?

public class RestTemplateProperties {

private boolean enable = true;

Copy link
Contributor

Choose a reason for hiding this comment

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

注入的场景补充下测试用例吧,默认值换成 Spring Boot 标准的写法, @ConditionalOnProperty(prefix = "com.alipay.sofa.tracer.resttemplate", name = "enabled", matchIfMissing = true)

另外建议考虑这个配置是否有必要存在,这个配置值只是让注入无效,并不是不打印日志。另外默认引入插件的方式不太友好,可以从这个版本就可以改了。

Copy link
Contributor Author

@glmapper glmapper Nov 21, 2018

Choose a reason for hiding this comment

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

@QilongZhang 加上这个配置com.alipay.sofa.tracer.resttemplate.enabled本意是因为如果用户引入了 tracer-sofa-boot-starter 依赖,当他在使用:

@Autowired
RestTemplate             restTemplate;

这种方式获取 RestTemplate 实例并且不想打印tracer日志,那么可以通过该配置项来进行控制。但是考虑到后面我们的插件不会被默认带出去,所以这里就没有必要在使用该配置项。因为在插件默认被带出去的前提下,即使用户将此值配置成false,依然可以通过API的方式来进行tracer日志打印。

@QilongZhang QilongZhang merged commit 8347b0f into sofastack:master Nov 21, 2018
@glmapper glmapper deleted the resttemplate-plugin branch February 20, 2021 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants