Apply attribute limits to exception events (#4423)

This commit is contained in:
Lauri Tulmin 2022-04-29 00:12:35 +03:00 committed by GitHub
parent ee4d771142
commit 16be81aed8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 8 deletions

View File

@ -124,7 +124,7 @@ final class SdkSpan implements ReadWriteSpan {
* @param name the displayed name for the new span.
* @param kind the span kind.
* @param parentSpan the parent span, or {@link Span#getInvalid()} if this span is a root span.
* @param spanLimits trace parameters like sampler and probability.
* @param spanLimits limits applied to this span.
* @param spanProcessor handler called when the span starts and ends.
* @param tracerClock the tracer's clock
* @param resource the resource associated with this span.
@ -399,7 +399,8 @@ final class SdkSpan implements ReadWriteSpan {
additionalAttributes = Attributes.empty();
}
addTimedEvent(ExceptionEventData.create(clock.now(), exception, additionalAttributes));
addTimedEvent(
ExceptionEventData.create(spanLimits, clock.now(), exception, additionalAttributes));
return this;
}

View File

@ -6,6 +6,7 @@
package io.opentelemetry.sdk.trace.internal.data;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.trace.SpanLimits;
import io.opentelemetry.sdk.trace.data.EventData;
/**
@ -19,14 +20,19 @@ public interface ExceptionEventData extends EventData {
/**
* Returns a new immutable {@link ExceptionEventData}.
*
* @param spanLimits limits applied to {@link ExceptionEventData}.
* @param epochNanos epoch timestamp in nanos of the {@link ExceptionEventData}.
* @param exception the {@link Throwable exception} of the {@code Event}.
* @param additionalAttributes the additional attributes of the {@link ExceptionEventData}.
* @return a new immutable {@link ExceptionEventData}
*/
static ExceptionEventData create(
long epochNanos, Throwable exception, Attributes additionalAttributes) {
return ImmutableExceptionEventData.create(epochNanos, exception, additionalAttributes);
SpanLimits spanLimits,
long epochNanos,
Throwable exception,
Attributes additionalAttributes) {
return ImmutableExceptionEventData.create(
spanLimits, epochNanos, exception, additionalAttributes);
}
/**

View File

@ -9,6 +9,8 @@ import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.internal.AttributeUtil;
import io.opentelemetry.sdk.trace.SpanLimits;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.PrintWriter;
import java.io.StringWriter;
@ -22,19 +24,26 @@ abstract class ImmutableExceptionEventData implements ExceptionEventData {
/**
* Returns a new immutable {@code Event}.
*
* @param spanLimits limits applied to {@code Event}.
* @param epochNanos epoch timestamp in nanos of the {@code Event}.
* @param exception the {@link Throwable exception} of the {@code Event}.
* @param additionalAttributes the additional {@link Attributes} of the {@code Event}.
* @return a new immutable {@code Event<T>}
*/
static ExceptionEventData create(
long epochNanos, Throwable exception, Attributes additionalAttributes) {
SpanLimits spanLimits,
long epochNanos,
Throwable exception,
Attributes additionalAttributes) {
return new AutoValue_ImmutableExceptionEventData(epochNanos, exception, additionalAttributes);
return new AutoValue_ImmutableExceptionEventData(
epochNanos, exception, additionalAttributes, spanLimits);
}
ImmutableExceptionEventData() {}
protected abstract SpanLimits getSpanLimits();
@Override
public final String getName() {
return SemanticAttributes.EXCEPTION_EVENT_NAME;
@ -61,7 +70,11 @@ abstract class ImmutableExceptionEventData implements ExceptionEventData {
attributesBuilder.put(SemanticAttributes.EXCEPTION_STACKTRACE, stringWriter.toString());
attributesBuilder.putAll(additionalAttributes);
return attributesBuilder.build();
SpanLimits spanLimits = getSpanLimits();
return AttributeUtil.applyAttributesLimit(
attributesBuilder.build(),
spanLimits.getMaxNumberOfAttributesPerEvent(),
spanLimits.getMaxAttributeValueLength());
}
@Override

View File

@ -725,8 +725,10 @@ class SdkSpanTest {
.put(doubleArrayKey("doubleArray"), Arrays.asList(1.0, 2.0))
.build();
span.setAllAttributes(attributes);
span.recordException(new IllegalStateException(tooLongStrVal));
attributes = span.toSpanData().getAttributes();
SpanData spanData = span.toSpanData();
attributes = spanData.getAttributes();
assertThat(attributes.get(stringKey("string"))).isEqualTo(strVal);
assertThat(attributes.get(booleanKey("boolean"))).isEqualTo(true);
assertThat(attributes.get(longKey("long"))).isEqualTo(1L);
@ -737,6 +739,16 @@ class SdkSpanTest {
.isEqualTo(Arrays.asList(true, false));
assertThat(attributes.get(longArrayKey("longArray"))).isEqualTo(Arrays.asList(1L, 2L));
assertThat(attributes.get(doubleArrayKey("doubleArray"))).isEqualTo(Arrays.asList(1.0, 2.0));
List<EventData> events = spanData.getEvents();
assertThat(events).hasSize(1);
EventData event = events.get(0);
assertThat(event.getName()).isEqualTo("exception");
assertThat(event.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE))
.isEqualTo("java.lang.IllegalStateException".substring(0, maxLength));
assertThat(event.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isEqualTo(strVal);
assertThat(event.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE).length())
.isLessThanOrEqualTo(maxLength);
} finally {
span.end();
}