From eb4a18febcfa508b1cc8ae60156160dbccadc0d8 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Sun, 4 Oct 2020 19:33:40 -0700 Subject: [PATCH] Remove Status object from API, keep StatusCanonicalCode (#1741) * Remove Status object from API, keep StatusCanonicalCode Signed-off-by: Bogdan Drutu * Respond to comments, change more old usages Signed-off-by: Bogdan Drutu * Fix build, run gojf Signed-off-by: Bogdan Drutu --- .../io/opentelemetry/trace/DefaultSpan.java | 3 +- .../java/io/opentelemetry/trace/Span.java | 12 +- .../java/io/opentelemetry/trace/Status.java | 204 ------------------ .../trace/StatusCanonicalCode.java | 43 ++++ .../opentelemetry/trace/DefaultSpanTest.java | 2 +- .../io/opentelemetry/trace/StatusTest.java | 41 ---- .../inmemory/InMemorySpanExporterTest.java | 4 +- .../exporters/jaeger/AdapterTest.java | 8 +- .../jaeger/JaegerGrpcSpanExporterTest.java | 4 +- .../logging/LoggingSpanExporterTest.java | 4 +- .../exporters/otlp/SpanAdapter.java | 6 +- .../otlp/OtlpGrpcSpanExporterTest.java | 23 +- .../exporters/otlp/SpanAdapterTest.java | 16 +- .../exporters/zipkin/ZipkinSpanExporter.java | 3 +- .../ZipkinSpanExporterEndToEndHttpTest.java | 4 +- .../zipkin/ZipkinSpanExporterTest.java | 8 +- .../extensions/trace/CurrentSpanUtils.java | 6 +- .../trace/CurrentSpanUtilsTest.java | 14 +- .../opentracingshim/SpanBuilderShim.java | 4 +- .../opentracingshim/SpanShim.java | 12 +- .../errorreporting/ErrorReportingTest.java | 10 +- .../sdk/trace/SpanBenchmark.java | 4 +- .../sdk/trace/SpanPipelineBenchmark.java | 4 +- .../export/MultiSpanExporterBenchmark.java | 4 +- .../sdk/trace/RecordEventsReadableSpan.java | 18 +- .../opentelemetry/sdk/trace/SpanWrapper.java | 1 - .../sdk/trace/data/ImmutableStatus.java | 68 ++++++ .../sdk/trace/data/SpanData.java | 45 +++- .../trace/RecordEventsReadableSpanTest.java | 43 ++-- .../io/opentelemetry/sdk/trace/TestUtils.java | 4 +- .../errorreporting/ErrorReportingTest.java | 21 +- .../trace/data/DelegatingSpanData.java | 1 - .../incubator/trace/data/SpanDataBuilder.java | 1 - .../trace/data/DelegatingSpanDataTest.java | 4 +- .../trace/data/SpanDataBuilderTest.java | 12 +- .../zpages/TracezDataAggregatorBenchmark.java | 4 +- .../zpages/TracezDataAggregator.java | 8 +- .../extensions/zpages/TracezSpanBuckets.java | 15 +- .../extensions/zpages/TracezZPageHandler.java | 7 +- .../zpages/TracezDataAggregatorTest.java | 23 +- .../zpages/TracezSpanProcessorTest.java | 4 +- .../zpages/TracezZPageHandlerTest.java | 15 +- .../opentelemetry/sdk/trace/TestSpanData.java | 1 - .../sdk/trace/TestSpanDataTest.java | 4 +- 44 files changed, 326 insertions(+), 416 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/trace/Status.java create mode 100644 api/src/main/java/io/opentelemetry/trace/StatusCanonicalCode.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/StatusTest.java create mode 100644 sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/ImmutableStatus.java diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java index 72d5a642c83..1ca6ea0eee2 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java @@ -7,6 +7,7 @@ import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -77,7 +78,7 @@ public void addEvent(String name, Attributes attributes) {} public void addEvent(String name, Attributes attributes, long timestamp) {} @Override - public void setStatus(Status status) {} + public void setStatus(StatusCanonicalCode canonicalCode, @Nullable String description) {} @Override public void recordException(Throwable exception) {} diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 919202762ce..743001a46dd 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -9,6 +9,7 @@ import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; /** @@ -201,17 +202,20 @@ default void setAttribute(AttributeKey key, int value) { void addEvent(String name, Attributes attributes, long timestamp); /** - * Sets the {@link Status} to the {@code Span}. + * Sets the status to the {@code Span}. * - *

If used, this will override the default {@code Span} status. Default is {@link Status#OK}. + *

If used, this will override the default {@code Span} status. Default status code is {@link + * StatusCanonicalCode#UNSET}. * *

Only the value of the last call will be recorded, and implementations are free to ignore * previous calls. * - * @param status the {@link Status} to set. + * @param canonicalCode the {@link StatusCanonicalCode} to set. + * @param description the description of the {@code Status}. * @since 0.1.0 */ - void setStatus(Status status); + // TODO: Add a setStatus overload without description. + void setStatus(StatusCanonicalCode canonicalCode, @Nullable String description); /** * Records information about the {@link Throwable} to the {@link Span}. diff --git a/api/src/main/java/io/opentelemetry/trace/Status.java b/api/src/main/java/io/opentelemetry/trace/Status.java deleted file mode 100644 index 288106a5ea9..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/Status.java +++ /dev/null @@ -1,204 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.trace; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Map; -import java.util.Objects; -import java.util.TreeMap; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * Defines the status of a {@link Span} by providing a standard {@link CanonicalCode} in conjunction - * with an optional descriptive message. Instances of {@code Status} are created by starting with - * the template for the appropriate {@link Status.CanonicalCode} and supplementing it with - * additional information: {@code Status.NOT_FOUND.withDescription("Could not find - * 'important_file.txt'");} - * - * @since 0.1.0 - */ -@Immutable -public final class Status { - /** - * The set of canonical status codes. If new codes are added over time they must choose a - * numerical value that does not collide with any previously used value. - * - * @since 0.1.0 - */ - public enum CanonicalCode { - - /** - * The operation has been validated by an Application developers or Operator to have completed - * successfully. - */ - OK(0), - - /** The default status. */ - UNSET(1), - - /** The operation contains an error. */ - ERROR(2); - - private final int value; - - CanonicalCode(int value) { - this.value = value; - } - - /** - * Returns the numerical value of the code. - * - * @return the numerical value of the code. - * @since 0.1.0 - */ - public int value() { - return value; - } - - /** - * Returns the status that has the current {@code CanonicalCode}.. - * - * @return the status that has the current {@code CanonicalCode}. - * @since 0.1.0 - */ - public Status toStatus() { - return STATUSES_BY_VALUE.get(value); - } - } - - // Create the canonical list of Status instances indexed by their code values. - private static final Map STATUSES_BY_VALUE = buildStatusList(); - - private static Map buildStatusList() { - TreeMap canonicalizer = new TreeMap<>(); - for (CanonicalCode code : CanonicalCode.values()) { - Status replaced = canonicalizer.put(code.value(), new Status(code, null)); - if (replaced != null) { - throw new IllegalStateException( - "Code value duplication between " - + replaced.getCanonicalCode().name() - + " & " - + code.name()); - } - } - return Collections.unmodifiableMap(canonicalizer); - } - - // A pseudo-enum of Status instances mapped 1:1 with values in CanonicalCode. This simplifies - // construction patterns for derived instances of Status. - /** - * The operation has been validated by an Application developers or Operator to have completed - * successfully. - */ - public static final Status OK = CanonicalCode.OK.toStatus(); - - /** The default status. */ - public static final Status UNSET = CanonicalCode.UNSET.toStatus(); - - /** The operation contains an error. */ - public static final Status ERROR = CanonicalCode.ERROR.toStatus(); - - // The canonical code of this message. - private final CanonicalCode canonicalCode; - - // An additional error message. - @Nullable private final String description; - - private Status(CanonicalCode canonicalCode, @Nullable String description) { - this.canonicalCode = Objects.requireNonNull(canonicalCode, "canonicalCode"); - this.description = description; - } - - /** - * Creates a derived instance of {@code Status} with the given description. - * - * @param description the new description of the {@code Status}. - * @return The newly created {@code Status} with the given description. - * @since 0.1.0 - */ - public Status withDescription(@Nullable String description) { - if (Objects.equals(this.description, description)) { - return this; - } - return new Status(this.canonicalCode, description); - } - - /** - * Returns the canonical status code. - * - * @return the canonical status code. - * @since 0.1.0 - */ - public CanonicalCode getCanonicalCode() { - return canonicalCode; - } - - /** - * Returns the description of this {@code Status} for human consumption. - * - * @return the description of this {@code Status}. - * @since 0.1.0 - */ - @Nullable - public String getDescription() { - return description; - } - - /** - * Returns {@code true} if this {@code Status} is UNSET, i.e., not an error. - * - * @return {@code true} if this {@code Status} is UNSET. - */ - public boolean isUnset() { - return CanonicalCode.UNSET == canonicalCode; - } - - /** - * Returns {@code true} if this {@code Status} is ok, i.e., status is not set, or has been - * overridden to be ok by an operator. - * - * @return {@code true} if this {@code Status} is OK or UNSET. - */ - public boolean isOk() { - return isUnset() || CanonicalCode.OK == canonicalCode; - } - - /** - * Equality on Statuses is not well defined. Instead, do comparison based on their CanonicalCode - * with {@link #getCanonicalCode}. The description of the Status is unlikely to be stable, and - * additional fields may be added to Status in the future. - */ - @Override - public boolean equals(@Nullable Object obj) { - if (obj == this) { - return true; - } - - if (!(obj instanceof Status)) { - return false; - } - - Status that = (Status) obj; - return canonicalCode == that.canonicalCode && Objects.equals(description, that.description); - } - - /** - * Hash codes on Statuses are not well defined. - * - * @see #equals - */ - @Override - public int hashCode() { - return Arrays.hashCode(new Object[] {canonicalCode, description}); - } - - @Override - public String toString() { - return "Status{canonicalCode=" + canonicalCode + ", description=" + description + "}"; - } -} diff --git a/api/src/main/java/io/opentelemetry/trace/StatusCanonicalCode.java b/api/src/main/java/io/opentelemetry/trace/StatusCanonicalCode.java new file mode 100644 index 00000000000..b92b34ae03f --- /dev/null +++ b/api/src/main/java/io/opentelemetry/trace/StatusCanonicalCode.java @@ -0,0 +1,43 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.trace; + +/** + * The set of canonical status codes. If new codes are added over time they must choose a numerical + * value that does not collide with any previously used value. + * + * @since 0.1.0 + */ +public enum StatusCanonicalCode { + + /** + * The operation has been validated by an Application developers or Operator to have completed + * successfully. + */ + OK(0), + + /** The default status. */ + UNSET(1), + + /** The operation contains an error. */ + ERROR(2); + + private final int value; + + StatusCanonicalCode(int value) { + this.value = value; + } + + /** + * Returns the numerical value of the code. + * + * @return the numerical value of the code. + * @since 0.1.0 + */ + public int value() { + return value; + } +} diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java index 2b1f683add2..d01f078c7c0 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java @@ -45,7 +45,7 @@ void doNotCrash() { span.addEvent("event", 0); span.addEvent("event", Attributes.of(booleanKey("MyBooleanAttributeKey"), true)); span.addEvent("event", Attributes.of(booleanKey("MyBooleanAttributeKey"), true), 0); - span.setStatus(Status.OK); + span.setStatus(StatusCanonicalCode.OK, null); span.recordException(new IllegalStateException()); span.recordException(new IllegalStateException(), Attributes.empty()); span.end(); diff --git a/api/src/test/java/io/opentelemetry/trace/StatusTest.java b/api/src/test/java/io/opentelemetry/trace/StatusTest.java deleted file mode 100644 index 60ca1f9cc2a..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/StatusTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.trace; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.common.testing.EqualsTester; -import org.junit.jupiter.api.Test; - -/** Unit tests for {@link Status}. */ -class StatusTest { - @Test - void status_Ok() { - assertThat(Status.OK.getCanonicalCode()).isEqualTo(Status.CanonicalCode.OK); - assertThat(Status.OK.getDescription()).isNull(); - assertThat(Status.OK.isOk()).isTrue(); - assertThat(Status.UNSET.isOk()).isTrue(); - } - - @Test - void createStatus_WithDescription() { - Status status = Status.ERROR.withDescription("This is an error."); - assertThat(status.getCanonicalCode()).isEqualTo(Status.CanonicalCode.ERROR); - assertThat(status.getDescription()).isEqualTo("This is an error."); - assertThat(status.isUnset()).isFalse(); - } - - @Test - void status_EqualsAndHashCode() { - EqualsTester tester = new EqualsTester(); - tester.addEqualityGroup(Status.OK, Status.OK.withDescription(null)); - tester.addEqualityGroup( - Status.ERROR.withDescription("ThisIsAnError"), - Status.ERROR.withDescription("ThisIsAnError")); - tester.addEqualityGroup(Status.ERROR.withDescription("This is an error.")); - tester.testEquals(); - } -} diff --git a/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java b/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java index 791aec5798b..228ccba221c 100644 --- a/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java +++ b/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java @@ -9,11 +9,11 @@ import io.opentelemetry.sdk.trace.TestSpanData; import io.opentelemetry.sdk.trace.TracerSdkProvider; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.Tracer; import java.util.Collections; @@ -94,7 +94,7 @@ static SpanData makeBasicSpan() { .setName("span") .setKind(Kind.SERVER) .setStartEpochNanos(100_000_000_100L) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setEndEpochNanos(200_000_000_200L) .setTotalRecordedLinks(0) .setTotalRecordedEvents(0) diff --git a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java index 2f8d648a079..2607ee39cbf 100644 --- a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java +++ b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java @@ -26,11 +26,11 @@ import io.opentelemetry.sdk.trace.TestSpanData; import io.opentelemetry.sdk.trace.data.ImmutableEvent; import io.opentelemetry.sdk.trace.data.ImmutableLink; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; import java.util.Arrays; @@ -220,7 +220,7 @@ void testStatusNotUnset() { .setStartEpochNanos(TimeUnit.MILLISECONDS.toNanos(startMs)) .setEndEpochNanos(TimeUnit.MILLISECONDS.toNanos(endMs)) .setKind(Span.Kind.SERVER) - .setStatus(Status.ERROR) + .setStatus(ImmutableStatus.ERROR) .setTotalRecordedEvents(0) .setTotalRecordedLinks(0) .build(); @@ -247,7 +247,7 @@ void testSpanError() { .setStartEpochNanos(TimeUnit.MILLISECONDS.toNanos(startMs)) .setEndEpochNanos(TimeUnit.MILLISECONDS.toNanos(endMs)) .setKind(Span.Kind.SERVER) - .setStatus(Status.ERROR) + .setStatus(ImmutableStatus.ERROR) .setAttributes(attributes) .setTotalRecordedEvents(0) .setTotalRecordedLinks(0) @@ -289,7 +289,7 @@ private static SpanData getSpanData(long startMs, long endMs) { .setTotalRecordedLinks(1) .setKind(Span.Kind.SERVER) .setResource(Resource.create(Attributes.empty())) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .build(); } diff --git a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/JaegerGrpcSpanExporterTest.java b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/JaegerGrpcSpanExporterTest.java index 96c3fdaaa94..2ce91b9ee7d 100644 --- a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/JaegerGrpcSpanExporterTest.java +++ b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/JaegerGrpcSpanExporterTest.java @@ -27,9 +27,9 @@ import io.opentelemetry.sdk.common.export.ConfigBuilder; import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils; import io.opentelemetry.sdk.trace.TestSpanData; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import java.net.InetAddress; import java.util.Collections; import java.util.HashMap; @@ -86,7 +86,7 @@ void testExport() throws Exception { .setName("GET /api/endpoint") .setStartEpochNanos(TimeUnit.MILLISECONDS.toNanos(startMs)) .setEndEpochNanos(TimeUnit.MILLISECONDS.toNanos(endMs)) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setKind(Kind.CONSUMER) .setLinks(Collections.emptyList()) .setTotalRecordedLinks(0) diff --git a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java index 9701fe4f82b..9fe5916bb3b 100644 --- a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java +++ b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java @@ -13,10 +13,10 @@ import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.trace.TestSpanData; import io.opentelemetry.sdk.trace.data.ImmutableEvent; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceId; import java.util.Collections; import java.util.concurrent.TimeUnit; @@ -53,7 +53,7 @@ void returnCode() { .setSpanId(SpanId.fromLong(9876L)) .setStartEpochNanos(epochNanos) .setEndEpochNanos(epochNanos + 1000) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setName("testSpan") .setKind(Kind.INTERNAL) .setEvents( diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java index 0b9a2f50d85..765f1e41861 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java @@ -27,7 +27,7 @@ import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status.CanonicalCode; +import io.opentelemetry.trace.StatusCanonicalCode; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -169,10 +169,10 @@ public void consume(AttributeKey key, T value) { return builder.build(); } - static Status toStatusProto(io.opentelemetry.trace.Status status) { + static Status toStatusProto(SpanData.Status status) { // todo: Update this when the proto definitions are updated to include UNSET and ERROR StatusCode protoStatusCode = StatusCode.STATUS_CODE_OK; - if (status.getCanonicalCode() == CanonicalCode.ERROR) { + if (status.getCanonicalCode() == StatusCanonicalCode.ERROR) { protoStatusCode = StatusCode.STATUS_CODE_UNKNOWN_ERROR; } Status.Builder builder = Status.newBuilder().setCode(protoStatusCode); diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/OtlpGrpcSpanExporterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/OtlpGrpcSpanExporterTest.java index b9fbba1758f..75d12b5e703 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/OtlpGrpcSpanExporterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/OtlpGrpcSpanExporterTest.java @@ -11,6 +11,7 @@ import com.google.common.io.Closer; import io.grpc.ManagedChannel; import io.grpc.Server; +import io.grpc.Status; import io.grpc.Status.Code; import io.grpc.inprocess.InProcessChannelBuilder; import io.grpc.inprocess.InProcessServerBuilder; @@ -22,9 +23,9 @@ import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.trace.TestSpanData; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -148,7 +149,7 @@ void testExport_AfterShutdown() { @Test void testExport_Cancelled() { - fakeCollector.setReturnedStatus(io.grpc.Status.CANCELLED); + fakeCollector.setReturnedStatus(Status.CANCELLED); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -161,7 +162,7 @@ void testExport_Cancelled() { @Test void testExport_DeadlineExceeded() { - fakeCollector.setReturnedStatus(io.grpc.Status.DEADLINE_EXCEEDED); + fakeCollector.setReturnedStatus(Status.DEADLINE_EXCEEDED); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -174,7 +175,7 @@ void testExport_DeadlineExceeded() { @Test void testExport_ResourceExhausted() { - fakeCollector.setReturnedStatus(io.grpc.Status.RESOURCE_EXHAUSTED); + fakeCollector.setReturnedStatus(Status.RESOURCE_EXHAUSTED); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -187,7 +188,7 @@ void testExport_ResourceExhausted() { @Test void testExport_OutOfRange() { - fakeCollector.setReturnedStatus(io.grpc.Status.OUT_OF_RANGE); + fakeCollector.setReturnedStatus(Status.OUT_OF_RANGE); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -200,7 +201,7 @@ void testExport_OutOfRange() { @Test void testExport_Unavailable() { - fakeCollector.setReturnedStatus(io.grpc.Status.UNAVAILABLE); + fakeCollector.setReturnedStatus(Status.UNAVAILABLE); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -213,7 +214,7 @@ void testExport_Unavailable() { @Test void testExport_DataLoss() { - fakeCollector.setReturnedStatus(io.grpc.Status.DATA_LOSS); + fakeCollector.setReturnedStatus(Status.DATA_LOSS); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -226,7 +227,7 @@ void testExport_DataLoss() { @Test void testExport_PermissionDenied() { - fakeCollector.setReturnedStatus(io.grpc.Status.PERMISSION_DENIED); + fakeCollector.setReturnedStatus(Status.PERMISSION_DENIED); OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.newBuilder().setChannel(inProcessChannel).build(); try { @@ -248,7 +249,7 @@ private static SpanData generateFakeSpan() { .setName("GET /api/endpoint") .setStartEpochNanos(startNs) .setEndEpochNanos(endNs) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setKind(Kind.SERVER) .setLinks(Collections.emptyList()) .setTotalRecordedLinks(0) @@ -258,7 +259,7 @@ private static SpanData generateFakeSpan() { private static final class FakeCollector extends TraceServiceGrpc.TraceServiceImplBase { private final List receivedSpans = new ArrayList<>(); - private io.grpc.Status returnedStatus = io.grpc.Status.OK; + private Status returnedStatus = Status.OK; @Override public void export( @@ -281,7 +282,7 @@ List getReceivedSpans() { return receivedSpans; } - void setReturnedStatus(io.grpc.Status returnedStatus) { + void setReturnedStatus(Status returnedStatus) { this.returnedStatus = returnedStatus; } } diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java index 74253a3966f..f0bff9a6da0 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java @@ -25,6 +25,7 @@ import io.opentelemetry.sdk.trace.TestSpanData; import io.opentelemetry.sdk.trace.data.ImmutableEvent; import io.opentelemetry.sdk.trace.data.ImmutableLink; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; @@ -67,7 +68,7 @@ void toProtoSpan() { .setTotalRecordedEvents(3) .setLinks(Collections.singletonList(ImmutableLink.create(SPAN_CONTEXT))) .setTotalRecordedLinks(2) - .setStatus(io.opentelemetry.trace.Status.OK) + .setStatus(ImmutableStatus.OK) .build()); assertThat(span.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES); @@ -109,20 +110,15 @@ void toProtoSpanKind() { @Test void toProtoStatus() { - assertThat(SpanAdapter.toStatusProto(io.opentelemetry.trace.Status.UNSET)) + assertThat(SpanAdapter.toStatusProto(ImmutableStatus.UNSET)) .isEqualTo(Status.newBuilder().setCode(STATUS_CODE_OK).build()); - assertThat( - SpanAdapter.toStatusProto(io.opentelemetry.trace.Status.ERROR.withDescription("ERROR"))) + assertThat(SpanAdapter.toStatusProto(ImmutableStatus.ERROR.withDescription("ERROR"))) .isEqualTo( Status.newBuilder().setCode(STATUS_CODE_UNKNOWN_ERROR).setMessage("ERROR").build()); - assertThat( - SpanAdapter.toStatusProto( - io.opentelemetry.trace.Status.ERROR.withDescription("UNKNOWN"))) + assertThat(SpanAdapter.toStatusProto(ImmutableStatus.ERROR.withDescription("UNKNOWN"))) .isEqualTo( Status.newBuilder().setCode(STATUS_CODE_UNKNOWN_ERROR).setMessage("UNKNOWN").build()); - assertThat( - SpanAdapter.toStatusProto( - io.opentelemetry.trace.Status.OK.withDescription("OK_OVERRIDE"))) + assertThat(SpanAdapter.toStatusProto(ImmutableStatus.OK.withDescription("OK_OVERRIDE"))) .isEqualTo(Status.newBuilder().setCode(STATUS_CODE_OK).setMessage("OK_OVERRIDE").build()); } diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index 9451898f8ac..6d7b6eee15f 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -21,7 +21,6 @@ import io.opentelemetry.sdk.trace.export.SpanExporter; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.attributes.SemanticAttributes; import java.io.IOException; import java.net.InetAddress; @@ -140,7 +139,7 @@ public void consume(AttributeKey key, T value) { spanBuilder.putTag(key.getKey(), valueToString(key, value)); } }); - Status status = spanData.getStatus(); + SpanData.Status status = spanData.getStatus(); // for GRPC spans, include status code & description. if (status != null && spanAttributes.get(SemanticAttributes.RPC_SERVICE) != null) { spanBuilder.putTag(OTEL_STATUS_CODE, status.getCanonicalCode().toString()); diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterEndToEndHttpTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterEndToEndHttpTest.java index e06fde8adf8..b22ac9d3555 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterEndToEndHttpTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterEndToEndHttpTest.java @@ -12,10 +12,10 @@ import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.trace.TestSpanData; import io.opentelemetry.sdk.trace.data.ImmutableEvent; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -143,7 +143,7 @@ private static TestSpanData.Builder buildStandardSpan() { .setSpanId(SPAN_ID) .setParentSpanId(PARENT_SPAN_ID) .setSampled(true) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setKind(Kind.SERVER) .setHasRemoteParent(true) .setName(SPAN_NAME) diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java index 532ba87056b..5e7c1af0def 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java @@ -28,10 +28,10 @@ import io.opentelemetry.sdk.resources.ResourceAttributes; import io.opentelemetry.sdk.trace.TestSpanData; import io.opentelemetry.sdk.trace.data.ImmutableEvent; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.attributes.SemanticAttributes; import java.io.IOException; import java.util.Arrays; @@ -198,7 +198,7 @@ void generateSpan_AlreadyHasHttpStatusInfo() { buildStandardSpan() .setAttributes(attributeMap) .setKind(Kind.CLIENT) - .setStatus(Status.ERROR) + .setStatus(ImmutableStatus.ERROR) .build(); assertThat(ZipkinSpanExporter.generateSpan(data, localEndpoint)) @@ -219,7 +219,7 @@ void generateSpan_WithRpcErrorStatus() { SpanData data = buildStandardSpan() - .setStatus(Status.ERROR.withDescription(errorMessage)) + .setStatus(ImmutableStatus.ERROR.withDescription(errorMessage)) .setAttributes(attributeMap) .build(); @@ -309,7 +309,7 @@ private static TestSpanData.Builder buildStandardSpan() { .setSpanId(SPAN_ID) .setParentSpanId(PARENT_SPAN_ID) .setSampled(true) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setKind(Kind.SERVER) .setHasRemoteParent(true) .setName("Recv.helloworld.Greeter.SayHello") diff --git a/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/CurrentSpanUtils.java b/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/CurrentSpanUtils.java index 924f59149db..4e09e8e0de7 100644 --- a/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/CurrentSpanUtils.java +++ b/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/CurrentSpanUtils.java @@ -7,7 +7,7 @@ import io.grpc.Context; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.TracingContextUtils; import java.util.concurrent.Callable; @@ -110,7 +110,7 @@ public V call() throws Exception { private static void setErrorStatus(Span span, Throwable t) { span.setStatus( - Status.ERROR.withDescription( - t.getMessage() == null ? t.getClass().getSimpleName() : t.getMessage())); + StatusCanonicalCode.ERROR, + t.getMessage() == null ? t.getClass().getSimpleName() : t.getMessage()); } } diff --git a/extensions/trace_utils/src/test/java/io/opentelemetry/extensions/trace/CurrentSpanUtilsTest.java b/extensions/trace_utils/src/test/java/io/opentelemetry/extensions/trace/CurrentSpanUtilsTest.java index 43b8e505254..10c5e23286c 100644 --- a/extensions/trace_utils/src/test/java/io/opentelemetry/extensions/trace/CurrentSpanUtilsTest.java +++ b/extensions/trace_utils/src/test/java/io/opentelemetry/extensions/trace/CurrentSpanUtilsTest.java @@ -11,7 +11,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.TracingContextUtils; import java.util.concurrent.Callable; import org.junit.jupiter.api.BeforeEach; @@ -89,7 +89,7 @@ void withSpanRunnable_WithError() { throw error; }; executeRunnableAndExpectError(runnable, error); - verify(span).setStatus(Status.ERROR.withDescription("MyError")); + verify(span).setStatus(StatusCanonicalCode.ERROR, "MyError"); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -105,7 +105,7 @@ void withSpanRunnable_WithErrorNoMessage() { throw error; }; executeRunnableAndExpectError(runnable, error); - verify(span).setStatus(Status.ERROR.withDescription("AssertionError")); + verify(span).setStatus(StatusCanonicalCode.ERROR, "AssertionError"); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -151,7 +151,7 @@ void withSpanCallable_WithException() { throw exception; }; executeCallableAndExpectError(callable, exception); - verify(span).setStatus(Status.ERROR.withDescription("MyException")); + verify(span).setStatus(StatusCanonicalCode.ERROR, "MyException"); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -167,7 +167,7 @@ void withSpanCallable_WithExceptionNoMessage() { throw exception; }; executeCallableAndExpectError(callable, exception); - verify(span).setStatus(Status.ERROR.withDescription("Exception")); + verify(span).setStatus(StatusCanonicalCode.ERROR, "Exception"); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -183,7 +183,7 @@ void withSpanCallable_WithError() { throw error; }; executeCallableAndExpectError(callable, error); - verify(span).setStatus(Status.ERROR.withDescription("MyError")); + verify(span).setStatus(StatusCanonicalCode.ERROR, "MyError"); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -199,7 +199,7 @@ void withSpanCallable_WithErrorNoMessage() { throw error; }; executeCallableAndExpectError(callable, error); - verify(span).setStatus(Status.ERROR.withDescription("AssertionError")); + verify(span).setStatus(StatusCanonicalCode.ERROR, "AssertionError"); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java index 378ec3b3d4f..43127953e0b 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java @@ -15,7 +15,7 @@ import io.opentelemetry.common.AttributeKey; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.TracingContextUtils; import io.opentracing.Span; import io.opentracing.SpanContext; @@ -210,7 +210,7 @@ public Span start() { span.setAttribute(key, value); } if (error) { - span.setStatus(Status.ERROR); + span.setStatus(StatusCanonicalCode.ERROR, null); } SpanShim spanShim = new SpanShim(telemetryInfo(), span); diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java index ed70e11e5b0..fce1b189d89 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java @@ -11,7 +11,7 @@ import static io.opentelemetry.common.AttributeKey.stringKey; import io.opentelemetry.common.Attributes; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentracing.Span; import io.opentracing.SpanContext; import io.opentracing.log.Fields; @@ -65,8 +65,9 @@ public Span setTag(String key, String value) { // TODO: confirm we can safely ignore span.kind after Span was created // https://github.com/bogdandrutu/opentelemetry/issues/42 } else if (Tags.ERROR.getKey().equals(key)) { - Status status = Boolean.parseBoolean(value) ? Status.ERROR : Status.UNSET; - span.setStatus(status); + StatusCanonicalCode canonicalCode = + Boolean.parseBoolean(value) ? StatusCanonicalCode.ERROR : StatusCanonicalCode.UNSET; + span.setStatus(canonicalCode, null); } else { span.setAttribute(key, value); } @@ -77,8 +78,9 @@ public Span setTag(String key, String value) { @Override public Span setTag(String key, boolean value) { if (Tags.ERROR.getKey().equals(key)) { - Status status = value ? Status.ERROR : Status.UNSET; - span.setStatus(status); + StatusCanonicalCode canonicalCode = + value ? StatusCanonicalCode.ERROR : StatusCanonicalCode.UNSET; + span.setStatus(canonicalCode, null); } else { span.setAttribute(key, value); } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java index e543c60b463..cb70a0e89a3 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java @@ -17,7 +17,7 @@ import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentracing.Scope; import io.opentracing.Span; import io.opentracing.Tracer; @@ -56,7 +56,7 @@ void testSimpleError() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertEquals(spans.size(), 1); - assertEquals(spans.get(0).getStatus().getCanonicalCode(), Status.ERROR.getCanonicalCode()); + assertEquals(spans.get(0).getStatus().getCanonicalCode(), StatusCanonicalCode.ERROR); } /* Error handling in a callback capturing/activating the Span */ @@ -80,7 +80,7 @@ void testCallbackError() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertEquals(spans.size(), 1); - assertEquals(spans.get(0).getStatus().getCanonicalCode(), Status.ERROR.getCanonicalCode()); + assertEquals(spans.get(0).getStatus().getCanonicalCode(), StatusCanonicalCode.ERROR); } /* Error handling for a max-retries task (such as url fetching). @@ -111,7 +111,7 @@ void testErrorRecovery() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertEquals(spans.size(), 1); - assertEquals(spans.get(0).getStatus().getCanonicalCode(), Status.ERROR.getCanonicalCode()); + assertEquals(spans.get(0).getStatus().getCanonicalCode(), StatusCanonicalCode.ERROR); List events = spans.get(0).getEvents(); assertEquals(events.size(), maxRetries); @@ -148,7 +148,7 @@ void testInstrumentationLayer() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertEquals(spans.size(), 1); - assertEquals(spans.get(0).getStatus().getCanonicalCode(), Status.ERROR.getCanonicalCode()); + assertEquals(spans.get(0).getStatus().getCanonicalCode(), StatusCanonicalCode.ERROR); } static class ScopedRunnable implements Runnable { diff --git a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanBenchmark.java b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanBenchmark.java index c2134a5a276..8bb9579d38a 100644 --- a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanBenchmark.java +++ b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanBenchmark.java @@ -7,7 +7,7 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.Tracer; import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.Benchmark; @@ -81,7 +81,7 @@ public void addAttributesEventsStatusEnd_10Threads() { private static void doSpanWork(RecordEventsReadableSpan span) { span.setAttribute("longAttribute", 33L); span.setAttribute("stringAttribute", "test_value"); - span.setStatus(Status.OK); + span.setStatus(StatusCanonicalCode.OK, null); span.addEvent("testEvent"); span.end(); diff --git a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java index b5e834deb70..1e6f6874594 100644 --- a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java +++ b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java @@ -20,7 +20,7 @@ import io.opentelemetry.sdk.trace.export.SpanExporter; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.Tracer; import java.util.Collection; import java.util.concurrent.TimeUnit; @@ -74,7 +74,7 @@ private void doWork() { span.setAttribute(STRING_ATTRIBUTE_KEY, "test_value"); span.setAttribute(DOUBLE_ATTRIBUTE_KEY, 4844.44d); span.setAttribute(BOOLEAN_ATTRIBUTE_KEY, false); - span.setStatus(Status.OK); + span.setStatus(StatusCanonicalCode.OK, null); span.addEvent("testEvent"); span.end(); diff --git a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/export/MultiSpanExporterBenchmark.java b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/export/MultiSpanExporterBenchmark.java index 898a5d6289f..5715af72706 100644 --- a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/export/MultiSpanExporterBenchmark.java +++ b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/export/MultiSpanExporterBenchmark.java @@ -7,10 +7,10 @@ import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.trace.TestSpanData; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceId; import java.util.Arrays; import java.util.Collection; @@ -73,7 +73,7 @@ public final void setup() { .setName("noop") .setKind(Span.Kind.CLIENT) .setStartEpochNanos(1) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setEndEpochNanos(2) .setHasEnded(true) .build(); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java index babf4a578e1..e4b8749ca07 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java @@ -20,12 +20,13 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.ImmutableEvent; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.EndSpanOptions; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.Tracer; import io.opentelemetry.trace.attributes.SemanticAttributes; import java.io.PrintWriter; @@ -89,7 +90,7 @@ final class RecordEventsReadableSpan implements ReadWriteSpan { // The status of the span. @GuardedBy("lock") @Nullable - private Status status = Status.UNSET; + private SpanData.Status status = ImmutableStatus.UNSET; // The end time of the span. @GuardedBy("lock") private long endEpochNanos; @@ -381,8 +382,8 @@ private void addTimedEvent(ImmutableEvent timedEvent) { } @Override - public void setStatus(Status status) { - if (status == null) { + public void setStatus(StatusCanonicalCode canonicalCode, @Nullable String description) { + if (canonicalCode == null) { return; } synchronized (lock) { @@ -390,7 +391,8 @@ public void setStatus(Status status) { logger.log(Level.FINE, "Calling setStatus() on an ended Span."); return; } - this.status = status; + // TODO: Optimize this to not always allocate a new Status object if description is null. + this.status = ImmutableStatus.create(canonicalCode, description); } } @@ -474,7 +476,7 @@ public boolean isRecording() { } @GuardedBy("lock") - private Status getStatusWithDefault() { + private SpanData.Status getStatusWithDefault() { synchronized (lock) { return status; } @@ -531,13 +533,13 @@ private ReadableAttributes getImmutableAttributes() { public String toString() { String name; String attributes; - Status status; + String status; long totalRecordedEvents; long endEpochNanos; synchronized (lock) { name = this.name; attributes = String.valueOf(this.attributes); - status = this.status; + status = String.valueOf(this.status); totalRecordedEvents = this.totalRecordedEvents; endEpochNanos = this.endEpochNanos; } diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanWrapper.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanWrapper.java index f1ad8419e74..1e00e0aa274 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanWrapper.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanWrapper.java @@ -11,7 +11,6 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceState; import java.util.List; import javax.annotation.concurrent.Immutable; diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/ImmutableStatus.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/ImmutableStatus.java new file mode 100644 index 00000000000..3a7b16dbaf5 --- /dev/null +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/ImmutableStatus.java @@ -0,0 +1,68 @@ +/* + * 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.trace.Span; +import io.opentelemetry.trace.StatusCanonicalCode; +import java.util.Objects; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * Defines the status of a {@link Span} by providing a standard {@link StatusCanonicalCode} in + * conjunction with an optional descriptive message. Instances of {@code Status} are created by + * starting with the template for the appropriate {@link StatusCanonicalCode} and supplementing it + * with additional information: {@code Status.NOT_FOUND.withDescription("Could not find + * 'important_file.txt'");} + */ +@AutoValue +@Immutable +public abstract class ImmutableStatus implements SpanData.Status { + /** + * The operation has been validated by an Application developers or Operator to have completed + * successfully. + */ + public static final ImmutableStatus OK = createInternal(StatusCanonicalCode.OK, null); + + /** The default status. */ + public static final ImmutableStatus UNSET = createInternal(StatusCanonicalCode.UNSET, null); + + /** The operation contains an error. */ + public static final ImmutableStatus ERROR = createInternal(StatusCanonicalCode.ERROR, null); + + /** + * Creates a derived instance of {@code Status} with the given description. + * + * @param description the new description of the {@code Status}. + * @return The newly created {@code Status} with the given description. + * @since 0.1.0 + */ + public static SpanData.Status create( + StatusCanonicalCode canonicalCode, @Nullable String description) { + return createInternal(canonicalCode, description); + } + + private static ImmutableStatus createInternal( + StatusCanonicalCode canonicalCode, @Nullable String description) { + return new AutoValue_ImmutableStatus(canonicalCode, description); + } + + /** + * Creates a derived instance of {@code Status} with the given description. + * + * @param description the new description of the {@code Status}. + * @return The newly created {@code Status} with the given description. + * @since 0.1.0 + */ + // TODO: Consider to remove this in a future PR. Avoid too many changes in the initial PR. + public ImmutableStatus withDescription(@Nullable String description) { + if (Objects.equals(getDescription(), description)) { + return this; + } + return createInternal(getCanonicalCode(), description); + } +} diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/SpanData.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/SpanData.java index 337d123dbb2..0f9de815aa0 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/SpanData.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/data/SpanData.java @@ -13,9 +13,10 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.TraceState; import java.util.List; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -239,4 +240,46 @@ interface Event { */ int getTotalAttributeCount(); } + + /** + * Defines the status of a {@link Span} by providing a standard {@link StatusCanonicalCode} in + * conjunction with an optional descriptive message. + */ + interface Status { + /** + * Returns the canonical status code. + * + * @return the canonical status code. + */ + StatusCanonicalCode getCanonicalCode(); + + /** + * Returns the description of this {@code Status} for human consumption. + * + * @return the description of this {@code Status}. + */ + @Nullable + String getDescription(); + + /** + * Returns {@code true} if this {@code Status} is UNSET, i.e., not an error. + * + * @return {@code true} if this {@code Status} is UNSET. + */ + // TODO: Consider to remove this in a future PR. Avoid too many changes in the initial PR. + default boolean isUnset() { + return StatusCanonicalCode.UNSET == getCanonicalCode(); + } + + /** + * Returns {@code true} if this {@code Status} is ok, i.e., status is not set, or has been + * overridden to be ok by an operator. + * + * @return {@code true} if this {@code Status} is OK or UNSET. + */ + // TODO: Consider to remove this in a future PR. Avoid too many changes in the initial PR. + default boolean isOk() { + return isUnset() || StatusCanonicalCode.OK == getCanonicalCode(); + } + } } diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index 32d02a54c18..191335696fb 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -26,12 +26,13 @@ import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.ImmutableEvent; import io.opentelemetry.sdk.trace.data.ImmutableLink; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.attributes.SemanticAttributes; @@ -101,7 +102,7 @@ void nothingChangedAfterEnd() { span.end(); // Check that adding trace events or update fields after Span#end() does not throw any thrown // and are ignored. - spanDoWork(span, Status.ERROR.withDescription("CANCELLED")); + spanDoWork(span, StatusCanonicalCode.ERROR, "CANCELLED"); SpanData spanData = span.toSpanData(); verifySpanData( spanData, @@ -111,7 +112,7 @@ void nothingChangedAfterEnd() { SPAN_NAME, START_EPOCH_NANOS, START_EPOCH_NANOS, - Status.UNSET, + ImmutableStatus.UNSET, /*hasEnded=*/ true); } @@ -130,7 +131,7 @@ void toSpanData_ActiveSpan() { RecordEventsReadableSpan span = createTestSpan(Kind.INTERNAL); try { assertThat(span.hasEnded()).isFalse(); - spanDoWork(span, null); + spanDoWork(span, null, null); SpanData spanData = span.toSpanData(); Event event = ImmutableEvent.create( @@ -143,7 +144,7 @@ void toSpanData_ActiveSpan() { SPAN_NEW_NAME, START_EPOCH_NANOS, 0, - Status.UNSET, + ImmutableStatus.UNSET, /*hasEnded=*/ false); assertThat(span.hasEnded()).isFalse(); } finally { @@ -156,7 +157,7 @@ void toSpanData_ActiveSpan() { void toSpanData_EndedSpan() { RecordEventsReadableSpan span = createTestSpan(Kind.INTERNAL); try { - spanDoWork(span, Status.ERROR.withDescription("CANCELLED")); + spanDoWork(span, StatusCanonicalCode.ERROR, "CANCELLED"); } finally { span.end(); } @@ -173,7 +174,7 @@ void toSpanData_EndedSpan() { SPAN_NEW_NAME, START_EPOCH_NANOS, testClock.now(), - Status.ERROR.withDescription("CANCELLED"), + ImmutableStatus.ERROR.withDescription("CANCELLED"), /*hasEnded=*/ true); } @@ -201,7 +202,7 @@ void toSpanData_immutableEvents() { void toSpanData_RootSpan() { RecordEventsReadableSpan span = createTestRootSpan(); try { - spanDoWork(span, null); + spanDoWork(span, null, null); } finally { span.end(); } @@ -258,14 +259,15 @@ void setStatus() { RecordEventsReadableSpan span = createTestSpan(Kind.CONSUMER); try { testClock.advanceMillis(MILLIS_PER_SECOND); - assertThat(span.toSpanData().getStatus()).isEqualTo(Status.UNSET); - span.setStatus(Status.ERROR.withDescription("CANCELLED")); + assertThat(span.toSpanData().getStatus()).isEqualTo(ImmutableStatus.UNSET); + span.setStatus(StatusCanonicalCode.ERROR, "CANCELLED"); assertThat(span.toSpanData().getStatus()) - .isEqualTo(Status.ERROR.withDescription("CANCELLED")); + .isEqualTo(ImmutableStatus.ERROR.withDescription("CANCELLED")); } finally { span.end(); } - assertThat(span.toSpanData().getStatus()).isEqualTo(Status.ERROR.withDescription("CANCELLED")); + assertThat(span.toSpanData().getStatus()) + .isEqualTo(ImmutableStatus.ERROR.withDescription("CANCELLED")); } @Test @@ -687,7 +689,7 @@ void badArgsIgnored() { // Should be no exceptions span.setAttribute(null, 0L); - span.setStatus(null); + span.setStatus(null, null); span.updateName(null); span.addEvent(null); span.addEvent(null, 0); @@ -699,7 +701,7 @@ void badArgsIgnored() { // Ignored the bad calls SpanData data = span.toSpanData(); assertThat(data.getAttributes().isEmpty()).isTrue(); - assertThat(data.getStatus()).isEqualTo(Status.UNSET); + assertThat(data.getStatus()).isEqualTo(ImmutableStatus.UNSET); assertThat(data.getName()).isEqualTo(SPAN_NAME); } @@ -762,15 +764,18 @@ private RecordEventsReadableSpan createTestSpan( return span; } - private void spanDoWork(RecordEventsReadableSpan span, @Nullable Status status) { + private void spanDoWork( + RecordEventsReadableSpan span, + @Nullable StatusCanonicalCode canonicalCode, + @Nullable String descriptio) { span.setAttribute("MySingleStringAttributeKey", "MySingleStringAttributeValue"); attributes.forEach(span::setAttribute); testClock.advanceMillis(MILLIS_PER_SECOND); span.addEvent("event2", Attributes.empty()); testClock.advanceMillis(MILLIS_PER_SECOND); span.updateName(SPAN_NEW_NAME); - if (status != null) { - span.setStatus(status); + if (canonicalCode != null) { + span.setStatus(canonicalCode, descriptio); } } @@ -782,7 +787,7 @@ private void verifySpanData( String spanName, long startEpochNanos, long endEpochNanos, - Status status, + SpanData.Status status, boolean hasEnded) { assertThat(spanData.getTraceId()).isEqualTo(traceId); assertThat(spanData.getSpanId()).isEqualTo(spanId); @@ -875,7 +880,7 @@ void testAsSpanData() { name, startEpochNanos, endEpochNanos, - Status.UNSET, + ImmutableStatus.UNSET, /* hasEnded= */ true); assertThat(result.getTotalRecordedLinks()).isEqualTo(1); assertThat(result.isSampled()).isEqualTo(false); diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java index e53d134b407..5f698004047 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java @@ -9,11 +9,11 @@ import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.config.TraceConfig; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.Tracer; import java.util.Collections; @@ -49,7 +49,7 @@ public static SpanData makeBasicSpan() { .setName("span") .setKind(Kind.SERVER) .setStartEpochNanos(TimeUnit.SECONDS.toNanos(100) + 100) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setEndEpochNanos(TimeUnit.SECONDS.toNanos(200) + 200) .setTotalRecordedLinks(0) .setTotalRecordedEvents(0) diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/errorreporting/ErrorReportingTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/errorreporting/ErrorReportingTest.java index 18970fdaec2..c3f1eb6a6ff 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/errorreporting/ErrorReportingTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/errorreporting/ErrorReportingTest.java @@ -18,7 +18,7 @@ import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.Tracer; import java.util.List; import java.util.concurrent.ExecutorService; @@ -42,7 +42,7 @@ void testSimpleError() { try (Scope ignored = tracer.withSpan(span)) { throw new RuntimeException("Invalid state"); } catch (Exception e) { - span.setStatus(Status.ERROR); + span.setStatus(StatusCanonicalCode.ERROR, null); } finally { span.end(); } @@ -51,8 +51,7 @@ void testSimpleError() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); - assertThat(spans.get(0).getStatus().getCanonicalCode().value()) - .isEqualTo(Status.ERROR.getCanonicalCode().value()); + assertThat(spans.get(0).getStatus().getCanonicalCode()).isEqualTo(StatusCanonicalCode.ERROR); } /* Error handling in a callback capturing/activating the Span */ @@ -64,7 +63,7 @@ void testCallbackError() { try (Scope ignored = tracer.withSpan(span)) { throw new RuntimeException("Invalid state"); } catch (Exception exc) { - span.setStatus(Status.ERROR); + span.setStatus(StatusCanonicalCode.ERROR, null); } finally { span.end(); } @@ -76,8 +75,7 @@ void testCallbackError() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); - assertThat(spans.get(0).getStatus().getCanonicalCode()) - .isEqualTo(Status.ERROR.getCanonicalCode()); + assertThat(spans.get(0).getStatus().getCanonicalCode()).isEqualTo(StatusCanonicalCode.ERROR); } /* Error handling for a max-retries task (such as url fetching). @@ -97,15 +95,14 @@ void testErrorRecovery() { } } - span.setStatus(Status.ERROR); // Could not fetch anything. + span.setStatus(StatusCanonicalCode.ERROR, null); // Could not fetch anything. span.end(); assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); - assertThat(spans.get(0).getStatus().getCanonicalCode()) - .isEqualTo(Status.ERROR.getCanonicalCode()); + assertThat(spans.get(0).getStatus().getCanonicalCode()).isEqualTo(StatusCanonicalCode.ERROR); List events = spans.get(0).getEvents(); assertEquals(events.size(), maxRetries); @@ -125,7 +122,7 @@ void testInstrumentationLayer() { try { throw new RuntimeException("Invalid state"); } catch (Exception exc) { - tracer.getCurrentSpan().setStatus(Status.ERROR); + tracer.getCurrentSpan().setStatus(StatusCanonicalCode.ERROR, null); } finally { tracer.getCurrentSpan().end(); } @@ -139,7 +136,7 @@ void testInstrumentationLayer() { List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertEquals(spans.size(), 1); - assertEquals(spans.get(0).getStatus().getCanonicalCode(), Status.ERROR.getCanonicalCode()); + assertEquals(spans.get(0).getStatus().getCanonicalCode(), StatusCanonicalCode.ERROR); } private static class ScopedRunnable implements Runnable { diff --git a/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanData.java b/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanData.java index a2546e0c554..de9814456d9 100644 --- a/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanData.java +++ b/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanData.java @@ -12,7 +12,6 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceState; import java.util.List; diff --git a/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilder.java b/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilder.java index 9db94d167b4..8c3745322cb 100644 --- a/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilder.java +++ b/sdk_extensions/tracing_incubator/src/main/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilder.java @@ -11,7 +11,6 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceState; import java.util.List; import javax.annotation.concurrent.Immutable; diff --git a/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanDataTest.java b/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanDataTest.java index 663627adca4..c08b9d6bfd3 100644 --- a/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanDataTest.java +++ b/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/DelegatingSpanDataTest.java @@ -13,10 +13,10 @@ import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.trace.TestSpanData; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.attributes.SemanticAttributes; import org.junit.jupiter.api.Test; @@ -109,7 +109,7 @@ private static TestSpanData.Builder createBasicSpanBuilder() { .setStartEpochNanos(100) .setEndEpochNanos(200) .setKind(Kind.SERVER) - .setStatus(Status.OK) + .setStatus(ImmutableStatus.OK) .setHasRemoteParent(false) .setTotalRecordedEvents(0) .setTotalRecordedLinks(0); diff --git a/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilderTest.java b/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilderTest.java index e439e16f59a..45907165351 100644 --- a/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilderTest.java +++ b/sdk_extensions/tracing_incubator/src/test/java/io/opentelemetry/sdk/extensions/incubator/trace/data/SpanDataBuilderTest.java @@ -10,9 +10,9 @@ import com.google.common.testing.EqualsTester; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.TestSpanData; +import io.opentelemetry.sdk.trace.data.ImmutableStatus; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.Status; import org.junit.jupiter.api.Test; class SpanDataBuilderTest { @@ -29,7 +29,7 @@ class SpanDataBuilderTest { .setStartEpochNanos(0) .setEndEpochNanos(100) .setKind(Span.Kind.SERVER) - .setStatus(Status.ERROR) + .setStatus(ImmutableStatus.ERROR) .setAttributes( Attributes.newBuilder() .setAttribute("cat", "meow") @@ -47,12 +47,12 @@ void noOp() { @Test void modifySpanData() { - assertThat(TEST_SPAN_DATA.getStatus()).isEqualTo(Status.ERROR); + assertThat(TEST_SPAN_DATA.getStatus()).isEqualTo(ImmutableStatus.ERROR); SpanData modified = SpanDataBuilder.newBuilder(TEST_SPAN_DATA) - .setStatus(Status.ERROR.withDescription("ABORTED")) + .setStatus(ImmutableStatus.ERROR.withDescription("ABORTED")) .build(); - assertThat(modified.getStatus()).isEqualTo(Status.ERROR.withDescription("ABORTED")); + assertThat(modified.getStatus()).isEqualTo(ImmutableStatus.ERROR.withDescription("ABORTED")); } @Test @@ -65,7 +65,7 @@ void equalsHashCode() { SpanDataBuilder.newBuilder(TEST_SPAN_DATA).build()) .addEqualityGroup( SpanDataBuilder.newBuilder(TEST_SPAN_DATA) - .setStatus(Status.ERROR.withDescription("ABORTED")) + .setStatus(ImmutableStatus.ERROR.withDescription("ABORTED")) .build()); tester.testEquals(); } diff --git a/sdk_extensions/zpages/src/jmh/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorBenchmark.java b/sdk_extensions/zpages/src/jmh/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorBenchmark.java index 60805d65119..87540cc961f 100644 --- a/sdk_extensions/zpages/src/jmh/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorBenchmark.java +++ b/sdk_extensions/zpages/src/jmh/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorBenchmark.java @@ -7,7 +7,7 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import io.opentelemetry.trace.Tracer; import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.Benchmark; @@ -43,7 +43,7 @@ public final void setup() { tracer.spanBuilder(runningSpan).startSpan(); tracer.spanBuilder(latencySpan).startSpan().end(); Span error = tracer.spanBuilder(errorSpan).startSpan(); - error.setStatus(Status.ERROR); + error.setStatus(StatusCanonicalCode.ERROR, null); error.end(); } } diff --git a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregator.java b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregator.java index 849e1dac298..db044b8deed 100644 --- a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregator.java +++ b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregator.java @@ -7,7 +7,7 @@ import io.opentelemetry.sdk.trace.ReadableSpan; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.StatusCanonicalCode; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -85,7 +85,7 @@ List getRunningSpans(String spanName) { } /** - * Returns a Map of span names to counts for all {@link Status#OK} spans in {@link + * Returns a Map of span names to counts for all {@link StatusCanonicalCode#OK} spans in {@link * TracezDataAggregator}. * * @return a Map of span names to counts, where the counts are further indexed by the latency @@ -102,8 +102,8 @@ Map> getSpanLatencyCounts() { } /** - * Returns a List of all {@link Status#OK} spans with a given span name between [lowerBound, - * upperBound) for {@link TracezDataAggregator}. + * Returns a List of all {@link StatusCanonicalCode#OK} spans with a given span name between + * [lowerBound, upperBound) for {@link TracezDataAggregator}. * * @param spanName name to filter returned spans. * @param lowerBound latency lower bound (inclusive) diff --git a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanBuckets.java b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanBuckets.java index f0455f125e5..d566c131856 100644 --- a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanBuckets.java +++ b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanBuckets.java @@ -7,8 +7,8 @@ import com.google.common.collect.ImmutableMap; import io.opentelemetry.sdk.trace.ReadableSpan; -import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.Status.CanonicalCode; +import io.opentelemetry.sdk.trace.data.SpanData; +import io.opentelemetry.trace.StatusCanonicalCode; import java.util.ArrayList; import java.util.EnumMap; import java.util.List; @@ -16,7 +16,7 @@ final class TracezSpanBuckets { private final ImmutableMap latencyBuckets; - private final ImmutableMap errorBuckets; + private final ImmutableMap errorBuckets; TracezSpanBuckets() { ImmutableMap.Builder latencyBucketsBuilder = @@ -25,9 +25,10 @@ final class TracezSpanBuckets { latencyBucketsBuilder.put(bucket, new SpanBucket(/* isLatencyBucket= */ true)); } latencyBuckets = latencyBucketsBuilder.build(); - ImmutableMap.Builder errorBucketsBuilder = ImmutableMap.builder(); - for (CanonicalCode code : CanonicalCode.values()) { - if (!code.toStatus().isOk()) { + ImmutableMap.Builder errorBucketsBuilder = + ImmutableMap.builder(); + for (StatusCanonicalCode code : StatusCanonicalCode.values()) { + if (code == StatusCanonicalCode.ERROR) { errorBucketsBuilder.put(code, new SpanBucket(/* isLatencyBucket= */ false)); } } @@ -35,7 +36,7 @@ final class TracezSpanBuckets { } void addToBucket(ReadableSpan span) { - Status status = span.toSpanData().getStatus(); + SpanData.Status status = span.toSpanData().getStatus(); if (status.isOk()) { latencyBuckets.get(LatencyBoundary.getBoundary(span.getLatencyNanos())).add(span); return; diff --git a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java index 326d050cc01..904559b4b1c 100644 --- a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java +++ b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java @@ -16,8 +16,7 @@ import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.Status.CanonicalCode; +import io.opentelemetry.trace.StatusCanonicalCode; import java.io.OutputStream; import java.io.PrintStream; import java.io.Serializable; @@ -325,7 +324,7 @@ private static boolean emitSingleSpan( "" + "

",
         zebraStripe ? ZEBRA_STRIPE_COLOR : "#fff");
-    Status status = span.getStatus();
+    SpanData.Status status = span.getStatus();
     if (status != null) {
       formatter.format("%s | ", htmlEscaper().escape(status.toString()));
     }
@@ -461,7 +460,7 @@ private void emitHtmlBody(Map queryMap, PrintStream out) {
                       latencyBoundary.getLatencyLowerBound(),
                       latencyBoundary.getLatencyUpperBound());
             } else {
-              if (subtype < 0 || subtype >= CanonicalCode.values().length) {
+              if (subtype < 0 || subtype >= StatusCanonicalCode.values().length) {
                 // N/A or out-of-bound cueck for error based subtype, valid values: [0, 15]
                 return;
               }
diff --git a/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorTest.java b/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorTest.java
index fe71e2c5abb..643d5d88dc6 100644
--- a/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorTest.java
+++ b/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezDataAggregatorTest.java
@@ -12,8 +12,7 @@
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.trace.Span;
-import io.opentelemetry.trace.Status;
-import io.opentelemetry.trace.Status.CanonicalCode;
+import io.opentelemetry.trace.StatusCanonicalCode;
 import io.opentelemetry.trace.Tracer;
 import java.util.List;
 import java.util.Map;
@@ -229,17 +228,17 @@ void getErrorSpanCounts_noCompletedSpans() {
     /* getErrorSpanCounts should return a an empty map */
     Span span = tracer.spanBuilder(SPAN_NAME_ONE).startSpan();
     Map counts = dataAggregator.getErrorSpanCounts();
-    span.setStatus(Status.ERROR);
+    span.setStatus(StatusCanonicalCode.ERROR, null);
     span.end();
     assertThat(counts).isEmpty();
   }
 
   @Test
   void getErrorSpanCounts_oneSpanPerErrorCode() {
-    for (CanonicalCode errorCode : CanonicalCode.values()) {
-      if (errorCode.equals(CanonicalCode.ERROR)) {
+    for (StatusCanonicalCode errorCode : StatusCanonicalCode.values()) {
+      if (errorCode.equals(StatusCanonicalCode.ERROR)) {
         Span span = tracer.spanBuilder(SPAN_NAME_ONE).startSpan();
-        span.setStatus(errorCode.toStatus());
+        span.setStatus(errorCode, null);
         span.end();
       }
     }
@@ -250,10 +249,10 @@ void getErrorSpanCounts_oneSpanPerErrorCode() {
   @Test
   void getErrorSpanCounts_twoSpanNames() {
     Span span1 = tracer.spanBuilder(SPAN_NAME_ONE).startSpan();
-    span1.setStatus(Status.ERROR);
+    span1.setStatus(StatusCanonicalCode.ERROR, null);
     span1.end();
     Span span2 = tracer.spanBuilder(SPAN_NAME_TWO).startSpan();
-    span2.setStatus(Status.ERROR);
+    span2.setStatus(StatusCanonicalCode.ERROR, null);
     span2.end();
     /* getErrorSpanCounts should return a map with 2 different span names */
     Map errorCounts = dataAggregator.getErrorSpanCounts();
@@ -274,9 +273,9 @@ void getErrorSpans_oneSpanNameWithDifferentErrors() {
     Span span2 = tracer.spanBuilder(SPAN_NAME_ONE).startSpan();
     /* getErrorSpans should return an empty List */
     assertThat(dataAggregator.getErrorSpans(SPAN_NAME_ONE)).isEmpty();
-    span1.setStatus(Status.ERROR);
+    span1.setStatus(StatusCanonicalCode.ERROR, null);
     span1.end();
-    span2.setStatus(Status.ERROR.withDescription("ABORTED"));
+    span2.setStatus(StatusCanonicalCode.ERROR, "ABORTED");
     span2.end();
     /* getErrorSpans should return a List with both spans */
     List errorSpans = dataAggregator.getErrorSpans(SPAN_NAME_ONE);
@@ -291,9 +290,9 @@ void getErrorSpans_twoSpanNames() {
     /* getErrorSpans should return an empty List for each span name */
     assertThat(dataAggregator.getErrorSpans(SPAN_NAME_ONE)).isEmpty();
     assertThat(dataAggregator.getErrorSpans(SPAN_NAME_TWO)).isEmpty();
-    span1.setStatus(Status.ERROR);
+    span1.setStatus(StatusCanonicalCode.ERROR, null);
     span1.end();
-    span2.setStatus(Status.ERROR);
+    span2.setStatus(StatusCanonicalCode.ERROR, null);
     span2.end();
     /* getErrorSpans should return a List with only the corresponding span */
     assertThat(dataAggregator.getErrorSpans(SPAN_NAME_ONE))
diff --git a/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessorTest.java b/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessorTest.java
index 8312b4c3f06..4301e4e67d7 100644
--- a/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessorTest.java
+++ b/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessorTest.java
@@ -10,10 +10,10 @@
 
 import io.opentelemetry.sdk.trace.ReadWriteSpan;
 import io.opentelemetry.sdk.trace.ReadableSpan;
+import io.opentelemetry.sdk.trace.data.ImmutableStatus;
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.trace.SpanContext;
 import io.opentelemetry.trace.SpanId;
-import io.opentelemetry.trace.Status;
 import io.opentelemetry.trace.TraceFlags;
 import io.opentelemetry.trace.TraceId;
 import io.opentelemetry.trace.TraceState;
@@ -38,7 +38,7 @@ class TracezSpanProcessorTest {
           TraceFlags.getSampled(),
           TraceState.builder().build());
   private static final SpanContext NOT_SAMPLED_SPAN_CONTEXT = SpanContext.getInvalid();
-  private static final Status SPAN_STATUS = Status.ERROR;
+  private static final SpanData.Status SPAN_STATUS = ImmutableStatus.ERROR;
 
   private static void assertSpanCacheSizes(
       TracezSpanProcessor spanProcessor, int runningSpanCacheSize, int completedSpanCacheSize) {
diff --git a/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandlerTest.java b/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandlerTest.java
index 2cd9b266c06..144a2ec9a42 100644
--- a/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandlerTest.java
+++ b/sdk_extensions/zpages/src/test/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandlerTest.java
@@ -13,8 +13,7 @@
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.trace.EndSpanOptions;
 import io.opentelemetry.trace.Span;
-import io.opentelemetry.trace.Status;
-import io.opentelemetry.trace.Status.CanonicalCode;
+import io.opentelemetry.trace.StatusCanonicalCode;
 import io.opentelemetry.trace.Tracer;
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
@@ -66,7 +65,7 @@ void summaryTable_emitRowForEachSpan() {
     latencySpan.end(endOptions);
 
     Span errorSpan = tracer.spanBuilder(ERROR_SPAN).startSpan();
-    errorSpan.setStatus(CanonicalCode.ERROR.toStatus());
+    errorSpan.setStatus(StatusCanonicalCode.ERROR, null);
     errorSpan.end();
 
     TracezZPageHandler tracezZPageHandler = new TracezZPageHandler(dataAggregator);
@@ -245,9 +244,9 @@ void summaryTable_linkForErrorSpans() {
     Span errorSpan2 = tracer.spanBuilder(ERROR_SPAN).startSpan();
     Span errorSpan3 = tracer.spanBuilder(ERROR_SPAN).startSpan();
     Span finishedSpan = tracer.spanBuilder(FINISHED_SPAN_ONE).startSpan();
-    errorSpan1.setStatus(Status.ERROR.withDescription("CANCELLED"));
-    errorSpan2.setStatus(Status.ERROR.withDescription("ABORTED"));
-    errorSpan3.setStatus(Status.ERROR.withDescription("DEADLINE_EXCEEDED"));
+    errorSpan1.setStatus(StatusCanonicalCode.ERROR, "CANCELLED");
+    errorSpan2.setStatus(StatusCanonicalCode.ERROR, "ABORTED");
+    errorSpan3.setStatus(StatusCanonicalCode.ERROR, "DEADLINE_EXCEEDED");
     errorSpan1.end();
     errorSpan2.end();
     errorSpan3.end();
@@ -312,8 +311,8 @@ void spanDetails_emitErrorSpanDetailsCorrectly() {
     OutputStream output = new ByteArrayOutputStream();
     Span errorSpan1 = tracer.spanBuilder(ERROR_SPAN).startSpan();
     Span errorSpan2 = tracer.spanBuilder(ERROR_SPAN).startSpan();
-    errorSpan1.setStatus(Status.ERROR.withDescription("CANCELLED"));
-    errorSpan2.setStatus(Status.ERROR.withDescription("ABORTED"));
+    errorSpan1.setStatus(StatusCanonicalCode.ERROR, "CANCELLED");
+    errorSpan2.setStatus(StatusCanonicalCode.ERROR, "ABORTED");
     errorSpan1.end();
     errorSpan2.end();
     Map queryMap =
diff --git a/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java b/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java
index ddaad93b7aa..2a7362c57d1 100644
--- a/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java
+++ b/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java
@@ -13,7 +13,6 @@
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.trace.Span.Kind;
 import io.opentelemetry.trace.SpanId;
-import io.opentelemetry.trace.Status;
 import io.opentelemetry.trace.TraceState;
 import java.util.ArrayList;
 import java.util.Collections;
diff --git a/testing_internal/src/test/java/io/opentelemetry/sdk/trace/TestSpanDataTest.java b/testing_internal/src/test/java/io/opentelemetry/sdk/trace/TestSpanDataTest.java
index b541c6c7eff..4c0a4ef7774 100644
--- a/testing_internal/src/test/java/io/opentelemetry/sdk/trace/TestSpanDataTest.java
+++ b/testing_internal/src/test/java/io/opentelemetry/sdk/trace/TestSpanDataTest.java
@@ -13,11 +13,11 @@
 import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
 import io.opentelemetry.sdk.trace.data.ImmutableEvent;
 import io.opentelemetry.sdk.trace.data.ImmutableLink;
+import io.opentelemetry.sdk.trace.data.ImmutableStatus;
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.trace.Span.Kind;
 import io.opentelemetry.trace.SpanContext;
 import io.opentelemetry.trace.SpanId;
-import io.opentelemetry.trace.Status;
 import io.opentelemetry.trace.TraceId;
 import java.util.ArrayList;
 import java.util.concurrent.TimeUnit;
@@ -113,7 +113,7 @@ private static TestSpanData.Builder createBasicSpanBuilder() {
         .setStartEpochNanos(START_EPOCH_NANOS)
         .setEndEpochNanos(END_EPOCH_NANOS)
         .setKind(Kind.SERVER)
-        .setStatus(Status.OK)
+        .setStatus(ImmutableStatus.OK)
         .setHasRemoteParent(false)
         .setTotalRecordedEvents(0)
         .setTotalRecordedLinks(0);