-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathStackTraceSpanProcessor.java
100 lines (85 loc) · 3.19 KB
/
StackTraceSpanProcessor.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.stacktrace;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.contrib.stacktrace.internal.AbstractSimpleChainingSpanProcessor;
import io.opentelemetry.contrib.stacktrace.internal.MutableSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StackTraceSpanProcessor extends AbstractSimpleChainingSpanProcessor {
// inlined incubating attribute to prevent direct dependency on incubating semconv
private static final AttributeKey<String> SPAN_STACKTRACE =
AttributeKey.stringKey("code.stacktrace");
private static final Logger logger = Logger.getLogger(StackTraceSpanProcessor.class.getName());
private final long minSpanDurationNanos;
private final Predicate<ReadableSpan> filterPredicate;
/**
* @param next next span processor to invoke
* @param minSpanDurationNanos minimum span duration in ns for stacktrace capture
* @param filterPredicate extra filter function to exclude spans if needed
*/
public StackTraceSpanProcessor(
SpanProcessor next, long minSpanDurationNanos, Predicate<ReadableSpan> filterPredicate) {
super(next);
this.minSpanDurationNanos = minSpanDurationNanos;
this.filterPredicate = filterPredicate;
logger.log(
Level.FINE,
"Stack traces will be added to spans with a minimum duration of {0} nanos",
minSpanDurationNanos);
}
@Override
protected boolean requiresStart() {
return false;
}
@Override
protected boolean requiresEnd() {
return true;
}
@Override
protected ReadableSpan doOnEnd(ReadableSpan span) {
if (span.getLatencyNanos() < minSpanDurationNanos) {
return span;
}
if (span.getAttribute(SPAN_STACKTRACE) != null) {
// Span already has a stacktrace, do not override
return span;
}
if (!filterPredicate.test(span)) {
return span;
}
MutableSpan mutableSpan = MutableSpan.makeMutable(span);
String stacktrace = generateSpanEndStacktrace();
mutableSpan.setAttribute(SPAN_STACKTRACE, stacktrace);
return mutableSpan;
}
private static String generateSpanEndStacktrace() {
Throwable exception = new Throwable();
StringWriter stringWriter = new StringWriter();
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
exception.printStackTrace(printWriter);
}
return removeInternalFrames(stringWriter.toString());
}
private static String removeInternalFrames(String stackTrace) {
String lastInternal = "at io.opentelemetry.sdk.trace.SdkSpan.end";
int idx = stackTrace.lastIndexOf(lastInternal);
if (idx == -1) {
// should usually not happen, this means that the span processor was called from somewhere
// else
return stackTrace;
}
int nextNewLine = stackTrace.indexOf('\n', idx);
if (nextNewLine == -1) {
nextNewLine = stackTrace.length() - 1;
}
return stackTrace.substring(nextNewLine + 1);
}
}