|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.stacktrace.internal; |
| 7 | + |
| 8 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 9 | +import io.opentelemetry.context.Context; |
| 10 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 11 | +import io.opentelemetry.sdk.trace.ReadWriteSpan; |
| 12 | +import io.opentelemetry.sdk.trace.ReadableSpan; |
| 13 | +import io.opentelemetry.sdk.trace.SpanProcessor; |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +/** |
| 17 | + * A @{@link SpanProcessor} which in addition to all standard operations is capable of modifying and |
| 18 | + * optionally filtering spans in the end-callback. |
| 19 | + * |
| 20 | + * <p>This is done by chaining processors and registering only the first processor with the SDK. |
| 21 | + * Mutations can be performed in {@link #doOnEnd(ReadableSpan)} by wrapping the span in a {@link |
| 22 | + * MutableSpan} |
| 23 | + */ |
| 24 | +public abstract class AbstractSimpleChainingSpanProcessor implements SpanProcessor { |
| 25 | + |
| 26 | + protected final SpanProcessor next; |
| 27 | + private final boolean nextRequiresStart; |
| 28 | + private final boolean nextRequiresEnd; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param next the next processor to be invoked after the one being constructed. |
| 32 | + */ |
| 33 | + public AbstractSimpleChainingSpanProcessor(SpanProcessor next) { |
| 34 | + this.next = next; |
| 35 | + nextRequiresStart = next.isStartRequired(); |
| 36 | + nextRequiresEnd = next.isEndRequired(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Equivalent of {@link SpanProcessor#onStart(Context, ReadWriteSpan)}. The onStart callback of |
| 41 | + * the next processor must not be invoked from this method, this is already handled by the |
| 42 | + * implementation of {@link #onStart(Context, ReadWriteSpan)}. |
| 43 | + */ |
| 44 | + protected void doOnStart(Context context, ReadWriteSpan readWriteSpan) {} |
| 45 | + |
| 46 | + /** |
| 47 | + * Equivalent of {@link SpanProcessor#onEnd(ReadableSpan)}}. |
| 48 | + * |
| 49 | + * <p>If this method returns null, the provided span will be dropped and not passed to the next |
| 50 | + * processor. If anything non-null is returned, the returned instance is passed to the next |
| 51 | + * processor. |
| 52 | + * |
| 53 | + * <p>So in order to mutate the span, simply use {@link MutableSpan#makeMutable(ReadableSpan)} on |
| 54 | + * the provided argument and return the {@link MutableSpan} from this method. |
| 55 | + */ |
| 56 | + @CanIgnoreReturnValue |
| 57 | + protected ReadableSpan doOnEnd(ReadableSpan readableSpan) { |
| 58 | + return readableSpan; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Indicates if span processor needs to be called on span start |
| 63 | + * |
| 64 | + * @return true, if this implementation would like {@link #doOnStart(Context, ReadWriteSpan)} to |
| 65 | + * be invoked. |
| 66 | + */ |
| 67 | + protected boolean requiresStart() { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Indicates if span processor needs to be called on span end |
| 73 | + * |
| 74 | + * @return true, if this implementation would like {@link #doOnEnd(ReadableSpan)} to be invoked. |
| 75 | + */ |
| 76 | + protected boolean requiresEnd() { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + protected CompletableResultCode doForceFlush() { |
| 81 | + return CompletableResultCode.ofSuccess(); |
| 82 | + } |
| 83 | + |
| 84 | + protected CompletableResultCode doShutdown() { |
| 85 | + return CompletableResultCode.ofSuccess(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public final void onStart(Context context, ReadWriteSpan readWriteSpan) { |
| 90 | + try { |
| 91 | + if (requiresStart()) { |
| 92 | + doOnStart(context, readWriteSpan); |
| 93 | + } |
| 94 | + } finally { |
| 95 | + if (nextRequiresStart) { |
| 96 | + next.onStart(context, readWriteSpan); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public final void onEnd(ReadableSpan readableSpan) { |
| 103 | + ReadableSpan mappedTo = readableSpan; |
| 104 | + try { |
| 105 | + if (requiresEnd()) { |
| 106 | + mappedTo = doOnEnd(readableSpan); |
| 107 | + } |
| 108 | + } finally { |
| 109 | + if (mappedTo != null && nextRequiresEnd) { |
| 110 | + next.onEnd(mappedTo); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public final boolean isStartRequired() { |
| 117 | + return requiresStart() || nextRequiresStart; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public final boolean isEndRequired() { |
| 122 | + return requiresEnd() || nextRequiresEnd; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public final CompletableResultCode shutdown() { |
| 127 | + return CompletableResultCode.ofAll(Arrays.asList(doShutdown(), next.shutdown())); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public final CompletableResultCode forceFlush() { |
| 132 | + return CompletableResultCode.ofAll(Arrays.asList(doForceFlush(), next.forceFlush())); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public final void close() { |
| 137 | + SpanProcessor.super.close(); |
| 138 | + } |
| 139 | +} |
0 commit comments