Skip to content

Commit d26d35a

Browse files
committed
Adds option to SpanLimits to exclude exception.stacktrace attribute
1 parent 56941a5 commit d26d35a

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
Comparing source compatibility of opentelemetry-sdk-trace-1.48.0-SNAPSHOT.jar against opentelemetry-sdk-trace-1.47.0.jar
2-
No changes.
2+
*** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.trace.SpanLimits (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) boolean isExcludeExceptionStackTrace()
5+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.trace.SpanLimitsBuilder (not serializable)
6+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
7+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.SpanLimitsBuilder setExcludeExceptionStackTrace(boolean)

sdk/all/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ void stringRepresentation() {
413413
+ "clock=SystemClock{}, "
414414
+ "idGenerator=RandomIdGenerator{}, "
415415
+ "resource=Resource{schemaUrl=null, attributes={service.name=\"otel-test\"}}, "
416-
+ "spanLimitsSupplier=SpanLimitsValue{maxNumberOfAttributes=128, maxNumberOfEvents=128, maxNumberOfLinks=128, maxNumberOfAttributesPerEvent=128, maxNumberOfAttributesPerLink=128, maxAttributeValueLength=2147483647}, "
416+
+ "spanLimitsSupplier=SpanLimitsValue{maxNumberOfAttributes=128, maxNumberOfEvents=128, maxNumberOfLinks=128, maxNumberOfAttributesPerEvent=128, maxNumberOfAttributesPerLink=128, maxAttributeValueLength=2147483647, excludeExceptionStackTrace=false}, "
417417
+ "sampler=ParentBased{root:AlwaysOnSampler,remoteParentSampled:AlwaysOnSampler,remoteParentNotSampled:AlwaysOffSampler,localParentSampled:AlwaysOnSampler,localParentNotSampled:AlwaysOffSampler}, "
418418
+ "spanProcessor=SimpleSpanProcessor{spanExporter=MultiSpanExporter{spanExporters=[MockSpanExporter{}, MockSpanExporter{}]}, exportUnsampledSpans=false}"
419419
+ "}, "

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,14 @@ public ReadWriteSpan recordException(Throwable exception, Attributes additionalA
479479
spanLimits.getMaxNumberOfAttributes(), spanLimits.getMaxAttributeValueLength());
480480
String exceptionName = exception.getClass().getCanonicalName();
481481
String exceptionMessage = exception.getMessage();
482-
StringWriter stringWriter = new StringWriter();
483-
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
484-
exception.printStackTrace(printWriter);
482+
String stackTrace = null;
483+
if (!spanLimits.isExcludeExceptionStackTrace()) {
484+
StringWriter stringWriter = new StringWriter();
485+
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
486+
exception.printStackTrace(printWriter);
487+
}
488+
stackTrace = stringWriter.toString();
485489
}
486-
String stackTrace = stringWriter.toString();
487490

488491
if (exceptionName != null) {
489492
attributes.put(EXCEPTION_TYPE, exceptionName);

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SpanLimits.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ static SpanLimits create(
3939
int maxNumLinks,
4040
int maxNumAttributesPerEvent,
4141
int maxNumAttributesPerLink,
42-
int maxAttributeLength) {
42+
int maxAttributeLength,
43+
boolean excludeExceptionStackTrace) {
4344
return new AutoValue_SpanLimits_SpanLimitsValue(
4445
maxNumAttributes,
4546
maxNumEvents,
4647
maxNumLinks,
4748
maxNumAttributesPerEvent,
4849
maxNumAttributesPerLink,
49-
maxAttributeLength);
50+
maxAttributeLength,
51+
excludeExceptionStackTrace);
5052
}
5153

5254
/**
@@ -102,6 +104,15 @@ public int getMaxAttributeValueLength() {
102104
return DEFAULT_SPAN_MAX_ATTRIBUTE_LENGTH;
103105
}
104106

107+
/**
108+
* Returns whether exception stack trace should be excluded from exception event attributes.
109+
*
110+
* @return whether exception stack trace should be excluded from exception event attributes.
111+
*/
112+
public boolean isExcludeExceptionStackTrace() {
113+
return false;
114+
}
115+
105116
/**
106117
* Returns a {@link SpanLimitsBuilder} initialized to the same property values as the current
107118
* instance.
@@ -116,7 +127,8 @@ public SpanLimitsBuilder toBuilder() {
116127
.setMaxNumberOfLinks(getMaxNumberOfLinks())
117128
.setMaxNumberOfAttributesPerEvent(getMaxNumberOfAttributesPerEvent())
118129
.setMaxNumberOfAttributesPerLink(getMaxNumberOfAttributesPerLink())
119-
.setMaxAttributeValueLength(getMaxAttributeValueLength());
130+
.setMaxAttributeValueLength(getMaxAttributeValueLength())
131+
.setExcludeExceptionStackTrace(isExcludeExceptionStackTrace());
120132
}
121133

122134
@AutoValue
@@ -129,5 +141,12 @@ abstract static class SpanLimitsValue extends SpanLimits {
129141
*/
130142
@Override
131143
public abstract int getMaxAttributeValueLength();
144+
145+
/**
146+
* Override {@link SpanLimits#isExcludeExceptionStackTrace()} to be abstract so autovalue can
147+
* implement it.
148+
*/
149+
@Override
150+
public abstract boolean isExcludeExceptionStackTrace();
132151
}
133152
}

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SpanLimitsBuilder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public final class SpanLimitsBuilder {
2323
private int maxNumAttributesPerEvent = DEFAULT_SPAN_MAX_NUM_ATTRIBUTES_PER_EVENT;
2424
private int maxNumAttributesPerLink = DEFAULT_SPAN_MAX_NUM_ATTRIBUTES_PER_LINK;
2525
private int maxAttributeValueLength = SpanLimits.DEFAULT_SPAN_MAX_ATTRIBUTE_LENGTH;
26+
private boolean excludeExceptionStackTrace = false;
2627

2728
SpanLimitsBuilder() {}
2829

@@ -109,6 +110,18 @@ public SpanLimitsBuilder setMaxAttributeValueLength(int maxAttributeValueLength)
109110
return this;
110111
}
111112

113+
/**
114+
* Sets whether exception stacktraces should be excluded from exception event attributes.
115+
*
116+
* @param excludeExceptionStackTrace whether exception stacktraces should be excluded from
117+
* exception event attributes.
118+
* @return this.
119+
*/
120+
public SpanLimitsBuilder setExcludeExceptionStackTrace(boolean excludeExceptionStackTrace) {
121+
this.excludeExceptionStackTrace = excludeExceptionStackTrace;
122+
return this;
123+
}
124+
112125
/** Builds and returns a {@link SpanLimits} with the values of this builder. */
113126
public SpanLimits build() {
114127
return SpanLimits.create(
@@ -117,6 +130,7 @@ public SpanLimits build() {
117130
maxNumLinks,
118131
maxNumAttributesPerEvent,
119132
maxNumAttributesPerLink,
120-
maxAttributeValueLength);
133+
maxAttributeValueLength,
134+
excludeExceptionStackTrace);
121135
}
122136
}

0 commit comments

Comments
 (0)