-
Notifications
You must be signed in to change notification settings - Fork 791
/
BaseTracer.java
218 lines (184 loc) · 7.26 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
211
212
213
214
215
216
217
218
/*
* 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.Iterator;
import java.util.List;
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);
private static final boolean FAIL_ON_CONTEXT_LEAK =
Boolean.getBoolean("otel.internal.failOnContextLeak");
// 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.
public 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.
// Visible for testing
public 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();
}
// TODO (trask) make the key private
protected final boolean inClientSpan(Context parentContext) {
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) != null;
}
// TODO (trask) make the key private
protected final Context withClientSpan(Context parentContext, Span span) {
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
}
public Scope startScope(Span span) {
return Context.current().with(span).makeCurrent();
}
public Span getCurrentSpan() {
return Span.current();
}
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(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(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) {
if (ContextPropagationDebug.isThreadPropagationDebuggerEnabled()) {
debugContextLeak();
}
// 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);
}
private static void debugContextLeak() {
Context current = Context.current();
if (current != Context.root()) {
log.error("Unexpected non-root current context found when extracting remote context!");
Span currentSpan = Span.fromContextOrNull(current);
if (currentSpan != null) {
log.error("It contains this span: {}", currentSpan);
}
List<StackTraceElement[]> locations = ContextPropagationDebug.getLocations(current);
if (locations != null) {
StringBuilder sb = new StringBuilder();
Iterator<StackTraceElement[]> i = locations.iterator();
while (i.hasNext()) {
for (StackTraceElement ste : i.next()) {
sb.append("\n");
sb.append(ste);
}
if (i.hasNext()) {
sb.append("\nwhich was propagated from:");
}
}
log.error("a context leak was detected. it was propagated from:{}", sb);
}
if (FAIL_ON_CONTEXT_LEAK) {
throw new IllegalStateException("Context leak detected");
}
}
}
/** 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);
}
}