-
Notifications
You must be signed in to change notification settings - Fork 905
/
Copy pathBaseTracer.java
210 lines (172 loc) · 6.81 KB
/
BaseTracer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.api.tracer;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Span.Kind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.api.InstrumentationVersion;
import io.opentelemetry.instrumentation.api.context.ContextPropagationDebug;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class BaseTracer {
private static final Logger log = LoggerFactory.getLogger(HttpServerTracer.class);
// Keeps track of the server span for the current trace.
// TODO(anuraaga): Should probably be renamed to local root key since it could be a consumer span
// or other non-server root.
private static final ContextKey<Span> CONTEXT_SERVER_SPAN_KEY =
ContextKey.named("opentelemetry-trace-server-span-key");
// Keeps track of the client span in a subtree corresponding to a client request.
private static final ContextKey<Span> CONTEXT_CLIENT_SPAN_KEY =
ContextKey.named("opentelemetry-trace-auto-client-span-key");
protected final Tracer tracer;
public BaseTracer() {
tracer = GlobalOpenTelemetry.getTracer(getInstrumentationName(), getVersion());
}
public BaseTracer(Tracer tracer) {
this.tracer = tracer;
}
public Span startSpan(Class<?> clazz) {
String spanName = spanNameForClass(clazz);
return startSpan(spanName, Kind.INTERNAL);
}
public Span startSpan(Method method) {
String spanName = spanNameForMethod(method);
return startSpan(spanName, Kind.INTERNAL);
}
public Span startSpan(String spanName, Kind kind) {
return tracer.spanBuilder(spanName).setSpanKind(kind).startSpan();
}
protected final Context withClientSpan(Context parentContext, Span span) {
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
}
protected final Context withServerSpan(Context parentContext, Span span) {
return parentContext.with(span).with(CONTEXT_SERVER_SPAN_KEY, span);
}
public Scope startScope(Span span) {
return Context.current().with(span).makeCurrent();
}
protected final boolean shouldStartSpan(Kind proposedKind, Context context) {
switch (proposedKind) {
case CLIENT:
return !inClientSpan(context);
case SERVER:
return !inServerSpan(context);
default:
return true;
}
}
private boolean inClientSpan(Context parentContext) {
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) != null;
}
private boolean inServerSpan(Context context) {
return getCurrentServerSpan(context) != null;
}
protected abstract String getInstrumentationName();
protected String getVersion() {
return InstrumentationVersion.VERSION;
}
/**
* This method is used to generate an acceptable span (operation) name based on a given method
* reference. Anonymous classes are named based on their parent.
*/
public String spanNameForMethod(Method method) {
return spanNameForClass(method.getDeclaringClass()) + "." + method.getName();
}
/**
* This method is used to generate an acceptable span (operation) name based on a given method
* reference. Anonymous classes are named based on their parent.
*
* @param method the method to get the name from, nullable
* @return the span name from the class and method
*/
protected String spanNameForMethod(Class<?> clazz, Method method) {
return spanNameForMethod(clazz, null == method ? null : method.getName());
}
protected String spanNameForMethod(Class<?> cl, String methodName) {
return spanNameForClass(cl) + "." + methodName;
}
/**
* This method is used to generate an acceptable span (operation) name based on a given class
* reference. Anonymous classes are named based on their parent.
*/
public String spanNameForClass(Class<?> clazz) {
if (!clazz.isAnonymousClass()) {
return clazz.getSimpleName();
}
String className = clazz.getName();
if (clazz.getPackage() != null) {
String pkgName = clazz.getPackage().getName();
if (!pkgName.isEmpty()) {
className = clazz.getName().replace(pkgName, "").substring(1);
}
}
return className;
}
public void end(Context context) {
end(context, -1);
}
public void end(Context context, long endTimeNanos) {
end(Span.fromContext(context), endTimeNanos);
}
public void end(Span span) {
end(span, -1);
}
public void end(Span span, long endTimeNanos) {
if (endTimeNanos > 0) {
span.end(endTimeNanos, TimeUnit.NANOSECONDS);
} else {
span.end();
}
}
public void endExceptionally(Context context, Throwable throwable) {
endExceptionally(context, throwable, -1);
}
public void endExceptionally(Context context, Throwable throwable, long endTimeNanos) {
endExceptionally(Span.fromContext(context), throwable, endTimeNanos);
}
public void endExceptionally(Span span, Throwable throwable) {
endExceptionally(span, throwable, -1);
}
public void endExceptionally(Span span, Throwable throwable, long endTimeNanos) {
span.setStatus(StatusCode.ERROR);
onError(span, unwrapThrowable(throwable));
end(span, endTimeNanos);
}
protected void onError(Span span, Throwable throwable) {
addThrowable(span, throwable);
}
protected Throwable unwrapThrowable(Throwable throwable) {
return throwable instanceof ExecutionException ? throwable.getCause() : throwable;
}
public void addThrowable(Span span, Throwable throwable) {
span.recordException(throwable);
}
public static <C> Context extract(C carrier, TextMapPropagator.Getter<C> getter) {
ContextPropagationDebug.debugContextLeakIfEnabled();
// Using Context.ROOT here may be quite unexpected, but the reason is simple.
// We want either span context extracted from the carrier or invalid one.
// We DO NOT want any span context potentially lingering in the current context.
return GlobalOpenTelemetry.getPropagators()
.getTextMapPropagator()
.extract(Context.root(), carrier, getter);
}
/** Returns span of type SERVER from the current context or <code>null</code> if not found. */
public static Span getCurrentServerSpan() {
return getCurrentServerSpan(Context.current());
}
/** Returns span of type SERVER from the given context or <code>null</code> if not found. */
public static Span getCurrentServerSpan(Context context) {
return context.get(CONTEXT_SERVER_SPAN_KEY);
}
}