Skip to content

Commit

Permalink
Remove SpanContext support from TracingContextUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosalberto committed Mar 20, 2020
1 parent a0ec628 commit 7a3cbeb
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.TracingContextUtils;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -71,8 +71,8 @@ public void setup() {
@Measurement(iterations = 15, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
public SpanContext measureExtract() {
return TracingContextUtils.getSpanContext(
public Span measureExtract() {
return TracingContextUtils.getSpan(
httpTraceContext.extract(Context.current(), carrier, getter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Setter;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.TraceFlags;
Expand Down Expand Up @@ -71,7 +72,8 @@ public void set(Map<String, String> carrier, String key, String value) {
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
public Map<String, String> measureInject() {
Context context = TracingContextUtils.withSpanContext(contextToTest, Context.current());
Context context =
TracingContextUtils.withSpan(DefaultSpan.create(contextToTest), Context.current());
httpTraceContext.inject(context, carrier, setter);
return carrier;
}
Expand Down
3 changes: 1 addition & 2 deletions api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.opentelemetry.trace;

import io.grpc.Context;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.context.Scope;
import io.opentelemetry.internal.Utils;
Expand Down Expand Up @@ -72,7 +71,7 @@ static NoopSpanBuilder create(String spanName) {
@Override
public Span startSpan() {
if (spanContext == null && !isRootSpan) {
spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current());
spanContext = TracingContextUtils.getCurrentSpan().getContext();
}

return spanContext != null && !SpanContext.getInvalid().equals(spanContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
public final class TracingContextUtils {
private static final Context.Key<Span> CONTEXT_SPAN_KEY =
Context.<Span>keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid());
private static final Context.Key<SpanContext> CONTEXT_SPANCONTEXT_KEY =
Context.<SpanContext>keyWithDefault(
"opentelemetry-trace-spancontext-key", SpanContext.getInvalid());

/**
* Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op
Expand All @@ -58,18 +55,6 @@ public static Context withSpan(Span span, Context context) {
return context.withValue(CONTEXT_SPAN_KEY, span);
}

/**
* Creates a new {@code Context} with the given {@link SpanContext} set.
*
* @param spanContext the value to be set.
* @param context the parent {@code Context}.
* @return a new context with the given value set.
* @since 0.3.0
*/
public static Context withSpanContext(SpanContext spanContext, Context context) {
return context.withValue(CONTEXT_SPANCONTEXT_KEY, spanContext);
}

/**
* Returns the {@link Span} from the specified {@code Context}, falling back to a default, no-op
* {@link Span}.
Expand All @@ -93,50 +78,7 @@ public static Span getSpan(Context context) {
@Nullable
public static Span getSpanWithoutDefault(Context context) {
Span span = CONTEXT_SPAN_KEY.get(context);
return span == DefaultSpan.getInvalid() ? null : span;
}

/**
* Returns the {@link SpanContext} from the specified {@code Context}, falling back to a default,
* no-op {@link SpanContext}.
*
* @param context the specified {@code Context}.
* @return the {@link SpanContext} from the specified {@code Context}.
* @since 0.3.0
*/
public static SpanContext getSpanContext(Context context) {
return CONTEXT_SPANCONTEXT_KEY.get(context);
}

/**
* Returns the {@link SpanContext} from the specified {@code Context}. If none is found, this
* method returns {@code null}.
*
* @param context the specified {@code Context}.
* @return the {@link SpanContext} from the specified {@code Context}.
* @since 0.3.0
*/
@Nullable
public static SpanContext getSpanContextWithoutDefault(Context context) {
SpanContext spanContext = CONTEXT_SPANCONTEXT_KEY.get(context);
return spanContext == SpanContext.getInvalid() ? null : spanContext;
}

/**
* Returns the effective {@link SpanContext} from the specified {@code Context}.
*
* <p>This method tries to get any effective non-null {@link SpanContext} in {@code Context},
* giving higher priority to {@link Span#getContext()} and then falling back to {@link
* SpanContext}. If none is found, this method returns {@code null}.
*
* @param context the specified {@code Context}.
* @return the value from the specified {@code Context}.
* @since 0.3.0
*/
@Nullable
public static SpanContext getEffectiveSpanContext(Context context) {
Span span = getSpanWithoutDefault(context);
return span != null ? span.getContext() : getSpanContextWithoutDefault(context);
return DefaultSpan.getInvalid().equals(span) ? null : span;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.internal.StringUtils;
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;
Expand Down Expand Up @@ -86,11 +88,12 @@ public <C> void inject(Context context, C carrier, Setter<C> setter) {
checkNotNull(setter, "setter");
checkNotNull(carrier, "carrier");

SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(context);
if (spanContext == null) {
Span span = TracingContextUtils.getSpanWithoutDefault(context);
if (span == null) {
return;
}

SpanContext spanContext = span.getContext();
String sampled = spanContext.getTraceFlags().isSampled() ? TRUE_INT : FALSE_INT;

if (singleHeader) {
Expand Down Expand Up @@ -122,7 +125,7 @@ public <C> Context extract(Context context, C carrier, Getter<C> getter) {
spanContext = getSpanContextFromMultipleHeaders(carrier, getter);
}

return TracingContextUtils.withSpanContext(spanContext, context);
return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context);
}

@SuppressWarnings("StringSplitter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat;
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;
Expand Down Expand Up @@ -79,12 +81,12 @@ public <C> void inject(Context context, C carrier, Setter<C> setter) {
checkNotNull(setter, "setter");
checkNotNull(carrier, "carrier");

SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(context);
if (spanContext == null) {
Span span = TracingContextUtils.getSpanWithoutDefault(context);
if (span == null) {
return;
}

injectImpl(spanContext, carrier, setter);
injectImpl(span.getContext(), carrier, setter);
}

private static <C> void injectImpl(SpanContext spanContext, C carrier, Setter<C> setter) {
Expand Down Expand Up @@ -124,7 +126,7 @@ private static <C> void injectImpl(SpanContext spanContext, C carrier, Setter<C>
checkNotNull(getter, "getter");

SpanContext spanContext = extractImpl(carrier, getter);
return TracingContextUtils.withSpanContext(spanContext, context);
return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context);
}

private static <C> SpanContext extractImpl(C carrier, Getter<C> getter) {
Expand Down
18 changes: 1 addition & 17 deletions api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,9 @@ public void testSpanContextPropagationCurrentSpan() {

@Test
public void testSpanContextPropagationCurrentSpanContext() {
Context context = TracingContextUtils.withSpanContext(spanContext, Context.current());
Scope scope = ContextUtils.withScopedContext(context);
try {
Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan();
assertThat(span.getContext()).isSameInstanceAs(spanContext);
} finally {
scope.close();
}
}

@Test
public void testSpanContextPropagationCurrentContextValues() {
Context context =
TracingContextUtils.withSpanContext(
SpanContext.create(
new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), TraceState.getDefault()),
TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current()));
TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.current());
Scope scope = ContextUtils.withScopedContext(context);
// Span in Context has higher priority than SpanContext.
try {
Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan();
assertThat(span.getContext()).isSameInstanceAs(spanContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
package io.opentelemetry.trace.propagation;

import static com.google.common.truth.Truth.assertThat;
import static io.opentelemetry.trace.TracingContextUtils.getSpanContext;
import static io.opentelemetry.trace.TracingContextUtils.withSpanContext;

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
import io.opentelemetry.context.propagation.HttpTextFormat.Setter;
import io.opentelemetry.internal.StringUtils;
import io.opentelemetry.trace.DefaultSpan;
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.TracingContextUtils;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -73,6 +73,14 @@ public String get(Map<String, String> carrier, String key) {
private final B3Propagator b3PropagatorSingleHeader = new B3Propagator(true);
@Rule public ExpectedException thrown = ExpectedException.none();

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
public void inject_SampledContext() {
Map<String, String> carrier = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@
package io.opentelemetry.trace.propagation;

import static com.google.common.truth.Truth.assertThat;
import static io.opentelemetry.trace.TracingContextUtils.getSpanContext;
import static io.opentelemetry.trace.TracingContextUtils.withSpan;
import static io.opentelemetry.trace.TracingContextUtils.withSpanContext;
import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_PARENT;
import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_STATE;

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
import io.opentelemetry.context.propagation.HttpTextFormat.Setter;
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;
import io.opentelemetry.trace.TraceState;
import io.opentelemetry.trace.TracingContextUtils;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -83,6 +80,14 @@ public String get(Map<String, String> carrier, String key) {
private final HttpTraceContext httpTraceContext = new HttpTraceContext();
@Rule public ExpectedException thrown = ExpectedException.none();

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
public void inject_Nothing() {
Map<String, String> carrier = new LinkedHashMap<String, String>();
Expand Down Expand Up @@ -141,35 +146,6 @@ public void inject_NotSampledContext_WithTraceState() {
TRACESTATE_NOT_DEFAULT_ENCODING);
}

@Test
public void inject_Span() {
Map<String, String> carrier = new LinkedHashMap<String, String>();
Span span =
DefaultSpan.create(
SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT));
Context context = withSpan(span, Context.current());
httpTraceContext.inject(context, carrier, setter);
assertThat(carrier)
.containsExactly(
TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED, TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING);
}

@Test
public void inject_Span_SpanContextPresent() {
// Span has higher priority than SpanContext.
Map<String, String> carrier = new LinkedHashMap<String, String>();
Span span =
DefaultSpan.create(
SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT));
SpanContext spanContext =
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT);
Context context = withSpanContext(spanContext, withSpan(span, Context.current()));
httpTraceContext.inject(context, carrier, setter);
assertThat(carrier)
.containsExactly(
TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED, TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING);
}

@Test
public void extract_SampledContext() {
Map<String, String> carrier = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.correlationcontext.CorrelationsContextUtils;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.TracingContextUtils;
import io.opentracing.propagation.TextMapExtract;
import io.opentracing.propagation.TextMapInject;
Expand All @@ -33,7 +34,8 @@ final class Propagation extends BaseShimObject {

public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) {
Context context =
TracingContextUtils.withSpanContext(contextShim.getSpanContext(), Context.current());
TracingContextUtils.withSpan(
DefaultSpan.create(contextShim.getSpanContext()), Context.current());
context =
CorrelationsContextUtils.withCorrelationContext(
contextShim.getCorrelationContext(), context);
Expand All @@ -53,13 +55,13 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) {
.getHttpTextFormat()
.extract(Context.current(), carrierMap, TextMapGetter.INSTANCE);

io.opentelemetry.trace.SpanContext spanContext = TracingContextUtils.getSpanContext(context);
if (!spanContext.isValid()) {
io.opentelemetry.trace.Span span = TracingContextUtils.getSpan(context);
if (!span.getContext().isValid()) {
return null;
}

return new SpanContextShim(
telemetryInfo, spanContext, CorrelationsContextUtils.getCorrelationContext(context));
telemetryInfo, span.getContext(), CorrelationsContextUtils.getCorrelationContext(context));
}

static final class TextMapSetter implements HttpTextFormat.Setter<TextMapInject> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private static SpanContext parent(
case NO_PARENT:
return null;
case CURRENT_CONTEXT:
return TracingContextUtils.getEffectiveSpanContext(Context.current());
return TracingContextUtils.getCurrentSpan().getContext();
case EXPLICIT_PARENT:
return explicitParent.getContext();
case EXPLICIT_REMOTE_PARENT:
Expand Down
Loading

0 comments on commit 7a3cbeb

Please sign in to comment.