提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.tracer.core.util; |
H |
2 |
|
|
3 |
import io.opentracing.Span; |
|
4 |
import io.opentracing.tag.Tags; |
|
5 |
|
|
6 |
import java.io.PrintWriter; |
|
7 |
import java.io.StringWriter; |
|
8 |
import java.util.HashMap; |
|
9 |
import java.util.Map; |
|
10 |
|
|
11 |
/** |
|
12 |
* 链路追踪 Util |
|
13 |
* |
|
14 |
* @author iailab |
|
15 |
*/ |
|
16 |
public class TracerFrameworkUtils { |
|
17 |
|
|
18 |
/** |
|
19 |
* 将异常记录到 Span 中,参考自 com.aliyuncs.utils.TraceUtils |
|
20 |
* |
|
21 |
* @param throwable 异常 |
|
22 |
* @param span Span |
|
23 |
*/ |
|
24 |
public static void onError(Throwable throwable, Span span) { |
|
25 |
Tags.ERROR.set(span, Boolean.TRUE); |
|
26 |
if (throwable != null) { |
|
27 |
span.log(errorLogs(throwable)); |
|
28 |
} |
|
29 |
} |
|
30 |
|
|
31 |
private static Map<String, Object> errorLogs(Throwable throwable) { |
|
32 |
Map<String, Object> errorLogs = new HashMap<String, Object>(10); |
|
33 |
errorLogs.put("event", Tags.ERROR.getKey()); |
|
34 |
errorLogs.put("error.object", throwable); |
|
35 |
errorLogs.put("error.kind", throwable.getClass().getName()); |
|
36 |
String message = throwable.getCause() != null ? throwable.getCause().getMessage() : throwable.getMessage(); |
|
37 |
if (message != null) { |
|
38 |
errorLogs.put("message", message); |
|
39 |
} |
|
40 |
StringWriter sw = new StringWriter(); |
|
41 |
throwable.printStackTrace(new PrintWriter(sw)); |
|
42 |
errorLogs.put("stack", sw.toString()); |
|
43 |
return errorLogs; |
|
44 |
} |
|
45 |
|
|
46 |
} |