forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTraceSpanProcessor.java
More file actions
118 lines (99 loc) · 3.45 KB
/
StackTraceSpanProcessor.java
File metadata and controls
118 lines (99 loc) · 3.45 KB
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.stacktrace;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.internal.ExtendedSpanProcessor;
import io.opentelemetry.semconv.CodeAttributes;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Predicate;
public class StackTraceSpanProcessor implements ExtendedSpanProcessor {
private final long minSpanDurationNanos;
private final Predicate<ReadableSpan> filterPredicate;
/**
* @param minSpanDurationNanos minimum span duration in ns for stacktrace capture
* @param filterPredicate extra filter function to exclude spans if needed
*/
public StackTraceSpanProcessor(
long minSpanDurationNanos, Predicate<ReadableSpan> filterPredicate) {
if (minSpanDurationNanos < 0) {
throw new IllegalArgumentException("minimal span duration must be positive or zero");
}
this.minSpanDurationNanos = minSpanDurationNanos;
this.filterPredicate = filterPredicate;
}
@Override
public boolean isStartRequired() {
return false;
}
@Override
public void onStart(Context context, ReadWriteSpan readWriteSpan) {}
@Override
public boolean isOnEndingRequired() {
return true;
}
@Override
public void onEnding(ReadWriteSpan span) {
if (span.getLatencyNanos() < minSpanDurationNanos) {
return;
}
if (span.getAttribute(CodeAttributes.CODE_STACKTRACE) != null) {
// Span already has a stacktrace, do not override
return;
}
if (!filterPredicate.test(span)) {
return;
}
// Inferred spans are generated from sampling, so this span processor is not actually called
// when the span is active, and stack trace collection should be skipped.
//
// We only get such spans when the inferred spans processor executes before this span
// processor, which may be considered a configuration error.
boolean isInferred = span.getInstrumentationScopeInfo().getName().equals("inferred-spans");
if (isInferred) {
return;
}
span.setAttribute(CodeAttributes.CODE_STACKTRACE, generateSpanEndStacktrace());
}
@Override
public boolean isEndRequired() {
return false;
}
@Override
public void onEnd(ReadableSpan readableSpan) {}
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);
}
@Override
public String toString() {
return "StackTraceSpanProcessor{"
+ "minSpanDurationNanos="
+ minSpanDurationNanos
+ ", filterPredicate="
+ filterPredicate
+ '}';
}
}