-
Notifications
You must be signed in to change notification settings - Fork 380
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
Resttemplate plugin #117
Conversation
# 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
…a-tracer into resttemplate-plugin
@glmapper |
<dependency> | ||
<groupId>com.alipay.sofa</groupId> | ||
<artifactId>tracer-sofa-boot-starter</artifactId> | ||
<version>2.3.0</version> |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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。
There was a problem hiding this comment.
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 块中将会捕获帮助我们定位
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我的意思是直接抛出来,不是包一层RuntimeException, 这个重载的方法默认是抛出IOException的
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 catch 的异常类型,把 RuntimeException 换成 Throwable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@QilongZhang 这里在外层还是抛出去吧,因为这里落的日志不会太详细。这里如果把异常吞掉的话不利于排查问题
There was a problem hiding this comment.
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));
}
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个版本号为什么降下来了? 2.5.2 里面修复tracer日志配置问题
There was a problem hiding this comment.
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以上版本
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯,所以升级到最新版的2.5.2吧。
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里加个 Condition,判断Template插件是否引入,否则会报错?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个类为什么是空的?
There was a problem hiding this comment.
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日志
There was a problem hiding this comment.
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; | ||
|
There was a problem hiding this comment.
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)
另外建议考虑这个配置是否有必要存在,这个配置值只是让注入无效,并不是不打印日志。另外默认引入插件的方式不太友好,可以从这个版本就可以改了。
There was a problem hiding this comment.
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日志打印。
Motivation:
Provide the RestTemplate plugin
Result:
Fixes #89