|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.trace.internal.data; |
| 7 | + |
| 8 | +import com.google.auto.value.AutoValue; |
| 9 | +import com.google.auto.value.extension.memoized.Memoized; |
| 10 | +import io.opentelemetry.api.common.AttributeKey; |
| 11 | +import io.opentelemetry.api.common.Attributes; |
| 12 | +import io.opentelemetry.sdk.internal.AttributesMap; |
| 13 | +import io.opentelemetry.sdk.trace.SpanLimits; |
| 14 | +import io.opentelemetry.sdk.trace.data.ExceptionEventData; |
| 15 | +import java.io.PrintWriter; |
| 16 | +import java.io.StringWriter; |
| 17 | +import javax.annotation.concurrent.Immutable; |
| 18 | + |
| 19 | +/** |
| 20 | + * An {@link ExceptionEventData} implementation with {@link #getAttributes()} lazily evaluated, |
| 21 | + * allowing the (relatively) expensive exception attribute rendering to take place off the hot path. |
| 22 | + * |
| 23 | + * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change |
| 24 | + * at any time. |
| 25 | + */ |
| 26 | +@AutoValue |
| 27 | +@Immutable |
| 28 | +public abstract class LazyExceptionEventData implements ExceptionEventData { |
| 29 | + |
| 30 | + private static final AttributeKey<String> EXCEPTION_TYPE = |
| 31 | + AttributeKey.stringKey("exception.type"); |
| 32 | + private static final AttributeKey<String> EXCEPTION_MESSAGE = |
| 33 | + AttributeKey.stringKey("exception.message"); |
| 34 | + private static final AttributeKey<String> EXCEPTION_STACKTRACE = |
| 35 | + AttributeKey.stringKey("exception.stacktrace"); |
| 36 | + private static final String EXCEPTION_EVENT_NAME = "exception"; |
| 37 | + |
| 38 | + @Override |
| 39 | + public final String getName() { |
| 40 | + return EXCEPTION_EVENT_NAME; |
| 41 | + } |
| 42 | + |
| 43 | + /** TODO. */ |
| 44 | + public static ExceptionEventData create( |
| 45 | + long epochNanos, |
| 46 | + Throwable exception, |
| 47 | + Attributes additionalAttributes, |
| 48 | + SpanLimits spanLimits) { |
| 49 | + return new AutoValue_LazyExceptionEventData( |
| 50 | + epochNanos, exception, spanLimits, additionalAttributes); |
| 51 | + } |
| 52 | + |
| 53 | + abstract SpanLimits getSpanLimits(); |
| 54 | + |
| 55 | + public abstract Attributes getAdditionalAttributes(); |
| 56 | + |
| 57 | + @Override |
| 58 | + @Memoized |
| 59 | + public Attributes getAttributes() { |
| 60 | + Throwable exception = getException(); |
| 61 | + SpanLimits spanLimits = getSpanLimits(); |
| 62 | + Attributes additionalAttributes = getAdditionalAttributes(); |
| 63 | + |
| 64 | + AttributesMap attributes = |
| 65 | + AttributesMap.create( |
| 66 | + spanLimits.getMaxNumberOfAttributes(), spanLimits.getMaxAttributeValueLength()); |
| 67 | + String exceptionName = exception.getClass().getCanonicalName(); |
| 68 | + String exceptionMessage = exception.getMessage(); |
| 69 | + StringWriter stringWriter = new StringWriter(); |
| 70 | + try (PrintWriter printWriter = new PrintWriter(stringWriter)) { |
| 71 | + exception.printStackTrace(printWriter); |
| 72 | + } |
| 73 | + String stackTrace = stringWriter.toString(); |
| 74 | + |
| 75 | + if (exceptionName != null) { |
| 76 | + attributes.put(EXCEPTION_TYPE, exceptionName); |
| 77 | + } |
| 78 | + if (exceptionMessage != null) { |
| 79 | + attributes.put(EXCEPTION_MESSAGE, exceptionMessage); |
| 80 | + } |
| 81 | + if (stackTrace != null) { |
| 82 | + attributes.put(EXCEPTION_STACKTRACE, stackTrace); |
| 83 | + } |
| 84 | + |
| 85 | + additionalAttributes.forEach(attributes::put); |
| 86 | + |
| 87 | + return attributes.immutableCopy(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int getTotalAttributeCount() { |
| 92 | + // getAttributes() lazily adds 3 attributes to getAdditionalAttributes(): |
| 93 | + // - exception.type |
| 94 | + // - exception.message |
| 95 | + // - exception.stacktrace |
| 96 | + return getAdditionalAttributes().size() + 3; |
| 97 | + } |
| 98 | + |
| 99 | + LazyExceptionEventData() {} |
| 100 | +} |
0 commit comments