Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize ExceptionEventData #6795

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
Comparing source compatibility of opentelemetry-sdk-trace-1.44.0-SNAPSHOT.jar against opentelemetry-sdk-trace-1.43.0.jar
No changes.
+++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.trace.data.ExceptionEventData (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW INTERFACE: io.opentelemetry.sdk.trace.data.EventData
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.data.ExceptionEventData create(long, java.lang.Throwable, io.opentelemetry.api.common.Attributes)
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.data.ExceptionEventData create(long, java.lang.Throwable, io.opentelemetry.api.common.Attributes, int)
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.Throwable getException()
67 changes: 63 additions & 4 deletions sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.internal.GuardedBy;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
Expand All @@ -20,11 +21,13 @@
import io.opentelemetry.sdk.internal.InstrumentationScopeUtil;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.data.EventData;
import io.opentelemetry.sdk.trace.data.ExceptionEventData;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.sdk.trace.internal.ExtendedSpanProcessor;
import io.opentelemetry.sdk.trace.internal.data.ExceptionEventData;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -436,7 +439,9 @@ public ReadWriteSpan setStatus(StatusCode statusCode, @Nullable String descripti

@Override
public ReadWriteSpan recordException(Throwable exception) {
recordException(exception, Attributes.empty());
Attributes attributes =
this.getAttributes() == null ? Attributes.empty() : this.getAttributes();
recordException(exception, attributes);
return this;
}

Expand All @@ -449,8 +454,62 @@ public ReadWriteSpan recordException(Throwable exception, Attributes additionalA
additionalAttributes = Attributes.empty();
}

addTimedEvent(
ExceptionEventData.create(spanLimits, clock.now(), exception, additionalAttributes));
AttributeKey<String> exceptionTypeAttributeKey = AttributeKey.stringKey("exception.type");
AttributeKey<String> exceptionMessageAttributeKey = AttributeKey.stringKey("exception.message");
AttributeKey<String> exceptionStackTraceAttributeKey =
AttributeKey.stringKey("exception.stacktrace");
vasantteja marked this conversation as resolved.
Show resolved Hide resolved

AttributesBuilder attributesBuilder = Attributes.builder();
String exceptionName = exception.getClass().getCanonicalName();
String exceptionMessage = exception.getMessage();
StringWriter stringWriter = new StringWriter();
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
exception.printStackTrace(printWriter);
}
String stackTrace = stringWriter.toString();

if (this.spanLimits.getMaxNumberOfAttributes() > 0
&& this.spanLimits.getMaxAttributeValueLength() != Integer.MAX_VALUE) {
if (exceptionName != null) {
exceptionName =
exceptionName.substring(
0, Math.min(exceptionName.length(), this.spanLimits.getMaxAttributeValueLength()));
}

if (exceptionMessage != null) {
exceptionMessage =
exceptionMessage.substring(
0,
Math.min(exceptionMessage.length(), this.spanLimits.getMaxAttributeValueLength()));
}

if (stackTrace != null) {
stackTrace =
stackTrace.substring(
0, Math.min(stackTrace.length(), this.spanLimits.getMaxAttributeValueLength()));
}
}

if (exceptionName != null) {
attributesBuilder.put(exceptionTypeAttributeKey, exceptionName);
}
if (exceptionMessage != null) {
attributesBuilder.put(exceptionMessageAttributeKey, exceptionMessage);
}
if (stackTrace != null) {
attributesBuilder.put(exceptionStackTraceAttributeKey, stackTrace);
}

attributesBuilder.putAll(additionalAttributes);

AttributeUtil.applyAttributesLimit(
attributesBuilder.build(),
spanLimits.getMaxNumberOfAttributesPerEvent(),
spanLimits.getMaxAttributeValueLength());

Attributes attributes = attributesBuilder.build();

addTimedEvent(ExceptionEventData.create(clock.now(), exception, attributes));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.trace.data;

import io.opentelemetry.api.common.Attributes;
import javax.annotation.concurrent.Immutable;

/** Data representation of an event for a recorded exception. */
vasantteja marked this conversation as resolved.
Show resolved Hide resolved
@Immutable
public interface ExceptionEventData extends EventData {

/**
* Returns a new immutable {@link ExceptionEventData}.
*
* @param epochNanos epoch timestamp in nanos of the {@link ExceptionEventData}.
* @param exception the {@link Throwable exception} of the {@code Event}.
* @param attributes the additional attributes of the {@link ExceptionEventData}.
* @return a new immutable {@link ExceptionEventData}
*/
static ExceptionEventData create(long epochNanos, Throwable exception, Attributes attributes) {
return ImmutableExceptionEventData.create(epochNanos, exception, attributes);
}

/**
* Returns a new immutable {@link ExceptionEventData}.
*
* @param epochNanos epoch timestamp in nanos of the {@link ExceptionEventData}.
* @param exception the {@link Throwable exception} of the {@code Event}.
* @param attributes the additional attributes of the {@link ExceptionEventData}.
* @param totalAttributeCount the total number of attributes for this {@code} Event.
* @return a new immutable {@link ExceptionEventData}
*/
static ExceptionEventData create(
long epochNanos, Throwable exception, Attributes attributes, int totalAttributeCount) {
return ImmutableExceptionEventData.create(

Check warning on line 38 in sdk/trace/src/main/java/io/opentelemetry/sdk/trace/data/ExceptionEventData.java

View check run for this annotation

Codecov / codecov/patch

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/data/ExceptionEventData.java#L38

Added line #L38 was not covered by tests
epochNanos, exception, attributes, totalAttributeCount);
}

/**
* Return the {@link Throwable exception} of the {@link ExceptionEventData}.
*
* @return the {@link Throwable exception} of the {@link ExceptionEventData}
*/
Throwable getException();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.trace.data;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import javax.annotation.concurrent.Immutable;

/** An effectively immutable implementation of {@link ExceptionEventData}. */
@AutoValue
@Immutable
abstract class ImmutableExceptionEventData implements ExceptionEventData {

private static final String EXCEPTION_EVENT_NAME = "exception";

@Override
public final String getName() {
return EXCEPTION_EVENT_NAME;
}

/**
* Returns a new immutable {@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) {

return create(epochNanos, exception, additionalAttributes, additionalAttributes.size());
vasantteja marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Returns a new immutable {@code Event}.
*
* @param epochNanos epoch timestamp in nanos of the {@code Event}.
* @param exception the {@link Throwable exception} of the {@code Event}.
* @param attributes the additional {@link Attributes} of the {@code Event}.
* @param totalAttributeCount the total number of attributes for this {@code} Event.
* @return a new immutable {@code Event<T>}
*/
static ExceptionEventData create(
long epochNanos, Throwable exception, Attributes attributes, int totalAttributeCount) {

vasantteja marked this conversation as resolved.
Show resolved Hide resolved
return new AutoValue_ImmutableExceptionEventData(
attributes, epochNanos, totalAttributeCount, exception);
}

ImmutableExceptionEventData() {}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading