diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java index 75398601c57..825b1fe2856 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -18,8 +18,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator.Setter; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.TracingContextUtils; @@ -43,13 +42,13 @@ @State(Scope.Thread) public class HttpTraceContextInjectBenchmark { - private static final List spanContexts = + private static final List spans = Arrays.asList( - createTestSpanContext("905734c59b913b4a905734c59b913b4a", "9909983295041501"), - createTestSpanContext("21196a77f299580e21196a77f299580e", "993a97ee3691eb26"), - createTestSpanContext("2e7d0ad2390617702e7d0ad239061770", "d49582a2de984b86"), - createTestSpanContext("905734c59b913b4a905734c59b913b4a", "776ff807b787538a"), - createTestSpanContext("68ec932c33b3f2ee68ec932c33b3f2ee", "68ec932c33b3f2ee")); + createTestSpan("905734c59b913b4a905734c59b913b4a", "9909983295041501"), + createTestSpan("21196a77f299580e21196a77f299580e", "993a97ee3691eb26"), + createTestSpan("2e7d0ad2390617702e7d0ad239061770", "d49582a2de984b86"), + createTestSpan("905734c59b913b4a905734c59b913b4a", "776ff807b787538a"), + createTestSpan("68ec932c33b3f2ee68ec932c33b3f2ee", "68ec932c33b3f2ee")); private static final int COUNT = 5; // spanContexts.size() private final HttpTraceContext httpTraceContext = HttpTraceContext.getInstance(); private final Map carrier = new HashMap<>(); @@ -60,7 +59,7 @@ public void set(Map carrier, String key, String value) { carrier.put(key, value); } }; - private final List contexts = createContexts(spanContexts); + private final List contexts = createContexts(spans); /** Benchmark for measuring inject with default trace state and sampled trace options. */ @Benchmark @@ -77,16 +76,16 @@ public Map measureInject() { return carrier; } - private static SpanContext createTestSpanContext(String traceId, String spanId) { + private static Span createTestSpan(String traceId, String spanId) { byte sampledTraceOptions = TraceFlags.getSampled(); TraceState traceStateDefault = TraceState.builder().build(); - return SpanContext.create(traceId, spanId, sampledTraceOptions, traceStateDefault); + return Span.getPropagated(traceId, spanId, sampledTraceOptions, traceStateDefault); } - private static List createContexts(List spanContexts) { + private static List createContexts(List spans) { List contexts = new ArrayList<>(); - for (SpanContext context : spanContexts) { - contexts.add(TracingContextUtils.withSpan(DefaultSpan.create(context), Context.ROOT)); + for (Span span : spans) { + contexts.add(TracingContextUtils.withSpan(span, Context.ROOT)); } return contexts; } diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java index 63ae327b35d..efc4d0dc266 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java @@ -18,6 +18,7 @@ import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; +import java.util.Objects; import javax.annotation.concurrent.Immutable; /** @@ -29,35 +30,63 @@ * @since 0.1.0 */ @Immutable -public final class DefaultSpan implements Span { - - private static final DefaultSpan INVALID = new DefaultSpan(SpanContext.getInvalid()); - - /** - * Returns a {@link DefaultSpan} with an invalid {@link SpanContext}. - * - * @return a {@code DefaultSpan} with an invalid {@code SpanContext}. - * @since 0.1.0 - */ - public static Span getInvalid() { - return INVALID; +final class DefaultSpan implements Span { + + static final Span INVALID = + new DefaultSpan( + TraceId.getInvalid(), + SpanId.getInvalid(), + TraceFlags.getDefault(), + TraceState.getDefault(), + /* isRemote= */ false); + + private final String traceIdHex; + private final String spanIdHex; + private final byte traceFlags; + private final TraceState traceState; + private final boolean isRemote; + + DefaultSpan( + String traceIdHex, + String spanIdHex, + byte traceFlags, + TraceState traceState, + boolean isRemote) { + this.traceIdHex = traceIdHex != null ? traceIdHex : TraceId.getInvalid(); + this.spanIdHex = spanIdHex != null ? spanIdHex : SpanId.getInvalid(); + this.traceFlags = traceFlags; + this.traceState = traceState != null ? traceState : TraceState.getDefault(); + this.isRemote = isRemote; } - /** - * Creates an instance of this class with the {@link SpanContext}. - * - * @param spanContext the {@code SpanContext}. - * @return a {@link DefaultSpan}. - * @since 0.1.0 - */ - public static Span create(SpanContext spanContext) { - return new DefaultSpan(spanContext); + @Override + public String getTraceIdAsHexString() { + return traceIdHex; + } + + @Override + public String getSpanIdAsHexString() { + return spanIdHex; } - private final SpanContext spanContext; + @Override + public byte getTraceFlags() { + return traceFlags; + } + + @Override + public TraceState getTraceState() { + return traceState; + } + + @Override + public boolean isSampled() { + return TraceFlags.isSampled(traceFlags); + } - DefaultSpan(SpanContext spanContext) { - this.spanContext = spanContext; + @Override + public boolean isRemote() { + return isRemote; } @Override @@ -111,11 +140,6 @@ public void end() {} @Override public void end(EndSpanOptions endOptions) {} - @Override - public SpanContext getContext() { - return spanContext; - } - @Override public boolean isRecording() { return false; @@ -125,4 +149,25 @@ public boolean isRecording() { public String toString() { return "DefaultSpan"; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof DefaultSpan)) { + return false; + } + DefaultSpan that = (DefaultSpan) o; + return traceFlags == that.traceFlags + && isRemote == that.isRemote + && traceIdHex.equals(that.traceIdHex) + && spanIdHex.equals(that.spanIdHex) + && traceState.equals(that.traceState); + } + + @Override + public int hashCode() { + return Objects.hash(traceIdHex, spanIdHex, traceFlags, traceState, isRemote); + } } diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 51239cb6ae4..d56d792957e 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -66,39 +66,37 @@ static NoopSpanBuilder create(String spanName) { return new NoopSpanBuilder(spanName); } - @Nullable private SpanContext spanContext; + @Nullable private Span parent; @Override public Span startSpan() { - if (spanContext == null) { - spanContext = TracingContextUtils.getCurrentSpan().getContext(); + if (parent == null) { + parent = TracingContextUtils.getCurrentSpan(); } - return spanContext != null && !SpanContext.getInvalid().equals(spanContext) - ? new DefaultSpan(spanContext) - : DefaultSpan.getInvalid(); + return parent.isValid() ? parent : Span.getInvalid(); } @Override public NoopSpanBuilder setParent(Context context) { Utils.checkNotNull(context, "context"); - spanContext = TracingContextUtils.getSpan(context).getContext(); + parent = TracingContextUtils.getSpan(context); return this; } @Override public NoopSpanBuilder setNoParent() { - spanContext = SpanContext.getInvalid(); + parent = Span.getInvalid(); return this; } @Override - public NoopSpanBuilder addLink(SpanContext spanContext) { + public NoopSpanBuilder addLink(Span span) { return this; } @Override - public NoopSpanBuilder addLink(SpanContext spanContext, Attributes attributes) { + public NoopSpanBuilder addLink(Span span, Attributes attributes) { return this; } diff --git a/api/src/main/java/io/opentelemetry/trace/Link.java b/api/src/main/java/io/opentelemetry/trace/Link.java index e0c6747d492..e7c33931d6a 100644 --- a/api/src/main/java/io/opentelemetry/trace/Link.java +++ b/api/src/main/java/io/opentelemetry/trace/Link.java @@ -29,13 +29,14 @@ */ @ThreadSafe public interface Link { - /** - * Returns the {@code SpanContext}. - * - * @return the {@code SpanContext}. - * @since 0.1.0 - */ - SpanContext getContext(); + + String getTraceIdAsHexString(); + + String getSpanIdAsHexString(); + + TraceState getTraceState(); + + byte getTraceFlags(); /** * Returns the set of attributes. diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 0fa259d7081..94244c23546 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -23,7 +23,7 @@ import javax.annotation.concurrent.ThreadSafe; /** - * An interface that represents a span. It has an associated {@link SpanContext}. + * An interface that represents a span. * *

Spans are created by the {@link Builder#startSpan} method. * @@ -34,6 +34,70 @@ @ThreadSafe public interface Span { + /** + * Returns an invalid {@link Span}. An invalid {@link Span} is used when tracing is disabled, + * usually because there is no OpenTelemetry SDK installed. + */ + static Span getInvalid() { + return DefaultSpan.INVALID; + } + + /** + * Returns a {@link Span} that has been propagated from a remote parent. This span does not + * correspond to processing in the current process and is used to set as the parent of one that + * is. + */ + static Span getPropagated(String traceId, String spanId, byte traceFlags, TraceState traceState) { + return new DefaultSpan(traceId, spanId, traceFlags, traceState, /* isRemote= */ true); + } + + /** + * Returns an unsampled {@link Span}. Because the {@link Span} is unsampled, it will not be + * exported and all operations are no-op. + */ + static Span getUnsampled(String traceId, String spanId, TraceState traceState) { + return new DefaultSpan( + traceId, spanId, TraceFlags.getDefault(), traceState, /* isRemote= */ false); + } + + /** Returns the trace ID of this {@link Span}. */ + String getTraceIdAsHexString(); + + String getSpanIdAsHexString(); + + byte getTraceFlags(); + + TraceState getTraceState(); + + default byte[] getTraceIdBytes() { + return TraceId.bytesFromHex(getTraceIdAsHexString(), 0); + } + + default byte[] getSpanIdBytes() { + return SpanId.bytesFromHex(getSpanIdAsHexString(), 0); + } + + boolean isSampled(); + + default void copyTraceFlagsHexTo(char[] dest, int destOffset) { + dest[destOffset] = '0'; + dest[destOffset + 1] = isSampled() ? '1' : '0'; + } + + /** + * Returns {@code true} if this {@link Span} is valid. An invalid {@link Span} is used when + * tracing isn't enabled, for example because there is no OpenTelemetry SDK registered. + */ + default boolean isValid() { + return TraceId.isValid(getTraceIdAsHexString()) && SpanId.isValid(getSpanIdAsHexString()); + } + + /** + * Returns {@code true} if this {@link Span} was propagated from a remote parent and does not + * correspond to any processing in this process. + */ + boolean isRemote(); + /** * Type of span. Can be used to specify additional relationships between spans in addition to a * parent/child relationship. @@ -291,14 +355,6 @@ enum Kind { */ void end(EndSpanOptions endOptions); - /** - * Returns the {@code SpanContext} associated with this {@code Span}. - * - * @return the {@code SpanContext} associated with this {@code Span}. - * @since 0.1.0 - */ - SpanContext getContext(); - /** * Returns {@code true} if this {@code Span} records tracing events (e.g. {@link * #addEvent(String)}, {@link #setAttribute(String, long)}). @@ -430,18 +486,18 @@ interface Builder { /** * Adds a {@link Link} to the newly created {@code Span}. * - * @param spanContext the context of the linked {@code Span}. + * @param span the {@link Span} to link to. * @return this. * @throws NullPointerException if {@code spanContext} is {@code null}. * @see #addLink(Link) * @since 0.1.0 */ - Builder addLink(SpanContext spanContext); + Builder addLink(Span span); /** * Adds a {@link Link} to the newly created {@code Span}. * - * @param spanContext the context of the linked {@code Span}. + * @param span the {@link Span} to link to. * @param attributes the attributes of the {@code Link}. * @return this. * @throws NullPointerException if {@code spanContext} is {@code null}. @@ -449,7 +505,7 @@ interface Builder { * @see #addLink(Link) * @since 0.1.0 */ - Builder addLink(SpanContext spanContext, Attributes attributes); + Builder addLink(Span span, Attributes attributes); /** * Adds a {@link Link} to the newly created {@code Span}. diff --git a/api/src/main/java/io/opentelemetry/trace/SpanContext.java b/api/src/main/java/io/opentelemetry/trace/SpanContext.java deleted file mode 100644 index dd907023447..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/SpanContext.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace; - -import com.google.auto.value.AutoValue; -import com.google.auto.value.extension.memoized.Memoized; -import javax.annotation.concurrent.Immutable; - -/** - * A class that represents a span context. A span context contains the state that must propagate to - * child {@link Span}s and across process boundaries. It contains the identifiers (a {@link TraceId - * trace_id} and {@link SpanId span_id}) associated with the {@link Span} and a set of options - * (currently only whether the context is sampled or not), as well as the {@link TraceState - * traceState} and the {@link boolean remote} flag. - * - * @since 0.1.0 - */ -@Immutable -@AutoValue -public abstract class SpanContext { - - private static final SpanContext INVALID = - create( - TraceId.getInvalid(), - SpanId.getInvalid(), - TraceFlags.getDefault(), - TraceState.getDefault()); - - /** - * Returns the invalid {@code SpanContext} that can be used for no-op operations. - * - * @return the invalid {@code SpanContext}. - */ - public static SpanContext getInvalid() { - return INVALID; - } - - /** - * Creates a new {@code SpanContext} with the given identifiers and options. - * - * @param traceIdHex the trace identifier of the span context. - * @param spanIdHex the span identifier of the span context. - * @param traceFlags the byte representation of the {@link TraceFlags} - * @param traceState the trace state for the span context. - * @return a new {@code SpanContext} with the given identifiers and options. - * @since 0.1.0 - */ - public static SpanContext create( - String traceIdHex, String spanIdHex, byte traceFlags, TraceState traceState) { - return create(traceIdHex, spanIdHex, traceFlags, traceState, /* remote=*/ false); - } - - private static SpanContext create( - String traceIdHex, String spanIdHex, byte traceFlags, TraceState traceState, boolean remote) { - return new AutoValue_SpanContext( - traceIdHex, spanIdHex, traceFlags, traceState, /* remote$=*/ remote); - } - - /** - * Creates a new {@code SpanContext} that was propagated from a remote parent, with the given - * identifiers and options. - * - * @param traceIdHex the trace identifier of the span context. - * @param spanIdHex the span identifier of the span context. - * @param traceFlags the byte representation of the {@link TraceFlags} - * @param traceState the trace state for the span context. - * @return a new {@code SpanContext} with the given identifiers and options. - * @since 0.1.0 - */ - public static SpanContext createFromRemoteParent( - String traceIdHex, String spanIdHex, byte traceFlags, TraceState traceState) { - return create(traceIdHex, spanIdHex, traceFlags, traceState, /* remote=*/ true); - } - - abstract String getTraceIdHex(); - - abstract String getSpanIdHex(); - - /** - * Returns the trace identifier associated with this {@code SpanContext}. - * - * @return the trace identifier associated with this {@code SpanContext}. - * @since 0.1.0 - */ - public String getTraceIdAsHexString() { - return getTraceIdHex(); - } - - /** - * Returns the byte[] representation of the trace identifier associated with this {@link - * SpanContext}. - * - * @since 0.8.0 - */ - @Memoized - public byte[] getTraceIdBytes() { - return TraceId.bytesFromHex(getTraceIdHex(), 0); - } - - /** - * Returns the span identifier associated with this {@code SpanContext}. - * - * @return the span identifier associated with this {@code SpanContext}. - * @since 0.1.0 - */ - public String getSpanIdAsHexString() { - return getSpanIdHex(); - } - - /** - * Returns the byte[] representation of the span identifier associated with this {@link - * SpanContext}. - * - * @since 0.8.0 - */ - @Memoized - public byte[] getSpanIdBytes() { - return SpanId.bytesFromHex(getSpanIdHex(), 0); - } - - /** Whether the span in this context is sampled. */ - public boolean isSampled() { - return (getTraceFlags() & 1) == 1; - } - - /** The byte-representation of {@link TraceFlags}. */ - public abstract byte getTraceFlags(); - - public void copyTraceFlagsHexTo(char[] dest, int destOffset) { - dest[destOffset] = '0'; - dest[destOffset + 1] = isSampled() ? '1' : '0'; - } - - /** - * Returns the {@code TraceState} associated with this {@code SpanContext}. - * - * @return the {@code TraceState} associated with this {@code SpanContext}. - * @since 0.1.0 - */ - public abstract TraceState getTraceState(); - - /** - * Returns {@code true} if this {@code SpanContext} is valid. - * - * @return {@code true} if this {@code SpanContext} is valid. - * @since 0.1.0 - */ - @Memoized - public boolean isValid() { - return TraceId.isValid(getTraceIdHex()) && SpanId.isValid(getSpanIdHex()); - } - - /** - * Returns {@code true} if the {@code SpanContext} was propagated from a remote parent. - * - * @return {@code true} if the {@code SpanContext} was propagated from a remote parent. - * @since 0.1.0 - */ - public abstract boolean isRemote(); -} diff --git a/api/src/main/java/io/opentelemetry/trace/TraceFlags.java b/api/src/main/java/io/opentelemetry/trace/TraceFlags.java index aeee9dbaf8a..8044948f95c 100644 --- a/api/src/main/java/io/opentelemetry/trace/TraceFlags.java +++ b/api/src/main/java/io/opentelemetry/trace/TraceFlags.java @@ -60,7 +60,12 @@ public static byte getDefault() { public static boolean isSampledFromHex(CharSequence src, int srcOffset) { // todo bypass the byte conversion and look directly at the hex. byte b = BigendianEncoding.byteFromBase16String(src, srcOffset); - return (b & IS_SAMPLED) != 0; + return isSampled(b); + } + + /** Extract the sampled flag from trace-flags. */ + public static boolean isSampled(byte traceFlags) { + return (traceFlags & IS_SAMPLED) != 0; } /** Extract the byte representation of the flags from a hex-representation. */ diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index cb02053181c..9394ae39639 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -79,9 +79,8 @@ public interface Tracer { * *

startSpan methods do NOT modify the current Context {@code Span}. * - * @return a default {@code Span} that does nothing and has an invalid {@link SpanContext} if no - * {@code Span} is associated with the current Context, otherwise the current {@code Span} - * from the Context. + * @return a default {@code Span} that does nothing and is invalid if no {@code Span} is + * associated with the current Context, otherwise the current {@code Span} from the Context. * @since 0.1.0 */ Span getCurrentSpan(); diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java index 6f38523ed02..7816e776ced 100644 --- a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java @@ -65,7 +65,7 @@ public static Span getCurrentSpan() { */ public static Span getSpan(Context context) { Span span = CONTEXT_SPAN_KEY.get(context); - return span == null ? DefaultSpan.getInvalid() : span; + return span == null ? Span.getInvalid() : span; } /** diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index 860b820a1df..beadb15e18b 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -22,8 +22,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.internal.TemporaryBuffers; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -104,8 +103,8 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); - if (!spanContext.isValid()) { + Span span = TracingContextUtils.getSpan(context); + if (!span.isValid()) { return; } @@ -114,22 +113,22 @@ public void inject(Context context, C carrier, Setter setter) { chars[1] = VERSION.charAt(1); chars[2] = TRACEPARENT_DELIMITER; - String traceId = spanContext.getTraceIdAsHexString(); + String traceId = span.getTraceIdAsHexString(); for (int i = 0; i < traceId.length(); i++) { chars[TRACE_ID_OFFSET + i] = traceId.charAt(i); } chars[SPAN_ID_OFFSET - 1] = TRACEPARENT_DELIMITER; - String spanId = spanContext.getSpanIdAsHexString(); + String spanId = span.getSpanIdAsHexString(); for (int i = 0; i < spanId.length(); i++) { chars[SPAN_ID_OFFSET + i] = spanId.charAt(i); } chars[TRACE_OPTION_OFFSET - 1] = TRACEPARENT_DELIMITER; - spanContext.copyTraceFlagsHexTo(chars, TRACE_OPTION_OFFSET); + span.copyTraceFlagsHexTo(chars, TRACE_OPTION_OFFSET); setter.set(carrier, TRACE_PARENT, new String(chars, 0, TRACEPARENT_HEADER_SIZE)); - List entries = spanContext.getTraceState().getEntries(); + List entries = span.getTraceState().getEntries(); if (entries.isEmpty()) { // No need to add an empty "tracestate" header. return; @@ -154,44 +153,44 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(carrier, "carrier"); checkNotNull(getter, "getter"); - SpanContext spanContext = extractImpl(carrier, getter); - if (!spanContext.isValid()) { + Span extracted = extractImpl(carrier, getter); + if (!extracted.isValid()) { return context; } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return TracingContextUtils.withSpan(extracted, context); } - private static SpanContext extractImpl(C carrier, Getter getter) { + private static Span extractImpl(C carrier, Getter getter) { String traceParent = getter.get(carrier, TRACE_PARENT); if (traceParent == null) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } - SpanContext contextFromParentHeader = extractContextFromTraceParent(traceParent); - if (!contextFromParentHeader.isValid()) { - return contextFromParentHeader; + Span spanFromParentHeader = extractContextFromTraceParent(traceParent); + if (!spanFromParentHeader.isValid()) { + return spanFromParentHeader; } String traceStateHeader = getter.get(carrier, TRACE_STATE); if (traceStateHeader == null || traceStateHeader.isEmpty()) { - return contextFromParentHeader; + return spanFromParentHeader; } try { TraceState traceState = extractTraceState(traceStateHeader); - return SpanContext.createFromRemoteParent( - contextFromParentHeader.getTraceIdAsHexString(), - contextFromParentHeader.getSpanIdAsHexString(), - contextFromParentHeader.getTraceFlags(), + return Span.getPropagated( + spanFromParentHeader.getTraceIdAsHexString(), + spanFromParentHeader.getSpanIdAsHexString(), + spanFromParentHeader.getTraceFlags(), traceState); } catch (IllegalArgumentException e) { logger.info("Unparseable tracestate header. Returning span context without state."); - return contextFromParentHeader; + return spanFromParentHeader; } } - private static SpanContext extractContextFromTraceParent(String traceparent) { + private static Span extractContextFromTraceParent(String traceparent) { // TODO(bdrutu): Do we need to verify that version is hex and that // for the version the length is the expected one? boolean isValid = @@ -203,13 +202,13 @@ private static SpanContext extractContextFromTraceParent(String traceparent) { && traceparent.charAt(TRACE_OPTION_OFFSET - 1) == TRACEPARENT_DELIMITER; if (!isValid) { logger.info("Unparseable traceparent header. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } try { String version = traceparent.substring(0, 2); if (!VALID_VERSIONS.contains(version)) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } String traceId = @@ -217,12 +216,12 @@ private static SpanContext extractContextFromTraceParent(String traceparent) { String spanId = traceparent.substring(SPAN_ID_OFFSET, SPAN_ID_OFFSET + SpanId.getHexLength()); if (TraceId.isValid(traceId) && SpanId.isValid(spanId)) { byte isSampled = TraceFlags.byteFromHex(traceparent, TRACE_OPTION_OFFSET); - return SpanContext.createFromRemoteParent(traceId, spanId, isSampled, TRACE_STATE_DEFAULT); + return Span.getPropagated(traceId, spanId, isSampled, TRACE_STATE_DEFAULT); } - return SpanContext.getInvalid(); + return Span.getInvalid(); } catch (IllegalArgumentException e) { logger.info("Unparseable traceparent header. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } } diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java index 2bbabe6c58a..f8b6aad41e7 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java @@ -33,14 +33,14 @@ class DefaultSpanTest { @Test void hasInvalidContextAndDefaultSpanOptions() { - SpanContext context = DefaultSpan.getInvalid().getContext(); - assertThat(context.getTraceFlags()).isEqualTo(TraceFlags.getDefault()); - assertThat(context.getTraceState()).isEqualTo(TraceState.getDefault()); + Span span = Span.getInvalid(); + assertThat(span.getTraceFlags()).isEqualTo(TraceFlags.getDefault()); + assertThat(span.getTraceState()).isEqualTo(TraceState.getDefault()); } @Test void doNotCrash() { - Span span = DefaultSpan.getInvalid(); + Span span = Span.getInvalid(); span.setAttribute(stringKey("MyStringAttributeKey"), "MyStringAttributeValue"); span.setAttribute(booleanKey("MyBooleanAttributeKey"), true); span.setAttribute(longKey("MyLongAttributeKey"), 123L); @@ -68,7 +68,7 @@ void doNotCrash() { @Test void defaultSpan_ToString() { - Span span = DefaultSpan.getInvalid(); + Span span = Span.getInvalid(); assertThat(span.toString()).isEqualTo("DefaultSpan"); } diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index aaeac94e475..fd78c90e532 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -34,8 +34,8 @@ class DefaultTracerTest { private static final byte[] firstBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'}; private static final byte[] spanBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'}; - private static final SpanContext spanContext = - SpanContext.create( + private static final Span propagatedSpan = + Span.getPropagated( TraceId.bytesToHex(firstBytes), SpanId.bytesToHex(spanBytes), TraceFlags.getDefault(), @@ -49,7 +49,7 @@ void defaultGetCurrentSpan() { @Test void getCurrentSpan_WithSpan() { assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - try (Scope ws = defaultTracer.withSpan(DefaultSpan.getInvalid())) { + try (Scope ws = defaultTracer.withSpan(Span.getInvalid())) { assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); } assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); @@ -85,27 +85,27 @@ void testSpanContextPropagationExplicitParent() { Span span = defaultTracer .spanBuilder(SPAN_NAME) - .setParent(TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.ROOT)) + .setParent(TracingContextUtils.withSpan(propagatedSpan, Context.ROOT)) .startSpan(); - assertThat(span.getContext()).isSameAs(spanContext); + assertThat(span).isSameAs(propagatedSpan); } @Test void testSpanContextPropagation() { - DefaultSpan parent = new DefaultSpan(spanContext); + Span parent = propagatedSpan; Span span = defaultTracer .spanBuilder(SPAN_NAME) .setParent(TracingContextUtils.withSpan(parent, Context.ROOT)) .startSpan(); - assertThat(span.getContext()).isSameAs(spanContext); + assertThat(span).isSameAs(propagatedSpan); } @Test void noSpanContextMakesInvalidSpans() { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - assertThat(span.getContext()).isSameAs(SpanContext.getInvalid()); + assertThat(span).isSameAs(Span.getInvalid()); } @Test @@ -117,44 +117,43 @@ void testSpanContextPropagation_nullContext() { @Test void testSpanContextPropagation_fromContext() { - Context context = TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current()); + Context context = TracingContextUtils.withSpan(propagatedSpan, Context.current()); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - assertThat(span.getContext()).isSameAs(spanContext); + assertThat(span).isSameAs(propagatedSpan); } @Test void testSpanContextPropagation_fromContextAfterNoParent() { - Context context = TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current()); + Context context = TracingContextUtils.withSpan(propagatedSpan, Context.current()); Span span = defaultTracer.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan(); - assertThat(span.getContext()).isSameAs(spanContext); + assertThat(span).isSameAs(propagatedSpan); } @Test void testSpanContextPropagation_fromContextThenNoParent() { - Context context = TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current()); + Context context = TracingContextUtils.withSpan(propagatedSpan, Context.current()); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).setNoParent().startSpan(); - assertThat(span.getContext()).isEqualTo(SpanContext.getInvalid()); + assertThat(span).isEqualTo(Span.getInvalid()); } @Test void testSpanContextPropagationCurrentSpan() { - DefaultSpan parent = new DefaultSpan(spanContext); + Span parent = propagatedSpan; try (Scope scope = defaultTracer.withSpan(parent)) { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - assertThat(span.getContext()).isSameAs(spanContext); + assertThat(span).isSameAs(propagatedSpan); } } @Test void testSpanContextPropagationCurrentSpanContext() { - Context context = - TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.current()); + Context context = TracingContextUtils.withSpan(propagatedSpan, Context.current()); try (Scope scope = ContextUtils.withScopedContext(context)) { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - assertThat(span.getContext()).isSameAs(spanContext); + assertThat(span).isSameAs(propagatedSpan); } } } diff --git a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java index bac804ffdd9..845b4fb79e4 100644 --- a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java +++ b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java @@ -33,18 +33,32 @@ class SpanBuilderTest { void doNotCrash_NoopImplementation() { Span.Builder spanBuilder = tracer.spanBuilder("MySpanName"); spanBuilder.setSpanKind(Kind.SERVER); - spanBuilder.setParent(TracingContextUtils.withSpan(DefaultSpan.create(null), Context.ROOT)); + spanBuilder.setParent( + TracingContextUtils.withSpan(Span.getPropagated(null, null, (byte) 0, null), Context.ROOT)); spanBuilder.setParent(Context.ROOT); spanBuilder.setNoParent(); - spanBuilder.addLink(DefaultSpan.getInvalid().getContext()); - spanBuilder.addLink(DefaultSpan.getInvalid().getContext(), Attributes.empty()); + spanBuilder.addLink(Span.getInvalid()); + spanBuilder.addLink(Span.getInvalid(), Attributes.empty()); spanBuilder.addLink( new Link() { - private final SpanContext spanContext = DefaultSpan.getInvalid().getContext(); + @Override + public String getTraceIdAsHexString() { + return TraceId.getInvalid(); + } + + @Override + public String getSpanIdAsHexString() { + return SpanId.getInvalid(); + } + + @Override + public TraceState getTraceState() { + return TraceState.getDefault(); + } @Override - public SpanContext getContext() { - return spanContext; + public byte getTraceFlags() { + return TraceFlags.getDefault(); } @Override diff --git a/api/src/test/java/io/opentelemetry/trace/SpanContextTest.java b/api/src/test/java/io/opentelemetry/trace/SpanContextTest.java deleted file mode 100644 index 33eb58532fd..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/SpanContextTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -/** Unit tests for {@link SpanContext}. */ -class SpanContextTest { - private static final byte[] firstTraceIdBytes = - new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'}; - private static final String FIRST_TRACE_ID = TraceId.bytesToHex(firstTraceIdBytes); - private static final byte[] secondTraceIdBytes = - new byte[] {0, 0, 0, 0, 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 0}; - private static final String SECOND_TRACE_ID = TraceId.bytesToHex(secondTraceIdBytes); - - private static final byte[] firstSpanIdBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'}; - private static final String FIRST_SPAN_ID = SpanId.bytesToHex(firstSpanIdBytes); - private static final byte[] secondSpanIdBytes = new byte[] {'0', 0, 0, 0, 0, 0, 0, 0}; - private static final String SECOND_SPAN_ID = SpanId.bytesToHex(secondSpanIdBytes); - private static final TraceState FIRST_TRACE_STATE = - TraceState.builder().set("foo", "bar").build(); - private static final TraceState SECOND_TRACE_STATE = - TraceState.builder().set("foo", "baz").build(); - private static final TraceState EMPTY_TRACE_STATE = TraceState.builder().build(); - private static final SpanContext first = - SpanContext.create(FIRST_TRACE_ID, FIRST_SPAN_ID, TraceFlags.getDefault(), FIRST_TRACE_STATE); - private static final SpanContext second = - SpanContext.create( - SECOND_TRACE_ID, SECOND_SPAN_ID, TraceFlags.getSampled(), SECOND_TRACE_STATE); - private static final SpanContext remote = - SpanContext.createFromRemoteParent( - SECOND_TRACE_ID, SECOND_SPAN_ID, TraceFlags.getSampled(), EMPTY_TRACE_STATE); - - @Test - void invalidSpanContext() { - assertThat(SpanContext.getInvalid().getTraceIdAsHexString()).isEqualTo(TraceId.getInvalid()); - assertThat(SpanContext.getInvalid().getSpanIdAsHexString()).isEqualTo(SpanId.getInvalid()); - assertThat(SpanContext.getInvalid().getTraceFlags()).isEqualTo(TraceFlags.getDefault()); - } - - @Test - void isValid() { - assertThat(SpanContext.getInvalid().isValid()).isFalse(); - assertThat( - SpanContext.create( - FIRST_TRACE_ID, SpanId.getInvalid(), TraceFlags.getDefault(), EMPTY_TRACE_STATE) - .isValid()) - .isFalse(); - assertThat( - SpanContext.create( - TraceId.getInvalid(), FIRST_SPAN_ID, TraceFlags.getDefault(), EMPTY_TRACE_STATE) - .isValid()) - .isFalse(); - assertThat(first.isValid()).isTrue(); - assertThat(second.isValid()).isTrue(); - } - - @Test - void getTraceId() { - assertThat(first.getTraceIdAsHexString()).isEqualTo(FIRST_TRACE_ID); - assertThat(second.getTraceIdAsHexString()).isEqualTo(SECOND_TRACE_ID); - } - - @Test - void getSpanId() { - assertThat(first.getSpanIdAsHexString()).isEqualTo(FIRST_SPAN_ID); - assertThat(second.getSpanIdAsHexString()).isEqualTo(SECOND_SPAN_ID); - } - - @Test - void getTraceFlags() { - assertThat(first.getTraceFlags()).isEqualTo(TraceFlags.getDefault()); - assertThat(second.getTraceFlags()).isEqualTo(TraceFlags.getSampled()); - } - - @Test - void getTraceState() { - assertThat(first.getTraceState()).isEqualTo(FIRST_TRACE_STATE); - assertThat(second.getTraceState()).isEqualTo(SECOND_TRACE_STATE); - } - - @Test - void isRemote() { - assertThat(first.isRemote()).isFalse(); - assertThat(second.isRemote()).isFalse(); - assertThat(remote.isRemote()).isTrue(); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java index 2fe45e9fd32..0c5a116a057 100644 --- a/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java @@ -27,12 +27,12 @@ public final class TracingContextUtilsTest { @Test void testGetCurrentSpan_Default() { Span span = TracingContextUtils.getCurrentSpan(); - assertThat(span).isSameAs(DefaultSpan.getInvalid()); + assertThat(span).isSameAs(Span.getInvalid()); } @Test void testGetCurrentSpan_SetSpan() { - Span span = DefaultSpan.create(SpanContext.getInvalid()); + Span span = Span.getInvalid(); Context orig = TracingContextUtils.withSpan(span, Context.current()).attach(); try { assertThat(TracingContextUtils.getCurrentSpan()).isSameAs(span); @@ -44,12 +44,12 @@ void testGetCurrentSpan_SetSpan() { @Test void testGetSpan_DefaultContext() { Span span = TracingContextUtils.getSpan(Context.current()); - assertThat(span).isSameAs(DefaultSpan.getInvalid()); + assertThat(span).isSameAs(Span.getInvalid()); } @Test void testGetSpan_ExplicitContext() { - Span span = DefaultSpan.create(SpanContext.getInvalid()); + Span span = Span.getInvalid(); Context context = TracingContextUtils.withSpan(span, Context.current()); assertThat(TracingContextUtils.getSpan(context)).isSameAs(span); } @@ -62,7 +62,7 @@ void testGetSpanWithoutDefault_DefaultContext() { @Test void testGetSpanWithoutDefault_ExplicitContext() { - Span span = DefaultSpan.create(SpanContext.getInvalid()); + Span span = Span.getInvalid(); Context context = TracingContextUtils.withSpan(span, Context.current()); assertThat(TracingContextUtils.getSpanWithoutDefault(context)).isSameAs(span); } diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index d87c51c2ed1..912baa5aab9 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -24,8 +24,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator.Getter; import io.opentelemetry.context.propagation.TextMapPropagator.Setter; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -58,14 +57,6 @@ class HttpTraceContextTest { "bar=baz , foo=bar"; private final HttpTraceContext httpTraceContext = HttpTraceContext.getInstance(); - private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); - } - - private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); - } - @Test void inject_Nothing() { Map carrier = new LinkedHashMap<>(); @@ -77,8 +68,8 @@ void inject_Nothing() { void inject_NullCarrierUsage() { final Map carrier = new LinkedHashMap<>(); Context context = - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject( @@ -92,8 +83,8 @@ void inject_NullCarrierUsage() { void inject_invalidContext() { Map carrier = new LinkedHashMap<>(); httpTraceContext.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TraceId.getInvalid(), SpanId.getInvalid(), SAMPLED_TRACE_OPTIONS, @@ -108,8 +99,8 @@ void inject_invalidContext() { void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); Context context = - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -120,8 +111,8 @@ void inject_SampledContext() { void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); Context context = - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -132,8 +123,8 @@ void inject_NotSampledContext() { void inject_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); Context context = - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -147,8 +138,8 @@ void inject_SampledContext_WithTraceState() { void inject_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); Context context = - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -171,9 +162,11 @@ void extract_Nothing() { void extract_SampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -181,9 +174,11 @@ void extract_SampledContext() { void extract_NotSampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -192,9 +187,11 @@ void extract_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)); } @@ -203,9 +200,11 @@ void extract_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); } @@ -213,9 +212,11 @@ void extract_NotSampledContext_WithTraceState() { void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -224,9 +225,11 @@ void extract_NotSampledContext_EmptyTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, ""); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -235,9 +238,11 @@ void extract_NotSampledContext_TraceStateWithSpaces() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING_WITH_SPACES); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); } @@ -245,8 +250,10 @@ void extract_NotSampledContext_TraceStateWithSpaces() { void extract_EmptyHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, ""); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -254,48 +261,60 @@ void extract_InvalidTraceId() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put( TRACE_PARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void extract_InvalidTraceId_Size() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void extract_InvalidSpanId() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void extract_InvalidSpanId_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void extract_InvalidTraceFlags() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void extract_InvalidTraceFlags_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -303,9 +322,11 @@ void extract_InvalidTracestate_EntriesDelimiter() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "foo=bar;test=test"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -314,9 +335,11 @@ void extract_InvalidTracestate_KeyValueDelimiter() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "foo=bar,test-test"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -325,9 +348,11 @@ void extract_InvalidTracestate_OneString() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "test-test"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -335,8 +360,10 @@ void extract_InvalidTracestate_OneString() { void extract_InvalidVersion_ff() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "ff-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -353,7 +380,9 @@ void headerNames() { @Test void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), emptyHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + httpTraceContext.extract(Context.current(), emptyHeaders, getter))) + .isSameAs(Span.getInvalid()); } } diff --git a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java index 2bca212a1c8..3a642e7b20f 100644 --- a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java +++ b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java @@ -257,8 +257,8 @@ static Collection toSpanRefs(List links) { @VisibleForTesting static Model.SpanRef toSpanRef(Link link) { Model.SpanRef.Builder builder = Model.SpanRef.newBuilder(); - builder.setTraceId(TraceProtoUtils.toProtoTraceId(link.getContext().getTraceIdAsHexString())); - builder.setSpanId(TraceProtoUtils.toProtoSpanId(link.getContext().getSpanIdAsHexString())); + builder.setTraceId(TraceProtoUtils.toProtoTraceId(link.getTraceIdAsHexString())); + builder.setSpanId(TraceProtoUtils.toProtoSpanId(link.getSpanIdAsHexString())); // we can assume that all links are *follows from* // https://github.com/open-telemetry/opentelemetry-java/issues/475 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 e428b778c30..63af08debfe 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 @@ -40,7 +40,6 @@ import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.sdk.trace.data.SpanData.Link; 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; @@ -193,8 +192,7 @@ void testKeyValue() { @Test void testSpanRefs() { // prepare - Link link = - Link.create(createSpanContext("00000000000000000000000000cba123", "0000000000fed456")); + Link link = Link.create(createSpan("00000000000000000000000000cba123", "0000000000fed456")); // test Collection spanRefs = Adapter.toSpanRefs(Collections.singletonList(link)); @@ -206,7 +204,7 @@ void testSpanRefs() { @Test void testSpanRef() { // prepare - Link link = Link.create(createSpanContext(TRACE_ID, SPAN_ID)); + Link link = Link.create(createSpan(TRACE_ID, SPAN_ID)); // test Model.SpanRef spanRef = Adapter.toSpanRef(link); @@ -281,7 +279,7 @@ private static EventImpl getTimedEvent() { private static SpanData getSpanData(long startMs, long endMs) { Attributes attributes = Attributes.of(booleanKey("valueB"), true); - Link link = Link.create(createSpanContext(LINK_TRACE_ID, LINK_SPAN_ID), attributes); + Link link = Link.create(createSpan(LINK_TRACE_ID, LINK_SPAN_ID), attributes); return TestSpanData.newBuilder() .setHasEnded(true) @@ -302,8 +300,8 @@ private static SpanData getSpanData(long startMs, long endMs) { .build(); } - private static SpanContext createSpanContext(String traceId, String spanId) { - return SpanContext.create( + private static Span createSpan(String traceId, String spanId) { + return Span.getPropagated( traceId, spanId, TraceFlags.getDefault(), TraceState.builder().build()); } 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 23a2ac4722f..3b17a608b40 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 @@ -164,8 +164,8 @@ public void consume(AttributeKey key, T value) { static Span.Link toProtoSpanLink(Link link) { final Span.Link.Builder builder = Span.Link.newBuilder(); - builder.setTraceId(TraceProtoUtils.toProtoTraceId(link.getContext().getTraceIdAsHexString())); - builder.setSpanId(TraceProtoUtils.toProtoSpanId(link.getContext().getSpanIdAsHexString())); + builder.setTraceId(TraceProtoUtils.toProtoTraceId(link.getTraceIdAsHexString())); + builder.setSpanId(TraceProtoUtils.toProtoSpanId(link.getSpanIdAsHexString())); // TODO: Set TraceState; Attributes attributes = link.getAttributes(); attributes.forEach( 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 4293aa47724..348be85c9ae 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 @@ -52,7 +52,6 @@ import io.opentelemetry.sdk.trace.data.EventImpl; import io.opentelemetry.sdk.trace.data.SpanData.Link; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -69,8 +68,9 @@ class SpanAdapterTest { private static final String SPAN_ID = SpanId.bytesToHex(SPAN_ID_BYTES); private static final TraceState TRACE_STATE = TraceState.builder().build(); - private static final SpanContext SPAN_CONTEXT = - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TRACE_STATE); + private static final io.opentelemetry.trace.Span SPAN = + io.opentelemetry.trace.Span.getPropagated( + TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TRACE_STATE); @Test void toProtoSpan() { @@ -91,7 +91,7 @@ void toProtoSpan() { Collections.singletonList( EventImpl.create(12347, "my_event", Attributes.empty()))) .setTotalRecordedEvents(3) - .setLinks(Collections.singletonList(Link.create(SPAN_CONTEXT))) + .setLinks(Collections.singletonList(Link.create(SPAN))) .setTotalRecordedLinks(2) .setStatus(io.opentelemetry.trace.Status.OK) .build()); @@ -285,7 +285,7 @@ void toProtoSpanEvent_WithAttributes() { @Test void toProtoSpanLink_WithoutAttributes() { - assertThat(SpanAdapter.toProtoSpanLink(Link.create(SPAN_CONTEXT))) + assertThat(SpanAdapter.toProtoSpanLink(Link.create(SPAN))) .isEqualTo( Span.Link.newBuilder() .setTraceId(ByteString.copyFrom(TRACE_ID_BYTES)) @@ -297,7 +297,7 @@ void toProtoSpanLink_WithoutAttributes() { void toProtoSpanLink_WithAttributes() { assertThat( SpanAdapter.toProtoSpanLink( - Link.create(SPAN_CONTEXT, Attributes.of(stringKey("key_string"), "string"), 5))) + Link.create(SPAN, Attributes.of(stringKey("key_string"), "string"), 5))) .isEqualTo( Span.Link.newBuilder() .setTraceId(ByteString.copyFrom(TRACE_ID_BYTES)) diff --git a/extensions/trace_propagators/src/jmh/java/io/opentelemetry/extensions/trace/propagation/PropagatorContextInjectBenchmark.java b/extensions/trace_propagators/src/jmh/java/io/opentelemetry/extensions/trace/propagation/PropagatorContextInjectBenchmark.java index 6d8b593e193..8d40d18de77 100644 --- a/extensions/trace_propagators/src/jmh/java/io/opentelemetry/extensions/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/extensions/trace_propagators/src/jmh/java/io/opentelemetry/extensions/trace/propagation/PropagatorContextInjectBenchmark.java @@ -18,8 +18,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.TracingContextUtils; @@ -52,18 +51,18 @@ private PropagatorContextInjectBenchmark() {} @State(Scope.Thread) public abstract static class AbstractContextInjectBenchmark { - private static final List spanContexts = + private static final List spans = Arrays.asList( - createTestSpanContext("905734c59b913b4a905734c59b913b4a", "9909983295041501"), - createTestSpanContext("21196a77f299580e21196a77f299580e", "993a97ee3691eb26"), - createTestSpanContext("2e7d0ad2390617702e7d0ad239061770", "d49582a2de984b86"), - createTestSpanContext("905734c59b913b4a905734c59b913b4a", "776ff807b787538a"), - createTestSpanContext("68ec932c33b3f2ee68ec932c33b3f2ee", "68ec932c33b3f2ee")); + createTestSpan("905734c59b913b4a905734c59b913b4a", "9909983295041501"), + createTestSpan("21196a77f299580e21196a77f299580e", "993a97ee3691eb26"), + createTestSpan("2e7d0ad2390617702e7d0ad239061770", "d49582a2de984b86"), + createTestSpan("905734c59b913b4a905734c59b913b4a", "776ff807b787538a"), + createTestSpan("68ec932c33b3f2ee68ec932c33b3f2ee", "68ec932c33b3f2ee")); private final Map carrier = new HashMap<>(); private Integer iteration = 0; - private SpanContext contextToTest = spanContexts.get(iteration); + private Span spanToTest = spans.get(iteration); /** Benchmark for measuring inject with default trace state and sampled trace options. */ @Benchmark @@ -73,8 +72,7 @@ public abstract static class AbstractContextInjectBenchmark { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Map measureInject() { - Context context = - TracingContextUtils.withSpan(DefaultSpan.create(contextToTest), Context.current()); + Context context = TracingContextUtils.withSpan(spanToTest, Context.current()); doInject(context, carrier); return carrier; } @@ -83,12 +81,12 @@ public Map measureInject() { @TearDown(Level.Iteration) public void tearDown() { - this.contextToTest = spanContexts.get(++iteration % spanContexts.size()); + this.spanToTest = spans.get(++iteration % spans.size()); } - private static SpanContext createTestSpanContext(String traceId, String spanId) { + private static Span createTestSpan(String traceId, String spanId) { TraceState traceStateDefault = TraceState.builder().build(); - return SpanContext.create(traceId, spanId, TraceFlags.getSampled(), traceStateDefault); + return Span.getPropagated(traceId, spanId, TraceFlags.getSampled(), traceStateDefault); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagator.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagator.java index 5098007008d..8fc3f8168d8 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagator.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagator.java @@ -18,9 +18,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -97,21 +95,19 @@ public void inject(Context context, @Nullable C carrier, Setter setter) { Objects.requireNonNull(setter, "setter"); Span span = TracingContextUtils.getSpan(context); - if (!span.getContext().isValid()) { + if (!span.isValid()) { return; } - SpanContext spanContext = span.getContext(); - - String otTraceId = spanContext.getTraceIdAsHexString(); + String otTraceId = span.getTraceIdAsHexString(); String xrayTraceId = TRACE_ID_VERSION + TRACE_ID_DELIMITER + otTraceId.substring(0, TRACE_ID_FIRST_PART_LENGTH) + TRACE_ID_DELIMITER + otTraceId.substring(TRACE_ID_FIRST_PART_LENGTH); - String parentId = spanContext.getSpanIdAsHexString(); - char samplingFlag = spanContext.isSampled() ? IS_SAMPLED : NOT_SAMPLED; + String parentId = span.getSpanIdAsHexString(); + char samplingFlag = span.isSampled() ? IS_SAMPLED : NOT_SAMPLED; // TODO: Add OT trace state to the X-Ray trace header String traceHeader = @@ -134,18 +130,18 @@ public Context extract(Context context, C carrier, Getter getter) { Objects.requireNonNull(carrier, "carrier"); Objects.requireNonNull(getter, "getter"); - SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - if (!spanContext.isValid()) { + Span extracted = extractSpanFromHeader(carrier, getter); + if (!extracted.isValid()) { return context; } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return TracingContextUtils.withSpan(extracted, context); } - private static SpanContext getSpanContextFromHeader(C carrier, Getter getter) { + private static Span extractSpanFromHeader(C carrier, Getter getter) { String traceHeader = getter.get(carrier, TRACE_HEADER_KEY); if (traceHeader == null || traceHeader.isEmpty()) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } String traceId = TraceId.getInvalid(); @@ -171,7 +167,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get "Error parsing X-Ray trace header. Invalid key value pair: " + part + " Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String value = trimmedPart.substring(equalsIndex + 1); @@ -192,7 +188,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get + "' with value " + traceHeader + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } if (!SpanId.isValid(spanId)) { @@ -202,7 +198,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get + "' with value " + traceHeader + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } if (isSampled == null) { @@ -212,11 +208,11 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get + "' with value " + traceHeader + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } byte traceFlags = isSampled ? TraceFlags.getSampled() : TraceFlags.getDefault(); - return SpanContext.createFromRemoteParent(traceId, spanId, traceFlags, TraceState.getDefault()); + return Span.getPropagated(traceId, spanId, traceFlags, TraceState.getDefault()); } private static String parseTraceId(String xrayTraceId) { diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorMultipleHeaders.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorMultipleHeaders.java index 0755e556cc7..04edc641548 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorMultipleHeaders.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorMultipleHeaders.java @@ -22,8 +22,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TracingContextUtils; import java.util.Objects; import java.util.logging.Logger; @@ -38,33 +37,33 @@ final class B3PropagatorExtractorMultipleHeaders implements B3PropagatorExtracto public Context extract(Context context, C carrier, TextMapPropagator.Getter getter) { Objects.requireNonNull(carrier, "carrier"); Objects.requireNonNull(getter, "getter"); - SpanContext spanContext = getSpanContextFromMultipleHeaders(carrier, getter); - if (!spanContext.isValid()) { + Span extracted = extractSpanFromMultipleHeaders(carrier, getter); + if (!extracted.isValid()) { return context; } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return TracingContextUtils.withSpan(extracted, context); } - private static SpanContext getSpanContextFromMultipleHeaders( + private static Span extractSpanFromMultipleHeaders( C carrier, TextMapPropagator.Getter getter) { String traceId = getter.get(carrier, TRACE_ID_HEADER); if (StringUtils.isNullOrEmpty(traceId)) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } if (!Common.isTraceIdValid(traceId)) { logger.fine( "Invalid TraceId in B3 header: " + traceId + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String spanId = getter.get(carrier, SPAN_ID_HEADER); if (!Common.isSpanIdValid(spanId)) { logger.fine("Invalid SpanId in B3 header: " + spanId + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String sampled = getter.get(carrier, SAMPLED_HEADER); - return Common.buildSpanContext(traceId, spanId, sampled); + return Common.buildPropagatedSpan(traceId, spanId, sampled); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorSingleHeader.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorSingleHeader.java index d3f073e46aa..c7d59d1bf9a 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorSingleHeader.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorExtractorSingleHeader.java @@ -21,8 +21,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TracingContextUtils; import java.util.Objects; import java.util.logging.Logger; @@ -37,20 +36,20 @@ final class B3PropagatorExtractorSingleHeader implements B3PropagatorExtractor { public Context extract(Context context, C carrier, TextMapPropagator.Getter getter) { Objects.requireNonNull(carrier, "carrier"); Objects.requireNonNull(getter, "getter"); - SpanContext spanContext = getSpanContextFromSingleHeader(carrier, getter); - if (!spanContext.isValid()) { + Span extracted = extractSpanFromSingleHeader(carrier, getter); + if (!extracted.isValid()) { return context; } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return TracingContextUtils.withSpan(extracted, context); } @SuppressWarnings("StringSplitter") - private static SpanContext getSpanContextFromSingleHeader( + private static Span extractSpanFromSingleHeader( C carrier, TextMapPropagator.Getter getter) { String value = getter.get(carrier, COMBINED_HEADER); if (StringUtils.isNullOrEmpty(value)) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } // must have between 2 and 4 hyphen delimited parts: @@ -60,25 +59,25 @@ private static SpanContext getSpanContextFromSingleHeader( if (parts.length < 2 || parts.length > 4) { logger.fine( "Invalid combined header '" + COMBINED_HEADER + ". Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String traceId = parts[0]; if (!Common.isTraceIdValid(traceId)) { logger.fine( "Invalid TraceId in B3 header: " + COMBINED_HEADER + ". Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String spanId = parts[1]; if (!Common.isSpanIdValid(spanId)) { logger.fine( "Invalid SpanId in B3 header: " + COMBINED_HEADER + ". Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String sampled = parts.length >= 3 ? parts[2] : null; - return Common.buildSpanContext(traceId, spanId, sampled); + return Common.buildPropagatedSpan(traceId, spanId, sampled); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorMultipleHeaders.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorMultipleHeaders.java index bcb84fe6bf1..2031b1b3ece 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorMultipleHeaders.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorMultipleHeaders.java @@ -18,7 +18,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TracingContextUtils; import java.util.Objects; import javax.annotation.concurrent.Immutable; @@ -30,15 +30,15 @@ public void inject(Context context, C carrier, TextMapPropagator.Setter s Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); - if (!spanContext.isValid()) { + Span span = TracingContextUtils.getSpan(context); + if (!span.isValid()) { return; } - String sampled = spanContext.isSampled() ? Common.TRUE_INT : Common.FALSE_INT; + String sampled = span.isSampled() ? Common.TRUE_INT : Common.FALSE_INT; - setter.set(carrier, B3Propagator.TRACE_ID_HEADER, spanContext.getTraceIdAsHexString()); - setter.set(carrier, B3Propagator.SPAN_ID_HEADER, spanContext.getSpanIdAsHexString()); + setter.set(carrier, B3Propagator.TRACE_ID_HEADER, span.getTraceIdAsHexString()); + setter.set(carrier, B3Propagator.SPAN_ID_HEADER, span.getSpanIdAsHexString()); setter.set(carrier, B3Propagator.SAMPLED_HEADER, sampled); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorSingleHeader.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorSingleHeader.java index 4de2c17d8f1..d32c7b2011d 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorSingleHeader.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorInjectorSingleHeader.java @@ -18,7 +18,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TracingContextUtils; @@ -41,22 +41,22 @@ public void inject(Context context, C carrier, TextMapPropagator.Setter s Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); - if (!spanContext.isValid()) { + Span span = TracingContextUtils.getSpan(context); + if (!span.isValid()) { return; } char[] chars = new char[COMBINED_HEADER_SIZE]; - String traceId = spanContext.getTraceIdAsHexString(); + String traceId = span.getTraceIdAsHexString(); traceId.getChars(0, traceId.length(), chars, 0); chars[SPAN_ID_OFFSET - 1] = B3Propagator.COMBINED_HEADER_DELIMITER_CHAR; - String spanId = spanContext.getSpanIdAsHexString(); + String spanId = span.getSpanIdAsHexString(); System.arraycopy(spanId.toCharArray(), 0, chars, SPAN_ID_OFFSET, SpanId.getHexLength()); chars[SAMPLED_FLAG_OFFSET - 1] = B3Propagator.COMBINED_HEADER_DELIMITER_CHAR; chars[SAMPLED_FLAG_OFFSET] = - spanContext.isSampled() ? B3Propagator.IS_SAMPLED : B3Propagator.NOT_SAMPLED; + span.isSampled() ? B3Propagator.IS_SAMPLED : B3Propagator.NOT_SAMPLED; setter.set(carrier, B3Propagator.COMBINED_HEADER, new String(chars)); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/Common.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/Common.java index 8016564d92f..55d3353c250 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/Common.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/Common.java @@ -16,7 +16,7 @@ package io.opentelemetry.extensions.trace.propagation; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -42,21 +42,21 @@ final class Common { private Common() {} - static SpanContext buildSpanContext(String traceId, String spanId, String sampled) { + static Span buildPropagatedSpan(String traceId, String spanId, String sampled) { try { byte traceFlags = TRUE_INT.equals(sampled) || Boolean.parseBoolean(sampled) // accept either "1" or "true" ? SAMPLED : NOT_SAMPLED; - return SpanContext.createFromRemoteParent( + return Span.getPropagated( StringUtils.padLeft(traceId, MAX_TRACE_ID_LENGTH), spanId, traceFlags, TraceState.getDefault()); } catch (Exception e) { logger.log(Level.INFO, "Error parsing header. Returning INVALID span context.", e); - return SpanContext.getInvalid(); + return Span.getInvalid(); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagator.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagator.java index 05836ed2468..c07ddf48ad1 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagator.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagator.java @@ -18,8 +18,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -94,20 +93,20 @@ public void inject(Context context, C carrier, Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); - if (!spanContext.isValid()) { + Span span = TracingContextUtils.getSpan(context); + if (!span.isValid()) { return; } char[] chars = new char[PROPAGATION_HEADER_SIZE]; - String traceId = spanContext.getTraceIdAsHexString(); + String traceId = span.getTraceIdAsHexString(); for (int i = 0; i < traceId.length(); i++) { chars[i] = traceId.charAt(i); } chars[SPAN_ID_OFFSET - 1] = PROPAGATION_HEADER_DELIMITER; - String spanId = spanContext.getSpanIdAsHexString(); + String spanId = span.getSpanIdAsHexString(); for (int i = 0; i < spanId.length(); i++) { chars[SPAN_ID_OFFSET + i] = spanId.charAt(i); } @@ -115,7 +114,7 @@ public void inject(Context context, C carrier, Setter setter) { chars[PARENT_SPAN_ID_OFFSET - 1] = PROPAGATION_HEADER_DELIMITER; chars[PARENT_SPAN_ID_OFFSET] = DEPRECATED_PARENT_SPAN; chars[SAMPLED_FLAG_OFFSET - 1] = PROPAGATION_HEADER_DELIMITER; - chars[SAMPLED_FLAG_OFFSET] = spanContext.isSampled() ? IS_SAMPLED_CHAR : NOT_SAMPLED_CHAR; + chars[SAMPLED_FLAG_OFFSET] = span.isSampled() ? IS_SAMPLED_CHAR : NOT_SAMPLED_CHAR; setter.set(carrier, PROPAGATION_HEADER, new String(chars)); } @@ -124,19 +123,19 @@ public Context extract(Context context, C carrier, Getter getter) { Objects.requireNonNull(carrier, "carrier"); Objects.requireNonNull(getter, "getter"); - SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - if (!spanContext.isValid()) { + Span extracted = extractSpanFromHeader(carrier, getter); + if (!extracted.isValid()) { return context; } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return TracingContextUtils.withSpan(extracted, context); } @SuppressWarnings("StringSplitter") - private static SpanContext getSpanContextFromHeader(C carrier, Getter getter) { + private static Span extractSpanFromHeader(C carrier, Getter getter) { String value = getter.get(carrier, PROPAGATION_HEADER); if (StringUtils.isNullOrEmpty(value)) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } // if the delimiter (:) cannot be found then the propagation value could be URL @@ -152,7 +151,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get + "' with value " + value + ". Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } } @@ -164,7 +163,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get + "' with value " + value + ". Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String traceId = parts[0]; @@ -175,7 +174,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get + "' with traceId " + traceId + ". Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String spanId = parts[1]; @@ -184,7 +183,7 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get "Invalid SpanId in Jaeger header: '" + PROPAGATION_HEADER + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } String flags = parts[3]; @@ -193,13 +192,13 @@ private static SpanContext getSpanContextFromHeader(C carrier, Getter get "Invalid Flags in Jaeger header: '" + PROPAGATION_HEADER + "'. Returning INVALID span context."); - return SpanContext.getInvalid(); + return Span.getInvalid(); } return buildSpanContext(traceId, spanId, flags); } - private static SpanContext buildSpanContext(String traceId, String spanId, String flags) { + private static Span buildSpanContext(String traceId, String spanId, String flags) { try { int flagsInt = Integer.parseInt(flags); byte traceFlags = ((flagsInt & 1) == 1) ? SAMPLED : NOT_SAMPLED; @@ -207,16 +206,15 @@ private static SpanContext buildSpanContext(String traceId, String spanId, Strin String otelTraceId = StringUtils.padLeft(traceId, MAX_TRACE_ID_LENGTH); String otelSpanId = StringUtils.padLeft(spanId, MAX_SPAN_ID_LENGTH); if (!TraceId.isValid(otelTraceId) || !SpanId.isValid(otelSpanId)) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } - return SpanContext.createFromRemoteParent( - otelTraceId, otelSpanId, traceFlags, TraceState.getDefault()); + return Span.getPropagated(otelTraceId, otelSpanId, traceFlags, TraceState.getDefault()); } catch (Exception e) { logger.log( Level.FINE, "Error parsing '" + PROPAGATION_HEADER + "' header. Returning INVALID span context.", e); - return SpanContext.getInvalid(); + return Span.getInvalid(); } } diff --git a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagator.java b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagator.java index 6095eed3649..46f27243d00 100644 --- a/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagator.java +++ b/extensions/trace_propagators/src/main/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagator.java @@ -20,8 +20,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TracingContextUtils; import java.util.Arrays; @@ -65,13 +64,13 @@ public void inject(Context context, C carrier, Setter setter) { if (context == null || setter == null) { return; } - final SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); - if (!spanContext.isValid()) { + final Span span = TracingContextUtils.getSpan(context); + if (!span.isValid()) { return; } - setter.set(carrier, TRACE_ID_HEADER, spanContext.getTraceIdAsHexString()); - setter.set(carrier, SPAN_ID_HEADER, spanContext.getSpanIdAsHexString()); - setter.set(carrier, SAMPLED_HEADER, String.valueOf(spanContext.isSampled())); + setter.set(carrier, TRACE_ID_HEADER, span.getTraceIdAsHexString()); + setter.set(carrier, SPAN_ID_HEADER, span.getSpanIdAsHexString()); + setter.set(carrier, SAMPLED_HEADER, String.valueOf(span.isSampled())); } @Override @@ -86,17 +85,17 @@ public Context extract(Context context, C carrier, Getter getter) { : StringUtils.padLeft(incomingTraceId, MAX_TRACE_ID_LENGTH); String spanId = getter.get(carrier, SPAN_ID_HEADER); String sampled = getter.get(carrier, SAMPLED_HEADER); - SpanContext spanContext = buildSpanContext(traceId, spanId, sampled); - if (!spanContext.isValid()) { + Span extracted = buildPropagatedSpan(traceId, spanId, sampled); + if (!extracted.isValid()) { return context; } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return TracingContextUtils.withSpan(extracted, context); } - static SpanContext buildSpanContext(String traceId, String spanId, String sampled) { + static Span buildPropagatedSpan(String traceId, String spanId, String sampled) { if (!Common.isTraceIdValid(traceId) || !Common.isSpanIdValid(spanId)) { - return SpanContext.getInvalid(); + return Span.getInvalid(); } - return Common.buildSpanContext(traceId, spanId, sampled); + return Common.buildPropagatedSpan(traceId, spanId, sampled); } } diff --git a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagatorTest.java b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagatorTest.java index fbcc4ca1dd9..a6e7cf308a6 100644 --- a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagatorTest.java +++ b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/AwsXRayPropagatorTest.java @@ -21,8 +21,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.TracingContextUtils; @@ -55,8 +54,8 @@ public String get(Map carrier, String key) { void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); xrayPropagator.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_FLAG, TRACE_STATE_DEFAULT), Context.current()), carrier, @@ -72,8 +71,8 @@ void inject_SampledContext() { void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); xrayPropagator.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()), carrier, @@ -89,8 +88,8 @@ void inject_NotSampledContext() { void inject_WithTraceState() { Map carrier = new LinkedHashMap<>(); xrayPropagator.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), @@ -122,9 +121,10 @@ void extract_SampledContext() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(xrayPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_FLAG, TRACE_STATE_DEFAULT)); } @@ -135,9 +135,10 @@ void extract_NotSampledContext() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(xrayPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -148,9 +149,10 @@ void extract_DifferentPartOrder() { TRACE_HEADER_KEY, "Parent=53995c3f42cd8ad8;Sampled=1;Root=1-8a3c60f7-d188f8fa79d48a391a778fa6"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(xrayPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_FLAG, TRACE_STATE_DEFAULT)); } @@ -162,9 +164,10 @@ void extract_AdditionalFields() { "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1;Foo=Bar"); // TODO: assert additional fields when the propagator supports it - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(xrayPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( TRACE_ID_BASE16, SPAN_ID_BASE16, SAMPLED_TRACE_FLAG, TRACE_STATE_DEFAULT)); } @@ -173,8 +176,10 @@ void extract_EmptyHeaderValue() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_HEADER_KEY, ""); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -184,8 +189,10 @@ void extract_InvalidTraceId() { TRACE_HEADER_KEY, "Root=abcdefghijklmnopabcdefghijklmnop;Parent=53995c3f42cd8ad8;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -195,8 +202,10 @@ void extract_InvalidTraceId_Size() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa600;Parent=53995c3f42cd8ad8;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -206,8 +215,10 @@ void extract_InvalidSpanId() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=abcdefghijklmnop;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -217,8 +228,10 @@ void extract_InvalidSpanId_Size() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad800;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -228,8 +241,10 @@ void extract_InvalidFlags() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled="); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -239,8 +254,10 @@ void extract_InvalidFlags_Size() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=10220"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -250,15 +267,9 @@ void extract_InvalidFlags_NonNumeric() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=a"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); - } - - private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); - } - - private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); + assertThat( + TracingContextUtils.getSpan( + xrayPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } } diff --git a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorTest.java b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorTest.java index 1d328414dd0..e45fa15f4f5 100644 --- a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorTest.java +++ b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/B3PropagatorTest.java @@ -21,8 +21,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator.Getter; import io.opentelemetry.context.propagation.TextMapPropagator.Setter; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -58,20 +57,12 @@ public String get(Map carrier, String key) { private final B3Propagator b3Propagator = B3Propagator.getMultipleHeaderPropagator(); private final B3Propagator b3PropagatorSingleHeader = B3Propagator.getSingleHeaderPropagator(); - private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); - } - - private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); - } - @Test void inject_invalidContext() { Map carrier = new LinkedHashMap<>(); b3Propagator.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TraceId.getInvalid(), SpanId.getInvalid(), SAMPLED_TRACE_OPTIONS, @@ -86,8 +77,8 @@ void inject_invalidContext() { void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); b3Propagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -100,8 +91,8 @@ void inject_SampledContext() { void inject_SampledContext_nullCarrierUsage() { final Map carrier = new LinkedHashMap<>(); b3Propagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), null, (Setter>) (ignored, key, value) -> carrier.put(key, value)); @@ -114,8 +105,8 @@ void inject_SampledContext_nullCarrierUsage() { void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); b3Propagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -140,10 +131,10 @@ void extract_SampledContext_Int() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -153,10 +144,10 @@ void extract_SampledContext_Bool() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -166,10 +157,10 @@ void extract_NotSampledContext() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(B3Propagator.SAMPLED_HEADER, Common.FALSE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @Test @@ -179,9 +170,10 @@ void extract_SampledContext_Int_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -192,9 +184,10 @@ void extract_SampledContext_Bool_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -205,9 +198,10 @@ void extract_NotSampledContext_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(B3Propagator.SAMPLED_HEADER, Common.FALSE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -217,8 +211,10 @@ void extract_InvalidTraceId_NotHex() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, "g" + TRACE_ID.substring(1)); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -227,8 +223,10 @@ void extract_InvalidTraceId_TooShort() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID.substring(2)); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -237,8 +235,10 @@ void extract_InvalidTraceId_TooLong() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID + "00"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -247,8 +247,10 @@ void extract_InvalidTraceId_AllZero() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_ALL_ZERO); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -257,8 +259,10 @@ void extract_InvalidSpanId_NotHex() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "g" + SPAN_ID.substring(1)); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -267,8 +271,10 @@ void extract_InvalidSpanId_TooShort() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID.substring(2)); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -277,8 +283,10 @@ void extract_InvalidSpanId_TooLong() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID + "00"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -287,16 +295,18 @@ void extract_InvalidSpanId_AllZeros() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_ALL_ZERO); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void inject_invalidContext_SingleHeader() { Map carrier = new LinkedHashMap<>(); b3PropagatorSingleHeader.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TraceId.getInvalid(), SpanId.getInvalid(), SAMPLED_TRACE_OPTIONS, @@ -311,8 +321,8 @@ void inject_invalidContext_SingleHeader() { void inject_SampledContext_SingleHeader() { Map carrier = new LinkedHashMap<>(); b3PropagatorSingleHeader.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -325,8 +335,8 @@ void inject_SampledContext_SingleHeader() { void inject_NotSampledContext_SingleHeader() { Map carrier = new LinkedHashMap<>(); b3PropagatorSingleHeader.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -349,10 +359,11 @@ void extract_SampledContext_Int_SingleHeader() { Map carrier = new LinkedHashMap<>(); carrier.put(B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -361,10 +372,11 @@ void extract_SampledContext_DebugFlag_SingleHeader() { carrier.put( B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + Common.TRUE_INT + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -372,10 +384,11 @@ void extract_SampledContext_Bool_SingleHeader() { Map carrier = new LinkedHashMap<>(); carrier.put(B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + "true"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -383,10 +396,11 @@ void extract_SampledContext_Bool_DebugFlag_SingleHeader() { Map carrier = new LinkedHashMap<>(); carrier.put(B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + "true" + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -394,10 +408,11 @@ void extract_NotSampledContext_SingleHeader() { Map carrier = new LinkedHashMap<>(); carrier.put(B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + Common.FALSE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @Test @@ -406,9 +421,11 @@ void extract_SampledContext_Int_SingleHeader_Short_TraceId() { carrier.put( B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID + "-" + SPAN_ID + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -419,9 +436,11 @@ void extract_SampledContext_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID + "-" + SPAN_ID + "-" + Common.TRUE_INT + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -430,9 +449,11 @@ void extract_SampledContext_Bool_SingleHeader_Short_TraceId() { Map carrier = new LinkedHashMap<>(); carrier.put(B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID + "-" + SPAN_ID + "-" + "true"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -442,9 +463,11 @@ void extract_SampledContext_Bool_DebugFlag_SingleHeader_Short_TraceId() { carrier.put( B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID + "-" + SPAN_ID + "-" + "true" + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -454,9 +477,11 @@ void extract_NotSampledContext_SingleHeader_Short_TraceId() { carrier.put( B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID + "-" + SPAN_ID + "-" + Common.FALSE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -466,9 +491,9 @@ void extract_Null_SingleHeader() { invalidHeaders.put(B3Propagator.COMBINED_HEADER, null); assertThat( - getSpanContext( + TracingContextUtils.getSpan( b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + .isSameAs(Span.getInvalid()); } @Test @@ -477,9 +502,9 @@ void extract_Empty_SingleHeader() { invalidHeaders.put(B3Propagator.COMBINED_HEADER, ""); assertThat( - getSpanContext( + TracingContextUtils.getSpan( b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + .isSameAs(Span.getInvalid()); } @Test @@ -490,9 +515,9 @@ void extract_InvalidTraceId_SingleHeader() { "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID + "-" + Common.TRUE_INT); assertThat( - getSpanContext( + TracingContextUtils.getSpan( b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + .isSameAs(Span.getInvalid()); } @Test @@ -503,9 +528,9 @@ void extract_InvalidTraceId_Size_SingleHeader() { "abcdefghijklmnopabcdefghijklmnop" + "00" + "-" + SPAN_ID + "-" + Common.TRUE_INT); assertThat( - getSpanContext( + TracingContextUtils.getSpan( b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + .isSameAs(Span.getInvalid()); } @Test @@ -515,9 +540,9 @@ void extract_InvalidSpanId_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + "abcdefghijklmnop" + "-" + Common.TRUE_INT); assertThat( - getSpanContext( + TracingContextUtils.getSpan( b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + .isSameAs(Span.getInvalid()); } @Test @@ -527,8 +552,10 @@ void extract_InvalidSpanId_Size_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + "abcdefghijklmnop" + "00" + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -536,8 +563,10 @@ void extract_TooFewParts_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, TRACE_ID); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -546,8 +575,10 @@ void extract_TooManyParts_SingleHeader() { invalidHeaders.put( B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + Common.TRUE_INT + "-extra"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -567,7 +598,9 @@ void headerNames() { @Test void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), emptyHeaders, getter))) - .isEqualTo(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + b3Propagator.extract(Context.current(), emptyHeaders, getter))) + .isEqualTo(Span.getInvalid()); } } diff --git a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagatorTest.java b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagatorTest.java index f98c4bfcd7e..aad91e84cda 100644 --- a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagatorTest.java +++ b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/JaegerPropagatorTest.java @@ -26,8 +26,7 @@ import io.jaegertracing.internal.propagation.TextMapCodec; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.context.propagation.TextMapPropagator.Setter; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -70,20 +69,12 @@ public String get(Map carrier, String key) { private final JaegerPropagator jaegerPropagator = JaegerPropagator.getInstance(); - private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); - } - - private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); - } - @Test void inject_invalidContext() { Map carrier = new LinkedHashMap<>(); jaegerPropagator.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TraceId.getInvalid(), SpanId.getInvalid(), SAMPLED_TRACE_OPTIONS, @@ -98,8 +89,8 @@ void inject_invalidContext() { void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); jaegerPropagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -116,8 +107,8 @@ void inject_SampledContext_nullCarrierUsage() { final Map carrier = new LinkedHashMap<>(); jaegerPropagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), null, (Setter>) (ignored, key, value) -> carrier.put(key, value)); @@ -133,8 +124,8 @@ void inject_SampledContext_nullCarrierUsage() { void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); jaegerPropagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -159,8 +150,10 @@ void extract_EmptyHeaderValue() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, ""); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -168,8 +161,10 @@ void extract_NotEnoughParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc"); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -177,8 +172,10 @@ void extract_TooManyParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc:dd:ee"); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -189,8 +186,10 @@ void extract_InvalidTraceId() { generateTraceIdHeaderValue( "abcdefghijklmnopabcdefghijklmnop", SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -201,8 +200,10 @@ void extract_InvalidTraceId_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16 + "00", SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -213,8 +214,10 @@ void extract_InvalidSpanId() { generateTraceIdHeaderValue( TRACE_ID_BASE16, "abcdefghijklmnop", DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -225,8 +228,10 @@ void extract_InvalidSpanId_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16 + "00", DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -236,8 +241,10 @@ void extract_InvalidFlags() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -248,8 +255,10 @@ void extract_InvalidFlags_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "10220")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -260,8 +269,10 @@ void extract_InvalidFlags_NonNumeric() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "abcdefr")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -272,10 +283,11 @@ void extract_SampledContext() { TRACE_ID_HI, TRACE_ID_LOW, SPAN_ID_LONG, DEPRECATED_PARENT_SPAN_LONG, (byte) 5); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -286,10 +298,11 @@ void extract_NotSampledContext() { TRACE_ID_HI, TRACE_ID_LOW, SPAN_ID_LONG, DEPRECATED_PARENT_SPAN_LONG, (byte) 0); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @Test @@ -304,9 +317,11 @@ void extract_SampledContext_Short_TraceId() { (byte) 1); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -319,10 +334,11 @@ void extract_UrlEncodedContext() throws UnsupportedEncodingException { carrier.put( PROPAGATION_HEADER, URLEncoder.encode(TextMapCodec.contextAsString(context), "UTF-8")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat( + TracingContextUtils.getSpan( + jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } private static String generateTraceIdHeaderValue( diff --git a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagatorTest.java b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagatorTest.java index 0a9bc7b9cba..731d2683c86 100644 --- a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagatorTest.java +++ b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/OtTracerPropagatorTest.java @@ -21,8 +21,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator.Getter; import io.opentelemetry.context.propagation.TextMapPropagator.Setter; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -46,20 +45,12 @@ class OtTracerPropagatorTest { private static final Getter> getter = Map::get; private final OtTracerPropagator propagator = OtTracerPropagator.getInstance(); - private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); - } - - private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); - } - @Test void inject_invalidContext() { Map carrier = new LinkedHashMap<>(); propagator.inject( - withSpanContext( - SpanContext.create( + TracingContextUtils.withSpan( + Span.getPropagated( TraceId.getInvalid(), SpanId.getInvalid(), SAMPLED_TRACE_OPTIONS, @@ -74,8 +65,8 @@ void inject_invalidContext() { void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); propagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -88,8 +79,8 @@ void inject_SampledContext() { void inject_SampledContext_nullCarrierUsage() { final Map carrier = new LinkedHashMap<>(); propagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()), null, (Setter>) (ignored, key, value) -> carrier.put(key, value)); @@ -102,8 +93,8 @@ void inject_SampledContext_nullCarrierUsage() { void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); propagator.inject( - withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + TracingContextUtils.withSpan( + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()), carrier, setter); @@ -127,10 +118,9 @@ void extract_SampledContext_Int() { carrier.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(OtTracerPropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter))) + assertThat(TracingContextUtils.getSpan(propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -140,10 +130,9 @@ void extract_SampledContext_Bool() { carrier.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(OtTracerPropagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter))) + assertThat(TracingContextUtils.getSpan(propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @Test @@ -153,10 +142,9 @@ void extract_NotSampledContext() { carrier.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(OtTracerPropagator.SAMPLED_HEADER, Common.FALSE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter))) + assertThat(TracingContextUtils.getSpan(propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); + Span.getPropagated(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @Test @@ -166,9 +154,9 @@ void extract_SampledContext_Int_Short_TraceId() { carrier.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(OtTracerPropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter))) + assertThat(TracingContextUtils.getSpan(propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -179,9 +167,9 @@ void extract_SampledContext_Bool_Short_TraceId() { carrier.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(OtTracerPropagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter))) + assertThat(TracingContextUtils.getSpan(propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); } @@ -192,9 +180,9 @@ void extract_NotSampledContext_Short_TraceId() { carrier.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); carrier.put(OtTracerPropagator.SAMPLED_HEADER, Common.FALSE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter))) + assertThat(TracingContextUtils.getSpan(propagator.extract(Context.current(), carrier, getter))) .isEqualTo( - SpanContext.createFromRemoteParent( + Span.getPropagated( SHORT_TRACE_ID_FULL, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); } @@ -204,8 +192,10 @@ void extract_InvalidTraceId() { invalidHeaders.put(OtTracerPropagator.TRACE_ID_HEADER, "abcdefghijklmnopabcdefghijklmnop"); invalidHeaders.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(OtTracerPropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -214,8 +204,10 @@ void extract_InvalidTraceId_Size() { invalidHeaders.put(OtTracerPropagator.TRACE_ID_HEADER, TRACE_ID + "00"); invalidHeaders.put(OtTracerPropagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(OtTracerPropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -224,8 +216,10 @@ void extract_InvalidSpanId() { invalidHeaders.put(OtTracerPropagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(OtTracerPropagator.SPAN_ID_HEADER, "abcdefghijklmnop"); invalidHeaders.put(OtTracerPropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test @@ -234,14 +228,18 @@ void extract_InvalidSpanId_Size() { invalidHeaders.put(OtTracerPropagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(OtTracerPropagator.SPAN_ID_HEADER, "abcdefghijklmnop" + "00"); invalidHeaders.put(OtTracerPropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + propagator.extract(Context.current(), invalidHeaders, getter))) + .isSameAs(Span.getInvalid()); } @Test void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(propagator.extract(Context.current(), emptyHeaders, getter))) - .isEqualTo(SpanContext.getInvalid()); + assertThat( + TracingContextUtils.getSpan( + propagator.extract(Context.current(), emptyHeaders, getter))) + .isEqualTo(Span.getInvalid()); } } diff --git a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/TraceMultiPropagatorTest.java b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/TraceMultiPropagatorTest.java index ec5cd154b61..2b8cae4b166 100644 --- a/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/TraceMultiPropagatorTest.java +++ b/extensions/trace_propagators/src/test/java/io/opentelemetry/extensions/trace/propagation/TraceMultiPropagatorTest.java @@ -25,9 +25,7 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -48,12 +46,11 @@ class TraceMultiPropagatorTest { private static final TextMapPropagator PROPAGATOR3 = HttpTraceContext.getInstance(); private static final Span SPAN = - DefaultSpan.create( - SpanContext.createFromRemoteParent( - TraceId.fromLongs(1245, 67890), - SpanId.fromLong(12345), - TraceFlags.getDefault(), - TraceState.getDefault())); + Span.getPropagated( + TraceId.fromLongs(1245, 67890), + SpanId.fromLong(12345), + TraceFlags.getDefault(), + TraceState.getDefault()); @BeforeEach void init() { @@ -124,12 +121,9 @@ void inject_allFormats() { Map carrier = new HashMap<>(); prop.inject(withSpan(SPAN, Context.current()), carrier, Map::put); - assertThat(getSpan(PROPAGATOR1.extract(Context.current(), carrier, Map::get)).getContext()) - .isEqualTo(SPAN.getContext()); - assertThat(getSpan(PROPAGATOR2.extract(Context.current(), carrier, Map::get)).getContext()) - .isEqualTo(SPAN.getContext()); - assertThat(getSpan(PROPAGATOR3.extract(Context.current(), carrier, Map::get)).getContext()) - .isEqualTo(SPAN.getContext()); + assertThat(getSpan(PROPAGATOR1.extract(Context.current(), carrier, Map::get))).isEqualTo(SPAN); + assertThat(getSpan(PROPAGATOR2.extract(Context.current(), carrier, Map::get))).isEqualTo(SPAN); + assertThat(getSpan(PROPAGATOR3.extract(Context.current(), carrier, Map::get))).isEqualTo(SPAN); } @Test @@ -153,8 +147,7 @@ void extract_found() { Map carrier = new HashMap<>(); PROPAGATOR2.inject(withSpan(SPAN, Context.current()), carrier, Map::put); - assertThat(getSpan(prop.extract(Context.current(), carrier, Map::get)).getContext()) - .isEqualTo(SPAN.getContext()); + assertThat(getSpan(prop.extract(Context.current(), carrier, Map::get))).isEqualTo(SPAN); } @Test @@ -177,8 +170,7 @@ void extract_stopWhenFound() { Map carrier = new HashMap<>(); PROPAGATOR3.inject(withSpan(SPAN, Context.current()), carrier, Map::put); - assertThat(getSpan(prop.extract(Context.current(), carrier, Map::get)).getContext()) - .isEqualTo(SPAN.getContext()); + assertThat(getSpan(prop.extract(Context.current(), carrier, Map::get))).isEqualTo(SPAN); verify(mockPropagator).fields(); verifyNoMoreInteractions(mockPropagator); } 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 aff00a9ca81..57d567c3b68 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 @@ -20,7 +20,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TracingContextUtils; @@ -65,7 +64,7 @@ private void executeCallableAndExpectError(Callable callable, Throwable @Test void withSpanRunnable() { - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Runnable runnable = () -> { // When we run the runnable we will have the span in the current Context. @@ -73,12 +72,12 @@ void withSpanRunnable() { }; CurrentSpanUtils.withSpan(span, false, runnable).run(); verifyNoInteractions(span); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanRunnable_EndSpan() { - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Runnable runnable = () -> { // When we run the runnable we will have the span in the current Context. @@ -86,13 +85,13 @@ void withSpanRunnable_EndSpan() { }; CurrentSpanUtils.withSpan(span, true, runnable).run(); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanRunnable_WithError() { final AssertionError error = new AssertionError("MyError"); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Runnable runnable = () -> { // When we run the runnable we will have the span in the current Context. @@ -102,13 +101,13 @@ void withSpanRunnable_WithError() { executeRunnableAndExpectError(runnable, error); verify(span).setStatus(Status.UNKNOWN.withDescription("MyError")); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanRunnable_WithErrorNoMessage() { final AssertionError error = new AssertionError(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Runnable runnable = () -> { // When we run the runnable we will have the span in the current Context. @@ -118,13 +117,13 @@ void withSpanRunnable_WithErrorNoMessage() { executeRunnableAndExpectError(runnable, error); verify(span).setStatus(Status.UNKNOWN.withDescription("AssertionError")); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanCallable() throws Exception { final Object ret = new Object(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Callable callable = () -> { // When we run the runnable we will have the span in the current Context. @@ -133,13 +132,13 @@ void withSpanCallable() throws Exception { }; assertThat(CurrentSpanUtils.withSpan(span, false, callable).call()).isEqualTo(ret); verifyNoInteractions(span); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanCallable_EndSpan() throws Exception { final Object ret = new Object(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Callable callable = () -> { // When we run the runnable we will have the span in the current Context. @@ -148,13 +147,13 @@ void withSpanCallable_EndSpan() throws Exception { }; assertThat(CurrentSpanUtils.withSpan(span, true, callable).call()).isEqualTo(ret); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanCallable_WithException() { final Exception exception = new Exception("MyException"); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Callable callable = () -> { // When we run the runnable we will have the span in the current Context. @@ -164,13 +163,13 @@ void withSpanCallable_WithException() { executeCallableAndExpectError(callable, exception); verify(span).setStatus(Status.UNKNOWN.withDescription("MyException")); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanCallable_WithExceptionNoMessage() { final Exception exception = new Exception(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Callable callable = () -> { // When we run the runnable we will have the span in the current Context. @@ -180,13 +179,13 @@ void withSpanCallable_WithExceptionNoMessage() { executeCallableAndExpectError(callable, exception); verify(span).setStatus(Status.UNKNOWN.withDescription("Exception")); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanCallable_WithError() { final AssertionError error = new AssertionError("MyError"); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Callable callable = () -> { // When we run the runnable we will have the span in the current Context. @@ -196,13 +195,13 @@ void withSpanCallable_WithError() { executeCallableAndExpectError(callable, error); verify(span).setStatus(Status.UNKNOWN.withDescription("MyError")); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } @Test void withSpanCallable_WithErrorNoMessage() { final AssertionError error = new AssertionError(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); Callable callable = () -> { // When we run the runnable we will have the span in the current Context. @@ -212,7 +211,7 @@ void withSpanCallable_WithErrorNoMessage() { executeCallableAndExpectError(callable, error); verify(span).setStatus(Status.UNKNOWN.withDescription("AssertionError")); verify(span).end(); - assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(getCurrentSpan().isValid()).isFalse(); } private static Span getCurrentSpan() { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 8448a9a3329..96ce3207aa8 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -19,7 +19,6 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.correlationcontext.CorrelationsContextUtils; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TracingContextUtils; import io.opentracing.propagation.TextMapExtract; @@ -35,8 +34,7 @@ final class Propagation extends BaseShimObject { public void injectTextMap(SpanContextShim contextShim, TextMapInject carrier) { Context context = - TracingContextUtils.withSpan( - DefaultSpan.create(contextShim.getSpanContext()), Context.current()); + TracingContextUtils.withSpan(contextShim.getPropagatedSpan(), Context.current()); context = CorrelationsContextUtils.withCorrelationContext( contextShim.getCorrelationContext(), context); @@ -57,12 +55,12 @@ public SpanContextShim extractTextMap(TextMapExtract carrier) { .extract(Context.current(), carrierMap, TextMapGetter.INSTANCE); Span span = TracingContextUtils.getSpan(context); - if (!span.getContext().isValid()) { + if (!span.isValid()) { return null; } return new SpanContextShim( - telemetryInfo, span.getContext(), CorrelationsContextUtils.getCorrelationContext(context)); + telemetryInfo, span, CorrelationsContextUtils.getCorrelationContext(context)); } static final class TextMapSetter implements TextMapPropagator.Setter { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java index 18ecc5265af..3f513e590a0 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java @@ -16,7 +16,6 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.trace.DefaultSpan; import io.opentracing.Scope; import io.opentracing.ScopeManager; import io.opentracing.Span; @@ -30,11 +29,9 @@ public ScopeManagerShim(TelemetryInfo telemetryInfo) { @Override @SuppressWarnings("ReturnMissingNullable") public Span activeSpan() { - // As OpenTracing simply returns null when no active instance is available, - // we need to do an explicit check against DefaultSpan, - // which is used in OpenTelemetry for this very case. + // OpenTracing simply returns null when the current span is not valid. io.opentelemetry.trace.Span span = tracer().getCurrentSpan(); - if (DefaultSpan.getInvalid().equals(span)) { + if (!span.isValid()) { return null; } 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 2a4d4a91fb3..4675b8608dd 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java @@ -24,7 +24,6 @@ import io.grpc.Context; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TracingContextUtils; @@ -45,7 +44,7 @@ final class SpanBuilderShim extends BaseShimObject implements SpanBuilder { private SpanContextShim parentSpanContext; private boolean ignoreActiveSpan; - private final List parentLinks = new ArrayList<>(); + private final List parentLinkedSpans = new ArrayList<>(); @SuppressWarnings("rawtypes") private final List spanBuilderAttributeKeys = new ArrayList<>(); @@ -71,7 +70,7 @@ public SpanBuilder asChildOf(Span parent) { if (parentSpan == null && parentSpanContext == null) { parentSpan = spanShim; } else { - parentLinks.add(spanShim.getSpan().getContext()); + parentLinkedSpans.add(spanShim.getSpan()); } return this; @@ -94,7 +93,7 @@ public SpanBuilder addReference(String referenceType, SpanContext referencedCont if (parentSpan == null && parentSpanContext == null) { parentSpanContext = contextShim; } else { - parentLinks.add(contextShim.getSpanContext()); + parentLinkedSpans.add(contextShim.getPropagatedSpan()); } return this; @@ -200,13 +199,12 @@ public Span start() { distContext = contextShim == null ? null : contextShim.getCorrelationContext(); } else if (parentSpanContext != null) { builder.setParent( - TracingContextUtils.withSpan( - DefaultSpan.create(parentSpanContext.getSpanContext()), Context.ROOT)); + TracingContextUtils.withSpan(parentSpanContext.getPropagatedSpan(), Context.ROOT)); distContext = parentSpanContext.getCorrelationContext(); } - for (io.opentelemetry.trace.SpanContext link : parentLinks) { - builder.addLink(link); + for (io.opentelemetry.trace.Span span : parentLinkedSpans) { + builder.addLink(span); } if (spanKind != null) { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java index 32185cb63c5..67a9d9054f6 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java @@ -19,6 +19,8 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryMetadata; +import io.opentelemetry.trace.Span; +import io.opentelemetry.trace.TraceState; import io.opentracing.SpanContext; import java.util.Iterator; import java.util.Map; @@ -27,26 +29,49 @@ final class SpanContextShim extends BaseShimObject implements SpanContext { static final EntryMetadata DEFAULT_ENTRY_METADATA = EntryMetadata.create(EntryMetadata.EntryTtl.UNLIMITED_PROPAGATION); - private final io.opentelemetry.trace.SpanContext context; + private final String traceId; + private final String spanId; + private final byte traceFlags; + private final TraceState traceState; + private final CorrelationContext distContext; public SpanContextShim(SpanShim spanShim) { this( spanShim.telemetryInfo(), - spanShim.getSpan().getContext(), + spanShim.getSpan(), spanShim.telemetryInfo().emptyCorrelationContext()); } - public SpanContextShim(TelemetryInfo telemetryInfo, io.opentelemetry.trace.SpanContext context) { - this(telemetryInfo, context, telemetryInfo.emptyCorrelationContext()); + public SpanContextShim(TelemetryInfo telemetryInfo, io.opentelemetry.trace.Span span) { + this(telemetryInfo, span, telemetryInfo.emptyCorrelationContext()); } public SpanContextShim( TelemetryInfo telemetryInfo, - io.opentelemetry.trace.SpanContext context, + io.opentelemetry.trace.Span span, + CorrelationContext distContext) { + this( + telemetryInfo, + span.getTraceIdAsHexString(), + span.getSpanIdAsHexString(), + span.getTraceFlags(), + span.getTraceState(), + distContext); + } + + private SpanContextShim( + TelemetryInfo telemetryInfo, + String traceId, + String spanId, + byte traceFlags, + TraceState traceState, CorrelationContext distContext) { super(telemetryInfo); - this.context = context; + this.traceId = traceId; + this.spanId = spanId; + this.traceFlags = traceFlags; + this.traceState = traceState; this.distContext = distContext; } @@ -54,11 +79,12 @@ SpanContextShim newWithKeyValue(String key, String value) { CorrelationContext.Builder builder = contextManager().contextBuilder().setParent(distContext); builder.put(key, value, DEFAULT_ENTRY_METADATA); - return new SpanContextShim(telemetryInfo(), context, builder.build()); + return new SpanContextShim( + telemetryInfo(), traceId, spanId, traceFlags, traceState, builder.build()); } - io.opentelemetry.trace.SpanContext getSpanContext() { - return context; + io.opentelemetry.trace.Span getPropagatedSpan() { + return Span.getPropagated(traceId, spanId, traceFlags, traceState); } CorrelationContext getCorrelationContext() { @@ -67,12 +93,12 @@ CorrelationContext getCorrelationContext() { @Override public String toTraceId() { - return context.getTraceIdAsHexString(); + return traceId; } @Override public String toSpanId() { - return context.getSpanIdAsHexString().toString(); + return spanId; } @Override diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShimTable.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShimTable.java index 6bb736c7a2c..cfa92ce0cfb 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShimTable.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShimTable.java @@ -96,9 +96,7 @@ public SpanContextShim create(SpanShim spanShim, CorrelationContext distContext) return contextShim; } - contextShim = - new SpanContextShim( - spanShim.telemetryInfo(), spanShim.getSpan().getContext(), distContext); + contextShim = new SpanContextShim(spanShim.telemetryInfo(), spanShim.getSpan(), distContext); shimsMap.put(spanShim.getSpan(), contextShim); return contextShim; diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java index 81d1e2c7689..885ea11dda0 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java @@ -58,9 +58,14 @@ void context_simple() { SpanContextShim contextShim = (SpanContextShim) spanShim.context(); assertNotNull(contextShim); - assertEquals(contextShim.getSpanContext(), span.getContext()); - assertEquals(contextShim.toTraceId(), span.getContext().getTraceIdAsHexString().toString()); - assertEquals(contextShim.toSpanId(), span.getContext().getSpanIdAsHexString().toString()); + assertEquals( + contextShim.getPropagatedSpan().getTraceIdAsHexString(), span.getTraceIdAsHexString()); + assertEquals( + contextShim.getPropagatedSpan().getSpanIdAsHexString(), span.getSpanIdAsHexString()); + assertEquals(contextShim.getPropagatedSpan().getTraceFlags(), span.getTraceFlags()); + assertEquals(contextShim.getPropagatedSpan().getTraceState(), span.getTraceState()); + assertEquals(contextShim.toTraceId(), span.getTraceIdAsHexString()); + assertEquals(contextShim.toSpanId(), span.getSpanIdAsHexString()); assertFalse(contextShim.baggageItems().iterator().hasNext()); } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java index 92c8b20e123..6992d027765 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java @@ -16,13 +16,12 @@ package io.opentelemetry.opentracingshim; +import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.trace.DefaultSpan; import io.opentracing.Scope; import io.opentracing.Span; import io.opentracing.SpanContext; @@ -93,6 +92,6 @@ void close() { tracerShim.close(); Span otSpan = tracerShim.buildSpan(null).start(); io.opentelemetry.trace.Span span = ((SpanShim) otSpan).getSpan(); - assertTrue(span instanceof DefaultSpan); + assertFalse(span.isValid()); } } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java index 8d9d94df969..7a0e3b632c4 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java @@ -16,6 +16,7 @@ package io.opentelemetry.opentracingshim.testbed; +import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -24,7 +25,6 @@ import io.opentelemetry.opentracingshim.TraceShim; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentracing.Scope; import io.opentracing.Span; import io.opentracing.Tracer; @@ -53,7 +53,7 @@ void sdkContinuesOpenTracingTrace() { } finally { otSpan.finish(); } - assertEquals(tracer.getCurrentSpan().getClass(), DefaultSpan.class); + assertFalse(tracer.getCurrentSpan().isValid()); assertNull(otTracer.activeSpan()); List finishedSpans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); @@ -70,7 +70,7 @@ void openTracingContinuesSdkTrace() { otelSpan.end(); } - assertEquals(tracer.getCurrentSpan().getClass(), DefaultSpan.class); + assertFalse(tracer.getCurrentSpan().isValid()); assertNull(otTracer.activeSpan()); List finishedSpans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); 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 15f8a3512b0..ed07a3e5911 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 @@ -32,8 +32,11 @@ import io.opentelemetry.trace.Link; import io.opentelemetry.trace.Span; 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.TraceFlags; +import io.opentelemetry.trace.TraceId; +import io.opentelemetry.trace.TraceState; import java.util.Collection; import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.Benchmark; @@ -115,9 +118,25 @@ public CompletableResultCode shutdown() { } private static class TestLink implements Link { + + @Override + public String getTraceIdAsHexString() { + return TraceId.getInvalid(); + } + + @Override + public String getSpanIdAsHexString() { + return SpanId.getInvalid(); + } + + @Override + public TraceState getTraceState() { + return TraceState.getDefault(); + } + @Override - public SpanContext getContext() { - return SpanContext.getInvalid(); + public byte getTraceFlags() { + return TraceFlags.getDefault(); } @Override diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java index 940c73aa5b7..bbed68bc884 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java @@ -19,19 +19,21 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.TraceState; /** The extend Span interface used by the SDK. */ public interface ReadableSpan { - /** - * Returns the {@link SpanContext} of the {@code Span}. - * - *

Equivalent with {@link Span#getContext()}. - * - * @return the {@link SpanContext} of the {@code Span}. - */ - SpanContext getSpanContext(); + /** Returns the trace ID of this {@link Span}. */ + String getTraceIdAsHexString(); + + String getSpanIdAsHexString(); + + byte getTraceFlags(); + + TraceState getTraceState(); + + boolean isSampled(); /** * Returns the name of the {@code Span}. 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 c2debc664c9..f24e6908fde 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 @@ -37,8 +37,9 @@ import io.opentelemetry.sdk.trace.data.SpanData.Link; import io.opentelemetry.trace.EndSpanOptions; 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 io.opentelemetry.trace.Tracer; import io.opentelemetry.trace.attributes.SemanticAttributes; import java.io.PrintWriter; @@ -58,10 +59,13 @@ final class RecordEventsReadableSpan implements ReadWriteSpan { private static final Logger logger = Logger.getLogger(Tracer.class.getName()); + private final String traceIdHex; + private final String spanIdHex; + private final byte traceFlags; + private final TraceState traceState; + // The config used when constructing this Span. private final TraceConfig traceConfig; - // Contains the identifiers associated with this Span. - private final SpanContext context; // The parent SpanId of this span. Invalid if this is a root span. private final String parentSpanId; // True if the parent is on a different process. @@ -111,7 +115,10 @@ final class RecordEventsReadableSpan implements ReadWriteSpan { private boolean hasEnded; private RecordEventsReadableSpan( - SpanContext context, + String traceIdHex, + String spanIdHex, + byte traceFlags, + TraceState traceState, String name, InstrumentationLibraryInfo instrumentationLibraryInfo, Kind kind, @@ -125,7 +132,10 @@ private RecordEventsReadableSpan( List links, int totalRecordedLinks, long startEpochNanos) { - this.context = context; + this.traceIdHex = traceIdHex; + this.spanIdHex = spanIdHex; + this.traceFlags = traceFlags; + this.traceState = traceState; this.instrumentationLibraryInfo = instrumentationLibraryInfo; this.parentSpanId = parentSpanId; this.hasRemoteParent = hasRemoteParent; @@ -146,7 +156,6 @@ private RecordEventsReadableSpan( /** * Creates and starts a span with the given configuration. * - * @param context supplies the trace_id and span_id for the newly started span. * @param name the displayed name for the new span. * @param kind the span kind. * @param parentSpanId the span_id of the parent span, or {@code Span.INVALID} if the new span is @@ -162,7 +171,10 @@ private RecordEventsReadableSpan( * @return a new and started span. */ static RecordEventsReadableSpan startSpan( - SpanContext context, + String traceIdHex, + String spanIdHex, + byte traceFlags, + TraceState traceState, String name, InstrumentationLibraryInfo instrumentationLibraryInfo, Kind kind, @@ -178,7 +190,10 @@ static RecordEventsReadableSpan startSpan( long startEpochNanos) { RecordEventsReadableSpan span = new RecordEventsReadableSpan( - context, + traceIdHex, + spanIdHex, + traceFlags, + traceState, name, instrumentationLibraryInfo, kind, @@ -223,11 +238,6 @@ public boolean hasEnded() { } } - @Override - public SpanContext getSpanContext() { - return getContext(); - } - /** * Returns the name of the {@code Span}. * @@ -286,6 +296,41 @@ Clock getClock() { return clock; } + @Override + public String getTraceIdAsHexString() { + return traceIdHex; + } + + @Override + public String getSpanIdAsHexString() { + return spanIdHex; + } + + @Override + public boolean isSampled() { + return TraceFlags.isSampled(traceFlags); + } + + @Override + public byte getTraceFlags() { + return traceFlags; + } + + @Override + public TraceState getTraceState() { + return traceState; + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public boolean isRemote() { + return false; + } + @Override public void setAttribute(String key, String value) { setAttribute(stringKey(key), value); @@ -498,11 +543,6 @@ private void endInternal(long endEpochNanos) { spanProcessor.onEnd(this); } - @Override - public SpanContext getContext() { - return context; - } - @Override public boolean isRecording() { return true; @@ -550,7 +590,14 @@ private List getImmutableLinks() { if (!(link instanceof Link)) { // Make a copy because the given Link may not be immutable and we may reference a lot of // memory. - newLink = Link.create(link.getContext(), link.getAttributes()); + newLink = + Link.create( + link.getTraceIdAsHexString(), + link.getSpanIdAsHexString(), + link.getTraceFlags(), + link.getTraceState(), + link.getAttributes(), + link.getAttributes().size()); } else { newLink = (Link) link; } @@ -613,9 +660,9 @@ public String toString() { } StringBuilder sb = new StringBuilder(); sb.append("RecordEventsReadableSpan{traceId="); - sb.append(context.getTraceIdAsHexString()); + sb.append(traceIdHex); sb.append(", spanId="); - sb.append(context.getSpanIdAsHexString()); + sb.append(spanIdHex); sb.append(", parentSpanId="); sb.append(parentSpanId); sb.append(", name="); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java index b321151d2e4..f5f2d45874e 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java @@ -21,7 +21,6 @@ import io.opentelemetry.trace.Link; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.TraceId; import java.util.List; import javax.annotation.concurrent.ThreadSafe; @@ -32,8 +31,8 @@ public interface Sampler { /** * Called during {@link Span} creation to make a sampling samplingResult. * - * @param parentContext the parent span's {@link SpanContext}. This can be {@code - * SpanContext.INVALID} if this is a root span. + * @param parentSpan the parent {@link Span}. This can be {@code SpanContext.INVALID} if this is a + * root span. * @param traceId the {@link TraceId} for the new {@code Span}. This will be identical to that in * the parentContext, unless this is a root span. * @param name the name of the new {@code Span}. @@ -43,7 +42,7 @@ public interface Sampler { * @return sampling samplingResult whether span should be sampled or not. */ SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, @@ -68,7 +67,7 @@ enum Decision { } /** - * Sampling result returned by {@link Sampler#shouldSample(SpanContext, String, String, Kind, + * Sampling result returned by {@link Sampler#shouldSample(Span, String, String, Kind, * ReadableAttributes, List)}. */ interface SamplingResult { diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java index f526f16f362..b08294154dc 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java @@ -28,7 +28,6 @@ import io.opentelemetry.trace.Link; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.TraceId; import java.util.List; import java.util.Objects; @@ -180,7 +179,7 @@ private enum AlwaysOnSampler implements Sampler { // Returns a "yes" {@link SamplingResult} for {@link Span} sampling. @Override public SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, @@ -202,7 +201,7 @@ private enum AlwaysOffSampler implements Sampler { // Returns a "no" {@link SamplingResult}T on {@link Span} sampling. @Override public SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, @@ -244,29 +243,28 @@ private ParentBased( // Otherwise, uses the delegateSampler provided at initialization to make a decision. @Override public SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, ReadableAttributes attributes, List parentLinks) { - if (!parentContext.isValid()) { - return this.root.shouldSample( - parentContext, traceId, name, spanKind, attributes, parentLinks); + if (!parentSpan.isValid()) { + return this.root.shouldSample(parentSpan, traceId, name, spanKind, attributes, parentLinks); } - if (parentContext.isRemote()) { - return parentContext.isSampled() + if (parentSpan.isRemote()) { + return parentSpan.isSampled() ? this.remoteParentSampled.shouldSample( - parentContext, traceId, name, spanKind, attributes, parentLinks) + parentSpan, traceId, name, spanKind, attributes, parentLinks) : this.remoteParentNotSampled.shouldSample( - parentContext, traceId, name, spanKind, attributes, parentLinks); + parentSpan, traceId, name, spanKind, attributes, parentLinks); } - return parentContext.isSampled() + return parentSpan.isSampled() ? this.localParentSampled.shouldSample( - parentContext, traceId, name, spanKind, attributes, parentLinks) + parentSpan, traceId, name, spanKind, attributes, parentLinks) : this.localParentNotSampled.shouldSample( - parentContext, traceId, name, spanKind, attributes, parentLinks); + parentSpan, traceId, name, spanKind, attributes, parentLinks); } @Override @@ -458,7 +456,7 @@ static TraceIdRatioBased create(double ratio) { @Override public final SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 2515eebba68..78ff724dbf7 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -35,10 +35,8 @@ import io.opentelemetry.sdk.trace.Sampler.SamplingResult; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData.Link; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.TracingContextUtils; @@ -105,17 +103,17 @@ public Span.Builder setSpanKind(Kind spanKind) { } @Override - public Span.Builder addLink(SpanContext spanContext) { - addLink(Link.create(spanContext)); + public Span.Builder addLink(Span span) { + addLink(Link.create(span)); return this; } @Override - public Span.Builder addLink(SpanContext spanContext, Attributes attributes) { + public Span.Builder addLink(Span span, Attributes attributes) { int totalAttributeCount = attributes.size(); addLink( Link.create( - spanContext, + span, RecordEventsReadableSpan.copyAndLimitAttributes( attributes, traceConfig.getMaxNumberOfAttributesPerLink()), totalAttributeCount)); @@ -192,17 +190,16 @@ public Span startSpan() { final Context parentContext = isRootSpan ? Context.ROOT : parent == null ? Context.current() : parent; final Span parentSpan = TracingContextUtils.getSpan(parentContext); - final SpanContext parentSpanContext = parentSpan.getContext(); String traceId; String spanId = idsGenerator.generateSpanId(); TraceState traceState = TraceState.getDefault(); - if (!parentSpanContext.isValid()) { + if (!parentSpan.isValid()) { // New root span. traceId = idsGenerator.generateTraceId(); } else { // New child span. - traceId = parentSpanContext.getTraceIdAsHexString(); - traceState = parentSpanContext.getTraceState(); + traceId = parentSpan.getTraceIdAsHexString(); + traceState = parentSpan.getTraceState(); } List immutableLinks = links == null @@ -216,19 +213,11 @@ public Span startSpan() { traceConfig .getSampler() .shouldSample( - parentSpanContext, - traceId, - spanName, - spanKind, - immutableAttributes, - immutableLinks); + parentSpan, traceId, spanName, spanKind, immutableAttributes, immutableLinks); Sampler.Decision samplingDecision = samplingResult.getDecision(); - SpanContext spanContext = - createSpanContext(traceId, spanId, traceState, Samplers.isSampled(samplingDecision)); - if (!Samplers.isRecording(samplingDecision)) { - return DefaultSpan.create(spanContext); + return Span.getUnsampled(traceId, spanId, traceState); } ReadableAttributes samplingAttributes = samplingResult.getAttributes(); if (!samplingAttributes.isEmpty()) { @@ -250,12 +239,15 @@ public void consume(AttributeKey key, T value) { attributes = null; return RecordEventsReadableSpan.startSpan( - spanContext, + traceId, + spanId, + TraceFlags.getSampled(), + traceState, spanName, instrumentationLibraryInfo, spanKind, - parentSpanContext.getSpanIdAsHexString(), - parentSpanContext.isRemote(), + parentSpan.getSpanIdAsHexString(), + parentSpan.isRemote(), traceConfig, spanProcessor, getClock(parentSpan, clock), @@ -266,12 +258,6 @@ public void consume(AttributeKey key, T value) { startEpochNanos); } - private static SpanContext createSpanContext( - String traceId, String spanId, TraceState traceState, boolean isSampled) { - byte traceFlags = isSampled ? TraceFlags.getSampled() : TraceFlags.getDefault(); - return SpanContext.create(traceId, spanId, traceFlags, traceState); - } - private static Clock getClock(Span parent, Clock clock) { if (parent instanceof RecordEventsReadableSpan) { RecordEventsReadableSpan parentRecordEventsSpan = (RecordEventsReadableSpan) parent; 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 9a7740dcc4d..0aac9eac279 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 @@ -90,22 +90,22 @@ static SpanWrapper create( @Override public String getTraceId() { - return delegate().getContext().getTraceIdAsHexString(); + return delegate().getTraceIdAsHexString(); } @Override public String getSpanId() { - return delegate().getContext().getSpanIdAsHexString(); + return delegate().getSpanIdAsHexString(); } @Override public boolean isSampled() { - return delegate().getSpanContext().isSampled(); + return delegate().isSampled(); } @Override public TraceState getTraceState() { - return delegate().getSpanContext().getTraceState(); + return delegate().getTraceState(); } @Override 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 4794f41c9f8..629064f3196 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 @@ -22,8 +22,8 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.config.TraceConfig; +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.TraceState; import java.util.List; @@ -192,36 +192,61 @@ abstract class Link implements io.opentelemetry.trace.Link { /** * Returns a new immutable {@code Link}. * - * @param spanContext the {@code SpanContext} of this {@code Link}. + * @param span the {@link Span} this {@link Link} links to. * @return a new immutable {@code Event} */ - public static Link create(SpanContext spanContext) { - return new AutoValue_SpanData_Link( - spanContext, DEFAULT_ATTRIBUTE_COLLECTION, DEFAULT_ATTRIBUTE_COUNT); + public static Link create(Span span) { + return create(span, DEFAULT_ATTRIBUTE_COLLECTION, DEFAULT_ATTRIBUTE_COUNT); } /** * Returns a new immutable {@code Link}. * - * @param spanContext the {@code SpanContext} of this {@code Link}. + * @param span the {@link Span} this {@link Link} links to. * @param attributes the attributes of this {@code Link}. * @return a new immutable {@code Event} */ - public static Link create(SpanContext spanContext, Attributes attributes) { - return new AutoValue_SpanData_Link(spanContext, attributes, attributes.size()); + public static Link create(Span span, Attributes attributes) { + return create(span, attributes, attributes.size()); } /** * Returns a new immutable {@code Link}. * - * @param spanContext the {@code SpanContext} of this {@code Link}. + * @param span the {@link Span} this {@link Link} links to. + * @param attributes the attributes of this {@code Link}. + * @param totalAttributeCount the total number of attributed for this {@code Link}. + * @return a new immutable {@code Event} + */ + public static Link create(Span span, Attributes attributes, int totalAttributeCount) { + return create( + span.getTraceIdAsHexString(), + span.getSpanIdAsHexString(), + span.getTraceFlags(), + span.getTraceState(), + attributes, + totalAttributeCount); + } + + /** + * Returns a new immutable {@code Link}. + * + * @param traceId the trace ID of the {@link Span} this {@link Link} links to. + * @param spanId the span ID of the {@link Span} this {@link Link} links to. + * @param traceState the trace state of the {@link Span} this {@link Link} links to. * @param attributes the attributes of this {@code Link}. * @param totalAttributeCount the total number of attributed for this {@code Link}. * @return a new immutable {@code Event} */ public static Link create( - SpanContext spanContext, Attributes attributes, int totalAttributeCount) { - return new AutoValue_SpanData_Link(spanContext, attributes, totalAttributeCount); + String traceId, + String spanId, + byte traceFlags, + TraceState traceState, + Attributes attributes, + int totalAttributeCount) { + return new AutoValue_SpanData_Link( + traceId, spanId, traceState, traceFlags, attributes, totalAttributeCount); } /** diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java index 6631ba8a1ea..a5557ca5870 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java @@ -113,7 +113,7 @@ public boolean isStartRequired() { @Override public void onEnd(ReadableSpan span) { - if (sampled && !span.getSpanContext().isSampled()) { + if (sampled && !span.isSampled()) { return; } worker.addSpan(span); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java index e495aa46e7a..30006f053a2 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java @@ -78,7 +78,7 @@ public boolean isStartRequired() { @Override public void onEnd(ReadableSpan span) { - if (sampled && !span.getSpanContext().isSampled()) { + if (sampled && !span.isSampled()) { return; } try { 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 9a9a59234b5..fd3d8e0be14 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 @@ -39,8 +39,8 @@ import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; import io.opentelemetry.sdk.trace.data.SpanData.Link; +import io.opentelemetry.trace.Span; 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.TraceFlags; @@ -78,14 +78,14 @@ class RecordEventsReadableSpanTest { private final String traceId = idsGenerator.generateTraceId(); private final String spanId = idsGenerator.generateSpanId(); private final String parentSpanId = idsGenerator.generateSpanId(); - private final SpanContext spanContext = - SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault()); private final Resource resource = Resource.getEmpty(); private final InstrumentationLibraryInfo instrumentationLibraryInfo = InstrumentationLibraryInfo.create("theName", null); private final Map attributes = new HashMap<>(); private Attributes expectedAttributes; - private final io.opentelemetry.trace.Link link = Link.create(spanContext); + private final io.opentelemetry.trace.Link link = + Link.create( + traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault(), Attributes.empty(), 0); @Mock private SpanProcessor spanProcessor; private TestClock testClock; @@ -131,9 +131,25 @@ void lazyLinksAreResolved() { final Attributes attributes = Attributes.of(stringKey("attr"), "val"); io.opentelemetry.trace.Link link = new io.opentelemetry.trace.Link() { + + @Override + public String getTraceIdAsHexString() { + return traceId; + } + + @Override + public String getSpanIdAsHexString() { + return spanId; + } + + @Override + public TraceState getTraceState() { + return TraceState.getDefault(); + } + @Override - public SpanContext getContext() { - return spanContext; + public byte getTraceFlags() { + return TraceFlags.getDefault(); } @Override @@ -151,7 +167,8 @@ public Attributes getAttributes() { Link resultingLink = span.toSpanData().getLinks().get(0); assertThat(resultingLink.getTotalAttributeCount()).isEqualTo(1); - assertThat(resultingLink.getContext()).isSameAs(spanContext); + assertThat(resultingLink.getTraceIdAsHexString()).isEqualTo(traceId); + assertThat(resultingLink.getSpanIdAsHexString()).isEqualTo(spanId); assertThat(resultingLink.getAttributes()).isEqualTo(attributes); } @@ -222,7 +239,7 @@ void toSpanData_immutableLinks() { assertThrows( UnsupportedOperationException.class, - () -> spanData.getLinks().add(Link.create(SpanContext.getInvalid()))); + () -> spanData.getLinks().add(Link.create(Span.getInvalid()))); } @Test @@ -816,7 +833,10 @@ private RecordEventsReadableSpan createTestSpan( RecordEventsReadableSpan span = RecordEventsReadableSpan.startSpan( - spanContext, + traceId, + spanId, + TraceFlags.getDefault(), + TraceState.getDefault(), SPAN_NAME, instrumentationLibraryInfo, kind, @@ -899,13 +919,16 @@ void testAsSpanData() { attributes.forEach(attributesWithCapacity::put); Attributes event1Attributes = TestUtils.generateRandomAttributes(); Attributes event2Attributes = TestUtils.generateRandomAttributes(); - SpanContext context = - SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault()); - Link link1 = Link.create(context, TestUtils.generateRandomAttributes()); + Span span = + Span.getPropagated(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault()); + Link link1 = Link.create(span, TestUtils.generateRandomAttributes()); RecordEventsReadableSpan readableSpan = RecordEventsReadableSpan.startSpan( - context, + span.getTraceIdAsHexString(), + span.getSpanIdAsHexString(), + span.getTraceFlags(), + span.getTraceState(), name, instrumentationLibraryInfo, kind, diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java index c594dba9a3d..ee806ba4ed8 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java @@ -21,15 +21,17 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.grpc.Context; +import io.opentelemetry.OpenTelemetry; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.Sampler.Decision; import io.opentelemetry.sdk.trace.Sampler.SamplingResult; import io.opentelemetry.sdk.trace.data.SpanData.Link; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; +import io.opentelemetry.trace.TracingContextUtils; import java.util.Collections; import java.util.List; import org.junit.jupiter.api.Test; @@ -43,18 +45,28 @@ class SamplersTest { private final String traceId = idsGenerator.generateTraceId(); private final String parentSpanId = idsGenerator.generateSpanId(); private final TraceState traceState = TraceState.builder().build(); - private final SpanContext sampledSpanContext = - SpanContext.create(traceId, parentSpanId, TraceFlags.getSampled(), traceState); - private final SpanContext notSampledSpanContext = - SpanContext.create(traceId, parentSpanId, TraceFlags.getDefault(), traceState); - private final SpanContext invalidSpanContext = SpanContext.getInvalid(); - private final io.opentelemetry.trace.Link sampledParentLink = Link.create(sampledSpanContext); - private final SpanContext sampledRemoteSpanContext = - SpanContext.createFromRemoteParent( - traceId, parentSpanId, TraceFlags.getSampled(), traceState); - private final SpanContext notSampledRemoteSpanContext = - SpanContext.createFromRemoteParent( - traceId, parentSpanId, TraceFlags.getDefault(), traceState); + private final Span sampledSpan = + OpenTelemetry.getTracer("test") + .spanBuilder("test") + .setParent( + TracingContextUtils.withSpan( + Span.getPropagated(traceId, parentSpanId, TraceFlags.getSampled(), traceState), + Context.ROOT)) + .startSpan(); + private final Span unsampledSpan = + OpenTelemetry.getTracer("test") + .spanBuilder("test") + .setParent( + TracingContextUtils.withSpan( + Span.getPropagated(traceId, parentSpanId, TraceFlags.getDefault(), traceState), + Context.ROOT)) + .startSpan(); + private final Span invalidSpan = Span.getInvalid(); + private final io.opentelemetry.trace.Link sampledParentLink = Link.create(sampledSpan); + private final Span sampledRemoteSpan = + Span.getPropagated(traceId, parentSpanId, TraceFlags.getSampled(), traceState); + private final Span notSampledRemoteSpan = + Span.getPropagated(traceId, parentSpanId, TraceFlags.getDefault(), traceState); @Test void emptySamplingDecision() { @@ -104,7 +116,7 @@ void alwaysOnSampler() { assertThat( Samplers.alwaysOn() .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -117,7 +129,7 @@ void alwaysOnSampler() { assertThat( Samplers.alwaysOn() .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -151,7 +163,7 @@ void alwaysOffSampler() { assertThat( Samplers.alwaysOff() .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -164,7 +176,7 @@ void alwaysOffSampler() { assertThat( Samplers.alwaysOff() .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -198,7 +210,7 @@ void parentBasedSampler_AlwaysOn() { assertThat( Samplers.parentBased(Samplers.alwaysOn()) .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -211,7 +223,7 @@ void parentBasedSampler_AlwaysOn() { assertThat( Samplers.parentBased(Samplers.alwaysOn()) .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -227,7 +239,7 @@ void parentBasedSampler_AlwaysOff() { assertThat( Samplers.parentBased(Samplers.alwaysOff()) .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -240,7 +252,7 @@ void parentBasedSampler_AlwaysOff() { assertThat( Samplers.parentBased(Samplers.alwaysOff()) .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -257,7 +269,7 @@ void parentBasedSampler_NotSampled_Remote_Parent() { .setRemoteParentNotSampled(Samplers.alwaysOn()) .build() .shouldSample( - notSampledRemoteSpanContext, + notSampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -271,7 +283,7 @@ void parentBasedSampler_NotSampled_Remote_Parent() { .setRemoteParentNotSampled(Samplers.alwaysOff()) .build() .shouldSample( - notSampledRemoteSpanContext, + notSampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -285,7 +297,7 @@ void parentBasedSampler_NotSampled_Remote_Parent() { .setRemoteParentNotSampled(Samplers.alwaysOff()) .build() .shouldSample( - notSampledRemoteSpanContext, + notSampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -299,7 +311,7 @@ void parentBasedSampler_NotSampled_Remote_Parent() { .setRemoteParentNotSampled(Samplers.alwaysOn()) .build() .shouldSample( - notSampledRemoteSpanContext, + notSampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -317,7 +329,7 @@ void parentBasedSampler_NotSampled_NotRemote_Parent() { .setLocalParentNotSampled(Samplers.alwaysOn()) .build() .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -331,7 +343,7 @@ void parentBasedSampler_NotSampled_NotRemote_Parent() { .setLocalParentNotSampled(Samplers.alwaysOff()) .build() .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -345,7 +357,7 @@ void parentBasedSampler_NotSampled_NotRemote_Parent() { .setLocalParentNotSampled(Samplers.alwaysOff()) .build() .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -359,7 +371,7 @@ void parentBasedSampler_NotSampled_NotRemote_Parent() { .setLocalParentNotSampled(Samplers.alwaysOn()) .build() .shouldSample( - notSampledSpanContext, + unsampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -376,7 +388,7 @@ void parentBasedSampler_Sampled_Remote_Parent() { .setRemoteParentSampled(Samplers.alwaysOff()) .build() .shouldSample( - sampledRemoteSpanContext, + sampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -390,7 +402,7 @@ void parentBasedSampler_Sampled_Remote_Parent() { .setRemoteParentSampled(Samplers.alwaysOn()) .build() .shouldSample( - sampledRemoteSpanContext, + sampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -404,7 +416,7 @@ void parentBasedSampler_Sampled_Remote_Parent() { .setRemoteParentSampled(Samplers.alwaysOn()) .build() .shouldSample( - sampledRemoteSpanContext, + sampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -418,7 +430,7 @@ void parentBasedSampler_Sampled_Remote_Parent() { .setRemoteParentSampled(Samplers.alwaysOff()) .build() .shouldSample( - sampledRemoteSpanContext, + sampledRemoteSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -435,7 +447,7 @@ void parentBasedSampler_Sampled_NotRemote_Parent() { .setLocalParentSampled(Samplers.alwaysOn()) .build() .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -449,7 +461,7 @@ void parentBasedSampler_Sampled_NotRemote_Parent() { .setLocalParentSampled(Samplers.alwaysOff()) .build() .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -463,7 +475,7 @@ void parentBasedSampler_Sampled_NotRemote_Parent() { .setLocalParentSampled(Samplers.alwaysOff()) .build() .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -477,7 +489,7 @@ void parentBasedSampler_Sampled_NotRemote_Parent() { .setLocalParentSampled(Samplers.alwaysOn()) .build() .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -492,7 +504,7 @@ void parentBasedSampler_invalid_Parent() { assertThat( Samplers.parentBased(Samplers.alwaysOff()) .shouldSample( - invalidSpanContext, + invalidSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -504,7 +516,7 @@ void parentBasedSampler_invalid_Parent() { assertThat( Samplers.parentBased(Samplers.alwaysOff()) .shouldSample( - invalidSpanContext, + invalidSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -521,7 +533,7 @@ void parentBasedSampler_invalid_Parent() { .setLocalParentNotSampled(Samplers.alwaysOn()) .build() .shouldSample( - invalidSpanContext, + invalidSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -533,7 +545,7 @@ void parentBasedSampler_invalid_Parent() { assertThat( Samplers.parentBased(Samplers.alwaysOn()) .shouldSample( - invalidSpanContext, + invalidSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -583,7 +595,7 @@ void traceIdRatioBased_getDescription() { // Applies the given sampler to NUM_SAMPLE_TRIES random traceId. private void assertSamplerSamplesWithProbability( Sampler sampler, - SpanContext parent, + Span parent, List parentLinks, double probability) { int count = 0; // Count of spans with sampling enabled @@ -625,7 +637,7 @@ void traceIdRatioBased_DifferentProbabilities_NotSampledParent() { private void assertTraceIdRatioBased_NotSampledParent(Sampler sampler, double probability) { assertSamplerSamplesWithProbability( - sampler, notSampledSpanContext, Collections.emptyList(), probability); + sampler, unsampledSpan, Collections.emptyList(), probability); } @Test @@ -642,8 +654,7 @@ void traceIdRatioBased_DifferentProbabilities_SampledParent() { } private void assertTraceIdRatioBased_SampledParent(Sampler sampler, double probability) { - assertSamplerSamplesWithProbability( - sampler, sampledSpanContext, Collections.emptyList(), probability); + assertSamplerSamplesWithProbability(sampler, sampledSpan, Collections.emptyList(), probability); } @Test @@ -679,13 +690,13 @@ void traceIdRatioBased_DifferentProbabilities_SampledParentLink() { private void assertTraceIdRatioBased_SampledParentLink(Sampler sampler, double probability) { assertSamplerSamplesWithProbability( - sampler, notSampledSpanContext, Collections.singletonList(sampledParentLink), probability); + sampler, unsampledSpan, Collections.singletonList(sampledParentLink), probability); } private void assertTraceIdRatioBased_SampledParentLinkContext( Sampler sampler, double probability) { assertSamplerSamplesWithProbability( - sampler, sampledSpanContext, Collections.singletonList(sampledParentLink), probability); + sampler, sampledSpan, Collections.singletonList(sampledParentLink), probability); } @Test @@ -715,7 +726,7 @@ void traceIdRatioBased_SampleBasedOnTraceId() { }); SamplingResult samplingResult1 = defaultProbability.shouldSample( - invalidSpanContext, + invalidSpan, notSampledTraceId, SPAN_NAME, SPAN_KIND, @@ -748,7 +759,7 @@ void traceIdRatioBased_SampleBasedOnTraceId() { }); SamplingResult samplingResult2 = defaultProbability.shouldSample( - invalidSpanContext, + invalidSpan, sampledTraceId, SPAN_NAME, SPAN_KIND, diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index b31e31b65e2..aec7f93ed8f 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -37,10 +37,8 @@ import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Link; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -55,8 +53,8 @@ /** Unit tests for {@link SpanBuilderSdk}. */ class SpanBuilderSdkTest { private static final String SPAN_NAME = "span_name"; - private final SpanContext sampledSpanContext = - SpanContext.create( + private final Span sampledSpan = + Span.getPropagated( TraceId.fromLongs(1000, 1000), SpanId.fromLong(3000), TraceFlags.getSampled(), @@ -81,9 +79,9 @@ void setParent_null() { void addLink() { // Verify methods do not crash. Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); - spanBuilder.addLink(Link.create(DefaultSpan.getInvalid().getContext())); - spanBuilder.addLink(DefaultSpan.getInvalid().getContext()); - spanBuilder.addLink(DefaultSpan.getInvalid().getContext(), Attributes.empty()); + spanBuilder.addLink(Link.create(Span.getInvalid())); + spanBuilder.addLink(Span.getInvalid()); + spanBuilder.addLink(Span.getInvalid(), Attributes.empty()); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { @@ -106,7 +104,7 @@ void truncateLink() { // Verify methods do not crash. Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); for (int i = 0; i < 2 * maxNumberOfLinks; i++) { - spanBuilder.addLink(sampledSpanContext); + spanBuilder.addLink(sampledSpan); } RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { @@ -114,7 +112,7 @@ void truncateLink() { List links = spanData.getLinks(); assertThat(links).hasSize(maxNumberOfLinks); for (int i = 0; i < maxNumberOfLinks; i++) { - assertThat(links.get(i)).isEqualTo(Link.create(sampledSpanContext)); + assertThat(links.get(i)).isEqualTo(Link.create(sampledSpan)); assertThat(spanData.getTotalRecordedLinks()).isEqualTo(2 * maxNumberOfLinks); } } finally { @@ -138,12 +136,11 @@ void truncateLinkAttributes() { stringKey("key0"), "str", stringKey("key1"), "str", stringKey("key2"), "str"); - spanBuilder.addLink(sampledSpanContext, attributes); + spanBuilder.addLink(sampledSpan, attributes); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { assertThat(span.toSpanData().getLinks()) - .containsExactly( - Link.create(sampledSpanContext, Attributes.of(stringKey("key0"), "str"), 3)); + .containsExactly(Link.create(sampledSpan, Attributes.of(stringKey("key0"), "str"), 3)); } finally { span.end(); tracerSdkFactory.updateActiveTraceConfig(TraceConfig.getDefault()); @@ -153,21 +150,21 @@ void truncateLinkAttributes() { @Test void addLink_NoEffectAfterStartSpan() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); - spanBuilder.addLink(sampledSpanContext); + spanBuilder.addLink(sampledSpan); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { assertThat(span.toSpanData().getLinks()) - .containsExactly(Link.create(sampledSpanContext, Attributes.empty())); - // Use a different sampledSpanContext to ensure no logic that avoids duplicate links makes + .containsExactly(Link.create(sampledSpan, Attributes.empty())); + // Use a different sampledSpan to ensure no logic that avoids duplicate links makes // this test to pass. spanBuilder.addLink( - SpanContext.create( + Span.getPropagated( TraceId.fromLongs(2000, 2000), SpanId.fromLong(4000), TraceFlags.getSampled(), TraceState.getDefault())); assertThat(span.toSpanData().getLinks()) - .containsExactly(Link.create(sampledSpanContext, Attributes.empty())); + .containsExactly(Link.create(sampledSpan, Attributes.empty())); } finally { span.end(); } @@ -181,25 +178,23 @@ void addLink_null() { } @Test - void addLinkSpanContext_null() { + void addLinkSpan_null() { assertThrows( - NullPointerException.class, - () -> tracerSdk.spanBuilder(SPAN_NAME).addLink((SpanContext) null)); + NullPointerException.class, () -> tracerSdk.spanBuilder(SPAN_NAME).addLink((Span) null)); } @Test - void addLinkSpanContextAttributes_nullContext() { + void addLinkSpanAttributes_nullContext() { assertThrows( NullPointerException.class, () -> tracerSdk.spanBuilder(SPAN_NAME).addLink(null, Attributes.empty())); } @Test - void addLinkSpanContextAttributes_nullAttributes() { + void addLinkSpanAttributes_nullAttributes() { assertThrows( NullPointerException.class, - () -> - tracerSdk.spanBuilder(SPAN_NAME).addLink(DefaultSpan.getInvalid().getContext(), null)); + () -> tracerSdk.spanBuilder(SPAN_NAME).addLink(Span.getInvalid(), null)); } @Test @@ -511,7 +506,7 @@ void sampler() { TestUtils.startSpanWithSampler(tracerSdkFactory, tracerSdk, SPAN_NAME, Samplers.alwaysOff()) .startSpan(); try { - assertThat(span.getContext().isSampled()).isFalse(); + assertThat(span.isSampled()).isFalse(); } finally { span.end(); } @@ -530,7 +525,7 @@ void sampler_decisionAttributes() { new Sampler() { @Override public SamplingResult shouldSample( - @Nullable SpanContext parentContext, + @Nullable Span parentSpan, String traceId, String name, Kind spanKind, @@ -557,7 +552,7 @@ public String getDescription() { Collections.singletonMap(samplerAttributeKey.getKey(), "none")) .startSpan(); try { - assertThat(span.getContext().isSampled()).isTrue(); + assertThat(span.isSampled()).isTrue(); assertThat(span.toSpanData().getAttributes().get(samplerAttributeKey)).isNotNull(); } finally { span.end(); @@ -569,10 +564,10 @@ void sampledViaParentLinks() { Span span = TestUtils.startSpanWithSampler( tracerSdkFactory, tracerSdk, SPAN_NAME, Samplers.traceIdRatioBased(0.0)) - .addLink(sampledSpanContext) + .addLink(sampledSpan) .startSpan(); try { - assertThat(span.getContext().isSampled()).isFalse(); + assertThat(span.isSampled()).isFalse(); } finally { if (span != null) { span.end(); @@ -586,8 +581,7 @@ void noParent() { try (Scope ignored = tracerSdk.withSpan(parent)) { Span span = tracerSdk.spanBuilder(SPAN_NAME).setNoParent().startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isNotEqualTo(parent.getContext().getTraceIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isNotEqualTo(parent.getTraceIdAsHexString()); Span spanNoParent = tracerSdk @@ -597,8 +591,7 @@ void noParent() { .setNoParent() .startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isNotEqualTo(parent.getContext().getTraceIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isNotEqualTo(parent.getTraceIdAsHexString()); } finally { spanNoParent.end(); } @@ -622,10 +615,8 @@ void noParent_override() { .setParent(TracingContextUtils.withSpan(parent, Context.current())) .startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isEqualTo(parent.getContext().getTraceIdAsHexString()); - assertThat(span.toSpanData().getParentSpanId()) - .isEqualTo(parent.getContext().getSpanIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isEqualTo(parent.getTraceIdAsHexString()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getSpanIdAsHexString()); RecordEventsReadableSpan span2 = (RecordEventsReadableSpan) @@ -635,8 +626,7 @@ void noParent_override() { .setParent(TracingContextUtils.withSpan(parent, Context.current())) .startSpan(); try { - assertThat(span2.getContext().getTraceIdAsHexString()) - .isEqualTo(parent.getContext().getTraceIdAsHexString()); + assertThat(span2.getTraceIdAsHexString()).isEqualTo(parent.getTraceIdAsHexString()); } finally { span2.end(); } @@ -661,10 +651,8 @@ void overrideNoParent_remoteParent() { .setParent(TracingContextUtils.withSpan(parent, Context.current())) .startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isEqualTo(parent.getContext().getTraceIdAsHexString()); - assertThat(span.toSpanData().getParentSpanId()) - .isEqualTo(parent.getContext().getSpanIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isEqualTo(parent.getTraceIdAsHexString()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getSpanIdAsHexString()); } finally { span.end(); } @@ -682,10 +670,8 @@ void parent_fromContext() { (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isEqualTo(parent.getContext().getTraceIdAsHexString()); - assertThat(span.toSpanData().getParentSpanId()) - .isEqualTo(parent.getContext().getSpanIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isEqualTo(parent.getTraceIdAsHexString()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getSpanIdAsHexString()); } finally { span.end(); } @@ -707,10 +693,8 @@ void parent_fromEmptyContext() { } try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isNotEqualTo(parent.getContext().getTraceIdAsHexString()); - assertThat(span.toSpanData().getParentSpanId()) - .isNotEqualTo(parent.getContext().getSpanIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isNotEqualTo(parent.getTraceIdAsHexString()); + assertThat(span.toSpanData().getParentSpanId()).isNotEqualTo(parent.getSpanIdAsHexString()); } finally { span.end(); } @@ -726,10 +710,8 @@ void parentCurrentSpan() { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isEqualTo(parent.getContext().getTraceIdAsHexString()); - assertThat(span.toSpanData().getParentSpanId()) - .isEqualTo(parent.getContext().getSpanIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isEqualTo(parent.getTraceIdAsHexString()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getSpanIdAsHexString()); } finally { span.end(); } @@ -740,7 +722,7 @@ void parentCurrentSpan() { @Test void parent_invalidContext() { - Span parent = DefaultSpan.getInvalid(); + Span parent = Span.getInvalid(); RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -749,8 +731,7 @@ void parent_invalidContext() { .setParent(TracingContextUtils.withSpan(parent, Context.current())) .startSpan(); try { - assertThat(span.getContext().getTraceIdAsHexString()) - .isNotEqualTo(parent.getContext().getTraceIdAsHexString()); + assertThat(span.getTraceIdAsHexString()).isNotEqualTo(parent.getTraceIdAsHexString()); assertFalse(SpanId.isValid(span.toSpanData().getParentSpanId())); } finally { span.end(); diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java index b165dce4d57..fa3388a6020 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java @@ -26,7 +26,6 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.config.TraceConfig; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -141,7 +140,7 @@ void shutdownTwice_OnlyFlushSpanProcessorOnce() { void returnNoopSpanAfterShutdown() { tracerFactory.shutdown(); Span span = tracerFactory.get("noop").spanBuilder("span").startSpan(); - assertThat(span).isInstanceOf(DefaultSpan.class); + assertThat(span.isValid()).isFalse(); span.end(); } } diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index 3a3ec854b7b..9ce311d4e08 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -26,7 +26,6 @@ import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; import io.opentelemetry.sdk.trace.export.SpanExporter; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.TracingContextUtils; import java.util.Collection; @@ -62,7 +61,7 @@ void setUp() { @Test void defaultGetCurrentSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); } @Test @@ -72,7 +71,7 @@ void defaultSpanBuilder() { @Test void getCurrentSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); Context origContext = TracingContextUtils.withSpan(span, Context.current()).attach(); // Make sure context is detached even if test fails. try { @@ -80,25 +79,25 @@ void getCurrentSpan() { } finally { Context.current().detach(origContext); } - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); } @Test void withSpan_NullSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); try (Scope ignored = tracer.withSpan(null)) { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); } - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); } @Test void getCurrentSpan_WithSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); try (Scope ignored = tracer.withSpan(span)) { assertThat(tracer.getCurrentSpan()).isSameAs(span); } - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(tracer.getCurrentSpan().isValid()).isFalse(); } @Test diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessorTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessorTest.java index fe5d8175b7c..9f9ec54cf9a 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessorTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessorTest.java @@ -32,9 +32,7 @@ import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.export.BatchSpanProcessorTest.WaitingSpanExporter; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.Tracer; @@ -62,19 +60,15 @@ class SimpleSpanProcessorTest { @Mock private SpanExporter spanExporter; private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build(); private final Tracer tracer = tracerSdkFactory.get("SimpleSpanProcessor"); - private static final SpanContext SAMPLED_SPAN_CONTEXT = - SpanContext.create( - TraceId.getInvalid(), - SpanId.getInvalid(), - TraceFlags.getSampled(), - TraceState.builder().build()); - private static final SpanContext NOT_SAMPLED_SPAN_CONTEXT = SpanContext.getInvalid(); private SimpleSpanProcessor simpleSampledSpansProcessor; @BeforeEach void setUp() { simpleSampledSpansProcessor = SimpleSpanProcessor.newBuilder(spanExporter).build(); + when(readableSpan.getTraceIdAsHexString()).thenReturn(TraceId.getInvalid()); + when(readableSpan.getSpanIdAsHexString()).thenReturn(SpanId.getInvalid()); + when(readableSpan.getTraceState()).thenReturn(TraceState.getDefault()); } @Test @@ -105,7 +99,7 @@ void onStartSync() { @Test void onEndSync_SampledSpan() { SpanData spanData = TestUtils.makeBasicSpan(); - when(readableSpan.getSpanContext()).thenReturn(SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(true); when(readableSpan.toSpanData()).thenReturn(spanData); simpleSampledSpansProcessor.onEnd(readableSpan); verify(spanExporter).export(Collections.singletonList(spanData)); @@ -113,14 +107,14 @@ void onEndSync_SampledSpan() { @Test void onEndSync_NotSampledSpan() { - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(false); simpleSampledSpansProcessor.onEnd(readableSpan); verifyNoInteractions(spanExporter); } @Test void onEndSync_OnlySampled_NotSampledSpan() { - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(false); when(readableSpan.toSpanData()) .thenReturn(TestUtils.makeBasicSpan()) .thenThrow(new RuntimeException()); @@ -131,7 +125,7 @@ void onEndSync_OnlySampled_NotSampledSpan() { @Test void onEndSync_OnlySampled_SampledSpan() { - when(readableSpan.getSpanContext()).thenReturn(SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(true); when(readableSpan.toSpanData()) .thenReturn(TestUtils.makeBasicSpan()) .thenThrow(new RuntimeException()); @@ -195,7 +189,7 @@ void tracerSdk_NotSampled_RecordingEventsSpan() { @Test void onEndSync_ExporterReturnError() { SpanData spanData = TestUtils.makeBasicSpan(); - when(readableSpan.getSpanContext()).thenReturn(SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(true); when(readableSpan.toSpanData()).thenReturn(spanData); simpleSampledSpansProcessor.onEnd(readableSpan); // Try again, now will no longer return error. @@ -215,7 +209,7 @@ void buildFromProperties_defaultSampledFlag() { SimpleSpanProcessor spanProcessor = SimpleSpanProcessor.newBuilder(spanExporter).readProperties(properties).build(); - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(false); spanProcessor.onEnd(readableSpan); verifyNoInteractions(spanExporter); } @@ -227,7 +221,7 @@ void buildFromProperties_onlySampledTrue() { SimpleSpanProcessor spanProcessor = SimpleSpanProcessor.newBuilder(spanExporter).readProperties(properties).build(); - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(false); spanProcessor.onEnd(readableSpan); verifyNoInteractions(spanExporter); } @@ -240,7 +234,7 @@ void buildFromProperties_onlySampledFalse() { SimpleSpanProcessor.newBuilder(spanExporter).readProperties(properties).build(); SpanData spanData = TestUtils.makeBasicSpan(); - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(false); when(readableSpan.toSpanData()).thenReturn(spanData); spanProcessor.onEnd(readableSpan); diff --git a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/JaegerRemoteSampler.java b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/JaegerRemoteSampler.java index 7a81e778842..6ce7338d055 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/JaegerRemoteSampler.java +++ b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/JaegerRemoteSampler.java @@ -28,8 +28,8 @@ import io.opentelemetry.sdk.trace.Sampler; import io.opentelemetry.sdk.trace.Samplers; import io.opentelemetry.trace.Link; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -77,13 +77,13 @@ public void run() { @Override public SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, ReadableAttributes attributes, List parentLinks) { - return sampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); + return sampler.shouldSample(parentSpan, traceId, name, spanKind, attributes, parentLinks); } private void getAndUpdateSampler() { diff --git a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/PerOperationSampler.java b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/PerOperationSampler.java index 32e43c39484..282a97eddfa 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/PerOperationSampler.java +++ b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/PerOperationSampler.java @@ -21,8 +21,8 @@ import io.opentelemetry.sdk.trace.Sampler; import io.opentelemetry.sdk.trace.Samplers; import io.opentelemetry.trace.Link; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -47,7 +47,7 @@ class PerOperationSampler implements Sampler { @Override public SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, @@ -57,7 +57,7 @@ public SamplingResult shouldSample( if (sampler == null) { sampler = this.defaultSampler; } - return sampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); + return sampler.shouldSample(parentSpan, traceId, name, spanKind, attributes, parentLinks); } @Override diff --git a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java index 7edc1f847e1..4793e83d7a4 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java +++ b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java @@ -25,8 +25,9 @@ import io.opentelemetry.sdk.trace.Sampler; import io.opentelemetry.sdk.trace.Samplers; import io.opentelemetry.trace.Link; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.TraceFlags; import java.util.List; /** @@ -60,21 +61,21 @@ class RateLimitingSampler implements Sampler { @Override public SamplingResult shouldSample( - SpanContext parentContext, + Span parentSpan, String traceId, String name, Kind spanKind, ReadableAttributes attributes, List parentLinks) { - if (parentContext.isSampled()) { + if (parentSpan.isSampled()) { return Samplers.alwaysOn() - .shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); + .shouldSample(parentSpan, traceId, name, spanKind, attributes, parentLinks); } if (parentLinks != null) { for (Link parentLink : parentLinks) { - if (parentLink.getContext().isSampled()) { + if (TraceFlags.isSampled(parentLink.getTraceFlags())) { return Samplers.alwaysOn() - .shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); + .shouldSample(parentSpan, traceId, name, spanKind, attributes, parentLinks); } } } diff --git a/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java b/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java index 6134ab186d8..a6cadd2c36c 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java +++ b/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java @@ -23,7 +23,6 @@ import io.opentelemetry.sdk.trace.Sampler.Decision; import io.opentelemetry.sdk.trace.Sampler.SamplingResult; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; @@ -37,10 +36,9 @@ class RateLimitingSamplerTest { private final String traceId = TraceId.fromLongs(150, 150); private final String parentSpanId = SpanId.fromLong(250); private final TraceState traceState = TraceState.builder().build(); - private final SpanContext sampledSpanContext = - SpanContext.create(traceId, parentSpanId, TraceFlags.getSampled(), traceState); - private final SpanContext notSampledSpanContext = - SpanContext.create(traceId, parentSpanId, TraceFlags.getDefault(), traceState); + private final Span sampledSpan = + Span.getPropagated(traceId, parentSpanId, TraceFlags.getSampled(), traceState); + private final Span notSampledSpan = Span.getUnsampled(traceId, parentSpanId, traceState); @Test void alwaysSampleSampledContext() { @@ -48,7 +46,7 @@ void alwaysSampleSampledContext() { assertThat( sampler .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -59,7 +57,7 @@ void alwaysSampleSampledContext() { assertThat( sampler .shouldSample( - sampledSpanContext, + sampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -74,7 +72,7 @@ void sampleOneTrace() { RateLimitingSampler sampler = new RateLimitingSampler(1); SamplingResult samplingResult = sampler.shouldSample( - notSampledSpanContext, + notSampledSpan, traceId, SPAN_NAME, SPAN_KIND, @@ -84,7 +82,7 @@ void sampleOneTrace() { assertThat( sampler .shouldSample( - notSampledSpanContext, + notSampledSpan, traceId, SPAN_NAME, SPAN_KIND, diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java index 41d80d70161..cd915d0ac60 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java @@ -26,7 +26,6 @@ import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.Tracer; @@ -72,7 +71,7 @@ void test() { assertThat(spans.get(0).getTraceId()).isNotEqualTo(spans.get(1).getTraceId()); assertThat(spans.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } private void submitAnotherTask(final Span initialSpan) { diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/actorpropagation/ActorPropagationTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/actorpropagation/ActorPropagationTest.java index a005b0222ca..74e0ceeb105 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/actorpropagation/ActorPropagationTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/actorpropagation/ActorPropagationTest.java @@ -23,7 +23,6 @@ import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; @@ -84,7 +83,7 @@ void testActorTell() { assertThat(TestUtils.getByKind(finished, Span.Kind.CONSUMER)).hasSize(2); assertThat(TestUtils.getOneByKind(finished, Span.Kind.PRODUCER)).isNotNull(); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } } @@ -125,7 +124,7 @@ void testActorAsk() throws ExecutionException, InterruptedException { assertThat(TestUtils.getByKind(finished, Span.Kind.CONSUMER)).hasSize(2); assertThat(TestUtils.getOneByKind(finished, Span.Kind.PRODUCER)).isNotNull(); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/clientserver/TestClientServerTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/clientserver/TestClientServerTest.java index 0cc8a36dcb9..ba257778610 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/clientserver/TestClientServerTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/clientserver/TestClientServerTest.java @@ -25,7 +25,7 @@ import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; import java.util.List; @@ -73,6 +73,6 @@ void test() throws Exception { assertThat(finished.get(0).getKind()).isEqualTo(Kind.CLIENT); assertThat(finished.get(1).getKind()).isEqualTo(Kind.SERVER); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java index 81edb9bf4fa..1102cf051a8 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java @@ -24,7 +24,6 @@ import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.Tracer; @@ -72,7 +71,7 @@ void two_requests() throws Exception { assertThat(finished.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); assertThat(finished.get(1).getParentSpanId()).isEqualTo(SpanId.getInvalid()); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } /** Active parent is not picked up by child. */ 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 cfd743f52a0..70445394983 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 @@ -27,7 +27,6 @@ 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.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; import io.opentelemetry.trace.Tracer; @@ -58,7 +57,7 @@ void testSimpleError() { span.end(); } - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); @@ -111,7 +110,7 @@ void testErrorRecovery() { span.setStatus(Status.UNKNOWN); // Could not fetch anything. span.end(); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/latespanfinish/LateSpanFinishTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/latespanfinish/LateSpanFinishTest.java index 67fb8bb830d..735f7e60fd2 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/latespanfinish/LateSpanFinishTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/latespanfinish/LateSpanFinishTest.java @@ -23,7 +23,6 @@ import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.List; @@ -62,7 +61,7 @@ void test() throws Exception { TestUtils.assertSameTrace(spans); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } /* diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/listenerperrequest/ListenerTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/listenerperrequest/ListenerTest.java index 342e6218fe5..a0378f3218c 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/listenerperrequest/ListenerTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/listenerperrequest/ListenerTest.java @@ -21,7 +21,7 @@ import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; import java.util.List; @@ -44,6 +44,6 @@ void test() throws Exception { assertThat(finished).hasSize(1); assertThat(finished.get(0).getKind()).isEqualTo(Kind.CLIENT); - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java index d0081c55120..8f4fda484c7 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java @@ -25,7 +25,6 @@ import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.List; @@ -75,6 +74,6 @@ void test() { assertThat(spans.get(i).getParentSpanId()).isEqualTo(parentSpan.getSpanId()); } - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index 6dedd06a7cd..904b8dfc224 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -27,7 +27,6 @@ import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.List; @@ -65,7 +64,7 @@ void test() { assertThat(attrs.get(stringKey("key" + i))).isEqualTo(Integer.toString(i)); } - assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); + assertThat(tracer.getCurrentSpan()).isSameAs(Span.getInvalid()); } private void submitCallbacks(final Span span) { diff --git a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessor.java b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessor.java index a1c33c8c689..9d23aa7c206 100644 --- a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessor.java +++ b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezSpanProcessor.java @@ -66,7 +66,7 @@ final class TracezSpanProcessor implements SpanProcessor { @Override public void onStart(ReadWriteSpan span) { - runningSpanCache.put(span.getSpanContext().getSpanIdAsHexString(), span); + runningSpanCache.put(span.getSpanIdAsHexString(), span); } @Override @@ -76,8 +76,8 @@ public boolean isStartRequired() { @Override public void onEnd(ReadableSpan span) { - runningSpanCache.remove(span.getSpanContext().getSpanIdAsHexString()); - if (!sampled || span.getSpanContext().isSampled()) { + runningSpanCache.remove(span.getSpanIdAsHexString()); + if (!sampled || span.isSampled()) { completedSpanCache.putIfAbsent(span.getName(), new TracezSpanBuckets()); completedSpanCache.get(span.getName()).addToBucket(span); } 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 e7c4040cd4e..483b2c031e9 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 @@ -22,7 +22,6 @@ import io.opentelemetry.sdk.trace.ReadWriteSpan; import io.opentelemetry.sdk.trace.ReadableSpan; 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; @@ -30,6 +29,7 @@ import io.opentelemetry.trace.TraceState; import java.util.Collection; import java.util.Properties; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -42,13 +42,6 @@ @MockitoSettings(strictness = Strictness.LENIENT) class TracezSpanProcessorTest { private static final String SPAN_NAME = "span"; - private static final SpanContext SAMPLED_SPAN_CONTEXT = - SpanContext.create( - TraceId.getInvalid(), - SpanId.getInvalid(), - TraceFlags.getSampled(), - TraceState.builder().build()); - private static final SpanContext NOT_SAMPLED_SPAN_CONTEXT = SpanContext.getInvalid(); private static final Status SPAN_STATUS = Status.UNKNOWN; private static void assertSpanCacheSizes( @@ -63,11 +56,21 @@ private static void assertSpanCacheSizes( @Mock private ReadWriteSpan readWriteSpan; @Mock private SpanData spanData; + @BeforeEach + void setUp() { + when(readableSpan.getTraceIdAsHexString()).thenReturn(TraceId.getInvalid()); + when(readableSpan.getSpanIdAsHexString()).thenReturn(SpanId.getInvalid()); + when(readableSpan.getTraceState()).thenReturn(TraceState.getDefault()); + when(readWriteSpan.getTraceIdAsHexString()).thenReturn(TraceId.getInvalid()); + when(readWriteSpan.getSpanIdAsHexString()).thenReturn(SpanId.getInvalid()); + when(readWriteSpan.getTraceState()).thenReturn(TraceState.getDefault()); + } + @Test void onStart_sampledSpan_inCache() { TracezSpanProcessor spanProcessor = TracezSpanProcessor.newBuilder().build(); /* Return a sampled span, which should be added to the running cache by default */ - when(readWriteSpan.getSpanContext()).thenReturn(SAMPLED_SPAN_CONTEXT); + when(readWriteSpan.getTraceFlags()).thenReturn(TraceFlags.getSampled()); spanProcessor.onStart(readWriteSpan); assertSpanCacheSizes(spanProcessor, 1, 0); } @@ -76,11 +79,11 @@ void onStart_sampledSpan_inCache() { void onEnd_sampledSpan_inCache() { TracezSpanProcessor spanProcessor = TracezSpanProcessor.newBuilder().build(); /* Return a sampled span, which should be added to the completed cache upon ending */ - when(readWriteSpan.getSpanContext()).thenReturn(SAMPLED_SPAN_CONTEXT); + when(readWriteSpan.getTraceFlags()).thenReturn(TraceFlags.getSampled()); when(readWriteSpan.getName()).thenReturn(SPAN_NAME); spanProcessor.onStart(readWriteSpan); - when(readableSpan.getSpanContext()).thenReturn(SAMPLED_SPAN_CONTEXT); + when(readableSpan.isSampled()).thenReturn(true); when(readableSpan.getName()).thenReturn(SPAN_NAME); when(readableSpan.toSpanData()).thenReturn(spanData); when(spanData.getStatus()).thenReturn(SPAN_STATUS); @@ -92,7 +95,7 @@ void onEnd_sampledSpan_inCache() { void onStart_notSampledSpan_inCache() { TracezSpanProcessor spanProcessor = TracezSpanProcessor.newBuilder().build(); /* Return a non-sampled span, which should not be added to the running cache by default */ - when(readWriteSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readWriteSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); spanProcessor.onStart(readWriteSpan); assertSpanCacheSizes(spanProcessor, 1, 0); } @@ -101,8 +104,8 @@ void onStart_notSampledSpan_inCache() { void onEnd_notSampledSpan_notInCache() { TracezSpanProcessor spanProcessor = TracezSpanProcessor.newBuilder().build(); /* Return a non-sampled span, which should not be added to the running cache by default */ - when(readWriteSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readWriteSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); + when(readableSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); spanProcessor.onStart(readWriteSpan); spanProcessor.onEnd(readableSpan); assertSpanCacheSizes(spanProcessor, 0, 0); @@ -117,10 +120,10 @@ void build_sampledFlagTrue_notInCache() { TracezSpanProcessor.newBuilder().readProperties(properties).build(); /* Return a non-sampled span, which should not be added to the completed cache */ - when(readWriteSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readWriteSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); spanProcessor.onStart(readWriteSpan); assertSpanCacheSizes(spanProcessor, 1, 0); - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); spanProcessor.onEnd(readableSpan); assertSpanCacheSizes(spanProcessor, 0, 0); } @@ -134,13 +137,13 @@ void build_sampledFlagFalse_inCache() { TracezSpanProcessor.newBuilder().readProperties(properties).build(); /* Return a non-sampled span, which should be added to the caches */ - when(readWriteSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readWriteSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); spanProcessor.onStart(readWriteSpan); assertSpanCacheSizes(spanProcessor, 1, 0); when(readableSpan.getName()).thenReturn(SPAN_NAME); - when(readableSpan.getSpanContext()).thenReturn(NOT_SAMPLED_SPAN_CONTEXT); + when(readableSpan.getTraceFlags()).thenReturn(TraceFlags.getDefault()); when(readableSpan.toSpanData()).thenReturn(spanData); when(spanData.getStatus()).thenReturn(SPAN_STATUS); spanProcessor.onEnd(readableSpan); 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 50f07c0cdda..b321b23a314 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 @@ -287,10 +287,8 @@ void spanDetails_emitRunningSpanDetailsCorrectly() { assertThat(output.toString()).contains("

Span Details

"); assertThat(output.toString()).contains(" Span Name: " + RUNNING_SPAN + ""); assertThat(output.toString()).contains(" Number of running: 1"); - assertThat(output.toString()) - .contains(runningSpan.getContext().getTraceIdAsHexString().toString()); - assertThat(output.toString()) - .contains(runningSpan.getContext().getSpanIdAsHexString().toString()); + assertThat(output.toString()).contains(runningSpan.getTraceIdAsHexString()); + assertThat(output.toString()).contains(runningSpan.getSpanIdAsHexString()); runningSpan.end(); } @@ -313,14 +311,10 @@ void spanDetails_emitLatencySpanDetailsCorrectly() { assertThat(output.toString()).contains("

Span Details

"); assertThat(output.toString()).contains(" Span Name: " + LATENCY_SPAN + ""); assertThat(output.toString()).contains(" Number of latency samples: 2"); - assertThat(output.toString()) - .contains(latencySpan1.getContext().getTraceIdAsHexString().toString()); - assertThat(output.toString()) - .contains(latencySpan1.getContext().getSpanIdAsHexString().toString()); - assertThat(output.toString()) - .contains(latencySpan2.getContext().getTraceIdAsHexString().toString()); - assertThat(output.toString()) - .contains(latencySpan2.getContext().getSpanIdAsHexString().toString()); + assertThat(output.toString()).contains(latencySpan1.getTraceIdAsHexString()); + assertThat(output.toString()).contains(latencySpan1.getSpanIdAsHexString()); + assertThat(output.toString()).contains(latencySpan2.getTraceIdAsHexString()); + assertThat(output.toString()).contains(latencySpan2.getSpanIdAsHexString()); } @Test @@ -341,14 +335,10 @@ void spanDetails_emitErrorSpanDetailsCorrectly() { assertThat(output.toString()).contains("

Span Details

"); assertThat(output.toString()).contains(" Span Name: " + ERROR_SPAN + ""); assertThat(output.toString()).contains(" Number of error samples: 2"); - assertThat(output.toString()) - .contains(errorSpan1.getContext().getTraceIdAsHexString().toString()); - assertThat(output.toString()) - .contains(errorSpan1.getContext().getSpanIdAsHexString().toString()); - assertThat(output.toString()) - .contains(errorSpan2.getContext().getTraceIdAsHexString().toString()); - assertThat(output.toString()) - .contains(errorSpan2.getContext().getSpanIdAsHexString().toString()); + assertThat(output.toString()).contains(errorSpan1.getTraceIdAsHexString()); + assertThat(output.toString()).contains(errorSpan1.getSpanIdAsHexString()); + assertThat(output.toString()).contains(errorSpan2.getTraceIdAsHexString()); + assertThat(output.toString()).contains(errorSpan2.getSpanIdAsHexString()); } @Test 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 30ef616b9ce..550e2bd5429 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 @@ -25,8 +25,8 @@ import io.opentelemetry.sdk.trace.data.EventImpl; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Link; +import io.opentelemetry.trace.Span; 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; @@ -82,13 +82,13 @@ void canSetTotalAttributeCountWithBuilder() { @Test void link_defaultTotalAttributeCountIsZero() { - Link link = Link.create(SpanContext.getInvalid()); + Link link = Link.create(Span.getInvalid()); assertThat(link.getTotalAttributeCount()).isEqualTo(0); } @Test void link_canSetTotalAttributeCount() { - Link link = Link.create(SpanContext.getInvalid()); + Link link = Link.create(Span.getInvalid()); assertThat(link.getTotalAttributeCount()).isEqualTo(0); } @@ -112,7 +112,7 @@ private static SpanData createSpanDataWithMutableCollections() { } private static Link emptyLink() { - return Link.create(SpanContext.getInvalid()); + return Link.create(Span.getInvalid()); } private static TestSpanData.Builder createBasicSpanBuilder() {