From e2f642b99d0c542aa580df2412b4538aa4b8413d Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Sun, 15 Dec 2019 23:56:58 -0600 Subject: [PATCH 01/42] Initial Propagators refactor. --- .../java/io/opentelemetry/OpenTelemetry.java | 35 +++ .../context/propagation/BinaryFormat.java | 49 ----- .../propagation/DefaultPropagators.java | 127 +++++++++++ .../context/propagation/HttpTextFormat.java | 36 +++- .../context/propagation/Propagators.java | 133 ++++++++++++ .../DefaultDistributedContextManager.java | 56 ----- .../DistributedContextManager.java | 90 -------- .../io/opentelemetry/trace/DefaultTracer.java | 22 +- .../java/io/opentelemetry/trace/Tracer.java | 108 ---------- .../trace/propagation/BinaryTraceContext.java | 151 ------------- .../{unsafe => propagation}/ContextUtils.java | 52 ++++- .../trace/propagation/HttpTraceContext.java | 37 +++- .../{unsafe => propagation}/SpanInScope.java | 4 +- .../io/opentelemetry/OpenTelemetryTest.java | 27 --- .../trace/DefaultTracerTest.java | 12 -- .../propagation/BinaryTraceContextTest.java | 200 ------------------ .../ContextUtilsTest.java | 14 +- .../propagation/HttpTraceContextTest.java | 72 ++++--- 18 files changed, 452 insertions(+), 773 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/context/propagation/BinaryFormat.java create mode 100644 api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java create mode 100644 api/src/main/java/io/opentelemetry/context/propagation/Propagators.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/propagation/BinaryTraceContext.java rename api/src/main/java/io/opentelemetry/trace/{unsafe => propagation}/ContextUtils.java (60%) rename api/src/main/java/io/opentelemetry/trace/{unsafe => propagation}/SpanInScope.java (93%) delete mode 100644 api/src/test/java/io/opentelemetry/trace/propagation/BinaryTraceContextTest.java rename api/src/test/java/io/opentelemetry/trace/{unsafe => propagation}/ContextUtilsTest.java (81%) diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 4e8372ef133..c1c751be5a3 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -16,9 +16,12 @@ package io.opentelemetry; +import io.opentelemetry.context.propagation.DefaultPropagators; +import io.opentelemetry.context.propagation.Propagators; import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; import io.opentelemetry.distributedcontext.DistributedContextManager; import io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider; +import io.opentelemetry.internal.Utils; import io.opentelemetry.metrics.DefaultMeterFactory; import io.opentelemetry.metrics.DefaultMeterFactoryProvider; import io.opentelemetry.metrics.Meter; @@ -28,6 +31,7 @@ import io.opentelemetry.trace.DefaultTracerFactoryProvider; import io.opentelemetry.trace.Tracer; import io.opentelemetry.trace.TracerFactory; +import io.opentelemetry.trace.propagation.HttpTraceContext; import io.opentelemetry.trace.spi.TracerFactoryProvider; import java.util.ServiceLoader; import javax.annotation.Nullable; @@ -52,6 +56,9 @@ public final class OpenTelemetry { private final MeterFactory meterFactory; private final DistributedContextManager contextManager; + private volatile Propagators propagators = + DefaultPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build(); + /** * Returns a singleton {@link TracerFactory}. * @@ -89,6 +96,34 @@ public static DistributedContextManager getDistributedContextManager() { return getInstance().contextManager; } + /** + * Returns a {@link Propagators} object, which can be used to access the set of registered + * propagators for each supported format. + * + * @return registered propagators container, defaulting to a {@link Propagators} object with + * {@code HttpTraceContext} registered. + * @throws IllegalStateException if a specified manager (via system properties) could not be + * found. + * @since 0.3.0 + */ + public static Propagators getPropagators() { + return getInstance().propagators; + } + + /** + * Sets the {@link Propagators} object, which can be used to access the set of registered + * propagators for each supported format. + * + * @param propagators the {@link Propagators} object to be registered. + * @throws IllegalStateException if a specified manager (via system properties) could not be + * found. + * @since 0.3.0 + */ + public static void setPropagators(Propagators propagators) { + Utils.checkNotNull(propagators, "propagators"); + getInstance().propagators = propagators; + } + /** Lazy loads an instance. */ private static OpenTelemetry getInstance() { if (instance == null) { diff --git a/api/src/main/java/io/opentelemetry/context/propagation/BinaryFormat.java b/api/src/main/java/io/opentelemetry/context/propagation/BinaryFormat.java deleted file mode 100644 index f25c0b1c808..00000000000 --- a/api/src/main/java/io/opentelemetry/context/propagation/BinaryFormat.java +++ /dev/null @@ -1,49 +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.context.propagation; - -import javax.annotation.concurrent.ThreadSafe; - -/** - * Formatter to serializing and deserializing a value with into a binary format. - * - * @since 0.1.0 - */ -@ThreadSafe -public interface BinaryFormat { - - /** - * Serializes the {@code value} into the on-the-wire representation. - * - * @param value the value to serialize. - * @return the on-the-wire representation of a {@code value}. - * @since 0.1.0 - */ - byte[] toByteArray(V value); - - /** - * Creates a value from the given on-the-wire encoded representation. - * - *

If the value could not be parsed, the underlying implementation will decide to return ether - * an empty value, an invalid value, or a valid value. - * - * @param bytes on-the-wire representation of the value. - * @return a value deserialized from {@code bytes}. - * @since 0.1.0 - */ - V fromByteArray(byte[] bytes); -} diff --git a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java new file mode 100644 index 00000000000..a98e29b0513 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java @@ -0,0 +1,127 @@ +/* + * 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.context.propagation; + +import io.grpc.Context; +import io.opentelemetry.internal.Utils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * {@code DefaultPropagators} is the default, built-in implementation of {@link Propagators}. + * + *

All the registered propagators are stored internally as a simple list, and are invoked + * synchronically upon injection and extraction. + * + * @since 0.3.0 + */ +public final class DefaultPropagators implements Propagators { + private final HttpTextFormat textFormat; + + @Override + public HttpTextFormat getHttpTextFormat() { + return textFormat; + } + + /** + * Returns a {@link Propagators.Builder} to create a new {@link Propagators} object. + * + *

See {@link Propagators.Builder}. + * + * @return a {@link Propagators.Builder}. + * @since 0.3.0 + */ + public static Builder builder() { + return new DefaultBuilder(); + } + + private DefaultPropagators(HttpTextFormat textFormat) { + this.textFormat = textFormat; + } + + private static final class DefaultBuilder implements Builder { + List textPropagators = new ArrayList<>(); + + @Override + public Builder addHttpTextFormat(HttpTextFormat textFormat) { + Utils.checkNotNull(textFormat, "textFormat"); + textPropagators.add(textFormat); + return this; + } + + @Override + public Propagators build() { + if (textPropagators.isEmpty()) { + return new DefaultPropagators(NoopHttpTextFormat.INSTANCE); + } + + return new DefaultPropagators(new MultiHttpTextFormat(textPropagators)); + } + } + + private static final class MultiHttpTextFormat implements HttpTextFormat { + private final HttpTextFormat[] textPropagators; + + private MultiHttpTextFormat(List textPropagators) { + this.textPropagators = new HttpTextFormat[textPropagators.size()]; + textPropagators.toArray(this.textPropagators); + } + + @Override + public List fields() { + List fields = new ArrayList<>(); + for (int i = 0; i < textPropagators.length; i++) { + fields.addAll(textPropagators[i].fields()); + } + + return fields; + } + + @Override + public void inject(Context context, C carrier, Setter setter) { + for (int i = 0; i < textPropagators.length; i++) { + textPropagators[i].inject(context, carrier, setter); + } + } + + @Override + public Context extract(Context context, C carrier, Getter getter) { + for (int i = 0; i < textPropagators.length; i++) { + context = textPropagators[i].extract(context, carrier, getter); + } + return context; + } + } + + private static final class NoopHttpTextFormat implements HttpTextFormat { + private static final NoopHttpTextFormat INSTANCE = new NoopHttpTextFormat(); + + @Override + public List fields() { + return Collections.emptyList(); + } + + @Override + public void inject(Context context, C carrier, Setter setter) {} + + @Override + public Context extract(Context context, C carrier, Getter getter) { + return context; + } + } +} diff --git a/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java b/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java index cd55e45fe52..14070c8e6b8 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java @@ -16,6 +16,7 @@ package io.opentelemetry.context.propagation; +import io.grpc.Context; import java.util.List; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -29,10 +30,28 @@ * usually an http request. Propagation is usually implemented via library- specific request * interceptors, where the client-side injects values and the server-side extracts them. * + *

Specific concern values (traces, correlations, etc) will be read from the specified {@code + * Context}, and resulting values will be stored in a new {@code Context} upon extraction. It is + * recommended to use a single {@code Context.Key} to store the entire concern data: + * + *

{@code
+ * public static final Context.Key CONCERN_KEY = Context.key("my-concern-key");
+ * public MyConcernPropagator implements HttpTextFormat {
+ *   public  void inject(Context context, C carrier, Setter setter) {
+ *     Object concernObj = CONCERN_KEY.get(context);
+ *     // Use concernObj in the specified context to propagate data.
+ *   }
+ *   public  Context extract(Context context, C carrier, Getter setter) {
+ *     // Use setter to get the data from the carrier.
+ *     return context.withValue(CONCERN_KEY, concernObj);
+ *   }
+ * }
+ * }
+ * * @since 0.1.0 */ @ThreadSafe -public interface HttpTextFormat { +public interface HttpTextFormat { /** * The propagation fields defined. If your carrier is reused, you should delete the fields here * before calling {@link #inject(Object, Object, Setter)} )}. @@ -52,13 +71,13 @@ public interface HttpTextFormat { /** * Injects the value downstream. For example, as http headers. * - * @param value the value to be injected. + * @param context the {@code Context} containing the value to be injected. * @param carrier holds propagation fields. For example, an outgoing message or http request. * @param setter invoked for each propagation key to add or remove. * @param carrier of propagation fields, such as an http request * @since 0.1.0 */ - void inject(V value, C carrier, Setter setter); + void inject(Context context, C carrier, Setter setter); /** * Class that allows a {@code HttpTextFormat} to set propagated fields into a carrier. @@ -88,17 +107,18 @@ interface Setter { /** * Extracts the value from upstream. For example, as http headers. * - *

If the value could not be parsed, the underlying implementation will decide to return an - * object representing either an empty value, an invalid value, or a valid value. Implementation - * must not return {@code null}. + *

If the value could not be parsed, the underlying implementation will decide to set an object + * representing either an empty value, an invalid value, or a valid value. Implementation must not + * set {@code null}. * + * @param context the {@code Context} used to store the extracted value. * @param carrier holds propagation fields. For example, an outgoing message or http request. * @param getter invoked for each propagation key to get. * @param carrier of propagation fields, such as an http request. - * @return the extracted value, never {@code null}. + * @return the {@code Context} containing the non-{@code null} extracted value. * @since 0.1.0 */ - V extract(C carrier, Getter getter); + Context extract(Context context, C carrier, Getter getter); /** * Interface that allows a {@code HttpTextFormat} to read propagated fields from a carrier. diff --git a/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java b/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java new file mode 100644 index 00000000000..2275ba54e51 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java @@ -0,0 +1,133 @@ +/* + * 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.context.propagation; + +import javax.annotation.concurrent.Immutable; + +/** + * A container of the registered propagators for every supported format. + * + *

This container can be used to access a single, composite propagator for each supported format, + * which will be responsible for injecting and extracting data for each registered concern (traces, + * correlations, etc). Propagation will happen through {@code io.grpc.Context}, from which values + * will be read upon injection, and which will store values from the extraction step. The resulting + * {@code Context} can then be used implicitly or explicitly by the OpenTelemetry API. + * + *

Example of usage on the client: + * + *

{@code
+ * private static final Tracer tracer = OpenTelemetry.getTracer();
+ * void onSendRequest() {
+ *   try (Scope scope = tracer.withSpan(span)) {
+ *     Propagators propagators = OpenTelemetry.getPropagators();
+ *     HttpTextFormat textFormat = propagators.getHttpTextFormat();
+ *
+ *     // Inject the span's SpanContext and other available concerns (such as correlations)
+ *     // contained in the specified Context.
+ *     Map map = new HashMap<>();
+ *     httpTextFormat.inject(Context.current(), map, new Setter() {
+ *       public void put(Map map, String key, String value) {
+ *         map.put(key, value);
+ *       }
+ *     });
+ *     // Send the request including the text map and wait for the response.
+ *   }
+ * }
+ * }
+ * + *

Example of usage in the server: + * + *

{@code
+ * private static final Tracer tracer = OpenTelemetry.getTracer();
+ * void onRequestReceived() {
+ *   Propagators propagators = OpenTelemetry.getPropagators();
+ *   HttpTextFormat textFormat = propagators.getHttpTextFormat();
+ *
+ *   // Extract and store the propagated span's SpanContext and other available concerns
+ *   // in the specified Context.
+ *   Context context = textFormat.extract(Context.current(), request, new Getter() {
+ *     public String get(Object request, String key) {
+ *       // Return the value associated to the key, if available.
+ *     }
+ *   });
+ *   Span span = tracer.spanBuilder("MyRequest")
+ *       .setParent(context)
+ *       .setSpanKind(Span.Kind.SERVER).startSpan();
+ *   try (Scope ss = tracer.withSpan(span)) {
+ *     // Handle request and send response back.
+ *   } finally {
+ *     span.end();
+ *   }
+ * }
+ * }
+ * + * @since 0.3.0 + */ +@Immutable +public interface Propagators { + + /** + * Returns a {@link HttpTextFormat} propagator. + * + *

The returned value will be a composite instance containing all the registered {@link + * HttpTextFormat} propagators. If none is registered, the returned value will be a no-op + * instance. + * + * @return the {@link HttpTextFormat} propagator to inject and extract data. + * @since 0.3.0 + */ + HttpTextFormat getHttpTextFormat(); + + /** + * {@link Builder} is used to construct a new {@code Propagators} object with the specified + * propagators. + * + *

This is a example of a {@code Propagators} object being created: + * + *

{@code
+   * Propagators propagators = DefaultPropagators.builder()
+   *     .addHttpTextFormat(new HttpTraceContext())
+   *     .addHttpTextFormat(new HttpCorrelationContext())
+   *     .addHttpTextFormat(new MyCustomContextPropagator())
+   *     .build();
+   * }
+ * + * @since 0.3.0 + */ + interface Builder { + /** + * Adds a {@link HttpTextFormat} propagator. + * + *

One propagator per concern (traces, correlations, etc) should be added if this format is + * supported. + * + * @param textFormat the propagator to be added. + * @return this. + * @throws NullPointerException if {@code textFormat} is {@code null}. + * @since 0.3.0 + */ + Builder addHttpTextFormat(HttpTextFormat textFormat); + + /** + * Builds a new {@code Propagators} with the specified propagators. + * + * @return the newly created {@code Propagators} instance. + * @since 0.3.0 + */ + Propagators build(); + } +} diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java b/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java index 5ee832e40ea..b45fbaeab7e 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java +++ b/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java @@ -17,12 +17,8 @@ package io.opentelemetry.distributedcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.distributedcontext.unsafe.ContextUtils; import io.opentelemetry.internal.Utils; -import java.util.Collections; -import java.util.List; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; @@ -35,9 +31,6 @@ public final class DefaultDistributedContextManager implements DistributedContextManager { private static final DefaultDistributedContextManager INSTANCE = new DefaultDistributedContextManager(); - private static final BinaryFormat BINARY_FORMAT = new NoopBinaryFormat(); - private static final HttpTextFormat HTTP_TEXT_FORMAT = - new NoopHttpTextFormat(); /** * Returns a {@code DistributedContextManager} singleton that is the default implementation for @@ -66,16 +59,6 @@ public Scope withContext(DistributedContext distContext) { return ContextUtils.withDistributedContext(distContext); } - @Override - public BinaryFormat getBinaryFormat() { - return BINARY_FORMAT; - } - - @Override - public HttpTextFormat getHttpTextFormat() { - return HTTP_TEXT_FORMAT; - } - @Immutable private static final class NoopDistributedContextBuilder implements DistributedContext.Builder { @Override @@ -109,43 +92,4 @@ public DistributedContext build() { return EmptyDistributedContext.getInstance(); } } - - @Immutable - private static final class NoopBinaryFormat implements BinaryFormat { - static final byte[] EMPTY_BYTE_ARRAY = {}; - - @Override - public byte[] toByteArray(DistributedContext distContext) { - Utils.checkNotNull(distContext, "distContext"); - return EMPTY_BYTE_ARRAY; - } - - @Override - public DistributedContext fromByteArray(byte[] bytes) { - Utils.checkNotNull(bytes, "bytes"); - return EmptyDistributedContext.getInstance(); - } - } - - @Immutable - private static final class NoopHttpTextFormat implements HttpTextFormat { - @Override - public List fields() { - return Collections.emptyList(); - } - - @Override - public void inject(DistributedContext distContext, C carrier, Setter setter) { - Utils.checkNotNull(distContext, "distContext"); - Utils.checkNotNull(carrier, "carrier"); - Utils.checkNotNull(setter, "setter"); - } - - @Override - public DistributedContext extract(C carrier, Getter getter) { - Utils.checkNotNull(carrier, "carrier"); - Utils.checkNotNull(getter, "getter"); - return EmptyDistributedContext.getInstance(); - } - } } diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java b/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java index cab86b3700f..edf7777246e 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java +++ b/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java @@ -17,8 +17,6 @@ package io.opentelemetry.distributedcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import javax.annotation.concurrent.ThreadSafe; /** @@ -64,92 +62,4 @@ public interface DistributedContextManager { * @since 0.1.0 */ Scope withContext(DistributedContext distContext); - - /** - * Returns the {@link BinaryFormat} for this implementation. - * - *

Example of usage on the client: - * - *

{@code
-   * private static final DistributedContextManager contextManager =
-   *     OpenTelemetry.getDistributedContextManager();
-   * private static final BinaryFormat binaryFormat = contextManager.getBinaryFormat();
-   *
-   * Request createRequest() {
-   *   Request req = new Request();
-   *   byte[] ctxBuffer = binaryFormat.toByteArray(contextManager.getCurrentContext());
-   *   request.addMetadata("distributedContext", ctxBuffer);
-   *   return request;
-   * }
-   * }
- * - *

Example of usage on the server: - * - *

{@code
-   * private static final DistributedContextManager contextManager =
-   *     OpenTelemetry.getDistributedContextManager();
-   * private static final BinaryFormat binaryFormat = contextManager.getBinaryFormat();
-   *
-   * void onRequestReceived(Request request) {
-   *   byte[] ctxBuffer = request.getMetadata("distributedContext");
-   *   DistributedContext distContext = textFormat.fromByteArray(ctxBuffer);
-   *   try (Scope s = contextManager.withContext(distContext)) {
-   *     // Handle request and send response back.
-   *   }
-   * }
-   * }
- * - * @return the {@code BinaryFormat} for this implementation. - * @since 0.1.0 - */ - BinaryFormat getBinaryFormat(); - - /** - * Returns the {@link HttpTextFormat} for this implementation. - * - *

Usually this will be the W3C Correlation Context as the HTTP text format. For more details, - * see correlation-context. - * - *

Example of usage on the client: - * - *

{@code
-   * private static final DistributedContextManager contextManager =
-   *     OpenTelemetry.getDistributedContextManager();
-   * private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
-   *
-   * private static final HttpTextFormat.Setter setter =
-   *     new HttpTextFormat.Setter() {
-   *       public void put(HttpURLConnection carrier, String key, String value) {
-   *         carrier.setRequestProperty(field, value);
-   *       }
-   *     };
-   *
-   * void makeHttpRequest() {
-   *   HttpURLConnection connection =
-   *       (HttpURLConnection) new URL("http://myserver").openConnection();
-   *   textFormat.inject(contextManager.getCurrentContext(), connection, httpURLConnectionSetter);
-   *   // Send the request, wait for response and maybe set the status if not ok.
-   * }
-   * }
- * - *

Example of usage on the server: - * - *

{@code
-   * private static final DistributedContextManager contextManager =
-   *     OpenTelemetry.getDistributedContextManager();
-   * private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
-   * private static final HttpTextFormat.Getter getter = ...;
-   *
-   * void onRequestReceived(HttpRequest request) {
-   *   DistributedContext distContext = textFormat.extract(request, getter);
-   *   try (Scope s = contextManager.withContext(distContext)) {
-   *     // Handle request and send response back.
-   *   }
-   * }
-   * }
- * - * @return the {@code HttpTextFormat} for this implementation. - * @since 0.1.0 - */ - HttpTextFormat getHttpTextFormat(); } diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index b94cdf2fe81..cc47549cc53 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -17,12 +17,8 @@ package io.opentelemetry.trace; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.Utils; -import io.opentelemetry.trace.propagation.BinaryTraceContext; -import io.opentelemetry.trace.propagation.HttpTraceContext; -import io.opentelemetry.trace.unsafe.ContextUtils; +import io.opentelemetry.trace.propagation.ContextUtils; import java.util.Map; import javax.annotation.concurrent.ThreadSafe; @@ -34,8 +30,6 @@ @ThreadSafe public final class DefaultTracer implements Tracer { private static final DefaultTracer INSTANCE = new DefaultTracer(); - private static final BinaryFormat BINARY_FORMAT = new BinaryTraceContext(); - private static final HttpTextFormat HTTP_TEXT_FORMAT = new HttpTraceContext(); /** * Returns a {@code Tracer} singleton that is the default implementations for {@link Tracer}. @@ -49,12 +43,12 @@ public static Tracer getInstance() { @Override public Span getCurrentSpan() { - return ContextUtils.getValue(); + return ContextUtils.getSpan(); } @Override public Scope withSpan(Span span) { - return ContextUtils.withSpan(span); + return ContextUtils.withScopedSpan(span); } @Override @@ -62,16 +56,6 @@ public Span.Builder spanBuilder(String spanName) { return NoopSpanBuilder.create(this, spanName); } - @Override - public BinaryFormat getBinaryFormat() { - return BINARY_FORMAT; - } - - @Override - public HttpTextFormat getHttpTextFormat() { - return HTTP_TEXT_FORMAT; - } - private DefaultTracer() {} // Noop implementation of Span.Builder. diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index a49df4584f6..cb02053181c 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -18,9 +18,6 @@ import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.trace.propagation.HttpTraceContext; import javax.annotation.concurrent.ThreadSafe; /** @@ -153,109 +150,4 @@ public interface Tracer { * @since 0.1.0 */ Span.Builder spanBuilder(String spanName); - - /** - * Returns the {@link BinaryFormat} for this tracer implementation. - * - *

If no tracer implementation is provided, this defaults to the W3C Trace Context binary - * format. For more details see W3C Trace - * Context binary protocol. - * - *

Example of usage on the client: - * - *

{@code
-   * private static final Tracer tracer = OpenTelemetry.getTracer();
-   * private static final BinaryFormat binaryFormat = tracer.getBinaryFormat();
-   * void onSendRequest() {
-   *   Span span = tracer.spanBuilder("MyRequest").setSpanKind(Span.Kind.CLIENT).startSpan();
-   *   try (Scope ss = tracer.withSpan(span)) {
-   *     byte[] binaryValue = binaryFormat.toByteArray(tracer.getCurrentContext().context());
-   *     // Send the request including the binaryValue and wait for the response.
-   *   } finally {
-   *     span.end();
-   *   }
-   * }
-   * }
- * - *

Example of usage on the server: - * - *

{@code
-   * private static final Tracer tracer = OpenTelemetry.getTracer();
-   * private static final BinaryFormat binaryFormat = tracer.getBinaryFormat();
-   * void onRequestReceived() {
-   *   // Get the binaryValue from the request.
-   *   SpanContext spanContext = SpanContext.INVALID;
-   *   if (binaryValue != null) {
-   *     spanContext = binaryFormat.fromByteArray(binaryValue);
-   *   }
-   *   Span span = tracer.spanBuilder("MyRequest")
-   *       .setParent(spanContext)
-   *       .setSpanKind(Span.Kind.SERVER).startSpan();
-   *   try (Scope ss = tracer.withSpan(span)) {
-   *     // Handle request and send response back.
-   *   } finally {
-   *     span.end();
-   *   }
-   * }
-   * }
- * - * @return the {@code BinaryFormat} for this implementation. - * @since 0.1.0 - */ - BinaryFormat getBinaryFormat(); - - /** - * Returns the {@link HttpTextFormat} for this tracer implementation. - * - *

If no tracer implementation is provided, this defaults to the W3C Trace Context HTTP text - * format ({@link HttpTraceContext}). For more details see W3C Trace Context. - * - *

Example of usage on the client: - * - *

{@code
-   * private static final Tracer tracer = OpenTelemetry.getTracer();
-   * private static final HttpTextFormat textFormat = tracer.getHttpTextFormat();
-   * private static final HttpTextFormat.Setter setter =
-   *         new HttpTextFormat.Setter() {
-   *   public void put(HttpURLConnection carrier, String key, String value) {
-   *     carrier.setRequestProperty(field, value);
-   *   }
-   * }
-   *
-   * void makeHttpRequest() {
-   *   Span span = tracer.spanBuilder("MyRequest").setSpanKind(Span.Kind.CLIENT).startSpan();
-   *   try (Scope s = tracer.withSpan(span)) {
-   *     HttpURLConnection connection =
-   *         (HttpURLConnection) new URL("http://myserver").openConnection();
-   *     textFormat.inject(span.getContext(), connection, httpURLConnectionSetter);
-   *     // Send the request, wait for response and maybe set the status if not ok.
-   *   }
-   *   span.end();  // Can set a status.
-   * }
-   * }
- * - *

Example of usage on the server: - * - *

{@code
-   * private static final Tracer tracer = OpenTelemetry.getTracer();
-   * private static final HttpTextFormat textFormat = tracer.getHttpTextFormat();
-   * private static final HttpTextFormat.Getter getter = ...;
-   *
-   * void onRequestReceived(HttpRequest request) {
-   *   SpanContext spanContext = textFormat.extract(request, getter);
-   *   Span span = tracer.spanBuilder("MyRequest")
-   *       .setParent(spanContext)
-   *       .setSpanKind(Span.Kind.SERVER).startSpan();
-   *   try (Scope s = tracer.withSpan(span)) {
-   *     // Handle request and send response back.
-   *   }
-   *   span.end()
-   * }
-   * }
- * - * @return the {@code HttpTextFormat} for this implementation. - * @since 0.1.0 - */ - HttpTextFormat getHttpTextFormat(); } diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/BinaryTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/BinaryTraceContext.java deleted file mode 100644 index 53c7582f52b..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/propagation/BinaryTraceContext.java +++ /dev/null @@ -1,151 +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.propagation; - -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.internal.Utils; -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 javax.annotation.concurrent.Immutable; - -/** - * Implementation of the binary propagation protocol on {@link SpanContext}. - * - *

Format: - * - *

    - *
  • Binary value: <version_id><version_format> - *
  • version_id: 1-byte representing the version id. - *
  • For version_id = 0: - *
      - *
    • version_format: <field><field> - *
    • field_format: <field_id><field_format> - *
    • Fields: - *
        - *
      • TraceId: (field_id = 0, len = 16, default = "0000000000000000") - - * 16-byte array representing the trace_id. - *
      • SpanId: (field_id = 1, len = 8, default = "00000000") - 8-byte array - * representing the span_id. - *
      • TraceFlags: (field_id = 2, len = 1, default = "0") - 1-byte array - * representing the trace_options. - *
      - *
    • Fields MUST be encoded using the field id order (smaller to higher). - *
    • Valid value example: - *
        - *
      • {0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, - * 98, 99, 100, 101, 102, 103, 104, 2, 1} - *
      • version_id = 0; - *
      • trace_id = {64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79} - *
      • span_id = {97, 98, 99, 100, 101, 102, 103, 104}; - *
      • trace_options = {1}; - *
      - *
    - *
- */ -@Immutable -public final class BinaryTraceContext implements BinaryFormat { - - private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build(); - private static final byte VERSION_ID = 0; - private static final int VERSION_ID_OFFSET = 0; - // The version_id/field_id size in bytes. - private static final byte ID_SIZE = 1; - private static final byte TRACE_ID_FIELD_ID = 0; - - // TODO: clarify if offsets are correct here. While the specification suggests you should stop - // parsing when you hit an unknown field, it does not suggest that fields must be declared in - // ID order. Rather it only groups by data type order, in this case Trace Context - // https://github.com/census-instrumentation/opencensus-specs/blob/master/encodings/BinaryEncoding.md#deserialization-rules - static final int TRACE_ID_FIELD_ID_OFFSET = VERSION_ID_OFFSET + ID_SIZE; - - private static final int TRACE_ID_OFFSET = TRACE_ID_FIELD_ID_OFFSET + ID_SIZE; - private static final byte SPAN_ID_FIELD_ID = 1; - - static final int SPAN_ID_FIELD_ID_OFFSET = TRACE_ID_OFFSET + TraceId.getSize(); - - private static final int SPAN_ID_OFFSET = SPAN_ID_FIELD_ID_OFFSET + ID_SIZE; - private static final byte TRACE_OPTION_FIELD_ID = 2; - - static final int TRACE_OPTION_FIELD_ID_OFFSET = SPAN_ID_OFFSET + SpanId.getSize(); - - private static final int TRACE_OPTIONS_OFFSET = TRACE_OPTION_FIELD_ID_OFFSET + ID_SIZE; - /** Version, Trace and Span IDs are required fields. */ - private static final int REQUIRED_FORMAT_LENGTH = - 3 * ID_SIZE + TraceId.getSize() + SpanId.getSize(); - /** Use {@link TraceFlags#getDefault()} unless its optional field is present. */ - private static final int ALL_FORMAT_LENGTH = - REQUIRED_FORMAT_LENGTH + ID_SIZE + TraceFlags.getSize(); - - @Override - public byte[] toByteArray(SpanContext spanContext) { - Utils.checkNotNull(spanContext, "spanContext"); - byte[] bytes = new byte[ALL_FORMAT_LENGTH]; - bytes[VERSION_ID_OFFSET] = VERSION_ID; - bytes[TRACE_ID_FIELD_ID_OFFSET] = TRACE_ID_FIELD_ID; - spanContext.getTraceId().copyBytesTo(bytes, TRACE_ID_OFFSET); - bytes[SPAN_ID_FIELD_ID_OFFSET] = SPAN_ID_FIELD_ID; - spanContext.getSpanId().copyBytesTo(bytes, SPAN_ID_OFFSET); - bytes[TRACE_OPTION_FIELD_ID_OFFSET] = TRACE_OPTION_FIELD_ID; - spanContext.getTraceFlags().copyBytesTo(bytes, TRACE_OPTIONS_OFFSET); - return bytes; - } - - // TODO: consider throwing checked exception in API signature - @Override - public SpanContext fromByteArray(byte[] bytes) { - Utils.checkNotNull(bytes, "bytes"); - if (bytes.length == 0 || bytes[0] != VERSION_ID) { - throw new IllegalArgumentException("Unsupported version."); - } - if (bytes.length < REQUIRED_FORMAT_LENGTH) { - throw new IllegalArgumentException("Invalid input: truncated"); - } - // TODO: the following logic assumes that fields are written in ID order. The spec does not say - // that. If it decides not to, this logic would need to be more like a loop - TraceId traceId; - SpanId spanId; - TraceFlags traceFlags = TraceFlags.getDefault(); - int pos = 1; - if (bytes[pos] == TRACE_ID_FIELD_ID) { - traceId = TraceId.fromBytes(bytes, pos + ID_SIZE); - pos += ID_SIZE + TraceId.getSize(); - } else { - // TODO: update the spec to suggest that the trace ID is not actually optional - throw new IllegalArgumentException("Invalid input: expected trace ID at offset " + pos); - } - if (bytes[pos] == SPAN_ID_FIELD_ID) { - spanId = SpanId.fromBytes(bytes, pos + ID_SIZE); - pos += ID_SIZE + SpanId.getSize(); - } else { - // TODO: update the spec to suggest that the span ID is not actually optional. - throw new IllegalArgumentException("Invalid input: expected span ID at offset " + pos); - } - // Check to see if we are long enough to include an options field, and also that the next field - // is an options field. Per spec we simply stop parsing at first unknown field instead of - // failing. - if (bytes.length > pos && bytes[pos] == TRACE_OPTION_FIELD_ID) { - if (bytes.length < ALL_FORMAT_LENGTH) { - throw new IllegalArgumentException("Invalid input: truncated"); - } - traceFlags = TraceFlags.fromByte(bytes[pos + ID_SIZE]); - } - return SpanContext.createFromRemoteParent(traceId, spanId, traceFlags, TRACESTATE_DEFAULT); - } -} diff --git a/api/src/main/java/io/opentelemetry/trace/unsafe/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java similarity index 60% rename from api/src/main/java/io/opentelemetry/trace/unsafe/ContextUtils.java rename to api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java index 61dd089b77c..755a322b754 100644 --- a/api/src/main/java/io/opentelemetry/trace/unsafe/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java @@ -14,12 +14,13 @@ * limitations under the License. */ -package io.opentelemetry.trace.unsafe; +package io.opentelemetry.trace.propagation; import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; +import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; import javax.annotation.concurrent.Immutable; @@ -35,6 +36,8 @@ public final class ContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); + private static final Context.Key CONTEXT_SPANCONTEXT_KEY = + Context.key("opentelemetry-trace-spancontext-key"); /** * Creates a new {@code Context} with the given value set. @@ -43,8 +46,8 @@ public final class ContextUtils { * @return a new context with the given value set. * @since 0.1.0 */ - public static Context withValue(Span span) { - return Context.current().withValue(CONTEXT_SPAN_KEY, span); + public static Context withSpan(Span span) { + return withSpan(span, Context.current()); } /** @@ -55,8 +58,33 @@ public static Context withValue(Span span) { * @return a new context with the given value set. * @since 0.1.0 */ - public static Context withValue(Span span, Context context) { - return context.withValue(CONTEXT_SPAN_KEY, span); + public static Context withSpan(Span span, Context context) { + return context.withValue(CONTEXT_SPAN_KEY, span).withValue(CONTEXT_SPANCONTEXT_KEY, null); + } + + /** + * Creates a new {@code Context} with the given {@code SpanContext} set. + * + * @param spanContext the value to be set. + * @return a new context with the given value set. + * @since 0.3.0 + */ + public static Context withSpanContext(SpanContext spanContext) { + return withSpanContext(spanContext, Context.current()); + } + + /** + * Creates a new {@code Context} with the given {@code 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_SPAN_KEY, null) + .withValue(CONTEXT_SPANCONTEXT_KEY, spanContext); } /** @@ -65,7 +93,7 @@ public static Context withValue(Span span, Context context) { * @return the value from the specified {@code Context}. * @since 0.1.0 */ - public static Span getValue() { + public static Span getSpan() { return CONTEXT_SPAN_KEY.get(); } @@ -76,10 +104,18 @@ public static Span getValue() { * @return the value from the specified {@code Context}. * @since 0.1.0 */ - public static Span getValue(Context context) { + public static Span getSpan(Context context) { return CONTEXT_SPAN_KEY.get(context); } + public static SpanContext getSpanContext() { + return CONTEXT_SPANCONTEXT_KEY.get(); + } + + public static SpanContext getSpanContext(Context context) { + return CONTEXT_SPANCONTEXT_KEY.get(context); + } + /** * Returns a new {@link Scope} encapsulating the provided {@code Span} added to the current {@code * Context}. @@ -88,7 +124,7 @@ public static Span getValue(Context context) { * @return the {@link Scope} for the updated {@code Context}. * @since 0.1.0 */ - public static Scope withSpan(Span span) { + public static Scope withScopedSpan(Span span) { return SpanInScope.create(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 164619c8f21..6083ad2a085 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -19,7 +19,10 @@ import static io.opentelemetry.internal.Utils.checkArgument; import static io.opentelemetry.internal.Utils.checkNotNull; +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; @@ -36,7 +39,7 @@ * href=https://github.com/w3c/distributed-tracing>w3c/distributed-tracing. */ @Immutable -public class HttpTraceContext implements HttpTextFormat { +public class HttpTraceContext implements HttpTextFormat { private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build(); static final String TRACEPARENT = "traceparent"; static final String TRACESTATE = "tracestate"; @@ -69,10 +72,28 @@ public List fields() { } @Override - public void inject(SpanContext spanContext, C carrier, Setter setter) { - checkNotNull(spanContext, "spanContext"); + public void inject(Context context, C carrier, Setter setter) { + checkNotNull(context, "context"); checkNotNull(setter, "setter"); checkNotNull(carrier, "carrier"); + + SpanContext spanContext = null; + Span span = ContextUtils.getSpan(context); + + if (DefaultSpan.getInvalid().equals(span)) { // No value is set. + spanContext = ContextUtils.getSpanContext(context); + } else { + spanContext = span.getContext(); + } + + if (spanContext == null) { + return; + } + + injectImpl(spanContext, carrier, setter); + } + + private static void injectImpl(SpanContext spanContext, C carrier, Setter setter) { char[] chars = new char[TRACEPARENT_HEADER_SIZE]; chars[0] = VERSION.charAt(0); chars[1] = VERSION.charAt(1); @@ -102,9 +123,17 @@ public void inject(SpanContext spanContext, C carrier, Setter setter) { } @Override - public >> extends @NonNull Object*/> SpanContext extract(C carrier, Getter getter) { + public >> extends @NonNull Object*/> Context extract( + Context context, C carrier, Getter getter) { + checkNotNull(carrier, "context"); checkNotNull(carrier, "carrier"); checkNotNull(getter, "getter"); + + SpanContext spanContext = extractImpl(carrier, getter); + return ContextUtils.withSpanContext(spanContext, context); + } + + private static SpanContext extractImpl(C carrier, Getter getter) { TraceId traceId; SpanId spanId; TraceFlags traceFlags; diff --git a/api/src/main/java/io/opentelemetry/trace/unsafe/SpanInScope.java b/api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java similarity index 93% rename from api/src/main/java/io/opentelemetry/trace/unsafe/SpanInScope.java rename to api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java index d1e9ed031cf..385dd4ee755 100644 --- a/api/src/main/java/io/opentelemetry/trace/unsafe/SpanInScope.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.trace.unsafe; +package io.opentelemetry.trace.propagation; import io.grpc.Context; import io.opentelemetry.context.Scope; @@ -30,7 +30,7 @@ final class SpanInScope implements Scope { private final Context current; private SpanInScope(Span span) { - current = ContextUtils.withValue(span); + current = ContextUtils.withSpan(span); previous = current.attach(); } diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index 4430f694e52..3eaa822089c 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -20,8 +20,6 @@ import static org.junit.Assert.assertTrue; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; import io.opentelemetry.distributedcontext.DistributedContext; import io.opentelemetry.distributedcontext.DistributedContextManager; @@ -42,7 +40,6 @@ import io.opentelemetry.metrics.spi.MeterFactoryProvider; import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; import io.opentelemetry.trace.TracerFactory; import io.opentelemetry.trace.spi.TracerFactoryProvider; @@ -270,18 +267,6 @@ public Span.Builder spanBuilder(String spanName) { return null; } - @Nullable - @Override - public BinaryFormat getBinaryFormat() { - return null; - } - - @Nullable - @Override - public HttpTextFormat getHttpTextFormat() { - return null; - } - @Override public TracerFactory create() { return new FirstTracerFactory(); @@ -433,17 +418,5 @@ public DistributedContext.Builder contextBuilder() { public Scope withContext(DistributedContext distContext) { return null; } - - @Nullable - @Override - public BinaryFormat getBinaryFormat() { - return null; - } - - @Nullable - @Override - public HttpTextFormat getHttpTextFormat() { - return null; - } } } diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 32ad198c684..a4cc46702a3 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -19,8 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import io.opentelemetry.context.Scope; -import io.opentelemetry.trace.propagation.BinaryTraceContext; -import io.opentelemetry.trace.propagation.HttpTraceContext; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -74,16 +72,6 @@ public void defaultSpanBuilderWithName() { assertThat(defaultTracer.spanBuilder(SPAN_NAME).startSpan()).isInstanceOf(DefaultSpan.class); } - @Test - public void defaultHttpTextFormat() { - assertThat(defaultTracer.getHttpTextFormat()).isInstanceOf(HttpTraceContext.class); - } - - @Test - public void defaultBinaryFormat() { - assertThat(defaultTracer.getBinaryFormat()).isInstanceOf(BinaryTraceContext.class); - } - @Test public void testInProcessContext() { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/BinaryTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/BinaryTraceContextTest.java deleted file mode 100644 index 1f6e6d41e50..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/propagation/BinaryTraceContextTest.java +++ /dev/null @@ -1,200 +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.propagation; - -import static com.google.common.truth.Truth.assertThat; - -import io.opentelemetry.context.propagation.BinaryFormat; -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 org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for {@link io.opentelemetry.trace.propagation.BinaryTraceContext}. */ -@RunWith(JUnit4.class) -public class BinaryTraceContextTest { - - private static final byte[] TRACE_ID_BYTES = - new byte[] {64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}; - private static final TraceId TRACE_ID = TraceId.fromBytes(TRACE_ID_BYTES, 0); - private static final byte[] SPAN_ID_BYTES = new byte[] {97, 98, 99, 100, 101, 102, 103, 104}; - private static final SpanId SPAN_ID = SpanId.fromBytes(SPAN_ID_BYTES, 0); - private static final byte TRACE_OPTIONS_BYTES = 1; - private static final TraceFlags TRACE_OPTIONS = TraceFlags.fromByte(TRACE_OPTIONS_BYTES); - private static final byte[] EXAMPLE_BYTES = - new byte[] { - 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, - 101, 102, 103, 104, 2, 1 - }; - private static final SpanContext INVALID_SPAN_CONTEXT = DefaultSpan.getInvalid().getContext(); - @Rule public ExpectedException expectedException = ExpectedException.none(); - private final BinaryFormat binaryFormat = new BinaryTraceContext(); - - private void testSpanContextConversion(SpanContext spanContext) { - SpanContext propagatedBinarySpanContext = - binaryFormat.fromByteArray(binaryFormat.toByteArray(spanContext)); - - assertThat(propagatedBinarySpanContext.getTraceId()).isEqualTo(spanContext.getTraceId()); - assertThat(propagatedBinarySpanContext.getSpanId()).isEqualTo(spanContext.getSpanId()); - assertThat(propagatedBinarySpanContext.getTraceFlags()).isEqualTo(spanContext.getTraceFlags()); - } - - @Test - public void propagate_SpanContextTracingEnabled() { - testSpanContextConversion( - SpanContext.create( - TRACE_ID, - SPAN_ID, - TraceFlags.builder().setIsSampled(true).build(), - Tracestate.getDefault())); - } - - @Test - public void propagate_SpanContextNoTracing() { - testSpanContextConversion( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), Tracestate.getDefault())); - } - - @Test(expected = NullPointerException.class) - public void toBinaryValue_NullSpanContext() { - binaryFormat.toByteArray(null); - } - - @Test - public void toBinaryValue_InvalidSpanContext() { - assertThat(binaryFormat.toByteArray(INVALID_SPAN_CONTEXT)) - .isEqualTo( - new byte[] { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 - }); - } - - @Test - public void fromBinaryValue_BinaryExampleValue() { - assertThat(binaryFormat.fromByteArray(EXAMPLE_BYTES)) - .isEqualTo( - SpanContext.createFromRemoteParent( - TRACE_ID, SPAN_ID, TRACE_OPTIONS, Tracestate.getDefault())); - } - - @Test(expected = NullPointerException.class) - public void fromBinaryValue_NullInput() { - binaryFormat.fromByteArray(null); - } - - @Test - public void fromBinaryValue_EmptyInput() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Unsupported version."); - binaryFormat.fromByteArray(new byte[0]); - } - - @Test - public void fromBinaryValue_UnsupportedVersionId() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Unsupported version."); - binaryFormat.fromByteArray( - new byte[] { - 66, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 97, 98, 99, 100, 101, - 102, 103, 104, 1 - }); - } - - @Test - public void fromBinaryValue_UnsupportedFieldIdFirst() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage( - "Invalid input: expected trace ID at offset " - + BinaryTraceContext.TRACE_ID_FIELD_ID_OFFSET); - binaryFormat.fromByteArray( - new byte[] { - 0, 4, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, - 101, 102, 103, 104, 2, 1 - }); - } - - @Test - public void fromBinaryValue_UnsupportedFieldIdSecond() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage( - "Invalid input: expected span ID at offset " + BinaryTraceContext.SPAN_ID_FIELD_ID_OFFSET); - binaryFormat.fromByteArray( - new byte[] { - 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 3, 97, 98, 99, 100, - 101, 102, 103, 104, 2, 1 - }); - } - - @Test - public void fromBinaryValue_UnsupportedFieldIdThird_skipped() { - assertThat( - binaryFormat - .fromByteArray( - new byte[] { - 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, - 98, 99, 100, 101, 102, 103, 104, 0, 1 - }) - .isValid()) - .isTrue(); - } - - @Test - public void fromBinaryValue_ShorterTraceId() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid input: truncated"); - binaryFormat.fromByteArray( - new byte[] {0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76}); - } - - @Test - public void fromBinaryValue_ShorterSpanId() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid input: truncated"); - binaryFormat.fromByteArray(new byte[] {0, 1, 97, 98, 99, 100, 101, 102, 103}); - } - - @Test - public void fromBinaryValue_ShorterTraceFlags() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid input: truncated"); - binaryFormat.fromByteArray( - new byte[] { - 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, - 101, 102, 103, 104, 2 - }); - } - - @Test - public void fromBinaryValue_MissingTraceFlagsOk() { - SpanContext extracted = - binaryFormat.fromByteArray( - new byte[] { - 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, - 100, 101, 102, 103, 104 - }); - - assertThat(extracted.isValid()).isTrue(); - assertThat(extracted.getTraceFlags()).isEqualTo(TraceFlags.getDefault()); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/unsafe/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java similarity index 81% rename from api/src/test/java/io/opentelemetry/trace/unsafe/ContextUtilsTest.java rename to api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java index e751586a935..aaf6aaf7de1 100644 --- a/api/src/test/java/io/opentelemetry/trace/unsafe/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.trace.unsafe; +package io.opentelemetry.trace.propagation; import static com.google.common.truth.Truth.assertThat; @@ -30,23 +30,23 @@ public final class ContextUtilsTest { @Test public void testGetCurrentSpan_DefaultContext() { - Span span = ContextUtils.getValue(Context.current()); + Span span = ContextUtils.getSpan(Context.current()); assertThat(span).isNotNull(); assertThat(span).isInstanceOf(DefaultSpan.class); } @Test public void testGetCurrentSpan_DefaultContext_WithoutExplicitContext() { - Span span = ContextUtils.getValue(); + Span span = ContextUtils.getSpan(); assertThat(span).isNotNull(); assertThat(span).isInstanceOf(DefaultSpan.class); } @Test public void testGetCurrentSpan_ContextSetToNull() { - Context orig = ContextUtils.withValue(null, Context.current()).attach(); + Context orig = ContextUtils.withSpan(null, Context.current()).attach(); try { - Span span = ContextUtils.getValue(Context.current()); + Span span = ContextUtils.getSpan(Context.current()); assertThat(span).isNotNull(); assertThat(span).isInstanceOf(DefaultSpan.class); } finally { @@ -56,9 +56,9 @@ public void testGetCurrentSpan_ContextSetToNull() { @Test public void testGetCurrentSpan_ContextSetToNull_WithoutExplicitContext() { - Context orig = ContextUtils.withValue(null).attach(); + Context orig = ContextUtils.withSpan(null).attach(); try { - Span span = ContextUtils.getValue(Context.current()); + Span span = ContextUtils.getSpan(Context.current()); assertThat(span).isNotNull(); assertThat(span).isInstanceOf(DefaultSpan.class); } finally { 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 f4a534c3f5a..8b0fddeab1b 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -20,6 +20,7 @@ import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACEPARENT; import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACESTATE; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.SpanContext; @@ -78,30 +79,30 @@ public String get(Map carrier, String key) { @Test public void inject_SampledContext() { Map carrier = new LinkedHashMap(); - httpTraceContext.inject( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT), - carrier, - setter); + Context context = + ContextUtils.withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT)); + httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); } @Test public void inject_NotSampledContext() { Map carrier = new LinkedHashMap(); - httpTraceContext.inject( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT), - carrier, - setter); + Context context = + ContextUtils.withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); + httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); } @Test public void inject_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap(); - httpTraceContext.inject( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT), - carrier, - setter); + Context context = + ContextUtils.withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)); + httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( TRACEPARENT, TRACEPARENT_HEADER_SAMPLED, TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); @@ -110,10 +111,10 @@ public void inject_SampledContext_WithTraceState() { @Test public void inject_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap(); - httpTraceContext.inject( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT), - carrier, - setter); + Context context = + ContextUtils.withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT)); + httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( TRACEPARENT, @@ -126,7 +127,8 @@ public void inject_NotSampledContext_WithTraceState() { public void extract_SampledContext() { Map carrier = new LinkedHashMap(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT)); @@ -136,7 +138,8 @@ public void extract_SampledContext() { public void extract_NotSampledContext() { Map carrier = new LinkedHashMap(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); @@ -147,7 +150,8 @@ public void extract_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)); @@ -158,7 +162,8 @@ public void extract_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT)); @@ -168,7 +173,8 @@ public void extract_NotSampledContext_WithTraceState() { public void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACEPARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); @@ -179,7 +185,8 @@ public void extract_NotSampledContext_EmptyTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACESTATE, ""); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); @@ -190,7 +197,8 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACESTATE, "foo=bar , bar=baz"); - assertThat(httpTraceContext.extract(carrier, getter)) + Context context = httpTraceContext.extract(Context.current(), carrier, getter); + assertThat(ContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT)); @@ -209,7 +217,7 @@ public void extract_InvalidTraceId() { + "-" + SPAN_ID_BASE16 + "-01"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -219,7 +227,7 @@ public void extract_InvalidTraceId_Size() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage( "Invalid traceparent: " + "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -229,7 +237,7 @@ public void extract_InvalidSpanId() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage( "Invalid traceparent: " + "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -239,7 +247,7 @@ public void extract_InvalidSpanId_Size() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage( "Invalid traceparent: " + "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -249,7 +257,7 @@ public void extract_InvalidTraceFlags() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage( "Invalid traceparent: " + "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -259,7 +267,7 @@ public void extract_InvalidTraceFlags_Size() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage( "Invalid traceparent: " + "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -269,7 +277,7 @@ public void extract_InvalidTracestate_EntriesDelimiter() { invalidHeaders.put(TRACESTATE, "foo=bar;test=test"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Invalid tracestate: " + "foo=bar;test=test"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -279,7 +287,7 @@ public void extract_InvalidTracestate_KeyValueDelimiter() { invalidHeaders.put(TRACESTATE, "foo=bar,test-test"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Invalid tracestate: " + "foo=bar,test-test"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test @@ -289,7 +297,7 @@ public void extract_InvalidTracestate_OneString() { invalidHeaders.put(TRACESTATE, "test-test"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Invalid tracestate: " + "test-test"); - httpTraceContext.extract(invalidHeaders, getter); + httpTraceContext.extract(Context.current(), invalidHeaders, getter); } @Test From d5d3e020bec73c33bd64dc51f461a33508461192 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 17 Dec 2019 01:27:05 -0600 Subject: [PATCH 02/42] Add tests for the new propagators changes. --- .../java/io/opentelemetry/OpenTelemetry.java | 1 + .../io/opentelemetry/OpenTelemetryTest.java | 17 ++ .../propagation/DefaultPropagatorsTest.java | 162 ++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index c1c751be5a3..7b8dc69eb9b 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -117,6 +117,7 @@ public static Propagators getPropagators() { * @param propagators the {@link Propagators} object to be registered. * @throws IllegalStateException if a specified manager (via system properties) could not be * found. + * @throws NullPointerException if {@code propagators} is {@code null}. * @since 0.3.0 */ public static void setPropagators(Propagators propagators) { diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index 3eaa822089c..ba5560fd51c 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -20,6 +20,8 @@ import static org.junit.Assert.assertTrue; import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.DefaultPropagators; +import io.opentelemetry.context.propagation.Propagators; import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; import io.opentelemetry.distributedcontext.DistributedContext; import io.opentelemetry.distributedcontext.DistributedContextManager; @@ -88,6 +90,8 @@ public void testDefault() { .isInstanceOf(DefaultDistributedContextManager.getInstance().getClass()); assertThat(OpenTelemetry.getDistributedContextManager()) .isEqualTo(OpenTelemetry.getDistributedContextManager()); + assertThat(OpenTelemetry.getPropagators()).isInstanceOf(DefaultPropagators.class); + assertThat(OpenTelemetry.getPropagators()).isEqualTo(OpenTelemetry.getPropagators()); } @Test @@ -206,6 +210,19 @@ public void testDistributedContextManagerNotFound() { OpenTelemetry.getDistributedContextManager(); } + @Test + public void testPropagatorsSet() { + Propagators propagators = DefaultPropagators.builder().build(); + OpenTelemetry.setPropagators(propagators); + assertThat(OpenTelemetry.getPropagators()).isEqualTo(propagators); + } + + @Test + public void testPropagatorsSetNull() { + thrown.expect(NullPointerException.class); + OpenTelemetry.setPropagators(null); + } + private static File createService(Class service, Class... impls) throws IOException { URL location = Tracer.class.getProtectionDomain().getCodeSource().getLocation(); File file = new File(location.getPath() + "META-INF/services/" + service.getName()); diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java new file mode 100644 index 00000000000..2672ed9b0e7 --- /dev/null +++ b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -0,0 +1,162 @@ +/* + * 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.context.propagation; + +import static com.google.common.truth.Truth.assertThat; + +import io.grpc.Context; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link DefaultPropagators}. */ +@RunWith(JUnit4.class) +public class DefaultPropagatorsTest { + + @Rule public final ExpectedException thrown = ExpectedException.none(); + + @Test + public void addHttpTextFormatNull() { + thrown.expect(NullPointerException.class); + DefaultPropagators.builder().addHttpTextFormat(null); + } + + @Test + public void testInject() { + CustomHttpTextFormat propagator1 = new CustomHttpTextFormat("prop1"); + CustomHttpTextFormat propagator2 = new CustomHttpTextFormat("prop2"); + Propagators propagators = + DefaultPropagators.builder() + .addHttpTextFormat(propagator1) + .addHttpTextFormat(propagator2) + .build(); + + Context context = Context.current(); + context = context.withValue(propagator1.getKey(), "value1"); + context = context.withValue(propagator2.getKey(), "value2"); + + Map map = new HashMap<>(); + propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); + assertThat(map.get(propagator1.getKeyName())).isEqualTo("value1"); + assertThat(map.get(propagator2.getKeyName())).isEqualTo("value2"); + } + + @Test + public void testExtract() { + CustomHttpTextFormat propagator1 = new CustomHttpTextFormat("prop1"); + CustomHttpTextFormat propagator2 = new CustomHttpTextFormat("prop2"); + CustomHttpTextFormat propagator3 = new CustomHttpTextFormat("prop3"); + Propagators propagators = + DefaultPropagators.builder() + .addHttpTextFormat(propagator1) + .addHttpTextFormat(propagator2) + .build(); + + // Put values for propagators 1 and 2 only. + Map map = new HashMap<>(); + map.put(propagator1.getKeyName(), "value1"); + map.put(propagator2.getKeyName(), "value2"); + + Context context = + propagators.getHttpTextFormat().extract(Context.current(), map, MapGetter.INSTANCE); + assertThat(propagator1.getKey().get(context)).isEqualTo("value1"); + assertThat(propagator2.getKey().get(context)).isEqualTo("value2"); + assertThat(propagator3.getKey().get(context)).isNull(); // Handle missing value. + } + + @Test + public void noopPropagator() { + Propagators propagators = DefaultPropagators.builder().build(); + + Context context = Context.current(); + Map map = new HashMap<>(); + propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); + assertThat(map).isEmpty(); + + assertThat(propagators.getHttpTextFormat().extract(context, map, MapGetter.INSTANCE)) + .isSameInstanceAs(context); + } + + class CustomHttpTextFormat implements HttpTextFormat { + private final String name; + private final Context.Key key; + + public CustomHttpTextFormat(String name) { + this.name = name; + this.key = Context.key(name); + } + + public Context.Key getKey() { + return key; + } + + public String getKeyName() { + return name; + } + + @Override + public List fields() { + return Collections.singletonList(name); + } + + @Override + public void inject(Context context, C carrier, Setter setter) { + Object payload = key.get(context); + if (payload != null) { + setter.put(carrier, name, payload.toString()); + } + } + + @Override + public Context extract(Context context, C carrier, Getter getter) { + String payload = getter.get(carrier, name); + if (payload != null) { + context = context.withValue(key, payload); + } + + return context; + } + } + + private static final class MapSetter implements HttpTextFormat.Setter> { + private static final MapSetter INSTANCE = new MapSetter(); + + @Override + public void put(Map map, String key, String value) { + map.put(key, value); + } + + private MapSetter() {} + } + + private static final class MapGetter implements HttpTextFormat.Getter> { + private static final MapGetter INSTANCE = new MapGetter(); + + @Override + public String get(Map map, String key) { + return map.get(key); + } + + private MapGetter() {} + } +} From b60b02df4fda8aed740464a932b317288e15803b Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 17 Dec 2019 01:49:02 -0600 Subject: [PATCH 03/42] Make the SDK compile/pass. --- .../DefaultDistributedContextManager.java | 2 +- .../{unsafe => propagation}/ContextUtils.java | 2 +- .../DistributedContextInScope.java | 2 +- .../DefaultDistributedContextManagerTest.java | 2 +- .../ContextUtilsTest.java | 2 +- .../DistributedContextManagerSdk.java | 17 +------------- .../sdk/trace/SpanBuilderSdk.java | 6 ++--- .../io/opentelemetry/sdk/trace/TracerSdk.java | 23 +++---------------- .../DistributedContextManagerSdkTest.java | 2 +- .../sdk/trace/TracerSdkTest.java | 16 ++----------- 10 files changed, 15 insertions(+), 59 deletions(-) rename api/src/main/java/io/opentelemetry/distributedcontext/{unsafe => propagation}/ContextUtils.java (98%) rename api/src/main/java/io/opentelemetry/distributedcontext/{unsafe => propagation}/DistributedContextInScope.java (96%) rename api/src/test/java/io/opentelemetry/distributedcontext/{unsafe => propagation}/ContextUtilsTest.java (97%) diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java b/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java index b45fbaeab7e..ed25a4c0516 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java +++ b/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java @@ -17,7 +17,7 @@ package io.opentelemetry.distributedcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.unsafe.ContextUtils; +import io.opentelemetry.distributedcontext.propagation.ContextUtils; import io.opentelemetry.internal.Utils; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/unsafe/ContextUtils.java b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java similarity index 98% rename from api/src/main/java/io/opentelemetry/distributedcontext/unsafe/ContextUtils.java rename to api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java index 209378aa8d3..38c1e25462c 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/unsafe/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext.unsafe; +package io.opentelemetry.distributedcontext.propagation; import io.grpc.Context; import io.opentelemetry.context.Scope; diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/unsafe/DistributedContextInScope.java b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java similarity index 96% rename from api/src/main/java/io/opentelemetry/distributedcontext/unsafe/DistributedContextInScope.java rename to api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java index a386ae1b8e7..b1ad03818ca 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/unsafe/DistributedContextInScope.java +++ b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext.unsafe; +package io.opentelemetry.distributedcontext.propagation; import io.grpc.Context; import io.opentelemetry.context.Scope; diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java b/api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java index 93fbf190255..341c3f91694 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java @@ -20,7 +20,7 @@ import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.unsafe.ContextUtils; +import io.opentelemetry.distributedcontext.propagation.ContextUtils; import java.util.Arrays; import java.util.Collection; import javax.annotation.Nullable; diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/unsafe/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/distributedcontext/propagation/ContextUtilsTest.java similarity index 97% rename from api/src/test/java/io/opentelemetry/distributedcontext/unsafe/ContextUtilsTest.java rename to api/src/test/java/io/opentelemetry/distributedcontext/propagation/ContextUtilsTest.java index 3c71bdf3c9f..02f67452e3a 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/unsafe/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/distributedcontext/propagation/ContextUtilsTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext.unsafe; +package io.opentelemetry.distributedcontext.propagation; import static com.google.common.truth.Truth.assertThat; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java index acc0e9816ee..a88b9c24659 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java @@ -17,12 +17,9 @@ package io.opentelemetry.sdk.distributedcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; import io.opentelemetry.distributedcontext.DistributedContext; import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.unsafe.ContextUtils; +import io.opentelemetry.distributedcontext.propagation.ContextUtils; /** * {@link DistributedContextManagerSdk} is SDK implementation of {@link DistributedContextManager}. @@ -43,16 +40,4 @@ public DistributedContext.Builder contextBuilder() { public Scope withContext(DistributedContext distContext) { return ContextUtils.withDistributedContext(distContext); } - - @Override - public BinaryFormat getBinaryFormat() { - // TODO: Implement this. - return DefaultDistributedContextManager.getInstance().getBinaryFormat(); - } - - @Override - public HttpTextFormat getHttpTextFormat() { - // TODO: Implement this. - return DefaultDistributedContextManager.getInstance().getHttpTextFormat(); - } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index c152f3a3457..f9d69d4a16b 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -33,7 +33,7 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.Tracestate; -import io.opentelemetry.trace.unsafe.ContextUtils; +import io.opentelemetry.trace.propagation.ContextUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -239,7 +239,7 @@ private static Clock getClock(Span parent, Clock clock) { @Nullable private static SpanContext parent( ParentType parentType, Span explicitParent, SpanContext remoteParent) { - Span currentSpan = ContextUtils.getValue(); + Span currentSpan = ContextUtils.getSpan(); switch (parentType) { case NO_PARENT: return null; @@ -257,7 +257,7 @@ private static SpanContext parent( private static Span parentSpan(ParentType parentType, Span explicitParent) { switch (parentType) { case CURRENT_SPAN: - return ContextUtils.getValue(); + return ContextUtils.getSpan(); case EXPLICIT_PARENT: return explicitParent; default: diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index 2257b7f31c4..2962cf3bcd6 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -17,21 +17,14 @@ package io.opentelemetry.sdk.trace; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; -import io.opentelemetry.trace.propagation.BinaryTraceContext; -import io.opentelemetry.trace.propagation.HttpTraceContext; -import io.opentelemetry.trace.unsafe.ContextUtils; +import io.opentelemetry.trace.propagation.ContextUtils; /** {@link TracerSdk} is SDK implementation of {@link Tracer}. */ public class TracerSdk implements Tracer { - private static final BinaryFormat BINARY_FORMAT = new BinaryTraceContext(); - private static final HttpTextFormat HTTP_TEXT_FORMAT = new HttpTraceContext(); private final TracerSharedState sharedState; private final InstrumentationLibraryInfo instrumentationLibraryInfo; @@ -42,12 +35,12 @@ public class TracerSdk implements Tracer { @Override public Span getCurrentSpan() { - return ContextUtils.getValue(); + return ContextUtils.getSpan(); } @Override public Scope withSpan(Span span) { - return ContextUtils.withSpan(span); + return ContextUtils.withScopedSpan(span); } @Override @@ -65,16 +58,6 @@ public Span.Builder spanBuilder(String spanName) { sharedState.getClock()); } - @Override - public BinaryFormat getBinaryFormat() { - return BINARY_FORMAT; - } - - @Override - public HttpTextFormat getHttpTextFormat() { - return HTTP_TEXT_FORMAT; - } - /** * Returns the instrumentation library specified when creating the tracer using {@link * TracerSdkFactory}. diff --git a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java index 84f55103228..16a07862358 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java @@ -22,7 +22,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.distributedcontext.DistributedContext; import io.opentelemetry.distributedcontext.EmptyDistributedContext; -import io.opentelemetry.distributedcontext.unsafe.ContextUtils; +import io.opentelemetry.distributedcontext.propagation.ContextUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index 17e710d40d9..9df74f36cb3 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -23,9 +23,7 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.propagation.BinaryTraceContext; -import io.opentelemetry.trace.propagation.HttpTraceContext; -import io.opentelemetry.trace.unsafe.ContextUtils; +import io.opentelemetry.trace.propagation.ContextUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -65,20 +63,10 @@ public void defaultSpanBuilder() { assertThat(tracer.spanBuilder(SPAN_NAME)).isInstanceOf(SpanBuilderSdk.class); } - @Test - public void defaultHttpTextFormat() { - assertThat(tracer.getHttpTextFormat()).isInstanceOf(HttpTraceContext.class); - } - - @Test - public void defaultBinaryFormat() { - assertThat(tracer.getBinaryFormat()).isInstanceOf(BinaryTraceContext.class); - } - @Test public void getCurrentSpan() { assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - Context origContext = ContextUtils.withValue(span).attach(); + Context origContext = ContextUtils.withSpan(span).attach(); // Make sure context is detached even if test fails. try { assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span); From 9160417dd10eaccc2a6cfa3a3d3ba417f581fda9 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 17 Dec 2019 02:33:01 -0600 Subject: [PATCH 04/42] Make the OT shim compile/pass. --- .../opentracingshim/BaseShimObject.java | 5 ++ .../opentracingshim/Propagation.java | 46 ++++++++----------- .../opentracingshim/TelemetryInfo.java | 9 +++- .../opentracingshim/TraceShim.java | 7 ++- .../opentracingshim/TracerShim.java | 9 ++-- .../opentracingshim/SpanBuilderShimTest.java | 3 +- .../opentracingshim/SpanShimTest.java | 3 +- .../opentracingshim/TracerShimTest.java | 3 +- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java index f2ba0b0daff..1136ef008bd 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java @@ -16,6 +16,7 @@ package io.opentelemetry.opentracingshim; +import io.opentelemetry.context.propagation.Propagators; import io.opentelemetry.distributedcontext.DistributedContextManager; import io.opentelemetry.trace.Tracer; @@ -41,4 +42,8 @@ DistributedContextManager contextManager() { SpanContextShimTable spanContextTable() { return telemetryInfo.spanContextTable(); } + + Propagators propagators() { + return telemetryInfo.propagators(); + } } 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 3cb18ca0b94..e6499f27974 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -16,11 +16,10 @@ package io.opentelemetry.opentracingshim; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentracing.propagation.Binary; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; -import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; @@ -30,12 +29,15 @@ final class Propagation extends BaseShimObject { } public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { - tracer() - .getHttpTextFormat() - .inject(contextShim.getSpanContext(), carrier, TextMapSetter.INSTANCE); - contextManager() - .getHttpTextFormat() - .inject(contextShim.getDistributedContext(), carrier, TextMapSetter.INSTANCE); + Context context = Context.current(); + context = + io.opentelemetry.trace.propagation.ContextUtils.withSpanContext( + contextShim.getSpanContext(), context); + context = + io.opentelemetry.distributedcontext.propagation.ContextUtils.withValue( + contextShim.getDistributedContext(), context); + + propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } public SpanContextShim extractTextFormat(TextMapExtract carrier) { @@ -44,12 +46,15 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { carrierMap.put(entry.getKey(), entry.getValue()); } - io.opentelemetry.trace.SpanContext context = - tracer().getHttpTextFormat().extract(carrierMap, TextMapGetter.INSTANCE); - io.opentelemetry.distributedcontext.DistributedContext distContext = - contextManager().getHttpTextFormat().extract(carrierMap, TextMapGetter.INSTANCE); + Context context = + propagators() + .getHttpTextFormat() + .extract(Context.current(), carrierMap, TextMapGetter.INSTANCE); - return new SpanContextShim(telemetryInfo, context, distContext); + return new SpanContextShim( + telemetryInfo, + io.opentelemetry.trace.propagation.ContextUtils.getSpanContext(context), + io.opentelemetry.distributedcontext.propagation.ContextUtils.getValue(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { @@ -75,19 +80,4 @@ public String get(Map carrier, String key) { return carrier.get(key); } } - - public void injectBinaryFormat(SpanContextShim context, Binary carrier) { - byte[] contextBuff = tracer().getBinaryFormat().toByteArray(context.getSpanContext()); - ByteBuffer byteBuff = carrier.injectionBuffer(contextBuff.length); - byteBuff.put(contextBuff); - } - - public SpanContextShim extractBinaryFormat(Binary carrier) { - - ByteBuffer byteBuff = carrier.extractionBuffer(); - byte[] buff = new byte[byteBuff.remaining()]; - byteBuff.get(buff); - - return new SpanContextShim(telemetryInfo, tracer().getBinaryFormat().fromByteArray(buff)); - } } diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java index 3e739380035..b892a1564b2 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java @@ -16,6 +16,7 @@ package io.opentelemetry.opentracingshim; +import io.opentelemetry.context.propagation.Propagators; import io.opentelemetry.distributedcontext.DistributedContext; import io.opentelemetry.distributedcontext.DistributedContextManager; import io.opentelemetry.trace.Tracer; @@ -28,11 +29,13 @@ final class TelemetryInfo { private final Tracer tracer; private final DistributedContextManager contextManager; private final DistributedContext emptyDistributedContext; + private final Propagators propagators; private final SpanContextShimTable spanContextTable; - TelemetryInfo(Tracer tracer, DistributedContextManager contextManager) { + TelemetryInfo(Tracer tracer, DistributedContextManager contextManager, Propagators propagators) { this.tracer = tracer; this.contextManager = contextManager; + this.propagators = propagators; this.emptyDistributedContext = contextManager.contextBuilder().build(); this.spanContextTable = new SpanContextShimTable(); } @@ -52,4 +55,8 @@ SpanContextShimTable spanContextTable() { DistributedContext emptyDistributedContext() { return emptyDistributedContext; } + + Propagators propagators() { + return propagators; + } } diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java index 510e868b8fc..c40960753db 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java @@ -36,7 +36,8 @@ public static io.opentracing.Tracer createTracerShim() { return new TracerShim( new TelemetryInfo( getTracer(OpenTelemetry.getTracerFactory()), - OpenTelemetry.getDistributedContextManager())); + OpenTelemetry.getDistributedContextManager(), + OpenTelemetry.getPropagators())); } /** @@ -52,7 +53,9 @@ public static io.opentracing.Tracer createTracerShim( TracerFactory tracerFactory, DistributedContextManager contextManager) { Utils.checkNotNull(tracerFactory, "tracer"); Utils.checkNotNull(contextManager, "contextManager"); - return new TracerShim(new TelemetryInfo(getTracer(tracerFactory), contextManager)); + return new TracerShim( + new TelemetryInfo( + getTracer(tracerFactory), contextManager, OpenTelemetry.getPropagators())); } private static Tracer getTracer(TracerFactory tracerFactory) { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java index ef410ccac11..e682545b5d0 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java @@ -21,7 +21,6 @@ import io.opentracing.Span; import io.opentracing.SpanContext; import io.opentracing.Tracer; -import io.opentracing.propagation.Binary; import io.opentracing.propagation.Format; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; @@ -73,8 +72,8 @@ public void inject(SpanContext context, Format format, C carrier) { || format == Format.Builtin.TEXT_MAP_INJECT || format == Format.Builtin.HTTP_HEADERS) { propagation.injectTextFormat(contextShim, (TextMapInject) carrier); - } else if (format == Format.Builtin.BINARY) { - propagation.injectBinaryFormat(contextShim, (Binary) carrier); + } else { + logger.log(Level.WARNING, "Binary format is not implemented."); } } @@ -86,8 +85,8 @@ public SpanContext extract(Format format, C carrier) { || format == Format.Builtin.TEXT_MAP_EXTRACT || format == Format.Builtin.HTTP_HEADERS) { return propagation.extractTextFormat((TextMapExtract) carrier); - } else if (format == Format.Builtin.BINARY) { - return propagation.extractBinaryFormat((Binary) carrier); + } else { + logger.log(Level.WARNING, "Binary format is not implemented."); } } catch (Exception e) { logger.log( diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java index 8b8b25c1bc2..04e631b518a 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import io.opentelemetry.OpenTelemetry; import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; import io.opentelemetry.sdk.trace.TracerSdkFactory; import io.opentelemetry.trace.Tracer; @@ -29,7 +30,7 @@ public class SpanBuilderShimTest { private final TracerSdkFactory tracerSdkFactory = TracerSdkFactory.create(); private final Tracer tracer = tracerSdkFactory.get("SpanShimTest"); private final TelemetryInfo telemetryInfo = - new TelemetryInfo(tracer, new DistributedContextManagerSdk()); + new TelemetryInfo(tracer, new DistributedContextManagerSdk(), OpenTelemetry.getPropagators()); private static final String SPAN_NAME = "Span"; 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 41178787311..722904a6926 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import io.opentelemetry.OpenTelemetry; import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; import io.opentelemetry.sdk.trace.TracerSdkFactory; import io.opentelemetry.trace.Tracer; @@ -35,7 +36,7 @@ public class SpanShimTest { private final TracerSdkFactory tracerSdkFactory = TracerSdkFactory.create(); private final Tracer tracer = tracerSdkFactory.get("SpanShimTest"); private final TelemetryInfo telemetryInfo = - new TelemetryInfo(tracer, new DistributedContextManagerSdk()); + new TelemetryInfo(tracer, new DistributedContextManagerSdk(), OpenTelemetry.getPropagators()); private io.opentelemetry.trace.Span span; private static final String SPAN_NAME = "Span"; 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 d7b7d4e0bf7..834b1ce4e0c 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java @@ -41,7 +41,8 @@ public void setUp() { new TracerShim( new TelemetryInfo( OpenTelemetry.getTracerFactory().get("opentracingshim"), - OpenTelemetry.getDistributedContextManager())); + OpenTelemetry.getDistributedContextManager(), + OpenTelemetry.getPropagators())); } @Test From b97d7002df490ff8f35b322a7b090e3e05c6abee Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 17 Dec 2019 16:35:27 -0600 Subject: [PATCH 05/42] Make contrib components compile/pass. --- .../opentelemetry/contrib/trace/CurrentSpanUtils.java | 6 +++--- .../contrib/trace/CurrentSpanUtilsTest.java | 4 ++-- .../contrib/trace/testbed/clientserver/Client.java | 6 ++++-- .../contrib/trace/testbed/clientserver/Server.java | 11 ++++++++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java index 52754b26ea2..6fa2e22f89b 100644 --- a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java +++ b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java @@ -19,7 +19,7 @@ import io.grpc.Context; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.unsafe.ContextUtils; +import io.opentelemetry.trace.propagation.ContextUtils; import java.util.concurrent.Callable; /** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */ @@ -65,7 +65,7 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) { @Override public void run() { - Context origContext = ContextUtils.withValue(span).attach(); + Context origContext = ContextUtils.withSpan(span).attach(); try { runnable.run(); } catch (Throwable t) { @@ -98,7 +98,7 @@ private CallableInSpan(Span span, Callable callable, boolean endSpan) { @Override public V call() throws Exception { - Context origContext = ContextUtils.withValue(span).attach(); + Context origContext = ContextUtils.withSpan(span).attach(); try { return callable.call(); } catch (Exception e) { diff --git a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java index ffbd78b3569..0b766528d1e 100644 --- a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java +++ b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java @@ -24,7 +24,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.unsafe.ContextUtils; +import io.opentelemetry.trace.propagation.ContextUtils; import java.util.concurrent.Callable; import org.junit.Before; import org.junit.Test; @@ -250,6 +250,6 @@ public Object call() throws Exception { } private static Span getCurrentSpan() { - return ContextUtils.getValue(Context.current()); + return ContextUtils.getSpan(Context.current()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index 2e842419070..cd7dd27dda2 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -16,6 +16,8 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; +import io.grpc.Context; +import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.Span; @@ -40,10 +42,10 @@ public void send() throws InterruptedException { span.setAttribute("component", "example-client"); try (Scope ignored = tracer.withSpan(span)) { - tracer + OpenTelemetry.getPropagators() .getHttpTextFormat() .inject( - span.getContext(), + Context.current(), message, new Setter() { @Override diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index c4777b152ff..4ac1e126d4f 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -16,12 +16,15 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; +import io.grpc.Context; +import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; +import io.opentelemetry.trace.propagation.ContextUtils; import java.util.concurrent.ArrayBlockingQueue; import javax.annotation.Nullable; @@ -36,10 +39,11 @@ public Server(ArrayBlockingQueue queue, Tracer tracer) { } private void process(Message message) { - SpanContext context = - tracer + Context context = + OpenTelemetry.getPropagators() .getHttpTextFormat() .extract( + Context.current(), message, new Getter() { @Nullable @@ -48,8 +52,9 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); + SpanContext spanContext = ContextUtils.getSpanContext(context); Span span = - tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(context).startSpan(); + tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); try (Scope ignored = tracer.withSpan(span)) { From 0b109f92d5092dc499349035285f96edbf5937b2 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Wed, 18 Dec 2019 00:18:44 -0600 Subject: [PATCH 06/42] Improvement over Span/SpanContext handling in Context. --- .../io/opentelemetry/trace/SpanContext.java | 2 +- .../trace/propagation/ContextUtils.java | 28 ++++++--- .../trace/propagation/HttpTraceContext.java | 4 -- .../trace/propagation/ContextUtilsTest.java | 58 +++++++++++++++++++ .../propagation/HttpTraceContextTest.java | 42 ++++++++++++++ 5 files changed, 121 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/SpanContext.java b/api/src/main/java/io/opentelemetry/trace/SpanContext.java index 0e2c9063418..ed1c34892b9 100644 --- a/api/src/main/java/io/opentelemetry/trace/SpanContext.java +++ b/api/src/main/java/io/opentelemetry/trace/SpanContext.java @@ -50,7 +50,7 @@ public final class SpanContext { * * @return the invalid {@code SpanContext}. */ - static SpanContext getInvalid() { + public static SpanContext getInvalid() { return INVALID; } diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java index 755a322b754..5f9f34abcf0 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java @@ -37,7 +37,8 @@ public final class ContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); private static final Context.Key CONTEXT_SPANCONTEXT_KEY = - Context.key("opentelemetry-trace-spancontext-key"); + Context.keyWithDefault( + "opentelemetry-trace-spancontext-key", SpanContext.getInvalid()); /** * Creates a new {@code Context} with the given value set. @@ -59,7 +60,7 @@ public static Context withSpan(Span span) { * @since 0.1.0 */ public static Context withSpan(Span span, Context context) { - return context.withValue(CONTEXT_SPAN_KEY, span).withValue(CONTEXT_SPANCONTEXT_KEY, null); + return context.withValue(CONTEXT_SPAN_KEY, span); } /** @@ -82,15 +83,13 @@ public static Context withSpanContext(SpanContext spanContext) { * @since 0.3.0 */ public static Context withSpanContext(SpanContext spanContext, Context context) { - return context - .withValue(CONTEXT_SPAN_KEY, null) - .withValue(CONTEXT_SPANCONTEXT_KEY, spanContext); + return context.withValue(CONTEXT_SPANCONTEXT_KEY, spanContext); } /** - * Returns the value from the current {@code Context}. + * Returns the {@code Span} from the current {@code Context}. * - * @return the value from the specified {@code Context}. + * @return the value from the current {@code Context}. * @since 0.1.0 */ public static Span getSpan() { @@ -98,7 +97,7 @@ public static Span getSpan() { } /** - * Returns the value from the specified {@code Context}. + * Returns the {@code Span} from the specified {@code Context}. * * @param context the specified {@code Context}. * @return the value from the specified {@code Context}. @@ -108,10 +107,23 @@ public static Span getSpan(Context context) { return CONTEXT_SPAN_KEY.get(context); } + /** + * Returns the {@link SpanContext} from the current {@code Context}. + * + * @return the value from the current {@code Context}. + * @since 0.3.0 + */ public static SpanContext getSpanContext() { return CONTEXT_SPANCONTEXT_KEY.get(); } + /** + * Returns the {@link SpanContext} from the specified {@code Context}. + * + * @param context the specified {@code Context}. + * @return the value from the specified {@code Context}. + * @since 0.3.0 + */ public static SpanContext getSpanContext(Context context) { return CONTEXT_SPANCONTEXT_KEY.get(context); } 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 6083ad2a085..b77d23cb8ed 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -86,10 +86,6 @@ public void inject(Context context, C carrier, Setter setter) { spanContext = span.getContext(); } - if (spanContext == null) { - return; - } - injectImpl(spanContext, carrier, setter); } diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java index aaf6aaf7de1..e4eb8231bdf 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java @@ -19,8 +19,14 @@ import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; +import io.opentelemetry.OpenTelemetry; 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 org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -65,4 +71,56 @@ public void testGetCurrentSpan_ContextSetToNull_WithoutExplicitContext() { Context.current().detach(orig); } } + + @Test + public void testGetCurrentSpanContext_DefaultContext() { + SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); + assertThat(spanContext).isNotNull(); + assertThat(spanContext).isSameInstanceAs(SpanContext.getInvalid()); + } + + @Test + public void testGetCurrentSpanContext_DefaultContext_WithoutExplicitContext() { + SpanContext spanContext = ContextUtils.getSpanContext(); + assertThat(spanContext).isNotNull(); + assertThat(spanContext).isSameInstanceAs(SpanContext.getInvalid()); + } + + @Test + public void testGetCurrentSpanContext_ContextSetToNull() { + Context orig = ContextUtils.withSpanContext(null, Context.current()).attach(); + try { + SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); + assertThat(spanContext).isNotNull(); + assertThat(spanContext).isSameInstanceAs(SpanContext.getInvalid()); + } finally { + Context.current().detach(orig); + } + } + + @Test + public void testSetSpanAndSpanContext() { + Span span = OpenTelemetry.getTracerFactory().get(null).spanBuilder("testSpan").startSpan(); + SpanContext spanContext = + SpanContext.create( + new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()); + + Context context1 = ContextUtils.withSpan(span).attach(); + try { + Context context2 = ContextUtils.withSpanContext(spanContext).attach(); + try { + SpanContext spanContextInContext = ContextUtils.getSpanContext(Context.current()); + assertThat(spanContextInContext).isNotNull(); + assertThat(spanContextInContext).isSameInstanceAs(spanContext); + + Span spanInContext = ContextUtils.getSpan(Context.current()); + assertThat(spanInContext).isNotNull(); + assertThat(spanInContext).isSameInstanceAs(span); + } finally { + Context.current().detach(context2); + } + } finally { + Context.current().detach(context1); + } + } } 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 8b0fddeab1b..fbe7eb22a19 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -21,8 +21,10 @@ import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACESTATE; import io.grpc.Context; +import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceFlags; @@ -123,6 +125,46 @@ public void inject_NotSampledContext_WithTraceState() { TRACESTATE_NOT_DEFAULT_ENCODING); } + @Test + public void inject_Span() { + // Use non-default values to verify that the Span is actually consumed. + Map carrier = new LinkedHashMap(); + Span span = + OpenTelemetry.getTracerFactory() + .get(null) + .spanBuilder("testSpan") + .setParent( + SpanContext.create( + TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)) + .startSpan(); + Context context = ContextUtils.withSpan(span); + httpTraceContext.inject(context, carrier, setter); + assertThat(carrier) + .containsExactly( + TRACEPARENT, TRACEPARENT_HEADER_SAMPLED, TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); + } + + @Test + public void inject_Span_SpanContextPresent() { + // Span has higher priority than SpanContext. + Map carrier = new LinkedHashMap(); + Span span = + OpenTelemetry.getTracerFactory() + .get(null) + .spanBuilder("testSpan") + .setParent( + SpanContext.create( + TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)) + .startSpan(); + SpanContext spanContext = + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT); + Context context = ContextUtils.withSpanContext(spanContext, ContextUtils.withSpan(span)); + httpTraceContext.inject(context, carrier, setter); + assertThat(carrier) + .containsExactly( + TRACEPARENT, TRACEPARENT_HEADER_SAMPLED, TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); + } + @Test public void extract_SampledContext() { Map carrier = new LinkedHashMap(); From 51732a9b982a80413cdf44ce24d00559256e1f8c Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 11:44:15 -0600 Subject: [PATCH 07/42] Add Span.setParent(Context) overload. --- .../io/opentelemetry/trace/DefaultTracer.java | 29 +++-- .../java/io/opentelemetry/trace/Span.java | 20 +++ .../io/opentelemetry/trace/SpanContext.java | 2 +- .../trace/propagation/ContextUtils.java | 2 +- .../trace/propagation/HttpTraceContext.java | 24 +++- .../trace/DefaultTracerTest.java | 56 +++++++++ .../trace/propagation/ContextUtilsTest.java | 6 +- .../propagation/HttpTraceContextTest.java | 7 ++ .../sdk/trace/SpanBuilderSdk.java | 40 ++++-- .../sdk/trace/SpanBuilderSdkTest.java | 118 ++++++++++++++++++ 10 files changed, 279 insertions(+), 25 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index cc47549cc53..72274ea47df 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -16,6 +16,7 @@ package io.opentelemetry.trace; +import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; import io.opentelemetry.trace.propagation.ContextUtils; @@ -53,25 +54,24 @@ public Scope withSpan(Span span) { @Override public Span.Builder spanBuilder(String spanName) { - return NoopSpanBuilder.create(this, spanName); + return NoopSpanBuilder.create(spanName); } private DefaultTracer() {} // Noop implementation of Span.Builder. private static final class NoopSpanBuilder implements Span.Builder { - static NoopSpanBuilder create(Tracer tracer, String spanName) { - return new NoopSpanBuilder(tracer, spanName); + static NoopSpanBuilder create(String spanName) { + return new NoopSpanBuilder(spanName); } - private final Tracer tracer; private boolean isRootSpan; private SpanContext spanContext; @Override public Span startSpan() { if (spanContext == null && !isRootSpan) { - spanContext = tracer.getCurrentSpan().getContext(); + spanContext = getAnySpanContext(Context.current()); } return spanContext != null && !SpanContext.getInvalid().equals(spanContext) @@ -93,6 +93,22 @@ public NoopSpanBuilder setParent(SpanContext remoteParent) { return this; } + @Override + public NoopSpanBuilder setParent(Context context) { + Utils.checkNotNull(context, "context"); + spanContext = getAnySpanContext(context); + return this; + } + + private static SpanContext getAnySpanContext(Context context) { + Span span = ContextUtils.getSpan(context); + if (span != DefaultSpan.getInvalid()) { + return span.getContext(); + } + + return ContextUtils.getSpanContext(context); + } + @Override public NoopSpanBuilder setNoParent() { isRootSpan = true; @@ -158,9 +174,8 @@ public NoopSpanBuilder setStartTimestamp(long startTimestamp) { return this; } - private NoopSpanBuilder(Tracer tracer, String name) { + private NoopSpanBuilder(String name) { Utils.checkNotNull(name, "name"); - this.tracer = tracer; } } } diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 7dc79cbe9e5..d0314d9d925 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -16,6 +16,7 @@ package io.opentelemetry.trace; +import io.grpc.Context; import java.util.Map; import javax.annotation.concurrent.ThreadSafe; @@ -399,6 +400,25 @@ interface Builder { */ Builder setParent(SpanContext remoteParent); + /** + * Sets the parent to use from the specified {@code Context}. If not set, the {@link Span} or + * {@link SpanContext} in {@code Context.current()} at {@link #startSpan()} time will be used as + * parent. + * + *

If no {@link Span} nor {@link SpanContext} is availablle in the specified {@code Context}, + * the resulting {@code Span} will be a root instance, as if {@link #setNoParent()} had been + * called. + * + *

If called multiple times, only the last specified value will be used. Observe that the + * state defined by a previous call to {@link #setNoParent()} will be discarded. + * + * @param context the {@code Context}. + * @return this. + * @throws NullPointerException if {@code context} is {@code null}. + * @since 0.3.0 + */ + Builder setParent(Context context); + /** * Sets the option to become a root {@code Span} for a new trace. If not set, the value of * {@code Tracer.getCurrentSpan()} at {@link #startSpan()} time will be used as parent. diff --git a/api/src/main/java/io/opentelemetry/trace/SpanContext.java b/api/src/main/java/io/opentelemetry/trace/SpanContext.java index ed1c34892b9..0e2c9063418 100644 --- a/api/src/main/java/io/opentelemetry/trace/SpanContext.java +++ b/api/src/main/java/io/opentelemetry/trace/SpanContext.java @@ -50,7 +50,7 @@ public final class SpanContext { * * @return the invalid {@code SpanContext}. */ - public static SpanContext getInvalid() { + static SpanContext getInvalid() { return INVALID; } diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java index 5f9f34abcf0..5c7ee045cd2 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java @@ -38,7 +38,7 @@ public final class ContextUtils { Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); private static final Context.Key CONTEXT_SPANCONTEXT_KEY = Context.keyWithDefault( - "opentelemetry-trace-spancontext-key", SpanContext.getInvalid()); + "opentelemetry-trace-spancontext-key", DefaultSpan.getInvalid().getContext()); /** * Creates a new {@code Context} with the given value set. 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 b77d23cb8ed..4fb1a35846d 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -32,6 +32,7 @@ import java.util.Collections; import java.util.List; import java.util.regex.Pattern; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -77,16 +78,27 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(setter, "setter"); checkNotNull(carrier, "carrier"); - SpanContext spanContext = null; + SpanContext spanContext = getSpanContext(context); + if (spanContext == null) { + return; + } + + injectImpl(spanContext, carrier, setter); + } + + @Nullable + private static SpanContext getSpanContext(Context context) { Span span = ContextUtils.getSpan(context); + if (!DefaultSpan.getInvalid().equals(span)) { + return span.getContext(); + } - if (DefaultSpan.getInvalid().equals(span)) { // No value is set. - spanContext = ContextUtils.getSpanContext(context); - } else { - spanContext = span.getContext(); + SpanContext spanContext = ContextUtils.getSpanContext(context); + if (!DefaultSpan.getInvalid().getContext().equals(spanContext)) { + return spanContext; } - injectImpl(spanContext, carrier, setter); + return null; } private static void injectImpl(SpanContext spanContext, C carrier, Setter setter) { diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index a4cc46702a3..667ffc143ff 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -18,7 +18,9 @@ import static com.google.common.truth.Truth.assertThat; +import io.grpc.Context; import io.opentelemetry.context.Scope; +import io.opentelemetry.trace.propagation.ContextUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -106,6 +108,27 @@ public void testSpanContextPropagation() { assertThat(span.getContext()).isSameInstanceAs(spanContext); } + @Test + public void testSpanContextPropagationFromSpanInContext() { + Context context = + ContextUtils.withSpanContext( + SpanContext.create( + new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()), + ContextUtils.withSpan(new DefaultSpan(spanContext))); + + // Span in Context has higher priority than SpanContext. + Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); + assertThat(span.getContext()).isSameInstanceAs(spanContext); + } + + @Test + public void testSpanContextPropagationFromContext() { + Context context = ContextUtils.withSpanContext(spanContext); + + Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); + assertThat(span.getContext()).isSameInstanceAs(spanContext); + } + @Test public void testSpanContextPropagationCurrentSpan() { DefaultSpan parent = new DefaultSpan(spanContext); @@ -117,4 +140,37 @@ public void testSpanContextPropagationCurrentSpan() { scope.close(); } } + + @Test + public void testSpanContextPropagationCurrentSpanContext() { + // TODO - Have an utility to create a Scope-d Context. + Context orig = ContextUtils.withSpanContext(spanContext).attach(); + try { + Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); + assertThat(span.getContext()).isSameInstanceAs(spanContext); + } finally { + Context.current().detach(orig); + } + } + + @Test + public void testSpanContextPropagationCurrentContextValues() { + // TODO - Have an utility to create a Scope-d Context. + Context orig = + ContextUtils.withSpanContext( + SpanContext.create( + new TraceId(1, 1), + new SpanId(1), + TraceFlags.getDefault(), + Tracestate.getDefault()), + ContextUtils.withSpan(new DefaultSpan(spanContext))) + .attach(); + // Span in Context has higher priority than SpanContext. + try { + Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); + assertThat(span.getContext()).isSameInstanceAs(spanContext); + } finally { + Context.current().detach(orig); + } + } } diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java index e4eb8231bdf..6090e510452 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java @@ -76,14 +76,14 @@ public void testGetCurrentSpan_ContextSetToNull_WithoutExplicitContext() { public void testGetCurrentSpanContext_DefaultContext() { SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(SpanContext.getInvalid()); + assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); } @Test public void testGetCurrentSpanContext_DefaultContext_WithoutExplicitContext() { SpanContext spanContext = ContextUtils.getSpanContext(); assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(SpanContext.getInvalid()); + assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); } @Test @@ -92,7 +92,7 @@ public void testGetCurrentSpanContext_ContextSetToNull() { try { SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(SpanContext.getInvalid()); + assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); } finally { Context.current().detach(orig); } 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 fbe7eb22a19..509533ec3dd 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -78,6 +78,13 @@ public String get(Map carrier, String key) { private final HttpTraceContext httpTraceContext = new HttpTraceContext(); @Rule public ExpectedException thrown = ExpectedException.none(); + @Test + public void inject_Nothing() { + Map carrier = new LinkedHashMap(); + httpTraceContext.inject(Context.current(), carrier, setter); + assertThat(carrier).hasSize(0); + } + @Test public void inject_SampledContext() { Map carrier = new LinkedHashMap(); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index f9d69d4a16b..8651889b728 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.trace; +import io.grpc.Context; import io.opentelemetry.internal.Utils; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -60,7 +61,7 @@ class SpanBuilderSdk implements Span.Builder { private Kind spanKind = Kind.INTERNAL; private final AttributesWithCapacity attributes; private List links; - private ParentType parentType = ParentType.CURRENT_SPAN; + private ParentType parentType = ParentType.CURRENT_CONTEXT; private long startEpochNanos = 0; SpanBuilderSdk( @@ -98,6 +99,22 @@ public Span.Builder setParent(SpanContext remoteParent) { return this; } + @Override + public Span.Builder setParent(Context context) { + Utils.checkNotNull(context, "context"); + + Span span = ContextUtils.getSpan(context); + SpanContext spanContext = ContextUtils.getSpanContext(context); + if (!DefaultSpan.getInvalid().equals(span)) { + setParent(span); + } else if (!DefaultSpan.getInvalid().getContext().equals(spanContext)) { + setParent(spanContext); + } else { + setNoParent(); + } + return this; + } + @Override public Span.Builder setNoParent() { this.parentType = ParentType.NO_PARENT; @@ -239,12 +256,11 @@ private static Clock getClock(Span parent, Clock clock) { @Nullable private static SpanContext parent( ParentType parentType, Span explicitParent, SpanContext remoteParent) { - Span currentSpan = ContextUtils.getSpan(); switch (parentType) { case NO_PARENT: return null; - case CURRENT_SPAN: - return currentSpan != null ? currentSpan.getContext() : null; + case CURRENT_CONTEXT: + return getAnySpanContext(Context.current()); case EXPLICIT_PARENT: return explicitParent.getContext(); case EXPLICIT_REMOTE_PARENT: @@ -256,8 +272,9 @@ private static SpanContext parent( @Nullable private static Span parentSpan(ParentType parentType, Span explicitParent) { switch (parentType) { - case CURRENT_SPAN: - return ContextUtils.getSpan(); + case CURRENT_CONTEXT: + Span span = ContextUtils.getSpan(); + return DefaultSpan.getInvalid().equals(span) ? null : span; case EXPLICIT_PARENT: return explicitParent; default: @@ -265,8 +282,17 @@ private static Span parentSpan(ParentType parentType, Span explicitParent) { } } + private static SpanContext getAnySpanContext(Context context) { + Span span = ContextUtils.getSpan(context); + if (!DefaultSpan.getInvalid().equals(span)) { + return span.getContext(); + } + + return ContextUtils.getSpanContext(context); + } + private enum ParentType { - CURRENT_SPAN, + CURRENT_CONTEXT, EXPLICIT_PARENT, EXPLICIT_REMOTE_PARENT, NO_PARENT, diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 191481a007a..1b447afb105 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertFalse; +import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.trace.AttributeValue; @@ -31,6 +32,7 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.Tracestate; +import io.opentelemetry.trace.propagation.ContextUtils; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -392,6 +394,83 @@ public void overrideNoParent_remoteParent() { } } + @Test + public void parentExplicitContext_withSpan() { + Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + Context context = ContextUtils.withSpan(parent); + try { + RecordEventsReadableSpan span = + (RecordEventsReadableSpan) + tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); + try { + assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); + assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + } finally { + span.end(); + } + } finally { + parent.end(); + } + } + + @Test + public void parentExplicitContext_withSpanContext() { + Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + Context context = ContextUtils.withSpanContext(parent.getContext()); + try { + RecordEventsReadableSpan span = + (RecordEventsReadableSpan) + tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); + try { + assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); + assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + } finally { + span.end(); + } + } finally { + parent.end(); + } + } + + @Test + public void parentExplicitContext_withValues() { + Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + Context context = + ContextUtils.withSpanContext(sampledSpanContext, ContextUtils.withSpan(parent)); + try { + RecordEventsReadableSpan span = + (RecordEventsReadableSpan) + tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); + try { + assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); + assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + } finally { + span.end(); + } + } finally { + parent.end(); + } + } + + @Test + public void parentExplicitContext_noValues() { + Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + Context context = Context.current(); + try { + RecordEventsReadableSpan span = + (RecordEventsReadableSpan) + tracerSdk.spanBuilder(SPAN_NAME).setParent(parent).setParent(context).startSpan(); + try { + assertThat(span.getContext().getTraceId()).isNotEqualTo(parent.getContext().getTraceId()); + assertThat(span.getParentSpanId()).isNotEqualTo(parent.getContext().getSpanId()); + } finally { + span.end(); + } + } finally { + parent.end(); + } + } + @Test public void parentCurrentSpan() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); @@ -411,6 +490,45 @@ public void parentCurrentSpan() { } } + @Test + public void parentCurrentSpanContext() { + Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + Context orig = ContextUtils.withSpanContext(parent.getContext()).attach(); + try { + RecordEventsReadableSpan span = + (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + try { + assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); + assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + } finally { + span.end(); + } + } finally { + Context.current().detach(orig); + parent.end(); + } + } + + @Test + public void parentCurrentContextValues() { + Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + Context orig = + ContextUtils.withSpanContext(sampledSpanContext, ContextUtils.withSpan(parent)).attach(); + try { + RecordEventsReadableSpan span = + (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); + try { + assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); + assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + } finally { + span.end(); + } + } finally { + Context.current().detach(orig); + parent.end(); + } + } + @Test public void parent_invalidContext() { Span parent = DefaultSpan.getInvalid(); From d6894d4c8f9977faf8dbf0a8128bf0ec9dec4436 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 14:41:56 -0600 Subject: [PATCH 08/42] Do not provide default values for Span/SpanContext keys in Context. --- .../io/opentelemetry/trace/DefaultTracer.java | 18 ++---- .../trace/propagation/ContextUtils.java | 39 +++++++++++- .../trace/propagation/HttpTraceContext.java | 20 +----- .../trace/propagation/ContextUtilsTest.java | 61 +++++++++++-------- .../contrib/trace/CurrentSpanUtilsTest.java | 2 +- .../sdk/trace/SpanBuilderSdk.java | 15 +---- .../io/opentelemetry/sdk/trace/TracerSdk.java | 3 +- 7 files changed, 86 insertions(+), 72 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 72274ea47df..ddffabed2f9 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -44,7 +44,7 @@ public static Tracer getInstance() { @Override public Span getCurrentSpan() { - return ContextUtils.getSpan(); + return ContextUtils.getSpanWithDefault(Context.current()); } @Override @@ -71,7 +71,7 @@ static NoopSpanBuilder create(String spanName) { @Override public Span startSpan() { if (spanContext == null && !isRootSpan) { - spanContext = getAnySpanContext(Context.current()); + spanContext = ContextUtils.getAnySpanContext(Context.current()); } return spanContext != null && !SpanContext.getInvalid().equals(spanContext) @@ -96,17 +96,11 @@ public NoopSpanBuilder setParent(SpanContext remoteParent) { @Override public NoopSpanBuilder setParent(Context context) { Utils.checkNotNull(context, "context"); - spanContext = getAnySpanContext(context); - return this; - } - - private static SpanContext getAnySpanContext(Context context) { - Span span = ContextUtils.getSpan(context); - if (span != DefaultSpan.getInvalid()) { - return span.getContext(); + spanContext = ContextUtils.getAnySpanContext(context); + if (spanContext == null) { + isRootSpan = true; } - - return ContextUtils.getSpanContext(context); + return this; } @Override diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java index 5c7ee045cd2..1552cc0867c 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java @@ -35,10 +35,10 @@ @Immutable public final class ContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = - Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); + Context.key("opentelemetry-trace-span-key"); private static final Context.Key CONTEXT_SPANCONTEXT_KEY = - Context.keyWithDefault( - "opentelemetry-trace-spancontext-key", DefaultSpan.getInvalid().getContext()); + Context.key("opentelemetry-trace-spancontext-key"); + private static final Span DEFAULT_SPAN = DefaultSpan.getInvalid(); /** * Creates a new {@code Context} with the given value set. @@ -107,6 +107,19 @@ public static Span getSpan(Context context) { return CONTEXT_SPAN_KEY.get(context); } + /** + * Returns the {@code Span} from the specified {@code Context}, falling back to a default, no-op + * {@code Span}. + * + * @param context the specified {@code Context}. + * @return the value from the specified {@code Context}. + * @since 0.3.0 + */ + public static Span getSpanWithDefault(Context context) { + Span span = CONTEXT_SPAN_KEY.get(context); + return span == null ? DEFAULT_SPAN : span; + } + /** * Returns the {@link SpanContext} from the current {@code Context}. * @@ -128,6 +141,26 @@ public static SpanContext getSpanContext(Context context) { return CONTEXT_SPANCONTEXT_KEY.get(context); } + /** + * Returns any {@link SpanContext} from the specified {@code Context}. + * + *

This method tries to get any non-null {@link SpanContext} in {@code Context}, giving higher + * priority to {@code Span#getContext()} and then falling back to {@code 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 + */ + public static SpanContext getAnySpanContext(Context context) { + Span span = ContextUtils.getSpan(context); + if (span != null) { + return span.getContext(); + } + + return ContextUtils.getSpanContext(context); + } + /** * Returns a new {@link Scope} encapsulating the provided {@code Span} added to the current {@code * Context}. 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 4fb1a35846d..4c8db348d6f 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -21,8 +21,6 @@ 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; @@ -32,7 +30,6 @@ import java.util.Collections; import java.util.List; import java.util.regex.Pattern; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -78,7 +75,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(setter, "setter"); checkNotNull(carrier, "carrier"); - SpanContext spanContext = getSpanContext(context); + SpanContext spanContext = ContextUtils.getAnySpanContext(context); if (spanContext == null) { return; } @@ -86,21 +83,6 @@ public void inject(Context context, C carrier, Setter setter) { injectImpl(spanContext, carrier, setter); } - @Nullable - private static SpanContext getSpanContext(Context context) { - Span span = ContextUtils.getSpan(context); - if (!DefaultSpan.getInvalid().equals(span)) { - return span.getContext(); - } - - SpanContext spanContext = ContextUtils.getSpanContext(context); - if (!DefaultSpan.getInvalid().getContext().equals(spanContext)) { - return spanContext; - } - - return null; - } - private static void injectImpl(SpanContext spanContext, C carrier, Setter setter) { char[] chars = new char[TRACEPARENT_HEADER_SIZE]; chars[0] = VERSION.charAt(0); diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java index 6090e510452..597f55dd0cc 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java @@ -37,36 +37,29 @@ public final class ContextUtilsTest { @Test public void testGetCurrentSpan_DefaultContext() { Span span = ContextUtils.getSpan(Context.current()); - assertThat(span).isNotNull(); - assertThat(span).isInstanceOf(DefaultSpan.class); + assertThat(span).isNull(); } @Test public void testGetCurrentSpan_DefaultContext_WithoutExplicitContext() { Span span = ContextUtils.getSpan(); - assertThat(span).isNotNull(); - assertThat(span).isInstanceOf(DefaultSpan.class); + assertThat(span).isNull(); } @Test - public void testGetCurrentSpan_ContextSetToNull() { - Context orig = ContextUtils.withSpan(null, Context.current()).attach(); - try { - Span span = ContextUtils.getSpan(Context.current()); - assertThat(span).isNotNull(); - assertThat(span).isInstanceOf(DefaultSpan.class); - } finally { - Context.current().detach(orig); - } + public void testGetCurrentSpanWithDefault_DefaultContext() { + Span span = ContextUtils.getSpanWithDefault(Context.current()); + assertThat(span).isNotNull(); + assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); } @Test - public void testGetCurrentSpan_ContextSetToNull_WithoutExplicitContext() { - Context orig = ContextUtils.withSpan(null).attach(); + public void testGetCurrentSpanWithDefault_ContextSetToNull() { + Context orig = ContextUtils.withSpan(null, Context.current()).attach(); try { - Span span = ContextUtils.getSpan(Context.current()); + Span span = ContextUtils.getSpanWithDefault(Context.current()); assertThat(span).isNotNull(); - assertThat(span).isInstanceOf(DefaultSpan.class); + assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); } finally { Context.current().detach(orig); } @@ -75,22 +68,38 @@ public void testGetCurrentSpan_ContextSetToNull_WithoutExplicitContext() { @Test public void testGetCurrentSpanContext_DefaultContext() { SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); - assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); + assertThat(spanContext).isNull(); } @Test public void testGetCurrentSpanContext_DefaultContext_WithoutExplicitContext() { SpanContext spanContext = ContextUtils.getSpanContext(); - assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); + assertThat(spanContext).isNull(); } @Test - public void testGetCurrentSpanContext_ContextSetToNull() { - Context orig = ContextUtils.withSpanContext(null, Context.current()).attach(); + public void testAnySpanContext_DefaultContext() { + SpanContext spanContext = ContextUtils.getAnySpanContext(Context.current()); + assertThat(spanContext).isNull(); + } + + @Test + public void testAnySpanContext_SetSpanContext() { + Context orig = ContextUtils.withSpanContext(DefaultSpan.getInvalid().getContext()).attach(); try { - SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); + SpanContext spanContext = ContextUtils.getAnySpanContext(Context.current()); + assertThat(spanContext).isNotNull(); + assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); + } finally { + Context.current().detach(orig); + } + } + + @Test + public void testAnySpanContext_SetSpan() { + Context orig = ContextUtils.withSpan(DefaultSpan.getInvalid()).attach(); + try { + SpanContext spanContext = ContextUtils.getAnySpanContext(Context.current()); assertThat(spanContext).isNotNull(); assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); } finally { @@ -116,6 +125,10 @@ public void testSetSpanAndSpanContext() { Span spanInContext = ContextUtils.getSpan(Context.current()); assertThat(spanInContext).isNotNull(); assertThat(spanInContext).isSameInstanceAs(span); + + SpanContext anySpanContext = ContextUtils.getAnySpanContext(Context.current()); + assertThat(anySpanContext).isNotNull(); + assertThat(anySpanContext).isSameInstanceAs(span.getContext()); } finally { Context.current().detach(context2); } diff --git a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java index 0b766528d1e..d5205fd9b65 100644 --- a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java +++ b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java @@ -250,6 +250,6 @@ public Object call() throws Exception { } private static Span getCurrentSpan() { - return ContextUtils.getSpan(Context.current()); + return ContextUtils.getSpanWithDefault(Context.current()); } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 8651889b728..de2b30dbb87 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -105,9 +105,9 @@ public Span.Builder setParent(Context context) { Span span = ContextUtils.getSpan(context); SpanContext spanContext = ContextUtils.getSpanContext(context); - if (!DefaultSpan.getInvalid().equals(span)) { + if (span != null) { setParent(span); - } else if (!DefaultSpan.getInvalid().getContext().equals(spanContext)) { + } else if (spanContext != null) { setParent(spanContext); } else { setNoParent(); @@ -260,7 +260,7 @@ private static SpanContext parent( case NO_PARENT: return null; case CURRENT_CONTEXT: - return getAnySpanContext(Context.current()); + return ContextUtils.getAnySpanContext(Context.current()); case EXPLICIT_PARENT: return explicitParent.getContext(); case EXPLICIT_REMOTE_PARENT: @@ -282,15 +282,6 @@ private static Span parentSpan(ParentType parentType, Span explicitParent) { } } - private static SpanContext getAnySpanContext(Context context) { - Span span = ContextUtils.getSpan(context); - if (!DefaultSpan.getInvalid().equals(span)) { - return span.getContext(); - } - - return ContextUtils.getSpanContext(context); - } - private enum ParentType { CURRENT_CONTEXT, EXPLICIT_PARENT, diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index 2962cf3bcd6..398eb05466c 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.trace; +import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultTracer; @@ -35,7 +36,7 @@ public class TracerSdk implements Tracer { @Override public Span getCurrentSpan() { - return ContextUtils.getSpan(); + return ContextUtils.getSpanWithDefault(Context.current()); } @Override From d5acde5788746770c328f2dac3e80bd2fc74dc91 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 16:20:13 -0600 Subject: [PATCH 09/42] Improve the Context's active state handling. --- .../context/propagation/ContextUtils.java | 61 +++++++++++++++++++ .../propagation/ContextUtils.java | 3 +- .../DistributedContextInScope.java | 49 --------------- .../trace/propagation/ContextUtils.java | 3 +- .../trace/propagation/SpanInScope.java | 51 ---------------- .../trace/DefaultTracerTest.java | 22 +++---- .../sdk/trace/SpanBuilderSdkTest.java | 12 ++-- 7 files changed, 81 insertions(+), 120 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java delete mode 100644 api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java diff --git a/api/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java new file mode 100644 index 00000000000..c0d7a7b10a1 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java @@ -0,0 +1,61 @@ +/* + * 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.context.propagation; + +import io.grpc.Context; +import io.opentelemetry.context.Scope; +import javax.annotation.concurrent.Immutable; + +/** + * Util methods/functionality to interact with the {@link io.grpc.Context}. + * + *

Users must interact with the current Context via the public APIs and avoid accessing this + * class directly. + * + * @since 0.1.0 + */ +@Immutable +public final class ContextUtils { + /** + * Sets the specified {@code Context} as {@code Context.current()}, returning a {@link Scope} to + * end its active state and restore the previous active {@code Context}. + * + * @param context the {@code Context} to be set as {@code Context.current()}. + * @return the {@link Scope} for the updated {@code Context}. + * @since 0.1.0 + */ + public static Scope withScopedContext(Context context) { + return new ContextInScope(context); + } + + private static final class ContextInScope implements Scope { + private final Context context; + private final Context previous; + + public ContextInScope(Context context) { + this.context = context; + this.previous = context.attach(); + } + + @Override + public void close() { + context.detach(previous); + } + } + + private ContextUtils() {} +} diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java index 38c1e25462c..86182955ca9 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java @@ -90,7 +90,8 @@ public static DistributedContext getValue(Context context) { * @since 0.1.0 */ public static Scope withDistributedContext(DistributedContext distContext) { - return DistributedContextInScope.create(distContext); + Context context = withValue(distContext); + return io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); } private ContextUtils() {} diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java b/api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java deleted file mode 100644 index b1ad03818ca..00000000000 --- a/api/src/main/java/io/opentelemetry/distributedcontext/propagation/DistributedContextInScope.java +++ /dev/null @@ -1,49 +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.distributedcontext.propagation; - -import io.grpc.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.DistributedContext; - -/** - * A scope that manages the {@link Context} for a {@link DistributedContext}. - * - * @since 0.1.0 - */ -final class DistributedContextInScope implements Scope { - private final Context orig; - - private DistributedContextInScope(DistributedContext distContext) { - orig = ContextUtils.withValue(distContext).attach(); - } - - /** - * Constructs a new {@link DistributedContextInScope}. - * - * @param distContext the {@code DistributedContext} to be added to the current {@code Context}. - * @since 0.1.0 - */ - static DistributedContextInScope create(DistributedContext distContext) { - return new DistributedContextInScope(distContext); - } - - @Override - public void close() { - Context.current().detach(orig); - } -} diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java index 1552cc0867c..131c0883917 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java @@ -170,7 +170,8 @@ public static SpanContext getAnySpanContext(Context context) { * @since 0.1.0 */ public static Scope withScopedSpan(Span span) { - return SpanInScope.create(span); + Context context = withSpan(span); + return io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); } private ContextUtils() {} diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java b/api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java deleted file mode 100644 index 385dd4ee755..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/propagation/SpanInScope.java +++ /dev/null @@ -1,51 +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.propagation; - -import io.grpc.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.trace.Span; - -/** - * A scope that manages the {@link Context} for a {@link Span}. - * - * @since 0.1.0 - */ -final class SpanInScope implements Scope { - private final Context previous; - private final Context current; - - private SpanInScope(Span span) { - current = ContextUtils.withSpan(span); - previous = current.attach(); - } - - /** - * Constructs a new {@link SpanInScope}. - * - * @param span the {@code Span} to be added to the current {@code Context}. - * @since 0.1.0 - */ - static SpanInScope create(Span span) { - return new SpanInScope(span); - } - - @Override - public void close() { - current.detach(previous); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 667ffc143ff..43e2f63602f 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -143,34 +143,30 @@ public void testSpanContextPropagationCurrentSpan() { @Test public void testSpanContextPropagationCurrentSpanContext() { - // TODO - Have an utility to create a Scope-d Context. - Context orig = ContextUtils.withSpanContext(spanContext).attach(); + Context context = ContextUtils.withSpanContext(spanContext); + Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); } finally { - Context.current().detach(orig); + scope.close(); } } @Test public void testSpanContextPropagationCurrentContextValues() { - // TODO - Have an utility to create a Scope-d Context. - Context orig = + Context context = ContextUtils.withSpanContext( - SpanContext.create( - new TraceId(1, 1), - new SpanId(1), - TraceFlags.getDefault(), - Tracestate.getDefault()), - ContextUtils.withSpan(new DefaultSpan(spanContext))) - .attach(); + SpanContext.create( + new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()), + ContextUtils.withSpan(new DefaultSpan(spanContext))); + Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); // Span in Context has higher priority than SpanContext. try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); } finally { - Context.current().detach(orig); + scope.close(); } } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 1b447afb105..85d107486c2 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -493,7 +493,8 @@ public void parentCurrentSpan() { @Test public void parentCurrentSpanContext() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context orig = ContextUtils.withSpanContext(parent.getContext()).attach(); + Context context = ContextUtils.withSpanContext(parent.getContext()); + Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); @@ -504,7 +505,7 @@ public void parentCurrentSpanContext() { span.end(); } } finally { - Context.current().detach(orig); + scope.close(); parent.end(); } } @@ -512,8 +513,9 @@ public void parentCurrentSpanContext() { @Test public void parentCurrentContextValues() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context orig = - ContextUtils.withSpanContext(sampledSpanContext, ContextUtils.withSpan(parent)).attach(); + Context context = + ContextUtils.withSpanContext(sampledSpanContext, ContextUtils.withSpan(parent)); + Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); @@ -524,7 +526,7 @@ public void parentCurrentContextValues() { span.end(); } } finally { - Context.current().detach(orig); + scope.close(); parent.end(); } } From 71f04d445a318f1533359e51d2e366f20430d80a Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 17:01:30 -0600 Subject: [PATCH 10/42] Rename DistributedContext to CorrelationContext. --- .../java/io/opentelemetry/OpenTelemetry.java | 24 +++--- .../CorrelationContext.java} | 36 ++++---- .../CorrelationContextManager.java} | 30 +++---- .../DefaultCorrelationContextManager.java} | 46 +++++------ .../EmptyCorrelationContext.java} | 16 ++-- .../Entry.java | 4 +- .../EntryKey.java | 4 +- .../EntryMetadata.java | 2 +- .../EntryValue.java | 2 +- .../package-info.java | 10 +-- .../propagation/ContextUtils.java | 28 +++---- .../CorrelationContextManagerProvider.java} | 18 ++-- .../io/opentelemetry/OpenTelemetryTest.java | 82 +++++++++---------- ...DefaultCorrelationContextManagerTest.java} | 72 ++++++++-------- .../EntryKeyTest.java | 2 +- .../EntryMetadataTest.java | 4 +- .../EntryTest.java | 4 +- .../EntryValueTest.java | 2 +- .../propagation/ContextUtilsTest.java | 12 +-- .../opentracingshim/BaseShimObject.java | 4 +- .../opentracingshim/Propagation.java | 6 +- .../opentracingshim/SpanBuilderShim.java | 8 +- .../opentracingshim/SpanContextShim.java | 22 ++--- .../opentracingshim/SpanContextShimTable.java | 8 +- .../opentracingshim/TelemetryInfo.java | 20 ++--- .../opentracingshim/TraceShim.java | 12 +-- .../opentracingshim/SpanBuilderShimTest.java | 4 +- .../opentracingshim/SpanShimTest.java | 4 +- .../opentracingshim/TraceShimTest.java | 8 +- .../opentracingshim/TracerShimTest.java | 2 +- .../OpenTelemetryInteroperabilityTest.java | 4 +- .../opentracingshim/testbed/TestUtils.java | 4 +- .../opentelemetry/sdk/OpenTelemetrySdk.java | 12 +-- .../CorrelationContextManagerSdk.java} | 22 ++--- ...CorrelationContextManagerSdkProvider.java} | 16 ++-- .../CorrelationContextSdk.java} | 48 +++++------ ...text.spi.CorrelationContextManagerProvider | 1 + ...text.spi.DistributedContextManagerProvider | 1 - .../sdk/OpenTelemetrySdkTest.java | 4 +- ...elationContextManagerSdkProviderTest.java} | 8 +- .../CorrelationContextManagerSdkTest.java} | 32 ++++---- .../CorrelationContextSdkTest.java} | 60 +++++++------- .../CorrelationContextTestUtil.java} | 16 ++-- .../ScopedCorrelationContextTest.java} | 54 ++++++------ 44 files changed, 389 insertions(+), 389 deletions(-) rename api/src/main/java/io/opentelemetry/{distributedcontext/DistributedContext.java => correlationcontext/CorrelationContext.java} (74%) rename api/src/main/java/io/opentelemetry/{distributedcontext/DistributedContextManager.java => correlationcontext/CorrelationContextManager.java} (61%) rename api/src/main/java/io/opentelemetry/{distributedcontext/DefaultDistributedContextManager.java => correlationcontext/DefaultCorrelationContextManager.java} (53%) rename api/src/main/java/io/opentelemetry/{distributedcontext/EmptyDistributedContext.java => correlationcontext/EmptyCorrelationContext.java} (71%) rename api/src/main/java/io/opentelemetry/{distributedcontext => correlationcontext}/Entry.java (94%) rename api/src/main/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryKey.java (95%) rename api/src/main/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryMetadata.java (98%) rename api/src/main/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryValue.java (98%) rename api/src/main/java/io/opentelemetry/{distributedcontext => correlationcontext}/package-info.java (78%) rename api/src/main/java/io/opentelemetry/{distributedcontext => correlationcontext}/propagation/ContextUtils.java (74%) rename api/src/main/java/io/opentelemetry/{distributedcontext/spi/DistributedContextManagerProvider.java => correlationcontext/spi/CorrelationContextManagerProvider.java} (62%) rename api/src/test/java/io/opentelemetry/{distributedcontext/DefaultDistributedContextManagerTest.java => correlationcontext/DefaultCorrelationContextManagerTest.java} (63%) rename api/src/test/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryKeyTest.java (98%) rename api/src/test/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryMetadataTest.java (92%) rename api/src/test/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryTest.java (95%) rename api/src/test/java/io/opentelemetry/{distributedcontext => correlationcontext}/EntryValueTest.java (98%) rename api/src/test/java/io/opentelemetry/{distributedcontext => correlationcontext}/propagation/ContextUtilsTest.java (83%) rename sdk/src/main/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextManagerSdk.java => correlationcontext/CorrelationContextManagerSdk.java} (53%) rename sdk/src/main/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextManagerSdkProvider.java => correlationcontext/CorrelationContextManagerSdkProvider.java} (62%) rename sdk/src/main/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextSdk.java => correlationcontext/CorrelationContextSdk.java} (72%) create mode 100644 sdk/src/main/resources/META-INF/services/io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider delete mode 100644 sdk/src/main/resources/META-INF/services/io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider rename sdk/src/test/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextManagerSdkProviderTest.java => correlationcontext/CorrelationContextManagerSdkProviderTest.java} (78%) rename sdk/src/test/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextManagerSdkTest.java => correlationcontext/CorrelationContextManagerSdkTest.java} (72%) rename sdk/src/test/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextSdkTest.java => correlationcontext/CorrelationContextSdkTest.java} (72%) rename sdk/src/test/java/io/opentelemetry/sdk/{distributedcontext/DistributedContextTestUtil.java => correlationcontext/CorrelationContextTestUtil.java} (66%) rename sdk/src/test/java/io/opentelemetry/sdk/{distributedcontext/ScopedDistributedContextTest.java => correlationcontext/ScopedCorrelationContextTest.java} (80%) diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 7b8dc69eb9b..515aec416ad 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -18,9 +18,9 @@ import io.opentelemetry.context.propagation.DefaultPropagators; import io.opentelemetry.context.propagation.Propagators; -import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; -import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider; +import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; +import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; import io.opentelemetry.internal.Utils; import io.opentelemetry.metrics.DefaultMeterFactory; import io.opentelemetry.metrics.DefaultMeterFactoryProvider; @@ -39,13 +39,13 @@ /** * This class provides a static global accessor for telemetry objects {@link Tracer}, {@link Meter} - * and {@link DistributedContextManager}. + * and {@link CorrelationContextManager}. * *

The telemetry objects are lazy-loaded singletons resolved via {@link ServiceLoader} mechanism. * * @see TracerFactory * @see MeterFactoryProvider - * @see DistributedContextManagerProvider + * @see CorrelationContextManagerProvider */ @ThreadSafe public final class OpenTelemetry { @@ -54,7 +54,7 @@ public final class OpenTelemetry { private final TracerFactory tracerFactory; private final MeterFactory meterFactory; - private final DistributedContextManager contextManager; + private final CorrelationContextManager contextManager; private volatile Propagators propagators = DefaultPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build(); @@ -84,15 +84,15 @@ public static MeterFactory getMeterFactory() { } /** - * Returns a singleton {@link DistributedContextManager}. + * Returns a singleton {@link CorrelationContextManager}. * * @return registered manager or default via {@link - * DefaultDistributedContextManager#getInstance()}. + * DefaultCorrelationContextManager#getInstance()}. * @throws IllegalStateException if a specified manager (via system properties) could not be * found. * @since 0.1.0 */ - public static DistributedContextManager getDistributedContextManager() { + public static CorrelationContextManager getCorrelationContextManager() { return getInstance().contextManager; } @@ -149,12 +149,12 @@ private OpenTelemetry() { meterFactoryProvider != null ? meterFactoryProvider.create() : DefaultMeterFactoryProvider.getInstance().create(); - DistributedContextManagerProvider contextManagerProvider = - loadSpi(DistributedContextManagerProvider.class); + CorrelationContextManagerProvider contextManagerProvider = + loadSpi(CorrelationContextManagerProvider.class); contextManager = contextManagerProvider != null ? contextManagerProvider.create() - : DefaultDistributedContextManager.getInstance(); + : DefaultCorrelationContextManager.getInstance(); } /** diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java similarity index 74% rename from api/src/main/java/io/opentelemetry/distributedcontext/DistributedContext.java rename to api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index f07ac1e950c..93815e2d115 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import java.util.Collection; import javax.annotation.Nullable; @@ -24,18 +24,18 @@ * A map from {@link EntryKey} to {@link EntryValue} and {@link EntryMetadata} that can be used to * label anything that is associated with a specific operation. * - *

For example, {@code DistributedContext}s can be used to label stats, log messages, or + *

For example, {@code CorrelationContext}s can be used to label stats, log messages, or * debugging information. * * @since 0.1.0 */ @Immutable -public interface DistributedContext { +public interface CorrelationContext { /** - * Returns an immutable collection of the entries in this {@code DistributedContext}. Order of + * Returns an immutable collection of the entries in this {@code CorrelationContext}. Order of * entries is not guaranteed. * - * @return an immutable collection of the entries in this {@code DistributedContext}. + * @return an immutable collection of the entries in this {@code CorrelationContext}. * @since 0.1.0 */ Collection getEntries(); @@ -45,39 +45,39 @@ public interface DistributedContext { * * @param entryKey entry key to return the value for. * @return the {@code EntryValue} associated with the given {@code EntryKey}, or {@code null} if - * no {@code Entry} with the given {@code entryKey} is in this {@code DistributedContext}. + * no {@code Entry} with the given {@code entryKey} is in this {@code CorrelationContext}. */ @Nullable EntryValue getEntryValue(EntryKey entryKey); /** - * Builder for the {@link DistributedContext} class. + * Builder for the {@link CorrelationContext} class. * * @since 0.1.0 */ interface Builder { /** - * Sets the parent {@link DistributedContext} to use. If no parent is provided, the value of - * {@link DistributedContextManager#getCurrentContext()} at {@link #build()} time will be used + * Sets the parent {@link CorrelationContext} to use. If no parent is provided, the value of + * {@link CorrelationContextManager#getCurrentContext()} at {@link #build()} time will be used * as parent, unless {@link #setNoParent()} was called. * - *

This must be used to create a {@link DistributedContext} when manual Context + *

This must be used to create a {@link CorrelationContext} when manual Context * propagation is used. * *

If called multiple times, only the last specified value will be used. * - * @param parent the {@link DistributedContext} used as parent, not null. + * @param parent the {@link CorrelationContext} used as parent, not null. * @return this. * @throws NullPointerException if {@code parent} is {@code null}. * @see #setNoParent() * @since 0.1.0 */ - Builder setParent(DistributedContext parent); + Builder setParent(CorrelationContext parent); /** - * Sets the option to become a root {@link DistributedContext} with no parent. If not - * called, the value provided using {@link #setParent(DistributedContext)} or otherwise {@link - * DistributedContextManager#getCurrentContext()} at {@link #build()} time will be used as + * Sets the option to become a root {@link CorrelationContext} with no parent. If not + * called, the value provided using {@link #setParent(CorrelationContext)} or otherwise {@link + * CorrelationContextManager#getCurrentContext()} at {@link #build()} time will be used as * parent. * * @return this. @@ -106,11 +106,11 @@ interface Builder { Builder remove(EntryKey key); /** - * Creates a {@code DistributedContext} from this builder. + * Creates a {@code CorrelationContext} from this builder. * - * @return a {@code DistributedContext} with the same entries as this builder. + * @return a {@code CorrelationContext} with the same entries as this builder. * @since 0.1.0 */ - DistributedContext build(); + CorrelationContext build(); } } diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java similarity index 61% rename from api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java rename to api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java index edf7777246e..be1285d6f6c 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/DistributedContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java @@ -14,34 +14,34 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import io.opentelemetry.context.Scope; import javax.annotation.concurrent.ThreadSafe; /** - * Object for creating new {@link DistributedContext}s and {@code DistributedContext}s based on the + * Object for creating new {@link CorrelationContext}s and {@code CorrelationContext}s based on the * current context. * - *

This class returns {@link DistributedContext.Builder builders} that can be used to create the - * implementation-dependent {@link DistributedContext}s. + *

This class returns {@link CorrelationContext.Builder builders} that can be used to create the + * implementation-dependent {@link CorrelationContext}s. * *

Implementations may have different constraints and are free to convert entry contexts to their * own subtypes. This means callers cannot assume the {@link #getCurrentContext() current context} - * is the same instance as the one {@link #withContext(DistributedContext) placed into scope}. + * is the same instance as the one {@link #withContext(CorrelationContext) placed into scope}. * * @since 0.1.0 */ @ThreadSafe -public interface DistributedContextManager { +public interface CorrelationContextManager { /** - * Returns the current {@code DistributedContext}. + * Returns the current {@code CorrelationContext}. * - * @return the current {@code DistributedContext}. + * @return the current {@code CorrelationContext}. * @since 0.1.0 */ - DistributedContext getCurrentContext(); + CorrelationContext getCurrentContext(); /** * Returns a new {@code Builder}. @@ -49,17 +49,17 @@ public interface DistributedContextManager { * @return a new {@code Builder}. * @since 0.1.0 */ - DistributedContext.Builder contextBuilder(); + CorrelationContext.Builder contextBuilder(); /** - * Enters the scope of code where the given {@code DistributedContext} is in the current context - * (replacing the previous {@code DistributedContext}) and returns an object that represents that + * Enters the scope of code where the given {@code CorrelationContext} is in the current context + * (replacing the previous {@code CorrelationContext}) and returns an object that represents that * scope. The scope is exited when the returned object is closed. * - * @param distContext the {@code DistributedContext} to be set as the current context. - * @return an object that defines a scope where the given {@code DistributedContext} is set as the + * @param distContext the {@code CorrelationContext} to be set as the current context. + * @return an object that defines a scope where the given {@code CorrelationContext} is set as the * current context. * @since 0.1.0 */ - Scope withContext(DistributedContext distContext); + Scope withContext(CorrelationContext distContext); } diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java similarity index 53% rename from api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java rename to api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index ed25a4c0516..ebadb452e5d 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -14,66 +14,66 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; import io.opentelemetry.internal.Utils; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; /** - * No-op implementations of {@link DistributedContextManager}. + * No-op implementations of {@link CorrelationContextManager}. * * @since 0.1.0 */ @ThreadSafe -public final class DefaultDistributedContextManager implements DistributedContextManager { - private static final DefaultDistributedContextManager INSTANCE = - new DefaultDistributedContextManager(); +public final class DefaultCorrelationContextManager implements CorrelationContextManager { + private static final DefaultCorrelationContextManager INSTANCE = + new DefaultCorrelationContextManager(); /** - * Returns a {@code DistributedContextManager} singleton that is the default implementation for - * {@link DistributedContextManager}. + * Returns a {@code CorrelationContextManager} singleton that is the default implementation for + * {@link CorrelationContextManager}. * - * @return a {@code DistributedContextManager} singleton that is the default implementation for - * {@link DistributedContextManager}. + * @return a {@code CorrelationContextManager} singleton that is the default implementation for + * {@link CorrelationContextManager}. * @since 0.1.0 */ - public static DistributedContextManager getInstance() { + public static CorrelationContextManager getInstance() { return INSTANCE; } @Override - public DistributedContext getCurrentContext() { + public CorrelationContext getCurrentContext() { return ContextUtils.getValue(); } @Override - public DistributedContext.Builder contextBuilder() { - return new NoopDistributedContextBuilder(); + public CorrelationContext.Builder contextBuilder() { + return new NoopCorrelationContextBuilder(); } @Override - public Scope withContext(DistributedContext distContext) { - return ContextUtils.withDistributedContext(distContext); + public Scope withContext(CorrelationContext distContext) { + return ContextUtils.withCorrelationContext(distContext); } @Immutable - private static final class NoopDistributedContextBuilder implements DistributedContext.Builder { + private static final class NoopCorrelationContextBuilder implements CorrelationContext.Builder { @Override - public DistributedContext.Builder setParent(DistributedContext parent) { + public CorrelationContext.Builder setParent(CorrelationContext parent) { Utils.checkNotNull(parent, "parent"); return this; } @Override - public DistributedContext.Builder setNoParent() { + public CorrelationContext.Builder setNoParent() { return this; } @Override - public DistributedContext.Builder put( + public CorrelationContext.Builder put( EntryKey key, EntryValue value, EntryMetadata tagMetadata) { Utils.checkNotNull(key, "key"); Utils.checkNotNull(value, "value"); @@ -82,14 +82,14 @@ public DistributedContext.Builder put( } @Override - public DistributedContext.Builder remove(EntryKey key) { + public CorrelationContext.Builder remove(EntryKey key) { Utils.checkNotNull(key, "key"); return this; } @Override - public DistributedContext build() { - return EmptyDistributedContext.getInstance(); + public CorrelationContext build() { + return EmptyCorrelationContext.getInstance(); } } } diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/EmptyDistributedContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java similarity index 71% rename from api/src/main/java/io/opentelemetry/distributedcontext/EmptyDistributedContext.java rename to api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java index 46bef8ea369..2a5b499d72b 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/EmptyDistributedContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import java.util.Collection; import java.util.Collections; @@ -22,23 +22,23 @@ import javax.annotation.concurrent.Immutable; /** - * An immutable implementation of the {@link DistributedContext} that does not contain any entries. + * An immutable implementation of the {@link CorrelationContext} that does not contain any entries. */ @Immutable -public class EmptyDistributedContext implements DistributedContext { +public class EmptyCorrelationContext implements CorrelationContext { private static final Collection EMPTY_COLLECTION = Collections.emptyList(); /** - * Returns the single instance of the {@link EmptyDistributedContext} class. + * Returns the single instance of the {@link EmptyCorrelationContext} class. * - * @return the single instance of the {@code EmptyDistributedContext} class. + * @return the single instance of the {@code EmptyCorrelationContext} class. * @since 0.1.0 */ - public static DistributedContext getInstance() { + public static CorrelationContext getInstance() { return INSTANCE; } - private static final DistributedContext INSTANCE = new EmptyDistributedContext(); + private static final CorrelationContext INSTANCE = new EmptyCorrelationContext(); @Override public Collection getEntries() { @@ -51,5 +51,5 @@ public EntryValue getEntryValue(EntryKey entryKey) { return null; } - private EmptyDistributedContext() {} + private EmptyCorrelationContext() {} } diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/Entry.java b/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java similarity index 94% rename from api/src/main/java/io/opentelemetry/distributedcontext/Entry.java rename to api/src/main/java/io/opentelemetry/correlationcontext/Entry.java index 6069243641b..be90f03bb8c 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/Entry.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import com.google.auto.value.AutoValue; -import io.opentelemetry.distributedcontext.EntryMetadata.EntryTtl; +import io.opentelemetry.correlationcontext.EntryMetadata.EntryTtl; import javax.annotation.concurrent.Immutable; /** diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/EntryKey.java b/api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java similarity index 95% rename from api/src/main/java/io/opentelemetry/distributedcontext/EntryKey.java rename to api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java index 59bbd5dff81..6f762094182 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/EntryKey.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import com.google.auto.value.AutoValue; import io.opentelemetry.internal.StringUtils; @@ -22,7 +22,7 @@ import javax.annotation.concurrent.Immutable; /** - * A key to a value stored in a {@link DistributedContext}. + * A key to a value stored in a {@link CorrelationContext}. * *

Each {@code EntryKey} has a {@code String} name. Names have a maximum length of {@link * #MAX_LENGTH} and contain only printable ASCII characters. diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/EntryMetadata.java b/api/src/main/java/io/opentelemetry/correlationcontext/EntryMetadata.java similarity index 98% rename from api/src/main/java/io/opentelemetry/distributedcontext/EntryMetadata.java rename to api/src/main/java/io/opentelemetry/correlationcontext/EntryMetadata.java index ce90a76d9f3..a9908642dca 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/EntryMetadata.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/EntryMetadata.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import com.google.auto.value.AutoValue; import javax.annotation.concurrent.Immutable; diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/EntryValue.java b/api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java similarity index 98% rename from api/src/main/java/io/opentelemetry/distributedcontext/EntryValue.java rename to api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java index 757ce379142..8742c16c778 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/EntryValue.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import com.google.auto.value.AutoValue; import io.opentelemetry.internal.StringUtils; diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/package-info.java b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java similarity index 78% rename from api/src/main/java/io/opentelemetry/distributedcontext/package-info.java rename to api/src/main/java/io/opentelemetry/correlationcontext/package-info.java index f842efa2eca..cafe13b13b3 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/package-info.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java @@ -21,13 +21,13 @@ * to label anything that is associated with a specific operation. For example, the {@code * opentelemetry.stats} package labels all stats with the current entries. * - *

{@link io.opentelemetry.distributedcontext.Entry Entrys} are key-value pairs. The {@link - * io.opentelemetry.distributedcontext.EntryKey keys} and {@link - * io.opentelemetry.distributedcontext.EntryValue values} are wrapped {@code String}s. They are - * stored as a map in a {@link io.opentelemetry.distributedcontext.DistributedContext}. + *

{@link io.opentelemetry.correlationcontext.Entry Entrys} are key-value pairs. The {@link + * io.opentelemetry.correlationcontext.EntryKey keys} and {@link + * io.opentelemetry.correlationcontext.EntryValue values} are wrapped {@code String}s. They are + * stored as a map in a {@link io.opentelemetry.correlationcontext.CorrelationContext}. * *

Note that entries are independent of the tracing data that is propagated in the {@code * io.grpc.Context}, such as trace ID. */ // TODO: Add code examples. -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java similarity index 74% rename from api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java rename to api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java index 86182955ca9..1bf6b5b046d 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java @@ -14,28 +14,28 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext.propagation; +package io.opentelemetry.correlationcontext.propagation; import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.EmptyDistributedContext; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.EmptyCorrelationContext; import javax.annotation.concurrent.Immutable; /** - * Utility methods for accessing the {@link DistributedContext} contained in the {@link + * Utility methods for accessing the {@link CorrelationContext} contained in the {@link * io.grpc.Context}. * *

Most code should interact with the current context via the public APIs in {@link - * DistributedContext} and avoid accessing this class directly. + * CorrelationContext} and avoid accessing this class directly. * * @since 0.1.0 */ @Immutable public final class ContextUtils { - private static final Context.Key DIST_CONTEXT_KEY = + private static final Context.Key DIST_CONTEXT_KEY = Context.keyWithDefault( - "opentelemetry-dist-context-key", EmptyDistributedContext.getInstance()); + "opentelemetry-dist-context-key", EmptyCorrelationContext.getInstance()); /** * Creates a new {@code Context} with the given value set. @@ -44,7 +44,7 @@ public final class ContextUtils { * @return a new context with the given value set. * @since 0.1.0 */ - public static Context withValue(DistributedContext distContext) { + public static Context withValue(CorrelationContext distContext) { return Context.current().withValue(DIST_CONTEXT_KEY, distContext); } @@ -56,7 +56,7 @@ public static Context withValue(DistributedContext distContext) { * @return a new context with the given value set. * @since 0.1.0 */ - public static Context withValue(DistributedContext distContext, Context context) { + public static Context withValue(CorrelationContext distContext, Context context) { return context.withValue(DIST_CONTEXT_KEY, distContext); } @@ -66,7 +66,7 @@ public static Context withValue(DistributedContext distContext, Context context) * @return the value from the specified {@code Context}. * @since 0.1.0 */ - public static DistributedContext getValue() { + public static CorrelationContext getValue() { return DIST_CONTEXT_KEY.get(); } @@ -77,19 +77,19 @@ public static DistributedContext getValue() { * @return the value from the specified {@code Context}. * @since 0.1.0 */ - public static DistributedContext getValue(Context context) { + public static CorrelationContext getValue(Context context) { return DIST_CONTEXT_KEY.get(context); } /** - * Returns a new {@link Scope} encapsulating the provided {@code DistributedContext} added to the + * Returns a new {@link Scope} encapsulating the provided {@code CorrelationContext} added to the * current {@code Context}. * - * @param distContext the {@code DistributedContext} to be added to the current {@code Context}. + * @param distContext the {@code CorrelationContext} to be added to the current {@code Context}. * @return the {@link Scope} for the updated {@code Context}. * @since 0.1.0 */ - public static Scope withDistributedContext(DistributedContext distContext) { + public static Scope withCorrelationContext(CorrelationContext distContext) { Context context = withValue(distContext); return io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); } diff --git a/api/src/main/java/io/opentelemetry/distributedcontext/spi/DistributedContextManagerProvider.java b/api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java similarity index 62% rename from api/src/main/java/io/opentelemetry/distributedcontext/spi/DistributedContextManagerProvider.java rename to api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java index 637ece87035..465f1268900 100644 --- a/api/src/main/java/io/opentelemetry/distributedcontext/spi/DistributedContextManagerProvider.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java @@ -14,31 +14,31 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext.spi; +package io.opentelemetry.correlationcontext.spi; -import io.opentelemetry.distributedcontext.DistributedContextManager; +import io.opentelemetry.correlationcontext.CorrelationContextManager; import javax.annotation.concurrent.ThreadSafe; /** - * DistributedContextManagerProvider is a service provider for {@link DistributedContextManager}. + * CorrelationContextManagerProvider is a service provider for {@link CorrelationContextManager}. * Fully qualified class name of the implementation should be registered in {@code - * META-INF/services/io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider}. + * META-INF/services/io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider}. *
*
* A specific implementation can be selected by a system property {@code - * io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider} with value of fully + * io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider} with value of fully * qualified class name. * * @see io.opentelemetry.OpenTelemetry */ @ThreadSafe -public interface DistributedContextManagerProvider { +public interface CorrelationContextManagerProvider { /** - * Creates a new {@code DistributedContextManager} instance. + * Creates a new {@code CorrelationContextManager} instance. * - * @return a {@code DistributedContextManager} instance. + * @return a {@code CorrelationContextManager} instance. * @since 0.1.0 */ - DistributedContextManager create(); + CorrelationContextManager create(); } diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index ba5560fd51c..53c4f7ffa74 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -22,10 +22,10 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.DefaultPropagators; import io.opentelemetry.context.propagation.Propagators; -import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; +import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; import io.opentelemetry.metrics.BatchRecorder; import io.opentelemetry.metrics.CounterDouble; import io.opentelemetry.metrics.CounterLong; @@ -74,7 +74,7 @@ public void after() { OpenTelemetry.reset(); System.clearProperty(TracerFactoryProvider.class.getName()); System.clearProperty(MeterFactoryProvider.class.getName()); - System.clearProperty(DistributedContextManagerProvider.class.getName()); + System.clearProperty(CorrelationContextManagerProvider.class.getName()); } @Test @@ -86,10 +86,10 @@ public void testDefault() { assertThat(OpenTelemetry.getMeterFactory()) .isInstanceOf(DefaultMeterFactory.getInstance().getClass()); assertThat(OpenTelemetry.getMeterFactory()).isEqualTo(OpenTelemetry.getMeterFactory()); - assertThat(OpenTelemetry.getDistributedContextManager()) - .isInstanceOf(DefaultDistributedContextManager.getInstance().getClass()); - assertThat(OpenTelemetry.getDistributedContextManager()) - .isEqualTo(OpenTelemetry.getDistributedContextManager()); + assertThat(OpenTelemetry.getCorrelationContextManager()) + .isInstanceOf(DefaultCorrelationContextManager.getInstance().getClass()); + assertThat(OpenTelemetry.getCorrelationContextManager()) + .isEqualTo(OpenTelemetry.getCorrelationContextManager()); assertThat(OpenTelemetry.getPropagators()).isInstanceOf(DefaultPropagators.class); assertThat(OpenTelemetry.getPropagators()).isEqualTo(OpenTelemetry.getPropagators()); } @@ -165,49 +165,49 @@ public void testMeterNotFound() { } @Test - public void testDistributedContextManagerLoadArbitrary() throws IOException { + public void testCorrelationContextManagerLoadArbitrary() throws IOException { File serviceFile = createService( - DistributedContextManagerProvider.class, - FirstDistributedContextManager.class, - SecondDistributedContextManager.class); + CorrelationContextManagerProvider.class, + FirstCorrelationContextManager.class, + SecondCorrelationContextManager.class); try { assertTrue( - (OpenTelemetry.getDistributedContextManager() instanceof FirstDistributedContextManager) - || (OpenTelemetry.getDistributedContextManager() - instanceof SecondDistributedContextManager)); - assertThat(OpenTelemetry.getDistributedContextManager()) - .isEqualTo(OpenTelemetry.getDistributedContextManager()); + (OpenTelemetry.getCorrelationContextManager() instanceof FirstCorrelationContextManager) + || (OpenTelemetry.getCorrelationContextManager() + instanceof SecondCorrelationContextManager)); + assertThat(OpenTelemetry.getCorrelationContextManager()) + .isEqualTo(OpenTelemetry.getCorrelationContextManager()); } finally { serviceFile.delete(); } } @Test - public void testDistributedContextManagerSystemProperty() throws IOException { + public void testCorrelationContextManagerSystemProperty() throws IOException { File serviceFile = createService( - DistributedContextManagerProvider.class, - FirstDistributedContextManager.class, - SecondDistributedContextManager.class); + CorrelationContextManagerProvider.class, + FirstCorrelationContextManager.class, + SecondCorrelationContextManager.class); System.setProperty( - DistributedContextManagerProvider.class.getName(), - SecondDistributedContextManager.class.getName()); + CorrelationContextManagerProvider.class.getName(), + SecondCorrelationContextManager.class.getName()); try { - assertThat(OpenTelemetry.getDistributedContextManager()) - .isInstanceOf(SecondDistributedContextManager.class); - assertThat(OpenTelemetry.getDistributedContextManager()) - .isEqualTo(OpenTelemetry.getDistributedContextManager()); + assertThat(OpenTelemetry.getCorrelationContextManager()) + .isInstanceOf(SecondCorrelationContextManager.class); + assertThat(OpenTelemetry.getCorrelationContextManager()) + .isEqualTo(OpenTelemetry.getCorrelationContextManager()); } finally { serviceFile.delete(); } } @Test - public void testDistributedContextManagerNotFound() { - System.setProperty(DistributedContextManagerProvider.class.getName(), "io.does.not.exists"); + public void testCorrelationContextManagerNotFound() { + System.setProperty(CorrelationContextManagerProvider.class.getName(), "io.does.not.exists"); thrown.expect(IllegalStateException.class); - OpenTelemetry.getDistributedContextManager(); + OpenTelemetry.getCorrelationContextManager(); } @Test @@ -404,35 +404,35 @@ public Meter get(String instrumentationName, String instrumentationVersion) { } } - public static class SecondDistributedContextManager extends FirstDistributedContextManager { + public static class SecondCorrelationContextManager extends FirstCorrelationContextManager { @Override - public DistributedContextManager create() { - return new SecondDistributedContextManager(); + public CorrelationContextManager create() { + return new SecondCorrelationContextManager(); } } - public static class FirstDistributedContextManager - implements DistributedContextManager, DistributedContextManagerProvider { + public static class FirstCorrelationContextManager + implements CorrelationContextManager, CorrelationContextManagerProvider { @Override - public DistributedContextManager create() { - return new FirstDistributedContextManager(); + public CorrelationContextManager create() { + return new FirstCorrelationContextManager(); } @Nullable @Override - public DistributedContext getCurrentContext() { + public CorrelationContext getCurrentContext() { return null; } @Nullable @Override - public DistributedContext.Builder contextBuilder() { + public CorrelationContext.Builder contextBuilder() { return null; } @Nullable @Override - public Scope withContext(DistributedContext distContext) { + public Scope withContext(CorrelationContext distContext) { return null; } } diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java similarity index 63% rename from api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 341c3f91694..ff0a30b0ee9 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/DefaultDistributedContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; import java.util.Arrays; import java.util.Collection; import javax.annotation.Nullable; @@ -30,16 +30,16 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Unit tests for {@link DefaultDistributedContextManager}. */ +/** Unit tests for {@link DefaultCorrelationContextManager}. */ @RunWith(JUnit4.class) -public final class DefaultDistributedContextManagerTest { - private static final DistributedContextManager defaultDistributedContextManager = - DefaultDistributedContextManager.getInstance(); +public final class DefaultCorrelationContextManagerTest { + private static final CorrelationContextManager defaultCorrelationContextManager = + DefaultCorrelationContextManager.getInstance(); private static final EntryKey KEY = EntryKey.create("key"); private static final EntryValue VALUE = EntryValue.create("value"); - private static final DistributedContext DIST_CONTEXT = - new DistributedContext() { + private static final CorrelationContext DIST_CONTEXT = + new CorrelationContext() { @Override public Collection getEntries() { @@ -57,20 +57,20 @@ public EntryValue getEntryValue(EntryKey entryKey) { @Test public void builderMethod() { - assertThat(defaultDistributedContextManager.contextBuilder().build().getEntries()).isEmpty(); + assertThat(defaultCorrelationContextManager.contextBuilder().build().getEntries()).isEmpty(); } @Test public void getCurrentContext_DefaultContext() { - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void getCurrentContext_ContextSetToNull() { Context orig = ContextUtils.withValue(null).attach(); try { - DistributedContext distContext = defaultDistributedContextManager.getCurrentContext(); + CorrelationContext distContext = defaultCorrelationContextManager.getCurrentContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { @@ -80,40 +80,40 @@ public void getCurrentContext_ContextSetToNull() { @Test public void withContext() { - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); - Scope wtm = defaultDistributedContextManager.withContext(DIST_CONTEXT); + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); + Scope wtm = defaultCorrelationContextManager.withContext(DIST_CONTEXT); try { - assertThat(defaultDistributedContextManager.getCurrentContext()) + assertThat(defaultCorrelationContextManager.getCurrentContext()) .isSameInstanceAs(DIST_CONTEXT); } finally { wtm.close(); } - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void withContext_nullContext() { - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); - Scope wtm = defaultDistributedContextManager.withContext(null); + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); + Scope wtm = defaultCorrelationContextManager.withContext(null); try { - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } finally { wtm.close(); } - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void withContextUsingWrap() { Runnable runnable; - Scope wtm = defaultDistributedContextManager.withContext(DIST_CONTEXT); + Scope wtm = defaultCorrelationContextManager.withContext(DIST_CONTEXT); try { - assertThat(defaultDistributedContextManager.getCurrentContext()) + assertThat(defaultCorrelationContextManager.getCurrentContext()) .isSameInstanceAs(DIST_CONTEXT); runnable = Context.current() @@ -121,50 +121,50 @@ public void withContextUsingWrap() { new Runnable() { @Override public void run() { - assertThat(defaultDistributedContextManager.getCurrentContext()) + assertThat(defaultCorrelationContextManager.getCurrentContext()) .isSameInstanceAs(DIST_CONTEXT); } }); } finally { wtm.close(); } - assertThat(defaultDistributedContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); - // When we run the runnable we will have the DistributedContext in the current Context. + assertThat(defaultCorrelationContextManager.getCurrentContext()) + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); + // When we run the runnable we will have the CorrelationContext in the current Context. runnable.run(); } @Test public void noopContextBuilder_SetParent_DisallowsNullParent() { - DistributedContext.Builder noopBuilder = defaultDistributedContextManager.contextBuilder(); + CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); noopBuilder.setParent(null); } @Test public void noopContextBuilder_Put_DisallowsNullKey() { - DistributedContext.Builder noopBuilder = defaultDistributedContextManager.contextBuilder(); + CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); noopBuilder.put(null, VALUE, Entry.METADATA_UNLIMITED_PROPAGATION); } @Test public void noopContextBuilder_Put_DisallowsNullValue() { - DistributedContext.Builder noopBuilder = defaultDistributedContextManager.contextBuilder(); + CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); noopBuilder.put(KEY, null, Entry.METADATA_UNLIMITED_PROPAGATION); } @Test public void noopContextBuilder_Put_DisallowsNullEntryMetadata() { - DistributedContext.Builder noopBuilder = defaultDistributedContextManager.contextBuilder(); + CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); noopBuilder.put(KEY, VALUE, null); } @Test public void noopContextBuilder_Remove_DisallowsNullKey() { - DistributedContext.Builder noopBuilder = defaultDistributedContextManager.contextBuilder(); + CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); noopBuilder.remove(null); } diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/EntryKeyTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java similarity index 98% rename from api/src/test/java/io/opentelemetry/distributedcontext/EntryKeyTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java index 0edc542bb4f..73f7a712a9f 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/EntryKeyTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import static com.google.common.truth.Truth.assertThat; diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/EntryMetadataTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryMetadataTest.java similarity index 92% rename from api/src/test/java/io/opentelemetry/distributedcontext/EntryMetadataTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/EntryMetadataTest.java index 6dc7bc9a90a..6c56a1bf3b2 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/EntryMetadataTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/EntryMetadataTest.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import static com.google.common.truth.Truth.assertThat; import com.google.common.testing.EqualsTester; -import io.opentelemetry.distributedcontext.EntryMetadata.EntryTtl; +import io.opentelemetry.correlationcontext.EntryMetadata.EntryTtl; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/EntryTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java similarity index 95% rename from api/src/test/java/io/opentelemetry/distributedcontext/EntryTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java index 7b041d94cb6..013fd0dc48a 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/EntryTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import static com.google.common.truth.Truth.assertThat; import com.google.common.testing.EqualsTester; -import io.opentelemetry.distributedcontext.EntryMetadata.EntryTtl; +import io.opentelemetry.correlationcontext.EntryMetadata.EntryTtl; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/EntryValueTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java similarity index 98% rename from api/src/test/java/io/opentelemetry/distributedcontext/EntryValueTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java index e7dea603c81..19786c3ab98 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/EntryValueTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext; +package io.opentelemetry.correlationcontext; import static com.google.common.truth.Truth.assertThat; diff --git a/api/src/test/java/io/opentelemetry/distributedcontext/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java similarity index 83% rename from api/src/test/java/io/opentelemetry/distributedcontext/propagation/ContextUtilsTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java index 02f67452e3a..b76d1819714 100644 --- a/api/src/test/java/io/opentelemetry/distributedcontext/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package io.opentelemetry.distributedcontext.propagation; +package io.opentelemetry.correlationcontext.propagation; import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; -import io.opentelemetry.distributedcontext.DistributedContext; +import io.opentelemetry.correlationcontext.CorrelationContext; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -29,14 +29,14 @@ public final class ContextUtilsTest { @Test public void testGetCurrentDistributedContex_DefaultContext() { - DistributedContext distContext = ContextUtils.getValue(Context.current()); + CorrelationContext distContext = ContextUtils.getValue(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @Test public void testGetCurrentDistributedContex_DefaultContext_WithoutExplicitContext() { - DistributedContext distContext = ContextUtils.getValue(); + CorrelationContext distContext = ContextUtils.getValue(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @@ -45,7 +45,7 @@ public void testGetCurrentDistributedContex_DefaultContext_WithoutExplicitContex public void testGetCurrentDistributedContex_ContextSetToNull() { Context orig = ContextUtils.withValue(null, Context.current()).attach(); try { - DistributedContext distContext = ContextUtils.getValue(Context.current()); + CorrelationContext distContext = ContextUtils.getValue(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { @@ -57,7 +57,7 @@ public void testGetCurrentDistributedContex_ContextSetToNull() { public void testGetCurrentDistributedContex_ContextSetToNull_WithoutExplicitContext() { Context orig = ContextUtils.withValue(null).attach(); try { - DistributedContext distContext = ContextUtils.getValue(); + CorrelationContext distContext = ContextUtils.getValue(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java index 1136ef008bd..5c3c9d289c6 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java @@ -17,7 +17,7 @@ package io.opentelemetry.opentracingshim; import io.opentelemetry.context.propagation.Propagators; -import io.opentelemetry.distributedcontext.DistributedContextManager; +import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.trace.Tracer; abstract class BaseShimObject { @@ -35,7 +35,7 @@ Tracer tracer() { return telemetryInfo.tracer(); } - DistributedContextManager contextManager() { + CorrelationContextManager contextManager() { return telemetryInfo.contextManager(); } 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 e6499f27974..3addbf3df6d 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -34,8 +34,8 @@ public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) io.opentelemetry.trace.propagation.ContextUtils.withSpanContext( contextShim.getSpanContext(), context); context = - io.opentelemetry.distributedcontext.propagation.ContextUtils.withValue( - contextShim.getDistributedContext(), context); + io.opentelemetry.correlationcontext.propagation.ContextUtils.withValue( + contextShim.getCorrelationContext(), context); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -54,7 +54,7 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { return new SpanContextShim( telemetryInfo, io.opentelemetry.trace.propagation.ContextUtils.getSpanContext(context), - io.opentelemetry.distributedcontext.propagation.ContextUtils.getValue(context)); + io.opentelemetry.correlationcontext.propagation.ContextUtils.getValue(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { 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 2059baf2997..be8bb4c9ebb 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java @@ -176,7 +176,7 @@ public SpanBuilder withStartTimestamp(long microseconds) { @Override public Span start() { - io.opentelemetry.distributedcontext.DistributedContext distContext = null; + io.opentelemetry.correlationcontext.CorrelationContext distContext = null; io.opentelemetry.trace.Span.Builder builder = tracer().spanBuilder(spanName); if (ignoreActiveSpan && parentSpan == null && parentSpanContext == null) { @@ -184,10 +184,10 @@ public Span start() { } else if (parentSpan != null) { builder.setParent(parentSpan.getSpan()); SpanContextShim contextShim = spanContextTable().get(parentSpan); - distContext = contextShim == null ? null : contextShim.getDistributedContext(); + distContext = contextShim == null ? null : contextShim.getCorrelationContext(); } else if (parentSpanContext != null) { builder.setParent(parentSpanContext.getSpanContext()); - distContext = parentSpanContext.getDistributedContext(); + distContext = parentSpanContext.getCorrelationContext(); } for (io.opentelemetry.trace.SpanContext link : parentLinks) { @@ -211,7 +211,7 @@ public Span start() { SpanShim spanShim = new SpanShim(telemetryInfo(), span); - if (distContext != null && distContext != telemetryInfo().emptyDistributedContext()) { + if (distContext != null && distContext != telemetryInfo().emptyCorrelationContext()) { spanContextTable().create(spanShim, distContext); } 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 3e0790f94dd..c12b0140cbe 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java @@ -16,11 +16,11 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.Entry; -import io.opentelemetry.distributedcontext.EntryKey; -import io.opentelemetry.distributedcontext.EntryMetadata; -import io.opentelemetry.distributedcontext.EntryValue; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.Entry; +import io.opentelemetry.correlationcontext.EntryKey; +import io.opentelemetry.correlationcontext.EntryMetadata; +import io.opentelemetry.correlationcontext.EntryValue; import io.opentracing.SpanContext; import java.util.Iterator; import java.util.Map; @@ -30,30 +30,30 @@ final class SpanContextShim extends BaseShimObject implements SpanContext { EntryMetadata.create(EntryMetadata.EntryTtl.UNLIMITED_PROPAGATION); private final io.opentelemetry.trace.SpanContext context; - private final DistributedContext distContext; + private final CorrelationContext distContext; public SpanContextShim(SpanShim spanShim) { this( spanShim.telemetryInfo(), spanShim.getSpan().getContext(), - spanShim.telemetryInfo().emptyDistributedContext()); + spanShim.telemetryInfo().emptyCorrelationContext()); } public SpanContextShim(TelemetryInfo telemetryInfo, io.opentelemetry.trace.SpanContext context) { - this(telemetryInfo, context, telemetryInfo.emptyDistributedContext()); + this(telemetryInfo, context, telemetryInfo.emptyCorrelationContext()); } public SpanContextShim( TelemetryInfo telemetryInfo, io.opentelemetry.trace.SpanContext context, - DistributedContext distContext) { + CorrelationContext distContext) { super(telemetryInfo); this.context = context; this.distContext = distContext; } SpanContextShim newWithKeyValue(String key, String value) { - DistributedContext.Builder builder = contextManager().contextBuilder().setParent(distContext); + CorrelationContext.Builder builder = contextManager().contextBuilder().setParent(distContext); builder.put(EntryKey.create(key), EntryValue.create(value), DEFAULT_ENTRY_METADATA); return new SpanContextShim(telemetryInfo(), context, builder.build()); @@ -63,7 +63,7 @@ io.opentelemetry.trace.SpanContext getSpanContext() { return context; } - DistributedContext getDistributedContext() { + CorrelationContext getCorrelationContext() { return distContext; } 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 93e9d4a6def..6bb736c7a2c 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShimTable.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShimTable.java @@ -16,7 +16,7 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.distributedcontext.DistributedContext; +import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span; import java.util.Map; import java.util.WeakHashMap; @@ -27,7 +27,7 @@ /* * SpanContextShimTable stores and manages OpenTracing SpanContext instances, * which are expected to a unmodfiable union of SpanContext and Baggage - * (DistributedContext/TagMap under OpenTelemetry). + * (CorrelationContext/TagMap under OpenTelemetry). * * This requires that changes on a given Span and its (new) SpanContext * are visible in all threads at *any* moment. The current approach uses @@ -85,10 +85,10 @@ public SpanContextShim get(SpanShim spanShim) { } public SpanContextShim create(SpanShim spanShim) { - return create(spanShim, spanShim.telemetryInfo().emptyDistributedContext()); + return create(spanShim, spanShim.telemetryInfo().emptyCorrelationContext()); } - public SpanContextShim create(SpanShim spanShim, DistributedContext distContext) { + public SpanContextShim create(SpanShim spanShim, CorrelationContext distContext) { lock.writeLock().lock(); try { SpanContextShim contextShim = shimsMap.get(spanShim.getSpan()); diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java index b892a1564b2..3f6ce3a08f9 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java @@ -17,26 +17,26 @@ package io.opentelemetry.opentracingshim; import io.opentelemetry.context.propagation.Propagators; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.DistributedContextManager; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.trace.Tracer; /** - * Utility class that holds a Tracer, a DistributedContextManager, and related objects that are core + * Utility class that holds a Tracer, a CorrelationContextManager, and related objects that are core * part of the OT Shim layer. */ final class TelemetryInfo { private final Tracer tracer; - private final DistributedContextManager contextManager; - private final DistributedContext emptyDistributedContext; + private final CorrelationContextManager contextManager; + private final CorrelationContext emptyCorrelationContext; private final Propagators propagators; private final SpanContextShimTable spanContextTable; - TelemetryInfo(Tracer tracer, DistributedContextManager contextManager, Propagators propagators) { + TelemetryInfo(Tracer tracer, CorrelationContextManager contextManager, Propagators propagators) { this.tracer = tracer; this.contextManager = contextManager; this.propagators = propagators; - this.emptyDistributedContext = contextManager.contextBuilder().build(); + this.emptyCorrelationContext = contextManager.contextBuilder().build(); this.spanContextTable = new SpanContextShimTable(); } @@ -44,7 +44,7 @@ Tracer tracer() { return tracer; } - DistributedContextManager contextManager() { + CorrelationContextManager contextManager() { return contextManager; } @@ -52,8 +52,8 @@ SpanContextShimTable spanContextTable() { return spanContextTable; } - DistributedContext emptyDistributedContext() { - return emptyDistributedContext; + CorrelationContext emptyCorrelationContext() { + return emptyCorrelationContext; } Propagators propagators() { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java index c40960753db..06d9755c95c 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java @@ -17,7 +17,7 @@ package io.opentelemetry.opentracingshim; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.distributedcontext.DistributedContextManager; +import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.internal.Utils; import io.opentelemetry.trace.Tracer; import io.opentelemetry.trace.TracerFactory; @@ -27,7 +27,7 @@ private TraceShim() {} /** * Creates a {@code io.opentracing.Tracer} shim out of {@code OpenTelemetry.getTracer()} and - * {@code OpenTelemetry.getDistributedContextManager()}. + * {@code OpenTelemetry.getCorrelationContextManager()}. * * @return a {@code io.opentracing.Tracer}. * @since 0.1.0 @@ -36,21 +36,21 @@ public static io.opentracing.Tracer createTracerShim() { return new TracerShim( new TelemetryInfo( getTracer(OpenTelemetry.getTracerFactory()), - OpenTelemetry.getDistributedContextManager(), + OpenTelemetry.getCorrelationContextManager(), OpenTelemetry.getPropagators())); } /** * Creates a {@code io.opentracing.Tracer} shim out the specified {@code Tracer} and {@code - * DistributedContextManager}. + * CorrelationContextManager}. * * @param tracerFactory the {@code TracerFactory} used by this shim. - * @param contextManager the {@code DistributedContextManager} used by this shim. + * @param contextManager the {@code CorrelationContextManager} used by this shim. * @return a {@code io.opentracing.Tracer}. * @since 0.1.0 */ public static io.opentracing.Tracer createTracerShim( - TracerFactory tracerFactory, DistributedContextManager contextManager) { + TracerFactory tracerFactory, CorrelationContextManager contextManager) { Utils.checkNotNull(tracerFactory, "tracer"); Utils.checkNotNull(contextManager, "contextManager"); return new TracerShim( diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java index 04e631b518a..fe5994e789b 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java @@ -21,7 +21,7 @@ import static org.junit.Assert.assertFalse; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; +import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; import io.opentelemetry.sdk.trace.TracerSdkFactory; import io.opentelemetry.trace.Tracer; import org.junit.Test; @@ -30,7 +30,7 @@ public class SpanBuilderShimTest { private final TracerSdkFactory tracerSdkFactory = TracerSdkFactory.create(); private final Tracer tracer = tracerSdkFactory.get("SpanShimTest"); private final TelemetryInfo telemetryInfo = - new TelemetryInfo(tracer, new DistributedContextManagerSdk(), OpenTelemetry.getPropagators()); + new TelemetryInfo(tracer, new CorrelationContextManagerSdk(), OpenTelemetry.getPropagators()); private static final String SPAN_NAME = "Span"; 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 722904a6926..90d949bc0e3 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java @@ -24,7 +24,7 @@ import static org.junit.Assert.assertTrue; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; +import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; import io.opentelemetry.sdk.trace.TracerSdkFactory; import io.opentelemetry.trace.Tracer; import java.util.Map; @@ -36,7 +36,7 @@ public class SpanShimTest { private final TracerSdkFactory tracerSdkFactory = TracerSdkFactory.create(); private final Tracer tracer = tracerSdkFactory.get("SpanShimTest"); private final TelemetryInfo telemetryInfo = - new TelemetryInfo(tracer, new DistributedContextManagerSdk(), OpenTelemetry.getPropagators()); + new TelemetryInfo(tracer, new CorrelationContextManagerSdk(), OpenTelemetry.getPropagators()); private io.opentelemetry.trace.Span span; private static final String SPAN_NAME = "Span"; diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java index 99816c776fb..74707eb28ab 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; +import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; import io.opentelemetry.sdk.trace.TracerSdkFactory; import org.junit.Test; @@ -29,12 +29,12 @@ public class TraceShimTest { public void createTracerShim_default() { TracerShim tracerShim = (TracerShim) TraceShim.createTracerShim(); assertEquals(OpenTelemetry.getTracerFactory().get("opentracingshim"), tracerShim.tracer()); - assertEquals(OpenTelemetry.getDistributedContextManager(), tracerShim.contextManager()); + assertEquals(OpenTelemetry.getCorrelationContextManager(), tracerShim.contextManager()); } @Test(expected = NullPointerException.class) public void createTracerShim_nullTracer() { - TraceShim.createTracerShim(null, OpenTelemetry.getDistributedContextManager()); + TraceShim.createTracerShim(null, OpenTelemetry.getCorrelationContextManager()); } @Test(expected = NullPointerException.class) @@ -45,7 +45,7 @@ public void createTracerShim_nullContextManager() { @Test public void createTracerShim() { TracerSdkFactory sdk = TracerSdkFactory.create(); - DistributedContextManagerSdk contextManager = new DistributedContextManagerSdk(); + CorrelationContextManagerSdk contextManager = new CorrelationContextManagerSdk(); TracerShim tracerShim = (TracerShim) TraceShim.createTracerShim(sdk, contextManager); assertEquals(sdk.get("opentracingshim"), tracerShim.tracer()); assertEquals(contextManager, tracerShim.contextManager()); 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 834b1ce4e0c..e76bbb37635 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java @@ -41,7 +41,7 @@ public void setUp() { new TracerShim( new TelemetryInfo( OpenTelemetry.getTracerFactory().get("opentracingshim"), - OpenTelemetry.getDistributedContextManager(), + OpenTelemetry.getCorrelationContextManager(), OpenTelemetry.getPropagators())); } 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 9f662233adf..4cd88376ed7 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 @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import io.opentelemetry.distributedcontext.DefaultDistributedContextManager; +import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; import io.opentelemetry.exporters.inmemory.InMemorySpanExporter; import io.opentelemetry.opentracingshim.TraceShim; import io.opentelemetry.sdk.trace.SpanData; @@ -38,7 +38,7 @@ public class OpenTelemetryInteroperabilityTest { private final io.opentelemetry.trace.Tracer tracer = sdk.get("opentracingshim"); private final InMemorySpanExporter spanExporter = InMemorySpanExporter.create(); private final Tracer otTracer = - TraceShim.createTracerShim(sdk, DefaultDistributedContextManager.getInstance()); + TraceShim.createTracerShim(sdk, DefaultCorrelationContextManager.getInstance()); { sdk.addSpanProcessor(SimpleSpansProcessor.newBuilder(spanExporter).build()); diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java index f7132bde449..03f346cd076 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java @@ -21,7 +21,7 @@ import io.opentelemetry.exporters.inmemory.InMemorySpanExporter; import io.opentelemetry.opentracingshim.TraceShim; -import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; +import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; import io.opentelemetry.sdk.trace.SpanData; import io.opentelemetry.sdk.trace.TracerSdkFactory; import io.opentelemetry.sdk.trace.export.SimpleSpansProcessor; @@ -47,7 +47,7 @@ private TestUtils() {} public static Tracer createTracerShim(InMemorySpanExporter exporter) { TracerSdkFactory tracerSdkFactory = TracerSdkFactory.create(); tracerSdkFactory.addSpanProcessor(SimpleSpansProcessor.newBuilder(exporter).build()); - return TraceShim.createTracerShim(tracerSdkFactory, new DistributedContextManagerSdk()); + return TraceShim.createTracerShim(tracerSdkFactory, new CorrelationContextManagerSdk()); } /** Returns the number of finished {@code Span}s in the specified {@code InMemorySpanExporter}. */ diff --git a/sdk/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java b/sdk/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java index 2fa1ef95192..3a71912b09d 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java @@ -17,14 +17,14 @@ package io.opentelemetry.sdk; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk; +import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; import io.opentelemetry.sdk.metrics.MeterSdkFactory; import io.opentelemetry.sdk.trace.TracerSdkFactory; import javax.annotation.concurrent.ThreadSafe; /** * This class provides a static global accessor for SDK telemetry objects {@link TracerSdkFactory}, - * {@link MeterSdkFactory} and {@link DistributedContextManagerSdk}. + * {@link MeterSdkFactory} and {@link CorrelationContextManagerSdk}. * *

This is a convenience class getting and casting the telemetry objects from {@link * OpenTelemetry}. @@ -54,13 +54,13 @@ public static MeterSdkFactory getMeterFactory() { } /** - * Returns a {@link DistributedContextManagerSdk}. + * Returns a {@link CorrelationContextManagerSdk}. * - * @return context manager returned by {@link OpenTelemetry#getDistributedContextManager()}. + * @return context manager returned by {@link OpenTelemetry#getCorrelationContextManager()}. * @since 0.1.0 */ - public static DistributedContextManagerSdk getDistributedContextManager() { - return (DistributedContextManagerSdk) OpenTelemetry.getDistributedContextManager(); + public static CorrelationContextManagerSdk getCorrelationContextManager() { + return (CorrelationContextManagerSdk) OpenTelemetry.getCorrelationContextManager(); } private OpenTelemetrySdk() {} diff --git a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java similarity index 53% rename from sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java rename to sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index a88b9c24659..6582b726a2b 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -14,30 +14,30 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; /** - * {@link DistributedContextManagerSdk} is SDK implementation of {@link DistributedContextManager}. + * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. */ -public class DistributedContextManagerSdk implements DistributedContextManager { +public class CorrelationContextManagerSdk implements CorrelationContextManager { @Override - public DistributedContext getCurrentContext() { + public CorrelationContext getCurrentContext() { return ContextUtils.getValue(); } @Override - public DistributedContext.Builder contextBuilder() { - return new DistributedContextSdk.Builder(); + public CorrelationContext.Builder contextBuilder() { + return new CorrelationContextSdk.Builder(); } @Override - public Scope withContext(DistributedContext distContext) { - return ContextUtils.withDistributedContext(distContext); + public Scope withContext(CorrelationContext distContext) { + return ContextUtils.withCorrelationContext(distContext); } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkProvider.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkProvider.java similarity index 62% rename from sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkProvider.java rename to sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkProvider.java index a0ba4e368f3..255559ee469 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkProvider.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkProvider.java @@ -14,22 +14,22 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; -import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider; +import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; /** - * {@code DistributedContextManager} provider implementation for {@link - * io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider}. + * {@code CorrelationContextManager} provider implementation for {@link + * io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider}. * *

This class is not intended to be used in application code and it is used only by {@link * io.opentelemetry.OpenTelemetry}. */ -public class DistributedContextManagerSdkProvider implements DistributedContextManagerProvider { +public class CorrelationContextManagerSdkProvider implements CorrelationContextManagerProvider { @Override - public DistributedContextManager create() { - return new DistributedContextManagerSdk(); + public CorrelationContextManager create() { + return new CorrelationContextManagerSdk(); } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java similarity index 72% rename from sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextSdk.java rename to sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index bd0d61fbd81..6179cb659b9 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -14,16 +14,16 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; import static io.opentelemetry.internal.Utils.checkNotNull; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.Entry; -import io.opentelemetry.distributedcontext.EntryKey; -import io.opentelemetry.distributedcontext.EntryMetadata; -import io.opentelemetry.distributedcontext.EntryValue; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.Entry; +import io.opentelemetry.correlationcontext.EntryKey; +import io.opentelemetry.correlationcontext.EntryMetadata; +import io.opentelemetry.correlationcontext.EntryValue; import io.opentelemetry.internal.Utils; import java.util.Collection; import java.util.Collections; @@ -36,20 +36,20 @@ @Immutable // TODO: Migrate to AutoValue // @AutoValue -class DistributedContextSdk implements DistributedContext { +class CorrelationContextSdk implements CorrelationContext { // The types of the EntryKey and Entry must match for each entry. private final Map entries; - @Nullable private final DistributedContext parent; + @Nullable private final CorrelationContext parent; /** - * Creates a new {@link DistributedContextSdk} with the given entries. + * Creates a new {@link CorrelationContextSdk} with the given entries. * - * @param entries the initial entries for this {@code DistributedContextSdk}. + * @param entries the initial entries for this {@code CorrelationContextSdk}. * @param parent providing a default set of entries */ - private DistributedContextSdk( - Map entries, DistributedContext parent) { + private CorrelationContextSdk( + Map entries, CorrelationContext parent) { this.entries = Collections.unmodifiableMap(new HashMap<>(checkNotNull(entries, "entries"))); this.parent = parent; } @@ -90,11 +90,11 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || !(o instanceof DistributedContextSdk)) { + if (o == null || !(o instanceof CorrelationContextSdk)) { return false; } - DistributedContextSdk distContextSdk = (DistributedContextSdk) o; + CorrelationContextSdk distContextSdk = (CorrelationContextSdk) o; if (!entries.equals(distContextSdk.entries)) { return false; @@ -111,31 +111,31 @@ public int hashCode() { // TODO: Migrate to AutoValue.Builder // @AutoValue.Builder - static class Builder implements DistributedContext.Builder { - @Nullable private DistributedContext parent; + static class Builder implements CorrelationContext.Builder { + @Nullable private CorrelationContext parent; private boolean noImplicitParent; private final Map entries; - /** Create a new empty DistributedContext builder. */ + /** Create a new empty CorrelationContext builder. */ Builder() { this.entries = new HashMap<>(); } @Override - public DistributedContext.Builder setParent(DistributedContext parent) { + public CorrelationContext.Builder setParent(CorrelationContext parent) { this.parent = Utils.checkNotNull(parent, "parent"); return this; } @Override - public DistributedContext.Builder setNoParent() { + public CorrelationContext.Builder setNoParent() { this.parent = null; noImplicitParent = true; return this; } @Override - public DistributedContext.Builder put( + public CorrelationContext.Builder put( EntryKey key, EntryValue value, EntryMetadata entryMetadata) { entries.put( checkNotNull(key, "key"), @@ -145,7 +145,7 @@ public DistributedContext.Builder put( } @Override - public DistributedContext.Builder remove(EntryKey key) { + public CorrelationContext.Builder remove(EntryKey key) { entries.remove(checkNotNull(key, "key")); if (parent != null && parent.getEntryValue(key) != null) { entries.put(key, null); @@ -154,11 +154,11 @@ public DistributedContext.Builder remove(EntryKey key) { } @Override - public DistributedContextSdk build() { + public CorrelationContextSdk build() { if (parent == null && !noImplicitParent) { - parent = OpenTelemetry.getDistributedContextManager().getCurrentContext(); + parent = OpenTelemetry.getCorrelationContextManager().getCurrentContext(); } - return new DistributedContextSdk(entries, parent); + return new CorrelationContextSdk(entries, parent); } } } diff --git a/sdk/src/main/resources/META-INF/services/io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider b/sdk/src/main/resources/META-INF/services/io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider new file mode 100644 index 00000000000..1a99b6205fe --- /dev/null +++ b/sdk/src/main/resources/META-INF/services/io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider @@ -0,0 +1 @@ +io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdkProvider diff --git a/sdk/src/main/resources/META-INF/services/io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider b/sdk/src/main/resources/META-INF/services/io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider deleted file mode 100644 index 7b3b28aebf1..00000000000 --- a/sdk/src/main/resources/META-INF/services/io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider +++ /dev/null @@ -1 +0,0 @@ -io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdkProvider diff --git a/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java index c17aad4a82d..1044562820c 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java @@ -30,8 +30,8 @@ public class OpenTelemetrySdkTest { public void testDefault() { assertThat(OpenTelemetrySdk.getTracerFactory()) .isSameInstanceAs(OpenTelemetry.getTracerFactory()); - assertThat(OpenTelemetrySdk.getDistributedContextManager()) - .isSameInstanceAs(OpenTelemetry.getDistributedContextManager()); + assertThat(OpenTelemetrySdk.getCorrelationContextManager()) + .isSameInstanceAs(OpenTelemetry.getCorrelationContextManager()); assertThat(OpenTelemetrySdk.getMeterFactory()) .isSameInstanceAs(OpenTelemetry.getMeterFactory()); } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkProviderTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkProviderTest.java similarity index 78% rename from sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkProviderTest.java rename to sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkProviderTest.java index fb49e787d94..c0d0dca03c9 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkProviderTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkProviderTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; import static com.google.common.truth.Truth.assertThat; @@ -23,11 +23,11 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class DistributedContextManagerSdkProviderTest { +public class CorrelationContextManagerSdkProviderTest { @Test public void testDefault() { - assertThat(new DistributedContextManagerSdkProvider().create()) - .isInstanceOf(DistributedContextManagerSdk.class); + assertThat(new CorrelationContextManagerSdkProvider().create()) + .isInstanceOf(CorrelationContextManagerSdk.class); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java similarity index 72% rename from sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java rename to sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index 16a07862358..7c98f7cd45d 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.EmptyDistributedContext; -import io.opentelemetry.distributedcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.EmptyCorrelationContext; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,14 +30,14 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -/** Unit tests for {@link DistributedContextManagerSdk}. */ +/** Unit tests for {@link CorrelationContextManagerSdk}. */ @RunWith(JUnit4.class) // Need to suppress warnings for MustBeClosed because Android 14 does not support // try-with-resources. @SuppressWarnings("MustBeClosedChecker") -public class DistributedContextManagerSdkTest { - @Mock private DistributedContext distContext; - private final DistributedContextManagerSdk contextManager = new DistributedContextManagerSdk(); +public class CorrelationContextManagerSdkTest { + @Mock private CorrelationContext distContext; + private final CorrelationContextManagerSdk contextManager = new CorrelationContextManagerSdk(); @Before public void setUp() { @@ -47,14 +47,14 @@ public void setUp() { @Test public void testGetCurrentContext_DefaultContext() { assertThat(contextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void testGetCurrentContext_ContextSetToNull() { Context orig = ContextUtils.withValue(null).attach(); try { - DistributedContext distContext = contextManager.getCurrentContext(); + CorrelationContext distContext = contextManager.getCurrentContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { @@ -63,9 +63,9 @@ public void testGetCurrentContext_ContextSetToNull() { } @Test - public void testWithDistributedContext() { + public void testWithCorrelationContext() { assertThat(contextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); Scope wtm = contextManager.withContext(distContext); try { assertThat(contextManager.getCurrentContext()).isSameInstanceAs(distContext); @@ -73,11 +73,11 @@ public void testWithDistributedContext() { wtm.close(); } assertThat(contextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test - public void testWithDistributedContextUsingWrap() { + public void testWithCorrelationContextUsingWrap() { Runnable runnable; Scope wtm = contextManager.withContext(distContext); try { @@ -95,8 +95,8 @@ public void run() { wtm.close(); } assertThat(contextManager.getCurrentContext()) - .isSameInstanceAs(EmptyDistributedContext.getInstance()); - // When we run the runnable we will have the DistributedContext in the current Context. + .isSameInstanceAs(EmptyCorrelationContext.getInstance()); + // When we run the runnable we will have the CorrelationContext in the current Context. runnable.run(); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java similarity index 72% rename from sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextSdkTest.java rename to sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index f015e745794..9b1709c0f0c 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -14,18 +14,18 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; import static com.google.common.truth.Truth.assertThat; -import static io.opentelemetry.sdk.distributedcontext.DistributedContextTestUtil.listToDistributedContext; +import static io.opentelemetry.sdk.correlationcontext.CorrelationContextTestUtil.listToCorrelationContext; import com.google.common.testing.EqualsTester; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.Entry; -import io.opentelemetry.distributedcontext.EntryKey; -import io.opentelemetry.distributedcontext.EntryMetadata; -import io.opentelemetry.distributedcontext.EntryValue; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.Entry; +import io.opentelemetry.correlationcontext.EntryKey; +import io.opentelemetry.correlationcontext.EntryMetadata; +import io.opentelemetry.correlationcontext.EntryValue; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -33,14 +33,14 @@ import org.junit.runners.JUnit4; /** - * Tests for {@link DistributedContextSdk} and {@link DistributedContextSdk.Builder}. + * Tests for {@link CorrelationContextSdk} and {@link CorrelationContextSdk.Builder}. * - *

Tests for scope management with {@link DistributedContextManagerSdk} are in {@link - * ScopedDistributedContextTest}. + *

Tests for scope management with {@link CorrelationContextManagerSdk} are in {@link + * ScopedCorrelationContextTest}. */ @RunWith(JUnit4.class) -public class DistributedContextSdkTest { - private final DistributedContextManager contextManager = new DistributedContextManagerSdk(); +public class CorrelationContextSdkTest { + private final CorrelationContextManager contextManager = new CorrelationContextManagerSdk(); private static final EntryMetadata TMD = EntryMetadata.create(EntryMetadata.EntryTtl.UNLIMITED_PROPAGATION); @@ -58,21 +58,21 @@ public class DistributedContextSdkTest { @Test public void getEntries_empty() { - DistributedContextSdk distContext = new DistributedContextSdk.Builder().build(); + CorrelationContextSdk distContext = new CorrelationContextSdk.Builder().build(); assertThat(distContext.getEntries()).isEmpty(); } @Test public void getEntries_nonEmpty() { - DistributedContextSdk distContext = listToDistributedContext(T1, T2); + CorrelationContextSdk distContext = listToCorrelationContext(T1, T2); assertThat(distContext.getEntries()).containsExactly(T1, T2); } @Test public void getEntries_chain() { Entry t1alt = Entry.create(K1, V2, TMD); - DistributedContextSdk parent = listToDistributedContext(T1, T2); - DistributedContext distContext = + CorrelationContextSdk parent = listToCorrelationContext(T1, T2); + CorrelationContext distContext = contextManager .contextBuilder() .setParent(parent) @@ -83,7 +83,7 @@ public void getEntries_chain() { @Test public void put_newKey() { - DistributedContextSdk distContext = listToDistributedContext(T1); + CorrelationContextSdk distContext = listToCorrelationContext(T1); assertThat( contextManager .contextBuilder() @@ -96,7 +96,7 @@ public void put_newKey() { @Test public void put_existingKey() { - DistributedContextSdk distContext = listToDistributedContext(T1); + CorrelationContextSdk distContext = listToCorrelationContext(T1); assertThat( contextManager .contextBuilder() @@ -109,8 +109,8 @@ public void put_existingKey() { @Test public void put_nullKey() { - DistributedContextSdk distContext = listToDistributedContext(T1); - DistributedContext.Builder builder = contextManager.contextBuilder().setParent(distContext); + CorrelationContextSdk distContext = listToCorrelationContext(T1); + CorrelationContext.Builder builder = contextManager.contextBuilder().setParent(distContext); thrown.expect(NullPointerException.class); thrown.expectMessage("key"); builder.put(null, V2, TMD); @@ -118,8 +118,8 @@ public void put_nullKey() { @Test public void put_nullValue() { - DistributedContextSdk distContext = listToDistributedContext(T1); - DistributedContext.Builder builder = contextManager.contextBuilder().setParent(distContext); + CorrelationContextSdk distContext = listToCorrelationContext(T1); + CorrelationContext.Builder builder = contextManager.contextBuilder().setParent(distContext); thrown.expect(NullPointerException.class); thrown.expectMessage("value"); builder.put(K2, null, TMD); @@ -127,22 +127,22 @@ public void put_nullValue() { @Test public void setParent_nullValue() { - DistributedContextSdk parent = listToDistributedContext(T1); + CorrelationContextSdk parent = listToCorrelationContext(T1); thrown.expect(NullPointerException.class); contextManager.contextBuilder().setParent(parent).setParent(null).build(); } @Test public void setParent_setNoParent() { - DistributedContextSdk parent = listToDistributedContext(T1); - DistributedContext distContext = + CorrelationContextSdk parent = listToCorrelationContext(T1); + CorrelationContext distContext = contextManager.contextBuilder().setParent(parent).setNoParent().build(); assertThat(distContext.getEntries()).isEmpty(); } @Test public void remove_existingKey() { - DistributedContextSdk.Builder builder = new DistributedContextSdk.Builder(); + CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(); builder.put(T1.getKey(), T1.getValue(), T1.getEntryMetadata()); builder.put(T2.getKey(), T2.getValue(), T2.getEntryMetadata()); @@ -151,7 +151,7 @@ public void remove_existingKey() { @Test public void remove_differentKey() { - DistributedContextSdk.Builder builder = new DistributedContextSdk.Builder(); + CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(); builder.put(T1.getKey(), T1.getValue(), T1.getEntryMetadata()); builder.put(T2.getKey(), T2.getValue(), T2.getEntryMetadata()); @@ -160,7 +160,7 @@ public void remove_differentKey() { @Test public void remove_keyFromParent() { - DistributedContextSdk distContext = listToDistributedContext(T1, T2); + CorrelationContextSdk distContext = listToCorrelationContext(T1, T2); assertThat( contextManager.contextBuilder().setParent(distContext).remove(K1).build().getEntries()) .containsExactly(T2); @@ -168,7 +168,7 @@ public void remove_keyFromParent() { @Test public void remove_nullKey() { - DistributedContext.Builder builder = contextManager.contextBuilder(); + CorrelationContext.Builder builder = contextManager.contextBuilder(); thrown.expect(NullPointerException.class); thrown.expectMessage("key"); builder.remove(null); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextTestUtil.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java similarity index 66% rename from sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextTestUtil.java rename to sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java index 613bfa28930..0db4f7860f5 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/DistributedContextTestUtil.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java @@ -14,25 +14,25 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; -import io.opentelemetry.distributedcontext.Entry; +import io.opentelemetry.correlationcontext.Entry; import java.util.Arrays; import java.util.List; -class DistributedContextTestUtil { +class CorrelationContextTestUtil { - static DistributedContextSdk listToDistributedContext(Entry... entries) { - return listToDistributedContext(Arrays.asList(entries)); + static CorrelationContextSdk listToCorrelationContext(Entry... entries) { + return listToCorrelationContext(Arrays.asList(entries)); } - static DistributedContextSdk listToDistributedContext(List entries) { - DistributedContextSdk.Builder builder = new DistributedContextSdk.Builder(); + static CorrelationContextSdk listToCorrelationContext(List entries) { + CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(); for (Entry entry : entries) { builder.put(entry.getKey(), entry.getValue(), entry.getEntryMetadata()); } return builder.build(); } - private DistributedContextTestUtil() {} + private CorrelationContextTestUtil() {} } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/ScopedDistributedContextTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java similarity index 80% rename from sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/ScopedDistributedContextTest.java rename to sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java index f034ce071ca..d7179a79b6d 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/distributedcontext/ScopedDistributedContextTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java @@ -14,28 +14,28 @@ * limitations under the License. */ -package io.opentelemetry.sdk.distributedcontext; +package io.opentelemetry.sdk.correlationcontext; import static com.google.common.truth.Truth.assertThat; import io.opentelemetry.context.Scope; -import io.opentelemetry.distributedcontext.DistributedContext; -import io.opentelemetry.distributedcontext.DistributedContextManager; -import io.opentelemetry.distributedcontext.EmptyDistributedContext; -import io.opentelemetry.distributedcontext.Entry; -import io.opentelemetry.distributedcontext.EntryKey; -import io.opentelemetry.distributedcontext.EntryMetadata; -import io.opentelemetry.distributedcontext.EntryValue; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.EmptyCorrelationContext; +import io.opentelemetry.correlationcontext.Entry; +import io.opentelemetry.correlationcontext.EntryKey; +import io.opentelemetry.correlationcontext.EntryMetadata; +import io.opentelemetry.correlationcontext.EntryValue; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** - * Unit tests for the methods in {@link DistributedContextManagerSdk} and {@link - * DistributedContextSdk.Builder} that interact with the current {@link DistributedContextSdk}. + * Unit tests for the methods in {@link CorrelationContextManagerSdk} and {@link + * CorrelationContextSdk.Builder} that interact with the current {@link CorrelationContextSdk}. */ @RunWith(JUnit4.class) -public class ScopedDistributedContextTest { +public class ScopedCorrelationContextTest { private static final EntryKey KEY_1 = EntryKey.create("key 1"); private static final EntryKey KEY_2 = EntryKey.create("key 2"); private static final EntryKey KEY_3 = EntryKey.create("key 3"); @@ -50,19 +50,19 @@ public class ScopedDistributedContextTest { private static final EntryMetadata METADATA_NO_PROPAGATION = EntryMetadata.create(EntryMetadata.EntryTtl.NO_PROPAGATION); - private final DistributedContextManager contextManager = new DistributedContextManagerSdk(); + private final CorrelationContextManager contextManager = new CorrelationContextManagerSdk(); @Test - public void emptyDistributedContext() { - DistributedContext defaultDistributedContext = contextManager.getCurrentContext(); - assertThat(defaultDistributedContext.getEntries()).isEmpty(); - assertThat(defaultDistributedContext).isInstanceOf(EmptyDistributedContext.class); + public void emptyCorrelationContext() { + CorrelationContext defaultCorrelationContext = contextManager.getCurrentContext(); + assertThat(defaultCorrelationContext.getEntries()).isEmpty(); + assertThat(defaultCorrelationContext).isInstanceOf(EmptyCorrelationContext.class); } @Test public void withContext() { assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); - DistributedContext scopedEntries = + CorrelationContext scopedEntries = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); try (Scope scope = contextManager.withContext(scopedEntries)) { assertThat(contextManager.getCurrentContext()).isSameInstanceAs(scopedEntries); @@ -72,10 +72,10 @@ public void withContext() { @Test public void createBuilderFromCurrentEntries() { - DistributedContext scopedDistContext = + CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); try (Scope scope = contextManager.withContext(scopedDistContext)) { - DistributedContext newEntries = + CorrelationContext newEntries = contextManager .contextBuilder() .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) @@ -91,7 +91,7 @@ public void createBuilderFromCurrentEntries() { @Test public void setCurrentEntriesWithBuilder() { assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); - DistributedContext scopedDistContext = + CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); try (Scope scope = contextManager.withContext(scopedDistContext)) { assertThat(contextManager.getCurrentContext().getEntries()) @@ -103,10 +103,10 @@ public void setCurrentEntriesWithBuilder() { @Test public void addToCurrentEntriesWithBuilder() { - DistributedContext scopedDistContext = + CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); try (Scope scope1 = contextManager.withContext(scopedDistContext)) { - DistributedContext innerDistContext = + CorrelationContext innerDistContext = contextManager .contextBuilder() .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) @@ -123,15 +123,15 @@ public void addToCurrentEntriesWithBuilder() { } @Test - public void multiScopeDistributedContextWithMetadata() { - DistributedContext scopedDistContext = + public void multiScopeCorrelationContextWithMetadata() { + CorrelationContext scopedDistContext = contextManager .contextBuilder() .put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION) .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) .build(); try (Scope scope1 = contextManager.withContext(scopedDistContext)) { - DistributedContext innerDistContext = + CorrelationContext innerDistContext = contextManager .contextBuilder() .put(KEY_3, VALUE_3, METADATA_NO_PROPAGATION) @@ -152,10 +152,10 @@ public void multiScopeDistributedContextWithMetadata() { @Test public void setNoParent_doesNotInheritContext() { assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); - DistributedContext scopedDistContext = + CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); try (Scope scope = contextManager.withContext(scopedDistContext)) { - DistributedContext innerDistContext = + CorrelationContext innerDistContext = contextManager .contextBuilder() .setNoParent() From eb1e4117d4b35965d4257129c70c7ffdaa418ad4 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 17:33:18 -0600 Subject: [PATCH 11/42] Improve names for correlationcontext's ContextUtils methods. --- .../DefaultCorrelationContextManager.java | 4 ++-- .../correlationcontext/propagation/ContextUtils.java | 12 ++++++------ .../DefaultCorrelationContextManagerTest.java | 2 +- .../propagation/ContextUtilsTest.java | 12 ++++++------ .../opentelemetry/opentracingshim/Propagation.java | 5 +++-- .../CorrelationContextManagerSdk.java | 4 ++-- .../CorrelationContextManagerSdkTest.java | 2 +- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index ebadb452e5d..dad0612ee70 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -46,7 +46,7 @@ public static CorrelationContextManager getInstance() { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getValue(); + return ContextUtils.getCorrelationContext(); } @Override @@ -56,7 +56,7 @@ public CorrelationContext.Builder contextBuilder() { @Override public Scope withContext(CorrelationContext distContext) { - return ContextUtils.withCorrelationContext(distContext); + return ContextUtils.withScopedCorrelationContext(distContext); } @Immutable diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java index 1bf6b5b046d..2e8e3039a56 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java @@ -44,7 +44,7 @@ public final class ContextUtils { * @return a new context with the given value set. * @since 0.1.0 */ - public static Context withValue(CorrelationContext distContext) { + public static Context withCorrelationContext(CorrelationContext distContext) { return Context.current().withValue(DIST_CONTEXT_KEY, distContext); } @@ -56,7 +56,7 @@ public static Context withValue(CorrelationContext distContext) { * @return a new context with the given value set. * @since 0.1.0 */ - public static Context withValue(CorrelationContext distContext, Context context) { + public static Context withCorrelationContext(CorrelationContext distContext, Context context) { return context.withValue(DIST_CONTEXT_KEY, distContext); } @@ -66,7 +66,7 @@ public static Context withValue(CorrelationContext distContext, Context context) * @return the value from the specified {@code Context}. * @since 0.1.0 */ - public static CorrelationContext getValue() { + public static CorrelationContext getCorrelationContext() { return DIST_CONTEXT_KEY.get(); } @@ -77,7 +77,7 @@ public static CorrelationContext getValue() { * @return the value from the specified {@code Context}. * @since 0.1.0 */ - public static CorrelationContext getValue(Context context) { + public static CorrelationContext getCorrelationContext(Context context) { return DIST_CONTEXT_KEY.get(context); } @@ -89,8 +89,8 @@ public static CorrelationContext getValue(Context context) { * @return the {@link Scope} for the updated {@code Context}. * @since 0.1.0 */ - public static Scope withCorrelationContext(CorrelationContext distContext) { - Context context = withValue(distContext); + public static Scope withScopedCorrelationContext(CorrelationContext distContext) { + Context context = withCorrelationContext(distContext); return io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); } diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index ff0a30b0ee9..35f91ea7e1f 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -68,7 +68,7 @@ public void getCurrentContext_DefaultContext() { @Test public void getCurrentContext_ContextSetToNull() { - Context orig = ContextUtils.withValue(null).attach(); + Context orig = ContextUtils.withCorrelationContext(null).attach(); try { CorrelationContext distContext = defaultCorrelationContextManager.getCurrentContext(); assertThat(distContext).isNotNull(); diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java index b76d1819714..cc7730a8cbd 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java @@ -29,23 +29,23 @@ public final class ContextUtilsTest { @Test public void testGetCurrentDistributedContex_DefaultContext() { - CorrelationContext distContext = ContextUtils.getValue(Context.current()); + CorrelationContext distContext = ContextUtils.getCorrelationContext(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @Test public void testGetCurrentDistributedContex_DefaultContext_WithoutExplicitContext() { - CorrelationContext distContext = ContextUtils.getValue(); + CorrelationContext distContext = ContextUtils.getCorrelationContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @Test public void testGetCurrentDistributedContex_ContextSetToNull() { - Context orig = ContextUtils.withValue(null, Context.current()).attach(); + Context orig = ContextUtils.withCorrelationContext(null, Context.current()).attach(); try { - CorrelationContext distContext = ContextUtils.getValue(Context.current()); + CorrelationContext distContext = ContextUtils.getCorrelationContext(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { @@ -55,9 +55,9 @@ public void testGetCurrentDistributedContex_ContextSetToNull() { @Test public void testGetCurrentDistributedContex_ContextSetToNull_WithoutExplicitContext() { - Context orig = ContextUtils.withValue(null).attach(); + Context orig = ContextUtils.withCorrelationContext(null).attach(); try { - CorrelationContext distContext = ContextUtils.getValue(); + CorrelationContext distContext = ContextUtils.getCorrelationContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { 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 3addbf3df6d..a1f92164879 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -34,7 +34,7 @@ public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) io.opentelemetry.trace.propagation.ContextUtils.withSpanContext( contextShim.getSpanContext(), context); context = - io.opentelemetry.correlationcontext.propagation.ContextUtils.withValue( + io.opentelemetry.correlationcontext.propagation.ContextUtils.withCorrelationContext( contextShim.getCorrelationContext(), context); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); @@ -54,7 +54,8 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { return new SpanContextShim( telemetryInfo, io.opentelemetry.trace.propagation.ContextUtils.getSpanContext(context), - io.opentelemetry.correlationcontext.propagation.ContextUtils.getValue(context)); + io.opentelemetry.correlationcontext.propagation.ContextUtils.getCorrelationContext( + context)); } static final class TextMapSetter implements HttpTextFormat.Setter { diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 6582b726a2b..8641a6f15ab 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -28,7 +28,7 @@ public class CorrelationContextManagerSdk implements CorrelationContextManager { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getValue(); + return ContextUtils.getCorrelationContext(); } @Override @@ -38,6 +38,6 @@ public CorrelationContext.Builder contextBuilder() { @Override public Scope withContext(CorrelationContext distContext) { - return ContextUtils.withCorrelationContext(distContext); + return ContextUtils.withScopedCorrelationContext(distContext); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index 7c98f7cd45d..f33ec51f474 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -52,7 +52,7 @@ public void testGetCurrentContext_DefaultContext() { @Test public void testGetCurrentContext_ContextSetToNull() { - Context orig = ContextUtils.withValue(null).attach(); + Context orig = ContextUtils.withCorrelationContext(null).attach(); try { CorrelationContext distContext = contextManager.getCurrentContext(); assertThat(distContext).isNotNull(); From bbfa035b35629442d9f3013ec9a8415f17c1d0f4 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 17:48:23 -0600 Subject: [PATCH 12/42] Don't provide an automatic default for current CorrelationContext. --- .../DefaultCorrelationContextManager.java | 3 +- .../propagation/ContextUtils.java | 17 +++++++++-- .../propagation/ContextUtilsTest.java | 29 ++++++++----------- .../CorrelationContextManagerSdk.java | 3 +- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index dad0612ee70..70ceea4c633 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -16,6 +16,7 @@ package io.opentelemetry.correlationcontext; +import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.propagation.ContextUtils; import io.opentelemetry.internal.Utils; @@ -46,7 +47,7 @@ public static CorrelationContextManager getInstance() { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getCorrelationContext(); + return ContextUtils.getCorrelationContextWithDefault(Context.current()); } @Override diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java index 2e8e3039a56..91f211b910e 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java @@ -34,8 +34,8 @@ @Immutable public final class ContextUtils { private static final Context.Key DIST_CONTEXT_KEY = - Context.keyWithDefault( - "opentelemetry-dist-context-key", EmptyCorrelationContext.getInstance()); + Context.key("opentelemetry-dist-context-key"); + private static final CorrelationContext DEFAULT_VALUE = EmptyCorrelationContext.getInstance(); /** * Creates a new {@code Context} with the given value set. @@ -81,6 +81,19 @@ public static CorrelationContext getCorrelationContext(Context context) { return DIST_CONTEXT_KEY.get(context); } + /** + * Returns the value from the specified {@code Context}, falling back to a default, no-op {@link + * CorrelationContext}. + * + * @param context the specified {@code Context}. + * @return the value from the specified {@code Context}. + * @since 0.1.0 + */ + public static CorrelationContext getCorrelationContextWithDefault(Context context) { + CorrelationContext corrContext = DIST_CONTEXT_KEY.get(context); + return corrContext == null ? DEFAULT_VALUE : corrContext; + } + /** * Returns a new {@link Scope} encapsulating the provided {@code CorrelationContext} added to the * current {@code Context}. diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java index cc7730a8cbd..0b2b7bdd862 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java @@ -28,36 +28,31 @@ @RunWith(JUnit4.class) public final class ContextUtilsTest { @Test - public void testGetCurrentDistributedContex_DefaultContext() { + public void testGetCurrentDistributedContext_DefaultContext() { CorrelationContext distContext = ContextUtils.getCorrelationContext(Context.current()); - assertThat(distContext).isNotNull(); - assertThat(distContext.getEntries()).isEmpty(); + assertThat(distContext).isNull(); } @Test public void testGetCurrentDistributedContex_DefaultContext_WithoutExplicitContext() { CorrelationContext distContext = ContextUtils.getCorrelationContext(); - assertThat(distContext).isNotNull(); - assertThat(distContext.getEntries()).isEmpty(); + assertThat(distContext).isNull(); } @Test - public void testGetCurrentDistributedContex_ContextSetToNull() { - Context orig = ContextUtils.withCorrelationContext(null, Context.current()).attach(); - try { - CorrelationContext distContext = ContextUtils.getCorrelationContext(Context.current()); - assertThat(distContext).isNotNull(); - assertThat(distContext.getEntries()).isEmpty(); - } finally { - Context.current().detach(orig); - } + public void testGetCurrentDistributedContextWithDefault_DefaultContext() { + CorrelationContext distContext = + ContextUtils.getCorrelationContextWithDefault(Context.current()); + assertThat(distContext).isNotNull(); + assertThat(distContext.getEntries()).isEmpty(); } @Test - public void testGetCurrentDistributedContex_ContextSetToNull_WithoutExplicitContext() { - Context orig = ContextUtils.withCorrelationContext(null).attach(); + public void testGetCurrentDistributedContextWithDefault_ContextSetToNull() { + Context orig = ContextUtils.withCorrelationContext(null, Context.current()).attach(); try { - CorrelationContext distContext = ContextUtils.getCorrelationContext(); + CorrelationContext distContext = + ContextUtils.getCorrelationContextWithDefault(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 8641a6f15ab..35303915d17 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.correlationcontext; +import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; @@ -28,7 +29,7 @@ public class CorrelationContextManagerSdk implements CorrelationContextManager { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getCorrelationContext(); + return ContextUtils.getCorrelationContextWithDefault(Context.current()); } @Override From b9f9b937c4d4ec70d7d4478e9c20641adda933a1 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 17:57:46 -0600 Subject: [PATCH 13/42] Improve the client-server example after the recent changes. --- .../sdk/contrib/trace/testbed/clientserver/Server.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 4ac1e126d4f..221746d40a6 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -22,9 +22,7 @@ import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; -import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; -import io.opentelemetry.trace.propagation.ContextUtils; import java.util.concurrent.ArrayBlockingQueue; import javax.annotation.Nullable; @@ -52,9 +50,8 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = ContextUtils.getSpanContext(context); Span span = - tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); + tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(context).startSpan(); span.setAttribute("component", "example-server"); try (Scope ignored = tracer.withSpan(span)) { From fdf644bc23fe3e17921d27ad706b4804945144b0 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 18:59:45 -0600 Subject: [PATCH 14/42] Adds CorrelationContext.Builder.setParent(Context) overload. --- .../CorrelationContext.java | 23 ++++++++++++++++ .../DefaultCorrelationContextManager.java | 6 +++++ .../DefaultCorrelationContextManagerTest.java | 9 ++++++- .../CorrelationContextSdk.java | 14 ++++++++++ .../CorrelationContextSdkTest.java | 27 ++++++++++++++++++- 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index 93815e2d115..fa1328b09e0 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -16,6 +16,7 @@ package io.opentelemetry.correlationcontext; +import io.grpc.Context; import java.util.Collection; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -74,6 +75,28 @@ interface Builder { */ Builder setParent(CorrelationContext parent); + /** + * Sets the parent {@link CorrelationContext} to use from the specified {@code Context}. If no + * parent is provided, the value of {@link CorrelationContextManager#getCurrentContext()} at + * {@link #build()} time will be used as parent, unless {@link #setNoParent()} was called. + * + *

If no {@link CorrelationContext} is available in the specified {@code Context}, the + * resulting {@link CorrelationContext} will become a root instance, as if {@link + * #setNoParent()} had been called. + * + *

This must be used to create a {@link CorrelationContext} when manual Context + * propagation is used. + * + *

If called multiple times, only the last specified value will be used. + * + * @param context the {@code Context}. + * @return this. + * @throws NullPointerException if {@code context} is {@code null}. + * @see #setNoParent() + * @since 0.3.0 + */ + Builder setParent(Context context); + /** * Sets the option to become a root {@link CorrelationContext} with no parent. If not * called, the value provided using {@link #setParent(CorrelationContext)} or otherwise {@link diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 70ceea4c633..06314922b04 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -68,6 +68,12 @@ public CorrelationContext.Builder setParent(CorrelationContext parent) { return this; } + @Override + public CorrelationContext.Builder setParent(Context context) { + Utils.checkNotNull(context, "context"); + return this; + } + @Override public CorrelationContext.Builder setNoParent() { return this; diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 35f91ea7e1f..7ac63a460a4 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -138,7 +138,14 @@ public void run() { public void noopContextBuilder_SetParent_DisallowsNullParent() { CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); - noopBuilder.setParent(null); + noopBuilder.setParent((CorrelationContext) null); + } + + @Test + public void noopContextBuilder_SetParent_DisallowsNullContext() { + CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); + thrown.expect(NullPointerException.class); + noopBuilder.setParent((Context) null); } @Test diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index 6179cb659b9..8e1d53128e3 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -18,12 +18,14 @@ import static io.opentelemetry.internal.Utils.checkNotNull; +import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; import io.opentelemetry.internal.Utils; import java.util.Collection; import java.util.Collections; @@ -127,6 +129,18 @@ public CorrelationContext.Builder setParent(CorrelationContext parent) { return this; } + @Override + public CorrelationContext.Builder setParent(Context context) { + Utils.checkNotNull(context, "context"); + CorrelationContext corrContext = ContextUtils.getCorrelationContext(context); + if (corrContext != null) { + setParent(corrContext); + } else { + setNoParent(); + } + return this; + } + @Override public CorrelationContext.Builder setNoParent() { this.parent = null; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index 9b1709c0f0c..cb7c8c57e60 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -20,12 +20,14 @@ import static io.opentelemetry.sdk.correlationcontext.CorrelationContextTestUtil.listToCorrelationContext; import com.google.common.testing.EqualsTester; +import io.grpc.Context; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -129,7 +131,30 @@ public void put_nullValue() { public void setParent_nullValue() { CorrelationContextSdk parent = listToCorrelationContext(T1); thrown.expect(NullPointerException.class); - contextManager.contextBuilder().setParent(parent).setParent(null).build(); + contextManager.contextBuilder().setParent(parent).setParent((CorrelationContext) null).build(); + } + + @Test + public void setParent_nullContext() { + CorrelationContextSdk parent = listToCorrelationContext(T1); + thrown.expect(NullPointerException.class); + contextManager.contextBuilder().setParent(parent).setParent((Context) null).build(); + } + + @Test + public void setParent_fromContext() { + Context context = ContextUtils.withCorrelationContext(listToCorrelationContext(T1)); + CorrelationContext corrContext = contextManager.contextBuilder().setParent(context).build(); + assertThat(corrContext.getEntries()).containsExactly(T1); + } + + @Test + public void setParent_fromEmptyContext() { + CorrelationContextSdk parent = listToCorrelationContext(T1); + Context context = Context.current(); + CorrelationContext corrContext = + contextManager.contextBuilder().setParent(parent).setParent(context).build(); + assertThat(corrContext.getEntries()).isEmpty(); } @Test From fd2b4a9f11057e65faa665484a7eff7890e2077b Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 19:01:10 -0600 Subject: [PATCH 15/42] s/be/become. --- api/src/main/java/io/opentelemetry/trace/Span.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index d0314d9d925..a453746c35e 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -406,7 +406,7 @@ interface Builder { * parent. * *

If no {@link Span} nor {@link SpanContext} is availablle in the specified {@code Context}, - * the resulting {@code Span} will be a root instance, as if {@link #setNoParent()} had been + * the resulting {@code Span} will become a root instance, as if {@link #setNoParent()} had been * called. * *

If called multiple times, only the last specified value will be used. Observe that the From 24fecd1caba9ac8ad85295544f44a540e7965afc Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Dec 2019 20:01:37 -0600 Subject: [PATCH 16/42] Fix javadoc. --- .../io/opentelemetry/context/propagation/HttpTextFormat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java b/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java index 14070c8e6b8..2dbf19c5137 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java @@ -54,7 +54,7 @@ public interface HttpTextFormat { /** * The propagation fields defined. If your carrier is reused, you should delete the fields here - * before calling {@link #inject(Object, Object, Setter)} )}. + * before calling {@link #inject(Context, Object, Setter)} )}. * *

For example, if the carrier is a single-use or immutable request object, you don't need to * clear fields as they couldn't have been set before. If it is a mutable, retryable object, From b56ac608ef2edde5f497cdf488908144b156f88b Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 9 Jan 2020 02:10:17 -0600 Subject: [PATCH 17/42] No need to use diamond. --- .../opentelemetry/context/propagation/DefaultPropagators.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java index a98e29b0513..eb27ad02d19 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java @@ -113,7 +113,7 @@ private static final class NoopHttpTextFormat implements HttpTextFormat { @Override public List fields() { - return Collections.emptyList(); + return Collections.emptyList(); } @Override From 08297c687a24ec43b1adceec6a836c42ba87ad95 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 9 Jan 2020 02:12:00 -0600 Subject: [PATCH 18/42] Simply import withScopedContext(). --- .../io/opentelemetry/trace/propagation/ContextUtils.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java index 131c0883917..f055da6d127 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java @@ -16,6 +16,8 @@ package io.opentelemetry.trace.propagation; +import static io.opentelemetry.context.propagation.ContextUtils.withScopedContext; + import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.trace.DefaultSpan; @@ -170,8 +172,7 @@ public static SpanContext getAnySpanContext(Context context) { * @since 0.1.0 */ public static Scope withScopedSpan(Span span) { - Context context = withSpan(span); - return io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); + return withScopedContext(withSpan(span)); } private ContextUtils() {} From e06c32fd99791e73c86f446f0897a60508c06b77 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 24 Jan 2020 00:18:10 -0600 Subject: [PATCH 19/42] Fix the API/SDK build. --- .../CorrelationContextManager.java | 90 ------------------- .../DefaultCorrelationContextManager.java | 69 +++----------- .../propagation/DefaultPropagatorsTest.java | 4 +- .../opentracingshim/SpanBuilderShimTest.java | 7 -- .../CorrelationContextManagerSdk.java | 22 +---- .../sdk/trace/SpanBuilderSdkTest.java | 13 +-- 6 files changed, 23 insertions(+), 182 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java index a6b4a525c91..be1285d6f6c 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java @@ -17,8 +17,6 @@ package io.opentelemetry.correlationcontext; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import javax.annotation.concurrent.ThreadSafe; /** @@ -64,92 +62,4 @@ public interface CorrelationContextManager { * @since 0.1.0 */ Scope withContext(CorrelationContext distContext); - - /** - * Returns the {@link BinaryFormat} for this implementation. - * - *

Example of usage on the client: - * - *

{@code
-   * private static final CorrelationContextManager contextManager =
-   *     OpenTelemetry.getCorrelationContextManager();
-   * private static final BinaryFormat binaryFormat = contextManager.getBinaryFormat();
-   *
-   * Request createRequest() {
-   *   Request req = new Request();
-   *   byte[] ctxBuffer = binaryFormat.toByteArray(contextManager.getCurrentContext());
-   *   request.addMetadata("distributedContext", ctxBuffer);
-   *   return request;
-   * }
-   * }
- * - *

Example of usage on the server: - * - *

{@code
-   * private static final CorrelationContextManager contextManager =
-   *     OpenTelemetry.getCorrelationContextManager();
-   * private static final BinaryFormat binaryFormat = contextManager.getBinaryFormat();
-   *
-   * void onRequestReceived(Request request) {
-   *   byte[] ctxBuffer = request.getMetadata("distributedContext");
-   *   CorrelationContext distContext = textFormat.fromByteArray(ctxBuffer);
-   *   try (Scope s = contextManager.withContext(distContext)) {
-   *     // Handle request and send response back.
-   *   }
-   * }
-   * }
- * - * @return the {@code BinaryFormat} for this implementation. - * @since 0.1.0 - */ - BinaryFormat getBinaryFormat(); - - /** - * Returns the {@link HttpTextFormat} for this implementation. - * - *

Usually this will be the W3C Correlation Context as the HTTP text format. For more details, - * see correlation-context. - * - *

Example of usage on the client: - * - *

{@code
-   * private static final CorrelationContextManager contextManager =
-   *     OpenTelemetry.getCorrelationContextManager();
-   * private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
-   *
-   * private static final HttpTextFormat.Setter setter =
-   *     new HttpTextFormat.Setter() {
-   *       public void put(HttpURLConnection carrier, String key, String value) {
-   *         carrier.setRequestProperty(field, value);
-   *       }
-   *     };
-   *
-   * void makeHttpRequest() {
-   *   HttpURLConnection connection =
-   *       (HttpURLConnection) new URL("http://myserver").openConnection();
-   *   textFormat.inject(contextManager.getCurrentContext(), connection, httpURLConnectionSetter);
-   *   // Send the request, wait for response and maybe set the status if not ok.
-   * }
-   * }
- * - *

Example of usage on the server: - * - *

{@code
-   * private static final CorrelationContextManager contextManager =
-   *     OpenTelemetry.getCorrelationContextManager();
-   * private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
-   * private static final HttpTextFormat.Getter getter = ...;
-   *
-   * void onRequestReceived(HttpRequest request) {
-   *   CorrelationContext distContext = textFormat.extract(request, getter);
-   *   try (Scope s = contextManager.withContext(distContext)) {
-   *     // Handle request and send response back.
-   *   }
-   * }
-   * }
- * - * @return the {@code HttpTextFormat} for this implementation. - * @since 0.1.0 - */ - HttpTextFormat getHttpTextFormat(); } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index f804ba8f74a..06314922b04 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -16,13 +16,10 @@ package io.opentelemetry.correlationcontext; +import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.correlationcontext.unsafe.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; import io.opentelemetry.internal.Utils; -import java.util.Collections; -import java.util.List; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; @@ -35,9 +32,6 @@ public final class DefaultCorrelationContextManager implements CorrelationContextManager { private static final DefaultCorrelationContextManager INSTANCE = new DefaultCorrelationContextManager(); - private static final BinaryFormat BINARY_FORMAT = new NoopBinaryFormat(); - private static final HttpTextFormat HTTP_TEXT_FORMAT = - new NoopHttpTextFormat(); /** * Returns a {@code CorrelationContextManager} singleton that is the default implementation for @@ -53,7 +47,7 @@ public static CorrelationContextManager getInstance() { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getValue(); + return ContextUtils.getCorrelationContextWithDefault(Context.current()); } @Override @@ -63,17 +57,7 @@ public CorrelationContext.Builder contextBuilder() { @Override public Scope withContext(CorrelationContext distContext) { - return ContextUtils.withCorrelationContext(distContext); - } - - @Override - public BinaryFormat getBinaryFormat() { - return BINARY_FORMAT; - } - - @Override - public HttpTextFormat getHttpTextFormat() { - return HTTP_TEXT_FORMAT; + return ContextUtils.withScopedCorrelationContext(distContext); } @Immutable @@ -84,6 +68,12 @@ public CorrelationContext.Builder setParent(CorrelationContext parent) { return this; } + @Override + public CorrelationContext.Builder setParent(Context context) { + Utils.checkNotNull(context, "context"); + return this; + } + @Override public CorrelationContext.Builder setNoParent() { return this; @@ -109,43 +99,4 @@ public CorrelationContext build() { return EmptyCorrelationContext.getInstance(); } } - - @Immutable - private static final class NoopBinaryFormat implements BinaryFormat { - static final byte[] EMPTY_BYTE_ARRAY = {}; - - @Override - public byte[] toByteArray(CorrelationContext distContext) { - Utils.checkNotNull(distContext, "distContext"); - return EMPTY_BYTE_ARRAY; - } - - @Override - public CorrelationContext fromByteArray(byte[] bytes) { - Utils.checkNotNull(bytes, "bytes"); - return EmptyCorrelationContext.getInstance(); - } - } - - @Immutable - private static final class NoopHttpTextFormat implements HttpTextFormat { - @Override - public List fields() { - return Collections.emptyList(); - } - - @Override - public void inject(CorrelationContext distContext, C carrier, Setter setter) { - Utils.checkNotNull(distContext, "distContext"); - Utils.checkNotNull(carrier, "carrier"); - Utils.checkNotNull(setter, "setter"); - } - - @Override - public CorrelationContext extract(C carrier, Getter getter) { - Utils.checkNotNull(carrier, "carrier"); - Utils.checkNotNull(getter, "getter"); - return EmptyCorrelationContext.getInstance(); - } - } } diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index 2672ed9b0e7..a2bfcfa4144 100644 --- a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -123,7 +123,7 @@ public List fields() { public void inject(Context context, C carrier, Setter setter) { Object payload = key.get(context); if (payload != null) { - setter.put(carrier, name, payload.toString()); + setter.set(carrier, name, payload.toString()); } } @@ -142,7 +142,7 @@ private static final class MapSetter implements HttpTextFormat.Setter map, String key, String value) { + public void set(Map map, String key, String value) { map.put(key, value); } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java index f1423093f55..5a65d706790 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java @@ -20,10 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -<<<<<<< HEAD import io.opentelemetry.OpenTelemetry; -======= ->>>>>>> master import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; import io.opentelemetry.sdk.trace.TracerSdkRegistry; import io.opentelemetry.trace.Tracer; @@ -33,11 +30,7 @@ public class SpanBuilderShimTest { private final TracerSdkRegistry tracerSdkFactory = TracerSdkRegistry.create(); private final Tracer tracer = tracerSdkFactory.get("SpanShimTest"); private final TelemetryInfo telemetryInfo = -<<<<<<< HEAD new TelemetryInfo(tracer, new CorrelationContextManagerSdk(), OpenTelemetry.getPropagators()); -======= - new TelemetryInfo(tracer, new CorrelationContextManagerSdk()); ->>>>>>> master private static final String SPAN_NAME = "Span"; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index b7938beae49..35303915d17 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -16,13 +16,11 @@ package io.opentelemetry.sdk.correlationcontext; +import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.BinaryFormat; -import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; -import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; -import io.opentelemetry.correlationcontext.unsafe.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.ContextUtils; /** * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. @@ -31,7 +29,7 @@ public class CorrelationContextManagerSdk implements CorrelationContextManager { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getValue(); + return ContextUtils.getCorrelationContextWithDefault(Context.current()); } @Override @@ -41,18 +39,6 @@ public CorrelationContext.Builder contextBuilder() { @Override public Scope withContext(CorrelationContext distContext) { - return ContextUtils.withCorrelationContext(distContext); - } - - @Override - public BinaryFormat getBinaryFormat() { - // TODO: Implement this. - return DefaultCorrelationContextManager.getInstance().getBinaryFormat(); - } - - @Override - public HttpTextFormat getHttpTextFormat() { - // TODO: Implement this. - return DefaultCorrelationContextManager.getInstance().getHttpTextFormat(); + return ContextUtils.withScopedCorrelationContext(distContext); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index b2609413b77..aade00d52d6 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -417,7 +417,7 @@ public void parentExplicitContext_withSpan() { tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); try { assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); } finally { span.end(); } @@ -436,7 +436,7 @@ public void parentExplicitContext_withSpanContext() { tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); try { assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); } finally { span.end(); } @@ -456,7 +456,7 @@ public void parentExplicitContext_withValues() { tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); try { assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); } finally { span.end(); } @@ -475,7 +475,8 @@ public void parentExplicitContext_noValues() { tracerSdk.spanBuilder(SPAN_NAME).setParent(parent).setParent(context).startSpan(); try { assertThat(span.getContext().getTraceId()).isNotEqualTo(parent.getContext().getTraceId()); - assertThat(span.getParentSpanId()).isNotEqualTo(parent.getContext().getSpanId()); + assertThat(span.toSpanData().getParentSpanId()) + .isNotEqualTo(parent.getContext().getSpanId()); } finally { span.end(); } @@ -513,7 +514,7 @@ public void parentCurrentSpanContext() { (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); try { assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); } finally { span.end(); } @@ -534,7 +535,7 @@ public void parentCurrentContextValues() { (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); try { assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); + assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); } finally { span.end(); } From 4c482667f08938b5b819eb025e8c82a0fa5a6141 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 24 Jan 2020 00:32:32 -0600 Subject: [PATCH 20/42] Remove the builder from the Propagators interface. --- .../propagation/DefaultPropagators.java | 39 +++++++++++++++++-- .../context/propagation/Propagators.java | 39 ------------------- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java index eb27ad02d19..9c95c6c1ec1 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java @@ -47,24 +47,55 @@ public HttpTextFormat getHttpTextFormat() { * @since 0.3.0 */ public static Builder builder() { - return new DefaultBuilder(); + return new Builder(); } private DefaultPropagators(HttpTextFormat textFormat) { this.textFormat = textFormat; } - private static final class DefaultBuilder implements Builder { + /** + * {@link Builder} is used to construct a new {@code Propagators} object with the specified + * propagators. + * + *

This is a example of a {@code Propagators} object being created: + * + *

{@code
+   * Propagators propagators = DefaultPropagators.builder()
+   *     .addHttpTextFormat(new HttpTraceContext())
+   *     .addHttpTextFormat(new HttpCorrelationContext())
+   *     .addHttpTextFormat(new MyCustomContextPropagator())
+   *     .build();
+   * }
+ * + * @since 0.3.0 + */ + public static final class Builder { List textPropagators = new ArrayList<>(); - @Override + /** + * Adds a {@link HttpTextFormat} propagator. + * + *

One propagator per concern (traces, correlations, etc) should be added if this format is + * supported. + * + * @param textFormat the propagator to be added. + * @return this. + * @throws NullPointerException if {@code textFormat} is {@code null}. + * @since 0.3.0 + */ public Builder addHttpTextFormat(HttpTextFormat textFormat) { Utils.checkNotNull(textFormat, "textFormat"); textPropagators.add(textFormat); return this; } - @Override + /** + * Builds a new {@code Propagators} with the specified propagators. + * + * @return the newly created {@code Propagators} instance. + * @since 0.3.0 + */ public Propagators build() { if (textPropagators.isEmpty()) { return new DefaultPropagators(NoopHttpTextFormat.INSTANCE); diff --git a/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java b/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java index 2275ba54e51..6d03778cc25 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java @@ -91,43 +91,4 @@ public interface Propagators { * @since 0.3.0 */ HttpTextFormat getHttpTextFormat(); - - /** - * {@link Builder} is used to construct a new {@code Propagators} object with the specified - * propagators. - * - *

This is a example of a {@code Propagators} object being created: - * - *

{@code
-   * Propagators propagators = DefaultPropagators.builder()
-   *     .addHttpTextFormat(new HttpTraceContext())
-   *     .addHttpTextFormat(new HttpCorrelationContext())
-   *     .addHttpTextFormat(new MyCustomContextPropagator())
-   *     .build();
-   * }
- * - * @since 0.3.0 - */ - interface Builder { - /** - * Adds a {@link HttpTextFormat} propagator. - * - *

One propagator per concern (traces, correlations, etc) should be added if this format is - * supported. - * - * @param textFormat the propagator to be added. - * @return this. - * @throws NullPointerException if {@code textFormat} is {@code null}. - * @since 0.3.0 - */ - Builder addHttpTextFormat(HttpTextFormat textFormat); - - /** - * Builds a new {@code Propagators} with the specified propagators. - * - * @return the newly created {@code Propagators} instance. - * @since 0.3.0 - */ - Propagators build(); - } } From 1328ec59546536c9bf154a195b6bde3e57181193 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 24 Jan 2020 00:36:29 -0600 Subject: [PATCH 21/42] Fix name. --- .../correlationcontext/DefaultCorrelationContextManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 06314922b04..66bf8f3d946 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -81,10 +81,10 @@ public CorrelationContext.Builder setNoParent() { @Override public CorrelationContext.Builder put( - EntryKey key, EntryValue value, EntryMetadata tagMetadata) { + EntryKey key, EntryValue value, EntryMetadata entryMetadata) { Utils.checkNotNull(key, "key"); Utils.checkNotNull(value, "value"); - Utils.checkNotNull(tagMetadata, "tagMetadata"); + Utils.checkNotNull(entryMetadata, "entryMetadata"); return this; } From d35f19e7bbefe6aed58e73afcf99a26cf1ab8d3e Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 24 Jan 2020 00:37:20 -0600 Subject: [PATCH 22/42] Use Collections.emptyList() directly. --- .../correlationcontext/EmptyCorrelationContext.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java index 2a5b499d72b..384dce01d9c 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java @@ -26,8 +26,6 @@ */ @Immutable public class EmptyCorrelationContext implements CorrelationContext { - private static final Collection EMPTY_COLLECTION = Collections.emptyList(); - /** * Returns the single instance of the {@link EmptyCorrelationContext} class. * @@ -42,7 +40,7 @@ public static CorrelationContext getInstance() { @Override public Collection getEntries() { - return EMPTY_COLLECTION; + return Collections.emptyList(); } @Nullable From 14acd5cfebdc699f13305439e81c9e2b575b3e68 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 24 Jan 2020 00:47:49 -0600 Subject: [PATCH 23/42] Rename Propagators to ContextPropagators. --- .../java/io/opentelemetry/OpenTelemetry.java | 22 +++++++------- ...opagators.java => ContextPropagators.java} | 6 ++-- ...rs.java => DefaultContextPropagators.java} | 30 +++++++++---------- .../io/opentelemetry/OpenTelemetryTest.java | 8 ++--- .../propagation/DefaultPropagatorsTest.java | 14 ++++----- .../opentracingshim/BaseShimObject.java | 4 +-- .../opentracingshim/TelemetryInfo.java | 9 +++--- 7 files changed, 47 insertions(+), 46 deletions(-) rename api/src/main/java/io/opentelemetry/context/propagation/{Propagators.java => ContextPropagators.java} (94%) rename api/src/main/java/io/opentelemetry/context/propagation/{DefaultPropagators.java => DefaultContextPropagators.java} (78%) diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 6d7a9927f6e..23f6a29dc92 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -16,8 +16,8 @@ package io.opentelemetry; -import io.opentelemetry.context.propagation.DefaultPropagators; -import io.opentelemetry.context.propagation.Propagators; +import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.context.propagation.DefaultContextPropagators; import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; @@ -56,8 +56,8 @@ public final class OpenTelemetry { private final MeterRegistry meterRegistry; private final CorrelationContextManager contextManager; - private volatile Propagators propagators = - DefaultPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build(); + private volatile ContextPropagators propagators = + DefaultContextPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build(); /** * Returns a singleton {@link TracerRegistry}. @@ -97,30 +97,30 @@ public static CorrelationContextManager getCorrelationContextManager() { } /** - * Returns a {@link Propagators} object, which can be used to access the set of registered + * Returns a {@link ContextPropagators} object, which can be used to access the set of registered * propagators for each supported format. * - * @return registered propagators container, defaulting to a {@link Propagators} object with - * {@code HttpTraceContext} registered. + * @return registered propagators container, defaulting to a {@link ContextPropagators} object + * with {@code HttpTraceContext} registered. * @throws IllegalStateException if a specified manager (via system properties) could not be * found. * @since 0.3.0 */ - public static Propagators getPropagators() { + public static ContextPropagators getPropagators() { return getInstance().propagators; } /** - * Sets the {@link Propagators} object, which can be used to access the set of registered + * Sets the {@link ContextPropagators} object, which can be used to access the set of registered * propagators for each supported format. * - * @param propagators the {@link Propagators} object to be registered. + * @param propagators the {@link ContextPropagators} object to be registered. * @throws IllegalStateException if a specified manager (via system properties) could not be * found. * @throws NullPointerException if {@code propagators} is {@code null}. * @since 0.3.0 */ - public static void setPropagators(Propagators propagators) { + public static void setPropagators(ContextPropagators propagators) { Utils.checkNotNull(propagators, "propagators"); getInstance().propagators = propagators; } diff --git a/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java similarity index 94% rename from api/src/main/java/io/opentelemetry/context/propagation/Propagators.java rename to api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index 6d03778cc25..38d2a51808e 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -33,7 +33,7 @@ * private static final Tracer tracer = OpenTelemetry.getTracer(); * void onSendRequest() { * try (Scope scope = tracer.withSpan(span)) { - * Propagators propagators = OpenTelemetry.getPropagators(); + * ContextPropagators propagators = OpenTelemetry.getPropagators(); * HttpTextFormat textFormat = propagators.getHttpTextFormat(); * * // Inject the span's SpanContext and other available concerns (such as correlations) @@ -54,7 +54,7 @@ *

{@code
  * private static final Tracer tracer = OpenTelemetry.getTracer();
  * void onRequestReceived() {
- *   Propagators propagators = OpenTelemetry.getPropagators();
+ *   ContextPropagators propagators = OpenTelemetry.getPropagators();
  *   HttpTextFormat textFormat = propagators.getHttpTextFormat();
  *
  *   // Extract and store the propagated span's SpanContext and other available concerns
@@ -78,7 +78,7 @@
  * @since 0.3.0
  */
 @Immutable
-public interface Propagators {
+public interface ContextPropagators {
 
   /**
    * Returns a {@link HttpTextFormat} propagator.
diff --git a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
similarity index 78%
rename from api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java
rename to api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
index 9c95c6c1ec1..be18f38b846 100644
--- a/api/src/main/java/io/opentelemetry/context/propagation/DefaultPropagators.java
+++ b/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
@@ -23,14 +23,15 @@
 import java.util.List;
 
 /**
- * {@code DefaultPropagators} is the default, built-in implementation of {@link Propagators}.
+ * {@code DefaultContextPropagators} is the default, built-in implementation of {@link
+ * ContextPropagators}.
  *
  * 

All the registered propagators are stored internally as a simple list, and are invoked * synchronically upon injection and extraction. * * @since 0.3.0 */ -public final class DefaultPropagators implements Propagators { +public final class DefaultContextPropagators implements ContextPropagators { private final HttpTextFormat textFormat; @Override @@ -39,29 +40,28 @@ public HttpTextFormat getHttpTextFormat() { } /** - * Returns a {@link Propagators.Builder} to create a new {@link Propagators} object. + * Returns a {@link DefaultContextPropagators.Builder} to create a new {@link ContextPropagators} + * object. * - *

See {@link Propagators.Builder}. - * - * @return a {@link Propagators.Builder}. + * @return a {@link DefaultContextPropagators.Builder}. * @since 0.3.0 */ public static Builder builder() { return new Builder(); } - private DefaultPropagators(HttpTextFormat textFormat) { + private DefaultContextPropagators(HttpTextFormat textFormat) { this.textFormat = textFormat; } /** - * {@link Builder} is used to construct a new {@code Propagators} object with the specified + * {@link Builder} is used to construct a new {@code ContextPropagators} object with the specified * propagators. * - *

This is a example of a {@code Propagators} object being created: + *

This is a example of a {@code ContextPropagators} object being created: * *

{@code
-   * Propagators propagators = DefaultPropagators.builder()
+   * ContextPropagators propagators = DefaultContextPropagators.builder()
    *     .addHttpTextFormat(new HttpTraceContext())
    *     .addHttpTextFormat(new HttpCorrelationContext())
    *     .addHttpTextFormat(new MyCustomContextPropagator())
@@ -91,17 +91,17 @@ public Builder addHttpTextFormat(HttpTextFormat textFormat) {
     }
 
     /**
-     * Builds a new {@code Propagators} with the specified propagators.
+     * Builds a new {@code ContextPropagators} with the specified propagators.
      *
-     * @return the newly created {@code Propagators} instance.
+     * @return the newly created {@code ContextPropagators} instance.
      * @since 0.3.0
      */
-    public Propagators build() {
+    public ContextPropagators build() {
       if (textPropagators.isEmpty()) {
-        return new DefaultPropagators(NoopHttpTextFormat.INSTANCE);
+        return new DefaultContextPropagators(NoopHttpTextFormat.INSTANCE);
       }
 
-      return new DefaultPropagators(new MultiHttpTextFormat(textPropagators));
+      return new DefaultContextPropagators(new MultiHttpTextFormat(textPropagators));
     }
   }
 
diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
index 33c38685d11..b37b9ddb946 100644
--- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
+++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
@@ -20,8 +20,8 @@
 import static org.junit.Assert.assertTrue;
 
 import io.opentelemetry.context.Scope;
-import io.opentelemetry.context.propagation.DefaultPropagators;
-import io.opentelemetry.context.propagation.Propagators;
+import io.opentelemetry.context.propagation.ContextPropagators;
+import io.opentelemetry.context.propagation.DefaultContextPropagators;
 import io.opentelemetry.correlationcontext.CorrelationContext;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
 import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager;
@@ -92,7 +92,7 @@ public void testDefault() {
         .isInstanceOf(DefaultCorrelationContextManager.getInstance().getClass());
     assertThat(OpenTelemetry.getCorrelationContextManager())
         .isEqualTo(OpenTelemetry.getCorrelationContextManager());
-    assertThat(OpenTelemetry.getPropagators()).isInstanceOf(DefaultPropagators.class);
+    assertThat(OpenTelemetry.getPropagators()).isInstanceOf(DefaultContextPropagators.class);
     assertThat(OpenTelemetry.getPropagators()).isEqualTo(OpenTelemetry.getPropagators());
   }
 
@@ -215,7 +215,7 @@ public void testCorrelationContextManagerNotFound() {
 
   @Test
   public void testPropagatorsSet() {
-    Propagators propagators = DefaultPropagators.builder().build();
+    ContextPropagators propagators = DefaultContextPropagators.builder().build();
     OpenTelemetry.setPropagators(propagators);
     assertThat(OpenTelemetry.getPropagators()).isEqualTo(propagators);
   }
diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java
index a2bfcfa4144..08d419c7e22 100644
--- a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java
+++ b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java
@@ -29,7 +29,7 @@
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
-/** Unit tests for {@link DefaultPropagators}. */
+/** Unit tests for {@link DefaultContextPropagators}. */
 @RunWith(JUnit4.class)
 public class DefaultPropagatorsTest {
 
@@ -38,15 +38,15 @@ public class DefaultPropagatorsTest {
   @Test
   public void addHttpTextFormatNull() {
     thrown.expect(NullPointerException.class);
-    DefaultPropagators.builder().addHttpTextFormat(null);
+    DefaultContextPropagators.builder().addHttpTextFormat(null);
   }
 
   @Test
   public void testInject() {
     CustomHttpTextFormat propagator1 = new CustomHttpTextFormat("prop1");
     CustomHttpTextFormat propagator2 = new CustomHttpTextFormat("prop2");
-    Propagators propagators =
-        DefaultPropagators.builder()
+    ContextPropagators propagators =
+        DefaultContextPropagators.builder()
             .addHttpTextFormat(propagator1)
             .addHttpTextFormat(propagator2)
             .build();
@@ -66,8 +66,8 @@ public void testExtract() {
     CustomHttpTextFormat propagator1 = new CustomHttpTextFormat("prop1");
     CustomHttpTextFormat propagator2 = new CustomHttpTextFormat("prop2");
     CustomHttpTextFormat propagator3 = new CustomHttpTextFormat("prop3");
-    Propagators propagators =
-        DefaultPropagators.builder()
+    ContextPropagators propagators =
+        DefaultContextPropagators.builder()
             .addHttpTextFormat(propagator1)
             .addHttpTextFormat(propagator2)
             .build();
@@ -86,7 +86,7 @@ public void testExtract() {
 
   @Test
   public void noopPropagator() {
-    Propagators propagators = DefaultPropagators.builder().build();
+    ContextPropagators propagators = DefaultContextPropagators.builder().build();
 
     Context context = Context.current();
     Map map = new HashMap<>();
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java
index 5c3c9d289c6..53f9389197f 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.opentracingshim;
 
-import io.opentelemetry.context.propagation.Propagators;
+import io.opentelemetry.context.propagation.ContextPropagators;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
 import io.opentelemetry.trace.Tracer;
 
@@ -43,7 +43,7 @@ SpanContextShimTable spanContextTable() {
     return telemetryInfo.spanContextTable();
   }
 
-  Propagators propagators() {
+  ContextPropagators propagators() {
     return telemetryInfo.propagators();
   }
 }
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java
index 3f6ce3a08f9..1af59880015 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.opentracingshim;
 
-import io.opentelemetry.context.propagation.Propagators;
+import io.opentelemetry.context.propagation.ContextPropagators;
 import io.opentelemetry.correlationcontext.CorrelationContext;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
 import io.opentelemetry.trace.Tracer;
@@ -29,10 +29,11 @@ final class TelemetryInfo {
   private final Tracer tracer;
   private final CorrelationContextManager contextManager;
   private final CorrelationContext emptyCorrelationContext;
-  private final Propagators propagators;
+  private final ContextPropagators propagators;
   private final SpanContextShimTable spanContextTable;
 
-  TelemetryInfo(Tracer tracer, CorrelationContextManager contextManager, Propagators propagators) {
+  TelemetryInfo(
+      Tracer tracer, CorrelationContextManager contextManager, ContextPropagators propagators) {
     this.tracer = tracer;
     this.contextManager = contextManager;
     this.propagators = propagators;
@@ -56,7 +57,7 @@ CorrelationContext emptyCorrelationContext() {
     return emptyCorrelationContext;
   }
 
-  Propagators propagators() {
+  ContextPropagators propagators() {
     return propagators;
   }
 }

From b0649dacf2038a1c1f94e9917ca89d3bd3958404 Mon Sep 17 00:00:00 2001
From: Carlos Alberto Cortez 
Date: Thu, 20 Feb 2020 06:06:51 +0100
Subject: [PATCH 24/42] Move context/ members in api/ to context_prop/

---
 .../context/propagation/ContextPropagators.java             | 0
 .../io/opentelemetry/context/propagation/ContextUtils.java  | 0
 .../context/propagation/DefaultContextPropagators.java      | 6 ++++--
 .../context/propagation/DefaultPropagatorsTest.java         | 0
 4 files changed, 4 insertions(+), 2 deletions(-)
 rename {api => context_prop}/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java (100%)
 rename {api => context_prop}/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java (100%)
 rename {api => context_prop}/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java (98%)
 rename {api => context_prop}/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java (100%)

diff --git a/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java
similarity index 100%
rename from api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java
rename to context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java
diff --git a/api/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java
similarity index 100%
rename from api/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java
rename to context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java
diff --git a/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
similarity index 98%
rename from api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
rename to context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
index be18f38b846..5c9d5a10ae1 100644
--- a/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
+++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java
@@ -17,7 +17,6 @@
 package io.opentelemetry.context.propagation;
 
 import io.grpc.Context;
-import io.opentelemetry.internal.Utils;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -85,7 +84,10 @@ public static final class Builder {
      * @since 0.3.0
      */
     public Builder addHttpTextFormat(HttpTextFormat textFormat) {
-      Utils.checkNotNull(textFormat, "textFormat");
+      if (textFormat == null) {
+        throw new NullPointerException("textFormat");
+      }
+
       textPropagators.add(textFormat);
       return this;
     }
diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java
similarity index 100%
rename from api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java
rename to context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java

From 762408ef2934e3584c6179c21606369b2df529e3 Mon Sep 17 00:00:00 2001
From: Carlos Alberto Cortez 
Date: Thu, 20 Feb 2020 06:32:34 +0100
Subject: [PATCH 25/42] Add check/tests for null scoped Contexts.

---
 .../context/propagation/ContextUtils.java     |  4 ++
 .../context/propagation/ContextUtilsTest.java | 66 +++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java

diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java
index c0d7a7b10a1..38fae8ef9cc 100644
--- a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java
+++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java
@@ -39,6 +39,10 @@ public final class ContextUtils {
    * @since 0.1.0
    */
   public static Scope withScopedContext(Context context) {
+    if (context == null) {
+      throw new NullPointerException("context");
+    }
+
     return new ContextInScope(context);
   }
 
diff --git a/context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java b/context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java
new file mode 100644
index 00000000000..228b11cf80a
--- /dev/null
+++ b/context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2020, 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.context.propagation;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import io.grpc.Context;
+import io.opentelemetry.context.Scope;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link DefaultContextPropagators}. */
+@RunWith(JUnit4.class)
+public class ContextUtilsTest {
+
+  @Rule public final ExpectedException thrown = ExpectedException.none();
+
+  private static final Context.Key SIMPLE_KEY = Context.key("simple");
+
+  @Test
+  public void withScopedContextNull() {
+    thrown.expect(NullPointerException.class);
+    ContextUtils.withScopedContext(null);
+  }
+
+  @Test
+  public void withScopedContext() {
+    Context prevCtx = Context.current();
+    Context ctx = Context.current().withValue(SIMPLE_KEY, "value1");
+    Scope scope = ContextUtils.withScopedContext(ctx);
+    try {
+      assertThat(scope).isNotNull();
+      assertThat(Context.current()).isEqualTo(ctx);
+
+      Context ctx2 = Context.current().withValue(SIMPLE_KEY, "value2");
+      Scope scope2 = ContextUtils.withScopedContext(ctx2);
+      try {
+        assertThat(scope2).isNotNull();
+        assertThat(Context.current()).isEqualTo(ctx2);
+      } finally {
+        scope2.close();
+      }
+      assertThat(Context.current()).isEqualTo(ctx);
+    } finally {
+      scope.close();
+    }
+    assertThat(Context.current()).isEqualTo(prevCtx);
+  }
+}

From 05b7628cd78e1d9db32145ec1128a86cd42463fd Mon Sep 17 00:00:00 2001
From: Carlos Alberto Cortez 
Date: Fri, 21 Feb 2020 04:36:23 +0100
Subject: [PATCH 26/42] Rename ContextUtils classes to better alternatives.

---
 .../DefaultCorrelationContextManager.java     |  6 +--
 ...ils.java => CorrelationsContextUtils.java} |  7 ++--
 .../io/opentelemetry/trace/DefaultTracer.java | 10 ++---
 .../trace/propagation/HttpTraceContext.java   |  4 +-
 ...extUtils.java => TracingContextUtils.java} | 23 ++++++-----
 .../DefaultCorrelationContextManagerTest.java |  4 +-
 ...java => CorrelationsContextUtilsTest.java} | 16 ++++----
 .../trace/DefaultTracerTest.java              | 14 +++----
 .../propagation/HttpTraceContextTest.java     | 27 ++++++-------
 ...Test.java => TracingContextUtilsTest.java} | 39 ++++++++++---------
 .../contrib/trace/CurrentSpanUtils.java       |  6 +--
 .../contrib/trace/CurrentSpanUtilsTest.java   |  4 +-
 .../opentracingshim/Propagation.java          | 12 +++---
 .../CorrelationContextManagerSdk.java         |  6 +--
 .../CorrelationContextSdk.java                |  4 +-
 .../sdk/trace/SpanBuilderSdk.java             | 10 ++---
 .../io/opentelemetry/sdk/trace/TracerSdk.java |  6 +--
 .../CorrelationContextManagerSdkTest.java     |  4 +-
 .../CorrelationContextSdkTest.java            |  4 +-
 .../sdk/trace/SpanBuilderSdkTest.java         | 14 ++++---
 .../sdk/trace/TracerSdkTest.java              |  4 +-
 21 files changed, 115 insertions(+), 109 deletions(-)
 rename api/src/main/java/io/opentelemetry/correlationcontext/propagation/{ContextUtils.java => CorrelationsContextUtils.java} (95%)
 rename api/src/main/java/io/opentelemetry/trace/propagation/{ContextUtils.java => TracingContextUtils.java} (88%)
 rename api/src/test/java/io/opentelemetry/correlationcontext/propagation/{ContextUtilsTest.java => CorrelationsContextUtilsTest.java} (74%)
 rename api/src/test/java/io/opentelemetry/trace/propagation/{ContextUtilsTest.java => TracingContextUtilsTest.java} (69%)

diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java
index 66bf8f3d946..b3063b32408 100644
--- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java
+++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java
@@ -18,7 +18,7 @@
 
 import io.grpc.Context;
 import io.opentelemetry.context.Scope;
-import io.opentelemetry.correlationcontext.propagation.ContextUtils;
+import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils;
 import io.opentelemetry.internal.Utils;
 import javax.annotation.concurrent.Immutable;
 import javax.annotation.concurrent.ThreadSafe;
@@ -47,7 +47,7 @@ public static CorrelationContextManager getInstance() {
 
   @Override
   public CorrelationContext getCurrentContext() {
-    return ContextUtils.getCorrelationContextWithDefault(Context.current());
+    return CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current());
   }
 
   @Override
@@ -57,7 +57,7 @@ public CorrelationContext.Builder contextBuilder() {
 
   @Override
   public Scope withContext(CorrelationContext distContext) {
-    return ContextUtils.withScopedCorrelationContext(distContext);
+    return CorrelationsContextUtils.withScopedCorrelationContext(distContext);
   }
 
   @Immutable
diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java
similarity index 95%
rename from api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java
rename to api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java
index 91f211b910e..1e2fe67798e 100644
--- a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/ContextUtils.java
+++ b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java
@@ -18,6 +18,7 @@
 
 import io.grpc.Context;
 import io.opentelemetry.context.Scope;
+import io.opentelemetry.context.propagation.ContextUtils;
 import io.opentelemetry.correlationcontext.CorrelationContext;
 import io.opentelemetry.correlationcontext.EmptyCorrelationContext;
 import javax.annotation.concurrent.Immutable;
@@ -32,7 +33,7 @@
  * @since 0.1.0
  */
 @Immutable
-public final class ContextUtils {
+public final class CorrelationsContextUtils {
   private static final Context.Key DIST_CONTEXT_KEY =
       Context.key("opentelemetry-dist-context-key");
   private static final CorrelationContext DEFAULT_VALUE = EmptyCorrelationContext.getInstance();
@@ -104,8 +105,8 @@ public static CorrelationContext getCorrelationContextWithDefault(Context contex
    */
   public static Scope withScopedCorrelationContext(CorrelationContext distContext) {
     Context context = withCorrelationContext(distContext);
-    return io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context);
+    return ContextUtils.withScopedContext(context);
   }
 
-  private ContextUtils() {}
+  private CorrelationsContextUtils() {}
 }
diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
index f6263070650..fc4ffe207c0 100644
--- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
+++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
@@ -19,7 +19,7 @@
 import io.grpc.Context;
 import io.opentelemetry.context.Scope;
 import io.opentelemetry.internal.Utils;
-import io.opentelemetry.trace.propagation.ContextUtils;
+import io.opentelemetry.trace.propagation.TracingContextUtils;
 import java.util.Map;
 import javax.annotation.concurrent.ThreadSafe;
 
@@ -44,12 +44,12 @@ public static Tracer getInstance() {
 
   @Override
   public Span getCurrentSpan() {
-    return ContextUtils.getSpanWithDefault(Context.current());
+    return TracingContextUtils.getSpanWithDefault(Context.current());
   }
 
   @Override
   public Scope withSpan(Span span) {
-    return ContextUtils.withScopedSpan(span);
+    return TracingContextUtils.withScopedSpan(span);
   }
 
   @Override
@@ -71,7 +71,7 @@ static NoopSpanBuilder create(String spanName) {
     @Override
     public Span startSpan() {
       if (spanContext == null && !isRootSpan) {
-        spanContext = ContextUtils.getAnySpanContext(Context.current());
+        spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current());
       }
 
       return spanContext != null && !SpanContext.getInvalid().equals(spanContext)
@@ -96,7 +96,7 @@ public NoopSpanBuilder setParent(SpanContext remoteParent) {
     @Override
     public NoopSpanBuilder setParent(Context context) {
       Utils.checkNotNull(context, "context");
-      spanContext = ContextUtils.getAnySpanContext(context);
+      spanContext = TracingContextUtils.getEffectiveSpanContext(context);
       if (spanContext == null) {
         isRootSpan = true;
       }
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 b3f37e8ba67..60db0ca5748 100644
--- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java
+++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java
@@ -75,7 +75,7 @@ public  void inject(Context context, C carrier, Setter setter) {
     checkNotNull(setter, "setter");
     checkNotNull(carrier, "carrier");
 
-    SpanContext spanContext = ContextUtils.getAnySpanContext(context);
+    SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(context);
     if (spanContext == null) {
       return;
     }
@@ -120,7 +120,7 @@ private static  void injectImpl(SpanContext spanContext, C carrier, Setter
     checkNotNull(getter, "getter");
 
     SpanContext spanContext = extractImpl(carrier, getter);
-    return ContextUtils.withSpanContext(spanContext, context);
+    return TracingContextUtils.withSpanContext(spanContext, context);
   }
 
   private static  SpanContext extractImpl(C carrier, Getter getter) {
diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java
similarity index 88%
rename from api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java
rename to api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java
index f055da6d127..7410aac8dad 100644
--- a/api/src/main/java/io/opentelemetry/trace/propagation/ContextUtils.java
+++ b/api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java
@@ -16,10 +16,9 @@
 
 package io.opentelemetry.trace.propagation;
 
-import static io.opentelemetry.context.propagation.ContextUtils.withScopedContext;
-
 import io.grpc.Context;
 import io.opentelemetry.context.Scope;
+import io.opentelemetry.context.propagation.ContextUtils;
 import io.opentelemetry.trace.DefaultSpan;
 import io.opentelemetry.trace.Span;
 import io.opentelemetry.trace.SpanContext;
@@ -35,7 +34,7 @@
  * @since 0.1.0
  */
 @Immutable
-public final class ContextUtils {
+public final class TracingContextUtils {
   private static final Context.Key CONTEXT_SPAN_KEY =
       Context.key("opentelemetry-trace-span-key");
   private static final Context.Key CONTEXT_SPANCONTEXT_KEY =
@@ -144,23 +143,23 @@ public static SpanContext getSpanContext(Context context) {
   }
 
   /**
-   * Returns any {@link SpanContext} from the specified {@code Context}.
+   * Returns the effective {@link SpanContext} from the specified {@code Context}.
    *
-   * 

This method tries to get any non-null {@link SpanContext} in {@code Context}, giving higher - * priority to {@code Span#getContext()} and then falling back to {@code SpanContext}. If none is - * found, this method returns {@code null}. + *

This method tries to get any effective non-null {@link SpanContext} in {@code Context}, + * giving higher priority to {@code Span#getContext()} and then falling back to {@code + * 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 */ - public static SpanContext getAnySpanContext(Context context) { - Span span = ContextUtils.getSpan(context); + public static SpanContext getEffectiveSpanContext(Context context) { + Span span = getSpan(context); if (span != null) { return span.getContext(); } - return ContextUtils.getSpanContext(context); + return getSpanContext(context); } /** @@ -172,8 +171,8 @@ public static SpanContext getAnySpanContext(Context context) { * @since 0.1.0 */ public static Scope withScopedSpan(Span span) { - return withScopedContext(withSpan(span)); + return ContextUtils.withScopedContext(withSpan(span)); } - private ContextUtils() {} + private TracingContextUtils() {} } diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 7ac63a460a4..fdb55c34c22 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -20,7 +20,7 @@ import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.correlationcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; import java.util.Arrays; import java.util.Collection; import javax.annotation.Nullable; @@ -68,7 +68,7 @@ public void getCurrentContext_DefaultContext() { @Test public void getCurrentContext_ContextSetToNull() { - Context orig = ContextUtils.withCorrelationContext(null).attach(); + Context orig = CorrelationsContextUtils.withCorrelationContext(null).attach(); try { CorrelationContext distContext = defaultCorrelationContextManager.getCurrentContext(); assertThat(distContext).isNotNull(); diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java similarity index 74% rename from api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java rename to api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java index 0b2b7bdd862..90f0e510972 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java @@ -24,35 +24,37 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Unit tests for {@link ContextUtils}. */ +/** Unit tests for {@link CorrelationsContextUtils}. */ @RunWith(JUnit4.class) -public final class ContextUtilsTest { +public final class CorrelationsContextUtilsTest { @Test public void testGetCurrentDistributedContext_DefaultContext() { - CorrelationContext distContext = ContextUtils.getCorrelationContext(Context.current()); + CorrelationContext distContext = + CorrelationsContextUtils.getCorrelationContext(Context.current()); assertThat(distContext).isNull(); } @Test public void testGetCurrentDistributedContex_DefaultContext_WithoutExplicitContext() { - CorrelationContext distContext = ContextUtils.getCorrelationContext(); + CorrelationContext distContext = CorrelationsContextUtils.getCorrelationContext(); assertThat(distContext).isNull(); } @Test public void testGetCurrentDistributedContextWithDefault_DefaultContext() { CorrelationContext distContext = - ContextUtils.getCorrelationContextWithDefault(Context.current()); + CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @Test public void testGetCurrentDistributedContextWithDefault_ContextSetToNull() { - Context orig = ContextUtils.withCorrelationContext(null, Context.current()).attach(); + Context orig = + CorrelationsContextUtils.withCorrelationContext(null, Context.current()).attach(); try { CorrelationContext distContext = - ContextUtils.getCorrelationContextWithDefault(Context.current()); + CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current()); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 43e2f63602f..0654ffbcf05 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -20,7 +20,7 @@ import io.grpc.Context; import io.opentelemetry.context.Scope; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -111,10 +111,10 @@ public void testSpanContextPropagation() { @Test public void testSpanContextPropagationFromSpanInContext() { Context context = - ContextUtils.withSpanContext( + TracingContextUtils.withSpanContext( SpanContext.create( new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()), - ContextUtils.withSpan(new DefaultSpan(spanContext))); + TracingContextUtils.withSpan(new DefaultSpan(spanContext))); // Span in Context has higher priority than SpanContext. Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); @@ -123,7 +123,7 @@ public void testSpanContextPropagationFromSpanInContext() { @Test public void testSpanContextPropagationFromContext() { - Context context = ContextUtils.withSpanContext(spanContext); + Context context = TracingContextUtils.withSpanContext(spanContext); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); @@ -143,7 +143,7 @@ public void testSpanContextPropagationCurrentSpan() { @Test public void testSpanContextPropagationCurrentSpanContext() { - Context context = ContextUtils.withSpanContext(spanContext); + Context context = TracingContextUtils.withSpanContext(spanContext); Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); @@ -156,10 +156,10 @@ public void testSpanContextPropagationCurrentSpanContext() { @Test public void testSpanContextPropagationCurrentContextValues() { Context context = - ContextUtils.withSpanContext( + TracingContextUtils.withSpanContext( SpanContext.create( new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()), - ContextUtils.withSpan(new DefaultSpan(spanContext))); + TracingContextUtils.withSpan(new DefaultSpan(spanContext))); Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); // Span in Context has higher priority than SpanContext. try { 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 2d6d0e706e6..b0662ee7ec8 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -91,7 +91,7 @@ public void inject_Nothing() { public void inject_SampledContext() { Map carrier = new LinkedHashMap(); Context context = - ContextUtils.withSpanContext( + TracingContextUtils.withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT)); httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); @@ -101,7 +101,7 @@ public void inject_SampledContext() { public void inject_NotSampledContext() { Map carrier = new LinkedHashMap(); Context context = - ContextUtils.withSpanContext( + TracingContextUtils.withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); @@ -111,7 +111,7 @@ public void inject_NotSampledContext() { public void inject_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap(); Context context = - ContextUtils.withSpanContext( + TracingContextUtils.withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) @@ -123,7 +123,7 @@ public void inject_SampledContext_WithTraceState() { public void inject_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap(); Context context = - ContextUtils.withSpanContext( + TracingContextUtils.withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT)); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) @@ -146,7 +146,7 @@ public void inject_Span() { SpanContext.create( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)) .startSpan(); - Context context = ContextUtils.withSpan(span); + Context context = TracingContextUtils.withSpan(span); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -167,7 +167,8 @@ public void inject_Span_SpanContextPresent() { .startSpan(); SpanContext spanContext = SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT); - Context context = ContextUtils.withSpanContext(spanContext, ContextUtils.withSpan(span)); + Context context = + TracingContextUtils.withSpanContext(spanContext, TracingContextUtils.withSpan(span)); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -179,7 +180,7 @@ public void extract_SampledContext() { Map carrier = new LinkedHashMap(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT)); @@ -190,7 +191,7 @@ public void extract_NotSampledContext() { Map carrier = new LinkedHashMap(); carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); @@ -202,7 +203,7 @@ public void extract_SampledContext_WithTraceState() { carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)); @@ -214,7 +215,7 @@ public void extract_NotSampledContext_WithTraceState() { carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT)); @@ -225,7 +226,7 @@ public void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACEPARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); @@ -237,7 +238,7 @@ public void extract_NotSampledContext_EmptyTraceState() { carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACESTATE, ""); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT)); @@ -249,7 +250,7 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING_WITH_SPACES); Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(ContextUtils.getSpanContext(context)) + assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT)); diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java similarity index 69% rename from api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java rename to api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java index 3b5098cd432..868da269ac0 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/ContextUtilsTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java @@ -31,33 +31,33 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Unit tests for {@link ContextUtils}. */ +/** Unit tests for {@link TracingContextUtils}. */ @RunWith(JUnit4.class) -public final class ContextUtilsTest { +public final class TracingContextUtilsTest { @Test public void testGetCurrentSpan_DefaultContext() { - Span span = ContextUtils.getSpan(Context.current()); + Span span = TracingContextUtils.getSpan(Context.current()); assertThat(span).isNull(); } @Test public void testGetCurrentSpan_DefaultContext_WithoutExplicitContext() { - Span span = ContextUtils.getSpan(); + Span span = TracingContextUtils.getSpan(); assertThat(span).isNull(); } @Test public void testGetCurrentSpanWithDefault_DefaultContext() { - Span span = ContextUtils.getSpanWithDefault(Context.current()); + Span span = TracingContextUtils.getSpanWithDefault(Context.current()); assertThat(span).isNotNull(); assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); } @Test public void testGetCurrentSpanWithDefault_ContextSetToNull() { - Context orig = ContextUtils.withSpan(null, Context.current()).attach(); + Context orig = TracingContextUtils.withSpan(null, Context.current()).attach(); try { - Span span = ContextUtils.getSpanWithDefault(Context.current()); + Span span = TracingContextUtils.getSpanWithDefault(Context.current()); assertThat(span).isNotNull(); assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); } finally { @@ -67,27 +67,28 @@ public void testGetCurrentSpanWithDefault_ContextSetToNull() { @Test public void testGetCurrentSpanContext_DefaultContext() { - SpanContext spanContext = ContextUtils.getSpanContext(Context.current()); + SpanContext spanContext = TracingContextUtils.getSpanContext(Context.current()); assertThat(spanContext).isNull(); } @Test public void testGetCurrentSpanContext_DefaultContext_WithoutExplicitContext() { - SpanContext spanContext = ContextUtils.getSpanContext(); + SpanContext spanContext = TracingContextUtils.getSpanContext(); assertThat(spanContext).isNull(); } @Test public void testAnySpanContext_DefaultContext() { - SpanContext spanContext = ContextUtils.getAnySpanContext(Context.current()); + SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); assertThat(spanContext).isNull(); } @Test public void testAnySpanContext_SetSpanContext() { - Context orig = ContextUtils.withSpanContext(DefaultSpan.getInvalid().getContext()).attach(); + Context orig = + TracingContextUtils.withSpanContext(DefaultSpan.getInvalid().getContext()).attach(); try { - SpanContext spanContext = ContextUtils.getAnySpanContext(Context.current()); + SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); assertThat(spanContext).isNotNull(); assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); } finally { @@ -97,9 +98,9 @@ public void testAnySpanContext_SetSpanContext() { @Test public void testAnySpanContext_SetSpan() { - Context orig = ContextUtils.withSpan(DefaultSpan.getInvalid()).attach(); + Context orig = TracingContextUtils.withSpan(DefaultSpan.getInvalid()).attach(); try { - SpanContext spanContext = ContextUtils.getAnySpanContext(Context.current()); + SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); assertThat(spanContext).isNotNull(); assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); } finally { @@ -114,19 +115,19 @@ public void testSetSpanAndSpanContext() { SpanContext.create( new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()); - Context context1 = ContextUtils.withSpan(span).attach(); + Context context1 = TracingContextUtils.withSpan(span).attach(); try { - Context context2 = ContextUtils.withSpanContext(spanContext).attach(); + Context context2 = TracingContextUtils.withSpanContext(spanContext).attach(); try { - SpanContext spanContextInContext = ContextUtils.getSpanContext(Context.current()); + SpanContext spanContextInContext = TracingContextUtils.getSpanContext(Context.current()); assertThat(spanContextInContext).isNotNull(); assertThat(spanContextInContext).isSameInstanceAs(spanContext); - Span spanInContext = ContextUtils.getSpan(Context.current()); + Span spanInContext = TracingContextUtils.getSpan(Context.current()); assertThat(spanInContext).isNotNull(); assertThat(spanInContext).isSameInstanceAs(span); - SpanContext anySpanContext = ContextUtils.getAnySpanContext(Context.current()); + SpanContext anySpanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); assertThat(anySpanContext).isNotNull(); assertThat(anySpanContext).isSameInstanceAs(span.getContext()); } finally { diff --git a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java index 6fa2e22f89b..dfc38d6c518 100644 --- a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java +++ b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java @@ -19,7 +19,7 @@ import io.grpc.Context; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; import java.util.concurrent.Callable; /** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */ @@ -65,7 +65,7 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) { @Override public void run() { - Context origContext = ContextUtils.withSpan(span).attach(); + Context origContext = TracingContextUtils.withSpan(span).attach(); try { runnable.run(); } catch (Throwable t) { @@ -98,7 +98,7 @@ private CallableInSpan(Span span, Callable callable, boolean endSpan) { @Override public V call() throws Exception { - Context origContext = ContextUtils.withSpan(span).attach(); + Context origContext = TracingContextUtils.withSpan(span).attach(); try { return callable.call(); } catch (Exception e) { diff --git a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java index d5205fd9b65..a3e68a01947 100644 --- a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java +++ b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java @@ -24,7 +24,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; import java.util.concurrent.Callable; import org.junit.Before; import org.junit.Test; @@ -250,6 +250,6 @@ public Object call() throws Exception { } private static Span getCurrentSpan() { - return ContextUtils.getSpanWithDefault(Context.current()); + return TracingContextUtils.getSpanWithDefault(Context.current()); } } 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 b26887e662f..44dbd21a525 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -31,11 +31,11 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = Context.current(); context = - io.opentelemetry.trace.propagation.ContextUtils.withSpanContext( + io.opentelemetry.trace.propagation.TracingContextUtils.withSpanContext( contextShim.getSpanContext(), context); context = - io.opentelemetry.correlationcontext.propagation.ContextUtils.withCorrelationContext( - contextShim.getCorrelationContext(), context); + io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils + .withCorrelationContext(contextShim.getCorrelationContext(), context); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -53,9 +53,9 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { return new SpanContextShim( telemetryInfo, - io.opentelemetry.trace.propagation.ContextUtils.getSpanContext(context), - io.opentelemetry.correlationcontext.propagation.ContextUtils.getCorrelationContext( - context)); + io.opentelemetry.trace.propagation.TracingContextUtils.getSpanContext(context), + io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils + .getCorrelationContext(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 35303915d17..49cfb4b67cf 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -20,7 +20,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; -import io.opentelemetry.correlationcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; /** * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. @@ -29,7 +29,7 @@ public class CorrelationContextManagerSdk implements CorrelationContextManager { @Override public CorrelationContext getCurrentContext() { - return ContextUtils.getCorrelationContextWithDefault(Context.current()); + return CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current()); } @Override @@ -39,6 +39,6 @@ public CorrelationContext.Builder contextBuilder() { @Override public Scope withContext(CorrelationContext distContext) { - return ContextUtils.withScopedCorrelationContext(distContext); + return CorrelationsContextUtils.withScopedCorrelationContext(distContext); } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index 8e1d53128e3..fbfe6f37682 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -25,7 +25,7 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; -import io.opentelemetry.correlationcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; import io.opentelemetry.internal.Utils; import java.util.Collection; import java.util.Collections; @@ -132,7 +132,7 @@ public CorrelationContext.Builder setParent(CorrelationContext parent) { @Override public CorrelationContext.Builder setParent(Context context) { Utils.checkNotNull(context, "context"); - CorrelationContext corrContext = ContextUtils.getCorrelationContext(context); + CorrelationContext corrContext = CorrelationsContextUtils.getCorrelationContext(context); if (corrContext != null) { setParent(corrContext); } else { diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 1cf0daaae34..ad64b6389d4 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -36,7 +36,7 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.Tracestate; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -106,8 +106,8 @@ public Span.Builder setParent(SpanContext remoteParent) { public Span.Builder setParent(Context context) { Utils.checkNotNull(context, "context"); - Span span = ContextUtils.getSpan(context); - SpanContext spanContext = ContextUtils.getSpanContext(context); + Span span = TracingContextUtils.getSpan(context); + SpanContext spanContext = TracingContextUtils.getSpanContext(context); if (span != null) { setParent(span); } else if (spanContext != null) { @@ -266,7 +266,7 @@ private static SpanContext parent( case NO_PARENT: return null; case CURRENT_CONTEXT: - return ContextUtils.getAnySpanContext(Context.current()); + return TracingContextUtils.getEffectiveSpanContext(Context.current()); case EXPLICIT_PARENT: return explicitParent.getContext(); case EXPLICIT_REMOTE_PARENT: @@ -279,7 +279,7 @@ private static SpanContext parent( private static Span parentSpan(ParentType parentType, Span explicitParent) { switch (parentType) { case CURRENT_CONTEXT: - Span span = ContextUtils.getSpan(); + Span span = TracingContextUtils.getSpan(); return DefaultSpan.getInvalid().equals(span) ? null : span; case EXPLICIT_PARENT: return explicitParent; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index b011d81898a..b0c5558f7d2 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -22,7 +22,7 @@ import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; /** {@link TracerSdk} is SDK implementation of {@link Tracer}. */ public class TracerSdk implements Tracer { @@ -36,12 +36,12 @@ public class TracerSdk implements Tracer { @Override public Span getCurrentSpan() { - return ContextUtils.getSpanWithDefault(Context.current()); + return TracingContextUtils.getSpanWithDefault(Context.current()); } @Override public Scope withSpan(Span span) { - return ContextUtils.withScopedSpan(span); + return TracingContextUtils.withScopedSpan(span); } @Override diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index f33ec51f474..3d88a8c3b96 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -22,7 +22,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.EmptyCorrelationContext; -import io.opentelemetry.correlationcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,7 +52,7 @@ public void testGetCurrentContext_DefaultContext() { @Test public void testGetCurrentContext_ContextSetToNull() { - Context orig = ContextUtils.withCorrelationContext(null).attach(); + Context orig = CorrelationsContextUtils.withCorrelationContext(null).attach(); try { CorrelationContext distContext = contextManager.getCurrentContext(); assertThat(distContext).isNotNull(); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index cb7c8c57e60..cdc588b6f53 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -27,7 +27,7 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; -import io.opentelemetry.correlationcontext.propagation.ContextUtils; +import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -143,7 +143,7 @@ public void setParent_nullContext() { @Test public void setParent_fromContext() { - Context context = ContextUtils.withCorrelationContext(listToCorrelationContext(T1)); + Context context = CorrelationsContextUtils.withCorrelationContext(listToCorrelationContext(T1)); CorrelationContext corrContext = contextManager.contextBuilder().setParent(context).build(); assertThat(corrContext.getEntries()).containsExactly(T1); } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index aade00d52d6..025436e637b 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -32,7 +32,7 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.Tracestate; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -410,7 +410,7 @@ public void overrideNoParent_remoteParent() { @Test public void parentExplicitContext_withSpan() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = ContextUtils.withSpan(parent); + Context context = TracingContextUtils.withSpan(parent); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -429,7 +429,7 @@ public void parentExplicitContext_withSpan() { @Test public void parentExplicitContext_withSpanContext() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = ContextUtils.withSpanContext(parent.getContext()); + Context context = TracingContextUtils.withSpanContext(parent.getContext()); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -449,7 +449,8 @@ public void parentExplicitContext_withSpanContext() { public void parentExplicitContext_withValues() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); Context context = - ContextUtils.withSpanContext(sampledSpanContext, ContextUtils.withSpan(parent)); + TracingContextUtils.withSpanContext( + sampledSpanContext, TracingContextUtils.withSpan(parent)); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -507,7 +508,7 @@ public void parentCurrentSpan() { @Test public void parentCurrentSpanContext() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = ContextUtils.withSpanContext(parent.getContext()); + Context context = TracingContextUtils.withSpanContext(parent.getContext()); Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); try { RecordEventsReadableSpan span = @@ -528,7 +529,8 @@ public void parentCurrentSpanContext() { public void parentCurrentContextValues() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); Context context = - ContextUtils.withSpanContext(sampledSpanContext, ContextUtils.withSpan(parent)); + TracingContextUtils.withSpanContext( + sampledSpanContext, TracingContextUtils.withSpan(parent)); Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); try { RecordEventsReadableSpan span = diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index 45d3dc5b6c5..62637b08b07 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -23,7 +23,7 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.propagation.ContextUtils; +import io.opentelemetry.trace.propagation.TracingContextUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -66,7 +66,7 @@ public void defaultSpanBuilder() { @Test public void getCurrentSpan() { assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - Context origContext = ContextUtils.withSpan(span).attach(); + Context origContext = TracingContextUtils.withSpan(span).attach(); // Make sure context is detached even if test fails. try { assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span); From 1113f61b7960fa8f6b675e49087ae4e5684d6291 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Mon, 9 Mar 2020 23:20:40 +0100 Subject: [PATCH 27/42] Update the context* util classes. --- .../CorrelationsContextUtils.java | 99 ++++++++++ .../propagation/CorrelationsContextUtils.java | 112 ----------- .../trace/TracingContextUtils.java | 97 ++++++++++ .../propagation/TracingContextUtils.java | 178 ------------------ .../CorrelationsContextUtilsTest.java | 83 ++++++++ .../CorrelationsContextUtilsTest.java | 64 ------- .../trace/TracingContextUtilsTest.java | 72 +++++++ .../propagation/TracingContextUtilsTest.java | 140 -------------- .../{propagation => }/ContextUtils.java | 6 +- .../{propagation => }/ContextUtilsTest.java | 3 +- 10 files changed, 353 insertions(+), 501 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java delete mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java create mode 100644 api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java create mode 100644 api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java delete mode 100644 api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java create mode 100644 api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java rename context_prop/src/main/java/io/opentelemetry/context/{propagation => }/ContextUtils.java (89%) rename context_prop/src/test/java/io/opentelemetry/context/{propagation => }/ContextUtilsTest.java (95%) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java new file mode 100644 index 00000000000..38e9ae45c52 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java @@ -0,0 +1,99 @@ +/* + * 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.correlationcontext; + +import io.grpc.Context; +import io.opentelemetry.context.ContextUtils; +import io.opentelemetry.context.Scope; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * Utility methods for accessing the {@link CorrelationContext} contained in the {@link + * io.grpc.Context}. + * + * @since 0.1.0 + */ +@Immutable +public final class CorrelationsContextUtils { + private static final Context.Key CORR_CONTEXT_KEY = + Context.key("opentelemetry-corr-context-key"); + + /** + * Creates a new {@code Context} with the given value set. + * + * @param corrContext the value to be set. + * @param context the parent {@code Context}. + * @return a new context with the given value set. + * @since 0.1.0 + */ + public static Context withCorrelationContext(CorrelationContext corrContext, Context context) { + return context.withValue(CORR_CONTEXT_KEY, corrContext); + } + + /** + * Returns the {@link CorrelationContext} from the current {@code Context}, falling back to an + * empty {@link CorrelationContext}. + * + * @return the {@link CorrelationContext} from the current {@code Context}. + * @since 0.3.0 + */ + public static CorrelationContext getCurrentCorrelationContext() { + return getCorrelationContext(Context.current()); + } + + /** + * Returns the {@link CorrelationContext} from the specified {@code Context}, falling back to an + * empty {@link CorrelationContext}. + * + * @param context the specified {@code Context}. + * @return the {@link CorrelationContext} from the specified {@code Context}. + * @since 0.3.0 + */ + public static CorrelationContext getCorrelationContext(Context context) { + CorrelationContext corrContext = CORR_CONTEXT_KEY.get(context); + return corrContext == null ? EmptyCorrelationContext.getInstance() : corrContext; + } + + /** + * Returns the {@link CorrelationContext} from the specified {@code Context}. If none is found, + * this method returns {code null}. + * + * @param context the specified {@code Context}. + * @return the {@link CorrelationContext} from the specified {@code Context}. + * @since 0.1.0 + */ + @Nullable + public static CorrelationContext getCorrelationContextWithoutDefault(Context context) { + return CORR_CONTEXT_KEY.get(context); + } + + /** + * Returns a new {@link Scope} encapsulating the provided {@link CorrelationContext} added to the + * current {@code Context}. + * + * @param corrContext the {@link CorrelationContext} to be added to the current {@code Context}. + * @return the {@link Scope} for the updated {@code Context}. + * @since 0.1.0 + */ + public static Scope withScopedCorrelationContext(CorrelationContext corrContext) { + Context context = withCorrelationContext(corrContext, Context.current()); + return ContextUtils.withScopedContext(context); + } + + private CorrelationsContextUtils() {} +} diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java deleted file mode 100644 index 1e2fe67798e..00000000000 --- a/api/src/main/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtils.java +++ /dev/null @@ -1,112 +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.correlationcontext.propagation; - -import io.grpc.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.ContextUtils; -import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.correlationcontext.EmptyCorrelationContext; -import javax.annotation.concurrent.Immutable; - -/** - * Utility methods for accessing the {@link CorrelationContext} contained in the {@link - * io.grpc.Context}. - * - *

Most code should interact with the current context via the public APIs in {@link - * CorrelationContext} and avoid accessing this class directly. - * - * @since 0.1.0 - */ -@Immutable -public final class CorrelationsContextUtils { - private static final Context.Key DIST_CONTEXT_KEY = - Context.key("opentelemetry-dist-context-key"); - private static final CorrelationContext DEFAULT_VALUE = EmptyCorrelationContext.getInstance(); - - /** - * Creates a new {@code Context} with the given value set. - * - * @param distContext the value to be set. - * @return a new context with the given value set. - * @since 0.1.0 - */ - public static Context withCorrelationContext(CorrelationContext distContext) { - return Context.current().withValue(DIST_CONTEXT_KEY, distContext); - } - - /** - * Creates a new {@code Context} with the given value set. - * - * @param distContext the value to be set. - * @param context the parent {@code Context}. - * @return a new context with the given value set. - * @since 0.1.0 - */ - public static Context withCorrelationContext(CorrelationContext distContext, Context context) { - return context.withValue(DIST_CONTEXT_KEY, distContext); - } - - /** - * Returns the value from the current {@code Context}. - * - * @return the value from the specified {@code Context}. - * @since 0.1.0 - */ - public static CorrelationContext getCorrelationContext() { - return DIST_CONTEXT_KEY.get(); - } - - /** - * Returns the value from the specified {@code Context}. - * - * @param context the specified {@code Context}. - * @return the value from the specified {@code Context}. - * @since 0.1.0 - */ - public static CorrelationContext getCorrelationContext(Context context) { - return DIST_CONTEXT_KEY.get(context); - } - - /** - * Returns the value from the specified {@code Context}, falling back to a default, no-op {@link - * CorrelationContext}. - * - * @param context the specified {@code Context}. - * @return the value from the specified {@code Context}. - * @since 0.1.0 - */ - public static CorrelationContext getCorrelationContextWithDefault(Context context) { - CorrelationContext corrContext = DIST_CONTEXT_KEY.get(context); - return corrContext == null ? DEFAULT_VALUE : corrContext; - } - - /** - * Returns a new {@link Scope} encapsulating the provided {@code CorrelationContext} added to the - * current {@code Context}. - * - * @param distContext the {@code CorrelationContext} to be added to the current {@code Context}. - * @return the {@link Scope} for the updated {@code Context}. - * @since 0.1.0 - */ - public static Scope withScopedCorrelationContext(CorrelationContext distContext) { - Context context = withCorrelationContext(distContext); - return ContextUtils.withScopedContext(context); - } - - private CorrelationsContextUtils() {} -} diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java new file mode 100644 index 00000000000..9300b42340e --- /dev/null +++ b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java @@ -0,0 +1,97 @@ +/* + * 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 io.grpc.Context; +import io.opentelemetry.context.ContextUtils; +import io.opentelemetry.context.Scope; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * Util methods/functionality to interact with the {@link io.grpc.Context}. + * + * @since 0.1.0 + */ +@Immutable +public final class TracingContextUtils { + private static final Context.Key CONTEXT_SPAN_KEY = + Context.key("opentelemetry-trace-span-key"); + + /** + * Creates a new {@code Context} with the given {@link Span} set. + * + * @param span the value to be set. + * @param context the parent {@code Context}. + * @return a new context with the given value set. + * @since 0.1.0 + */ + public static Context withSpan(Span span, Context context) { + return context.withValue(CONTEXT_SPAN_KEY, span); + } + + /** + * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op + * {@link Span}. + * + * @return the {@link Span} from the current {@code Context}. + * @since 0.3.0 + */ + public static Span getCurrentSpan() { + return getSpan(Context.current()); + } + + /** + * Returns the {@link Span} from the specified {@code Context}, falling back to a default, no-op + * {@link Span}. + * + * @param context the specified {@code Context}. + * @return the {@link Span} from the specified {@code Context}. + * @since 0.3.0 + */ + public static Span getSpan(Context context) { + Span span = CONTEXT_SPAN_KEY.get(context); + return span == null ? DefaultSpan.getInvalid() : span; + } + + /** + * Returns the {@link Span} from the specified {@code Context}. If none is found, this method + * returns {code null}. + * + * @param context the specified {@code Context}. + * @return the {@link Span} from the specified {@code Context}. + * @since 0.1.0 + */ + @Nullable + public static Span getSpanWithoutDefault(Context context) { + return CONTEXT_SPAN_KEY.get(context); + } + + /** + * Returns a new {@link Scope} encapsulating the provided {@link Span} added to the current {@code + * Context}. + * + * @param span the {@link Span} to be added to the current {@code Context}. + * @return the {@link Scope} for the updated {@code Context}. + * @since 0.1.0 + */ + public static Scope withScopedSpan(Span span) { + return ContextUtils.withScopedContext(withSpan(span, Context.current())); + } + + private TracingContextUtils() {} +} diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java deleted file mode 100644 index 7410aac8dad..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/propagation/TracingContextUtils.java +++ /dev/null @@ -1,178 +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.propagation; - -import io.grpc.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.ContextUtils; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanContext; -import io.opentelemetry.trace.Tracer; -import javax.annotation.concurrent.Immutable; - -/** - * Util methods/functionality to interact with the {@link io.grpc.Context}. - * - *

Users must interact with the current Context via the public APIs in {@link Tracer} and avoid - * accessing this class directly. - * - * @since 0.1.0 - */ -@Immutable -public final class TracingContextUtils { - private static final Context.Key CONTEXT_SPAN_KEY = - Context.key("opentelemetry-trace-span-key"); - private static final Context.Key CONTEXT_SPANCONTEXT_KEY = - Context.key("opentelemetry-trace-spancontext-key"); - private static final Span DEFAULT_SPAN = DefaultSpan.getInvalid(); - - /** - * Creates a new {@code Context} with the given value set. - * - * @param span the value to be set. - * @return a new context with the given value set. - * @since 0.1.0 - */ - public static Context withSpan(Span span) { - return withSpan(span, Context.current()); - } - - /** - * Creates a new {@code Context} with the given value set. - * - * @param span the value to be set. - * @param context the parent {@code Context}. - * @return a new context with the given value set. - * @since 0.1.0 - */ - public static Context withSpan(Span span, Context context) { - return context.withValue(CONTEXT_SPAN_KEY, span); - } - - /** - * Creates a new {@code Context} with the given {@code SpanContext} set. - * - * @param spanContext the value to be set. - * @return a new context with the given value set. - * @since 0.3.0 - */ - public static Context withSpanContext(SpanContext spanContext) { - return withSpanContext(spanContext, Context.current()); - } - - /** - * Creates a new {@code Context} with the given {@code 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 {@code Span} from the current {@code Context}. - * - * @return the value from the current {@code Context}. - * @since 0.1.0 - */ - public static Span getSpan() { - return CONTEXT_SPAN_KEY.get(); - } - - /** - * Returns the {@code Span} from the specified {@code Context}. - * - * @param context the specified {@code Context}. - * @return the value from the specified {@code Context}. - * @since 0.1.0 - */ - public static Span getSpan(Context context) { - return CONTEXT_SPAN_KEY.get(context); - } - - /** - * Returns the {@code Span} from the specified {@code Context}, falling back to a default, no-op - * {@code Span}. - * - * @param context the specified {@code Context}. - * @return the value from the specified {@code Context}. - * @since 0.3.0 - */ - public static Span getSpanWithDefault(Context context) { - Span span = CONTEXT_SPAN_KEY.get(context); - return span == null ? DEFAULT_SPAN : span; - } - - /** - * Returns the {@link SpanContext} from the current {@code Context}. - * - * @return the value from the current {@code Context}. - * @since 0.3.0 - */ - public static SpanContext getSpanContext() { - return CONTEXT_SPANCONTEXT_KEY.get(); - } - - /** - * Returns the {@link SpanContext} from the specified {@code Context}. - * - * @param context the specified {@code Context}. - * @return the value from the specified {@code Context}. - * @since 0.3.0 - */ - public static SpanContext getSpanContext(Context context) { - return CONTEXT_SPANCONTEXT_KEY.get(context); - } - - /** - * Returns the effective {@link SpanContext} from the specified {@code Context}. - * - *

This method tries to get any effective non-null {@link SpanContext} in {@code Context}, - * giving higher priority to {@code Span#getContext()} and then falling back to {@code - * 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 - */ - public static SpanContext getEffectiveSpanContext(Context context) { - Span span = getSpan(context); - if (span != null) { - return span.getContext(); - } - - return getSpanContext(context); - } - - /** - * Returns a new {@link Scope} encapsulating the provided {@code Span} added to the current {@code - * Context}. - * - * @param span the {@code Span} to be added to the current {@code Context}. - * @return the {@link Scope} for the updated {@code Context}. - * @since 0.1.0 - */ - public static Scope withScopedSpan(Span span) { - return ContextUtils.withScopedContext(withSpan(span)); - } - - private TracingContextUtils() {} -} diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java new file mode 100644 index 00000000000..13b8b0c8168 --- /dev/null +++ b/api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java @@ -0,0 +1,83 @@ +/* + * 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.correlationcontext; + +import static com.google.common.truth.Truth.assertThat; + +import io.grpc.Context; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link CorrelationsContextUtils}. */ +@RunWith(JUnit4.class) +public final class CorrelationsContextUtilsTest { + + @Test + public void testGetCurrentCorrelationContext_Default() { + CorrelationContext corrContext = CorrelationsContextUtils.getCurrentCorrelationContext(); + assertThat(corrContext).isSameInstanceAs(EmptyCorrelationContext.getInstance()); + } + + @Test + public void testGetCurrentCorrelationContext_SetCorrContext() { + CorrelationContext corrContext = + DefaultCorrelationContextManager.getInstance().contextBuilder().build(); + Context orig = + CorrelationsContextUtils.withCorrelationContext(corrContext, Context.current()).attach(); + try { + assertThat(CorrelationsContextUtils.getCurrentCorrelationContext()) + .isSameInstanceAs(corrContext); + } finally { + Context.current().detach(orig); + } + } + + @Test + public void testGetCorrelationContext_DefaultContext() { + CorrelationContext corrContext = + CorrelationsContextUtils.getCorrelationContext(Context.current()); + assertThat(corrContext).isSameInstanceAs(EmptyCorrelationContext.getInstance()); + } + + @Test + public void testGetCorrelationContext_ExplicitContext() { + CorrelationContext corrContext = + DefaultCorrelationContextManager.getInstance().contextBuilder().build(); + Context context = + CorrelationsContextUtils.withCorrelationContext(corrContext, Context.current()); + assertThat(CorrelationsContextUtils.getCorrelationContext(context)) + .isSameInstanceAs(corrContext); + } + + @Test + public void testGetCorrelationContextWithoutDefault_DefaultContext() { + CorrelationContext corrContext = + CorrelationsContextUtils.getCorrelationContextWithoutDefault(Context.current()); + assertThat(corrContext).isNull(); + } + + @Test + public void testGetCorrelationContextWithoutDefault_ExplicitContext() { + CorrelationContext corrContext = + DefaultCorrelationContextManager.getInstance().contextBuilder().build(); + Context context = + CorrelationsContextUtils.withCorrelationContext(corrContext, Context.current()); + assertThat(CorrelationsContextUtils.getCorrelationContext(context)) + .isSameInstanceAs(corrContext); + } +} diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java deleted file mode 100644 index 90f0e510972..00000000000 --- a/api/src/test/java/io/opentelemetry/correlationcontext/propagation/CorrelationsContextUtilsTest.java +++ /dev/null @@ -1,64 +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.correlationcontext.propagation; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Context; -import io.opentelemetry.correlationcontext.CorrelationContext; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link CorrelationsContextUtils}. */ -@RunWith(JUnit4.class) -public final class CorrelationsContextUtilsTest { - @Test - public void testGetCurrentDistributedContext_DefaultContext() { - CorrelationContext distContext = - CorrelationsContextUtils.getCorrelationContext(Context.current()); - assertThat(distContext).isNull(); - } - - @Test - public void testGetCurrentDistributedContex_DefaultContext_WithoutExplicitContext() { - CorrelationContext distContext = CorrelationsContextUtils.getCorrelationContext(); - assertThat(distContext).isNull(); - } - - @Test - public void testGetCurrentDistributedContextWithDefault_DefaultContext() { - CorrelationContext distContext = - CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current()); - assertThat(distContext).isNotNull(); - assertThat(distContext.getEntries()).isEmpty(); - } - - @Test - public void testGetCurrentDistributedContextWithDefault_ContextSetToNull() { - Context orig = - CorrelationsContextUtils.withCorrelationContext(null, Context.current()).attach(); - try { - CorrelationContext distContext = - CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current()); - assertThat(distContext).isNotNull(); - assertThat(distContext.getEntries()).isEmpty(); - } finally { - Context.current().detach(orig); - } - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java new file mode 100644 index 00000000000..422a6b002c8 --- /dev/null +++ b/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java @@ -0,0 +1,72 @@ +/* + * 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 com.google.common.truth.Truth.assertThat; + +import io.grpc.Context; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link TracingContextUtils}. */ +@RunWith(JUnit4.class) +public final class TracingContextUtilsTest { + + @Test + public void testGetCurrentSpan_Default() { + Span span = TracingContextUtils.getCurrentSpan(); + assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); + } + + @Test + public void testGetCurrentSpan_SetSpan() { + Span span = DefaultSpan.create(SpanContext.getInvalid()); + Context orig = TracingContextUtils.withSpan(span, Context.current()).attach(); + try { + assertThat(TracingContextUtils.getCurrentSpan()).isSameInstanceAs(span); + } finally { + Context.current().detach(orig); + } + } + + @Test + public void testGetSpan_DefaultContext() { + Span span = TracingContextUtils.getSpan(Context.current()); + assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); + } + + @Test + public void testGetSpan_ExplicitContext() { + Span span = DefaultSpan.create(SpanContext.getInvalid()); + Context context = TracingContextUtils.withSpan(span, Context.current()); + assertThat(TracingContextUtils.getSpan(context)).isSameInstanceAs(span); + } + + @Test + public void testGetSpanWithoutDefault_DefaultContext() { + Span span = TracingContextUtils.getSpanWithoutDefault(Context.current()); + assertThat(span).isNull(); + } + + @Test + public void testGetSpanWithoutDefault_ExplicitContext() { + Span span = DefaultSpan.create(SpanContext.getInvalid()); + Context context = TracingContextUtils.withSpan(span, Context.current()); + assertThat(TracingContextUtils.getSpanWithoutDefault(context)).isSameInstanceAs(span); + } +} diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java deleted file mode 100644 index 868da269ac0..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/propagation/TracingContextUtilsTest.java +++ /dev/null @@ -1,140 +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.propagation; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; -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 org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link TracingContextUtils}. */ -@RunWith(JUnit4.class) -public final class TracingContextUtilsTest { - @Test - public void testGetCurrentSpan_DefaultContext() { - Span span = TracingContextUtils.getSpan(Context.current()); - assertThat(span).isNull(); - } - - @Test - public void testGetCurrentSpan_DefaultContext_WithoutExplicitContext() { - Span span = TracingContextUtils.getSpan(); - assertThat(span).isNull(); - } - - @Test - public void testGetCurrentSpanWithDefault_DefaultContext() { - Span span = TracingContextUtils.getSpanWithDefault(Context.current()); - assertThat(span).isNotNull(); - assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); - } - - @Test - public void testGetCurrentSpanWithDefault_ContextSetToNull() { - Context orig = TracingContextUtils.withSpan(null, Context.current()).attach(); - try { - Span span = TracingContextUtils.getSpanWithDefault(Context.current()); - assertThat(span).isNotNull(); - assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); - } finally { - Context.current().detach(orig); - } - } - - @Test - public void testGetCurrentSpanContext_DefaultContext() { - SpanContext spanContext = TracingContextUtils.getSpanContext(Context.current()); - assertThat(spanContext).isNull(); - } - - @Test - public void testGetCurrentSpanContext_DefaultContext_WithoutExplicitContext() { - SpanContext spanContext = TracingContextUtils.getSpanContext(); - assertThat(spanContext).isNull(); - } - - @Test - public void testAnySpanContext_DefaultContext() { - SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); - assertThat(spanContext).isNull(); - } - - @Test - public void testAnySpanContext_SetSpanContext() { - Context orig = - TracingContextUtils.withSpanContext(DefaultSpan.getInvalid().getContext()).attach(); - try { - SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); - assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); - } finally { - Context.current().detach(orig); - } - } - - @Test - public void testAnySpanContext_SetSpan() { - Context orig = TracingContextUtils.withSpan(DefaultSpan.getInvalid()).attach(); - try { - SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); - assertThat(spanContext).isNotNull(); - assertThat(spanContext).isSameInstanceAs(DefaultSpan.getInvalid().getContext()); - } finally { - Context.current().detach(orig); - } - } - - @Test - public void testSetSpanAndSpanContext() { - Span span = OpenTelemetry.getTracerRegistry().get(null).spanBuilder("testSpan").startSpan(); - SpanContext spanContext = - SpanContext.create( - new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()); - - Context context1 = TracingContextUtils.withSpan(span).attach(); - try { - Context context2 = TracingContextUtils.withSpanContext(spanContext).attach(); - try { - SpanContext spanContextInContext = TracingContextUtils.getSpanContext(Context.current()); - assertThat(spanContextInContext).isNotNull(); - assertThat(spanContextInContext).isSameInstanceAs(spanContext); - - Span spanInContext = TracingContextUtils.getSpan(Context.current()); - assertThat(spanInContext).isNotNull(); - assertThat(spanInContext).isSameInstanceAs(span); - - SpanContext anySpanContext = TracingContextUtils.getEffectiveSpanContext(Context.current()); - assertThat(anySpanContext).isNotNull(); - assertThat(anySpanContext).isSameInstanceAs(span.getContext()); - } finally { - Context.current().detach(context2); - } - } finally { - Context.current().detach(context1); - } - } -} diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java b/context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java similarity index 89% rename from context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java rename to context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java index 38fae8ef9cc..aedce36d750 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextUtils.java +++ b/context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java @@ -14,18 +14,14 @@ * limitations under the License. */ -package io.opentelemetry.context.propagation; +package io.opentelemetry.context; import io.grpc.Context; -import io.opentelemetry.context.Scope; import javax.annotation.concurrent.Immutable; /** * Util methods/functionality to interact with the {@link io.grpc.Context}. * - *

Users must interact with the current Context via the public APIs and avoid accessing this - * class directly. - * * @since 0.1.0 */ @Immutable diff --git a/context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java b/context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java similarity index 95% rename from context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java rename to context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java index 228b11cf80a..c3b511a7459 100644 --- a/context_prop/src/test/java/io/opentelemetry/context/propagation/ContextUtilsTest.java +++ b/context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java @@ -14,12 +14,11 @@ * limitations under the License. */ -package io.opentelemetry.context.propagation; +package io.opentelemetry.context; import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; -import io.opentelemetry.context.Scope; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; From 43e42d47fab52071e245a9da589586736c817a1b Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 10 Mar 2020 07:05:03 +0100 Subject: [PATCH 28/42] Make the code compile after the latest master merge. --- .../HttpTraceContextExtractBenchmark.java | 5 +- .../HttpTraceContextInjectBenchmark.java | 5 +- .../io/opentelemetry/trace/DefaultTracer.java | 3 +- .../trace/TracingContextUtils.java | 65 ++++++++++++-- .../trace/propagation/HttpTraceContext.java | 4 +- .../trace/DefaultTracerTest.java | 17 ++-- .../propagation/HttpTraceContextTest.java | 84 ++++++++++--------- .../opentracingshim/Propagation.java | 16 ++-- .../opentracingshim/TracerShimTest.java | 2 +- .../CorrelationContextSdk.java | 2 +- .../sdk/trace/SpanBuilderSdk.java | 5 +- .../CorrelationContextSdkTest.java | 6 +- .../sdk/trace/SpanBuilderSdkTest.java | 16 ++-- 13 files changed, 148 insertions(+), 82 deletions(-) diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java index 5dc9c88e608..4406c3fa6cb 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java @@ -16,8 +16,10 @@ package io.opentelemetry.trace.propagation; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.TracingContextUtils; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -70,7 +72,8 @@ public void setup() { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public SpanContext measureExtract() { - return httpTraceContext.extract(carrier, getter); + return TracingContextUtils.getSpanContext( + httpTraceContext.extract(Context.current(), carrier, getter)); } @TearDown(Level.Iteration) 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 b069140efbd..0288fb2bbfc 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -16,12 +16,14 @@ package io.opentelemetry.trace.propagation; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; 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.Arrays; import java.util.HashMap; import java.util.List; @@ -69,7 +71,8 @@ public void set(Map carrier, String key, String value) { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public Map measureInject() { - httpTraceContext.inject(contextToTest, carrier, setter); + Context context = TracingContextUtils.withSpanContext(contextToTest, Context.current()); + httpTraceContext.inject(context, carrier, setter); return carrier; } diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 02474b2d964..5a008ab7239 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -19,7 +19,6 @@ import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; -import io.opentelemetry.trace.propagation.TracingContextUtils; import java.util.Map; import javax.annotation.concurrent.ThreadSafe; @@ -54,7 +53,7 @@ public Scope withSpan(Span span) { @Override public Span.Builder spanBuilder(String spanName) { - return NoopSpanBuilder.create(this, spanName); + return NoopSpanBuilder.create(spanName); } private DefaultTracer() {} diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java index 9300b42340e..2b1aa65d49b 100644 --- a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java @@ -31,6 +31,19 @@ public final class TracingContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = Context.key("opentelemetry-trace-span-key"); + private static final Context.Key CONTEXT_SPANCONTEXT_KEY = + Context.key("opentelemetry-trace-spancontext-key"); + + /** + * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op + * {@link Span}. + * + * @return the {@link Span} from the current {@code Context}. + * @since 0.3.0 + */ + public static Span getCurrentSpan() { + return getSpan(Context.current()); + } /** * Creates a new {@code Context} with the given {@link Span} set. @@ -45,14 +58,15 @@ public static Context withSpan(Span span, Context context) { } /** - * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op - * {@link Span}. + * Creates a new {@code Context} with the given {@link SpanContext} set. * - * @return the {@link Span} from the current {@code Context}. + * @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 Span getCurrentSpan() { - return getSpan(Context.current()); + public static Context withSpanContext(SpanContext spanContext, Context context) { + return context.withValue(CONTEXT_SPANCONTEXT_KEY, spanContext); } /** @@ -81,6 +95,47 @@ public static Span getSpanWithoutDefault(Context context) { return CONTEXT_SPAN_KEY.get(context); } + /** + * 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) { + SpanContext spanContext = CONTEXT_SPANCONTEXT_KEY.get(context); + return spanContext == null ? SpanContext.getInvalid() : spanContext; + } + + /** + * 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 + */ + public static SpanContext getSpanContextWithoutDefault(Context context) { + return CONTEXT_SPANCONTEXT_KEY.get(context); + } + + /** + * Returns the effective {@link SpanContext} from the specified {@code Context}. + * + *

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 + */ + public static SpanContext getEffectiveSpanContext(Context context) { + Span span = getSpanWithoutDefault(context); + return span != null ? span.getContext() : getSpanContextWithoutDefault(context); + } + /** * Returns a new {@link Scope} encapsulating the provided {@link Span} added to the current {@code * Context}. 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 4003c3d2378..85949151d21 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -26,6 +26,7 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; +import io.opentelemetry.trace.TracingContextUtils; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -127,9 +128,6 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter } private static SpanContext extractImpl(C carrier, Getter getter) { - TraceId traceId; - SpanId spanId; - TraceFlags traceFlags; String traceparent = getter.get(carrier, TRACE_PARENT); if (traceparent == null) { return SpanContext.getInvalid(); diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 24b1404e31b..8b46fa16147 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; +import io.opentelemetry.context.ContextUtils; import io.opentelemetry.context.Scope; import org.junit.Rule; import org.junit.Test; @@ -112,8 +113,8 @@ public void testSpanContextPropagationFromSpanInContext() { Context context = TracingContextUtils.withSpanContext( SpanContext.create( - new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), Tracestate.getDefault()), - TracingContextUtils.withSpan(new DefaultSpan(spanContext))); + new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), TraceState.getDefault()), + TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current())); // Span in Context has higher priority than SpanContext. Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); @@ -122,7 +123,7 @@ public void testSpanContextPropagationFromSpanInContext() { @Test public void testSpanContextPropagationFromContext() { - Context context = TracingContextUtils.withSpanContext(spanContext); + Context context = TracingContextUtils.withSpanContext(spanContext, Context.current()); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); @@ -142,8 +143,8 @@ public void testSpanContextPropagationCurrentSpan() { @Test public void testSpanContextPropagationCurrentSpanContext() { - Context context = TracingContextUtils.withSpanContext(spanContext); - Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); + 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); @@ -157,9 +158,9 @@ 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))); - Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); + new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), TraceState.getDefault()), + TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current())); + Scope scope = io.opentelemetry.context.ContextUtils.withScopedContext(context); // Span in Context has higher priority than SpanContext. try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); 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 1351999b34e..d23c323ef4c 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -30,6 +30,7 @@ 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; @@ -92,10 +93,10 @@ public void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); Context context = TracingContextUtils.withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT), + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); - assertThat(carrier).containsExactly(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); + assertThat(carrier).containsExactly(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); } @Test @@ -103,10 +104,10 @@ public void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); Context context = TracingContextUtils.withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT), + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); - assertThat(carrier).containsExactly(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); + assertThat(carrier).containsExactly(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); } @Test @@ -114,7 +115,7 @@ public void inject_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); Context context = TracingContextUtils.withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT), + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) @@ -127,7 +128,7 @@ public void inject_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); Context context = TracingContextUtils.withSpanContext( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT), + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) @@ -143,18 +144,18 @@ public void inject_Span() { // Use non-default values to verify that the Span is actually consumed. Map carrier = new LinkedHashMap(); Span span = - OpenTelemetry.getTracerRegistry() + OpenTelemetry.getTracerProvider() .get(null) .spanBuilder("testSpan") .setParent( SpanContext.create( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)) + TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)) .startSpan(); - Context context = TracingContextUtils.withSpan(span); + Context context = TracingContextUtils.withSpan(span, Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( - TRACEPARENT, TRACEPARENT_HEADER_SAMPLED, TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); + TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED, TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); } @Test @@ -162,27 +163,28 @@ public void inject_Span_SpanContextPresent() { // Span has higher priority than SpanContext. Map carrier = new LinkedHashMap(); Span span = - OpenTelemetry.getTracerRegistry() + OpenTelemetry.getTracerProvider() .get(null) .spanBuilder("testSpan") .setParent( SpanContext.create( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT)) + TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)) .startSpan(); SpanContext spanContext = - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT); + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT); Context context = - TracingContextUtils.withSpanContext(spanContext, TracingContextUtils.withSpan(span)); + TracingContextUtils.withSpanContext( + spanContext, TracingContextUtils.withSpan(span, Context.current())); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( - TRACEPARENT, TRACEPARENT_HEADER_SAMPLED, TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); + TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED, TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); } @Test public void extract_SampledContext() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); + carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -193,7 +195,7 @@ public void extract_SampledContext() { @Test public void extract_NotSampledContext() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); + carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -204,8 +206,8 @@ public void extract_NotSampledContext() { @Test public void extract_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED); - carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); + carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); + carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -216,8 +218,8 @@ public void extract_SampledContext_WithTraceState() { @Test public void extract_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING); + carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); + carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -228,7 +230,7 @@ public void extract_NotSampledContext_WithTraceState() { @Test public void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); + carrier.put(TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -239,8 +241,8 @@ public void extract_NotSampledContext_NextVersion() { @Test public void extract_NotSampledContext_EmptyTraceState() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - carrier.put(TRACESTATE, ""); + carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); + carrier.put(TRACE_STATE, ""); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -251,8 +253,8 @@ public void extract_NotSampledContext_EmptyTraceState() { @Test public void extract_NotSampledContext_TraceStateWithSpaces() { Map carrier = new LinkedHashMap<>(); - carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING_WITH_SPACES); + carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); + carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING_WITH_SPACES); Context context = httpTraceContext.extract(Context.current(), carrier, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -264,7 +266,7 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { public void extract_InvalidTraceId() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put( - TRACEPARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); + TRACE_PARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isSameInstanceAs(SpanContext.getInvalid()); @@ -273,7 +275,7 @@ public void extract_InvalidTraceId() { @Test public void extract_InvalidTraceId_Size() { Map invalidHeaders = new LinkedHashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isSameInstanceAs(SpanContext.getInvalid()); @@ -282,15 +284,16 @@ public void extract_InvalidTraceId_Size() { @Test public void extract_InvalidSpanId() { Map invalidHeaders = new HashMap(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpan(context)).isSameInstanceAs(SpanContext.getInvalid()); + assertThat(TracingContextUtils.getSpanContext(context)) + .isSameInstanceAs(SpanContext.getInvalid()); } @Test public void extract_InvalidSpanId_Size() { Map invalidHeaders = new HashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isSameInstanceAs(SpanContext.getInvalid()); @@ -299,7 +302,7 @@ public void extract_InvalidSpanId_Size() { @Test public void extract_InvalidTraceFlags() { Map invalidHeaders = new HashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isSameInstanceAs(SpanContext.getInvalid()); @@ -308,7 +311,7 @@ public void extract_InvalidTraceFlags() { @Test public void extract_InvalidTraceFlags_Size() { Map invalidHeaders = new HashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isSameInstanceAs(SpanContext.getInvalid()); @@ -317,8 +320,8 @@ public void extract_InvalidTraceFlags_Size() { @Test public void extract_InvalidTracestate_EntriesDelimiter() { Map invalidHeaders = new HashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); - invalidHeaders.put(TRACESTATE, "foo=bar;test=test"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); + invalidHeaders.put(TRACE_STATE, "foo=bar;test=test"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -329,8 +332,8 @@ public void extract_InvalidTracestate_EntriesDelimiter() { @Test public void extract_InvalidTracestate_KeyValueDelimiter() { Map invalidHeaders = new HashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); - invalidHeaders.put(TRACESTATE, "foo=bar,test-test"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); + invalidHeaders.put(TRACE_STATE, "foo=bar,test-test"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -341,8 +344,8 @@ public void extract_InvalidTracestate_KeyValueDelimiter() { @Test public void extract_InvalidTracestate_OneString() { Map invalidHeaders = new HashMap<>(); - invalidHeaders.put(TRACEPARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); - invalidHeaders.put(TRACESTATE, "test-test"); + invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); + invalidHeaders.put(TRACE_STATE, "test-test"); Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); assertThat(TracingContextUtils.getSpanContext(context)) .isEqualTo( @@ -364,6 +367,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(httpTraceContext.extract(emptyHeaders, getter)).isEqualTo(SpanContext.getInvalid()); + Context context = httpTraceContext.extract(Context.current(), emptyHeaders, getter); + assertThat(TracingContextUtils.getSpanContext(context)).isEqualTo(SpanContext.getInvalid()); } } 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 16a08bbfb2d..e2b134d1c6c 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -18,6 +18,8 @@ import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; +import io.opentelemetry.correlationcontext.CorrelationsContextUtils; +import io.opentelemetry.trace.TracingContextUtils; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; import java.util.HashMap; @@ -30,13 +32,11 @@ final class Propagation extends BaseShimObject { } public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { - Context context = Context.current(); - context = - io.opentelemetry.trace.propagation.TracingContextUtils.withSpanContext( - contextShim.getSpanContext(), context); + Context context = + TracingContextUtils.withSpanContext(contextShim.getSpanContext(), Context.current()); context = - io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils - .withCorrelationContext(contextShim.getCorrelationContext(), context); + CorrelationsContextUtils.withCorrelationContext( + contextShim.getCorrelationContext(), context); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -59,9 +59,7 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { } return new SpanContextShim( - telemetryInfo, - io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils - .getCorrelationContext(context)); + telemetryInfo, spanContext, CorrelationsContextUtils.getCorrelationContext(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { 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 33a2d70f2c5..48b4f652642 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java @@ -40,7 +40,7 @@ public void setUp() { tracerShim = new TracerShim( new TelemetryInfo( - OpenTelemetry.getTracerRegistry().get("opentracingshim"), + OpenTelemetry.getTracerProvider().get("opentracingshim"), OpenTelemetry.getCorrelationContextManager(), OpenTelemetry.getPropagators())); } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index fbfe6f37682..23c01e293c6 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -21,11 +21,11 @@ import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; -import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; import io.opentelemetry.internal.Utils; import java.util.Collection; import java.util.Collections; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 884c29d099e..f65565e5175 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -36,6 +36,7 @@ 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.ArrayList; import java.util.Collections; @@ -106,8 +107,8 @@ public Span.Builder setParent(SpanContext remoteParent) { public Span.Builder setParent(Context context) { Utils.checkNotNull(context, "context"); - Span span = TracingContextUtils.getSpan(context); - SpanContext spanContext = TracingContextUtils.getSpanContext(context); + Span span = TracingContextUtils.getSpanWithoutDefault(context); + SpanContext spanContext = TracingContextUtils.getSpanContextWithoutDefault(context); if (span != null) { setParent(span); } else if (spanContext != null) { diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index cdc588b6f53..f71becb6eec 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -23,11 +23,11 @@ import io.grpc.Context; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; -import io.opentelemetry.correlationcontext.propagation.CorrelationsContextUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -143,7 +143,9 @@ public void setParent_nullContext() { @Test public void setParent_fromContext() { - Context context = CorrelationsContextUtils.withCorrelationContext(listToCorrelationContext(T1)); + Context context = + CorrelationsContextUtils.withCorrelationContext( + listToCorrelationContext(T1), Context.current()); CorrelationContext corrContext = contextManager.contextBuilder().setParent(context).build(); assertThat(corrContext.getEntries()).containsExactly(T1); } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 290841964ce..5b32f9946ec 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertFalse; import io.grpc.Context; +import io.opentelemetry.context.ContextUtils; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; @@ -32,6 +33,7 @@ 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.Collections; import java.util.LinkedHashMap; @@ -410,7 +412,7 @@ public void overrideNoParent_remoteParent() { @Test public void parentExplicitContext_withSpan() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = TracingContextUtils.withSpan(parent); + Context context = TracingContextUtils.withSpan(parent, Context.current()); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -429,7 +431,7 @@ public void parentExplicitContext_withSpan() { @Test public void parentExplicitContext_withSpanContext() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = TracingContextUtils.withSpanContext(parent.getContext()); + Context context = TracingContextUtils.withSpanContext(parent.getContext(), Context.current()); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -450,7 +452,7 @@ public void parentExplicitContext_withValues() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); Context context = TracingContextUtils.withSpanContext( - sampledSpanContext, TracingContextUtils.withSpan(parent)); + sampledSpanContext, TracingContextUtils.withSpan(parent, Context.current())); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) @@ -506,8 +508,8 @@ public void parentCurrentSpan() { @Test public void parentCurrentSpanContext() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = TracingContextUtils.withSpanContext(parent.getContext()); - Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); + Context context = TracingContextUtils.withSpanContext(parent.getContext(), Context.current()); + Scope scope = ContextUtils.withScopedContext(context); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); @@ -528,8 +530,8 @@ public void parentCurrentContextValues() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); Context context = TracingContextUtils.withSpanContext( - sampledSpanContext, TracingContextUtils.withSpan(parent)); - Scope scope = io.opentelemetry.context.propagation.ContextUtils.withScopedContext(context); + sampledSpanContext, TracingContextUtils.withSpan(parent, Context.current())); + Scope scope = ContextUtils.withScopedContext(context); try { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); From 014b3c4ac2d0f0920c170d39e1ee1adde9569129 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 10 Mar 2020 18:48:52 +0100 Subject: [PATCH 29/42] Cache the fields() in our default composite propagator. --- .../context/propagation/DefaultContextPropagators.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java index 5c9d5a10ae1..b09751e77e7 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java @@ -109,14 +109,20 @@ public ContextPropagators build() { private static final class MultiHttpTextFormat implements HttpTextFormat { private final HttpTextFormat[] textPropagators; + private final List allFields; private MultiHttpTextFormat(List textPropagators) { this.textPropagators = new HttpTextFormat[textPropagators.size()]; textPropagators.toArray(this.textPropagators); + this.allFields = getAllFields(this.textPropagators); } @Override public List fields() { + return allFields; + } + + private static List getAllFields(HttpTextFormat[] textPropagators) { List fields = new ArrayList<>(); for (int i = 0; i < textPropagators.length; i++) { fields.addAll(textPropagators[i].fields()); From f3d29a38f7789c05962a4cf0f5050eafe72424bc Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 10 Mar 2020 20:27:38 +0100 Subject: [PATCH 30/42] Remove the overloads of setParent(Context) for now. --- .../CorrelationContext.java | 23 ------ .../DefaultCorrelationContextManager.java | 7 -- .../io/opentelemetry/trace/DefaultTracer.java | 10 --- .../java/io/opentelemetry/trace/Span.java | 20 ----- .../DefaultCorrelationContextManagerTest.java | 7 -- .../trace/DefaultTracerTest.java | 21 ----- .../CorrelationContextSdk.java | 14 ---- .../sdk/trace/SpanBuilderSdk.java | 16 ---- .../CorrelationContextSdkTest.java | 27 ------- .../sdk/trace/SpanBuilderSdkTest.java | 79 ------------------- 10 files changed, 224 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index fa1328b09e0..93815e2d115 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -16,7 +16,6 @@ package io.opentelemetry.correlationcontext; -import io.grpc.Context; import java.util.Collection; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -75,28 +74,6 @@ interface Builder { */ Builder setParent(CorrelationContext parent); - /** - * Sets the parent {@link CorrelationContext} to use from the specified {@code Context}. If no - * parent is provided, the value of {@link CorrelationContextManager#getCurrentContext()} at - * {@link #build()} time will be used as parent, unless {@link #setNoParent()} was called. - * - *

If no {@link CorrelationContext} is available in the specified {@code Context}, the - * resulting {@link CorrelationContext} will become a root instance, as if {@link - * #setNoParent()} had been called. - * - *

This must be used to create a {@link CorrelationContext} when manual Context - * propagation is used. - * - *

If called multiple times, only the last specified value will be used. - * - * @param context the {@code Context}. - * @return this. - * @throws NullPointerException if {@code context} is {@code null}. - * @see #setNoParent() - * @since 0.3.0 - */ - Builder setParent(Context context); - /** * Sets the option to become a root {@link CorrelationContext} with no parent. If not * called, the value provided using {@link #setParent(CorrelationContext)} or otherwise {@link diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 6806ce54877..4f7ce989260 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -16,7 +16,6 @@ package io.opentelemetry.correlationcontext; -import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; import javax.annotation.concurrent.Immutable; @@ -66,12 +65,6 @@ public CorrelationContext.Builder setParent(CorrelationContext parent) { return this; } - @Override - public CorrelationContext.Builder setParent(Context context) { - Utils.checkNotNull(context, "context"); - return this; - } - @Override public CorrelationContext.Builder setNoParent() { return this; diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 5a008ab7239..482b421a2ca 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -92,16 +92,6 @@ public NoopSpanBuilder setParent(SpanContext remoteParent) { return this; } - @Override - public NoopSpanBuilder setParent(Context context) { - Utils.checkNotNull(context, "context"); - spanContext = TracingContextUtils.getEffectiveSpanContext(context); - if (spanContext == null) { - isRootSpan = true; - } - return this; - } - @Override public NoopSpanBuilder setNoParent() { isRootSpan = true; diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 5a2bf35caa0..5ee1377ba2e 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -16,7 +16,6 @@ package io.opentelemetry.trace; -import io.grpc.Context; import java.util.Map; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -406,25 +405,6 @@ interface Builder { */ Builder setParent(SpanContext remoteParent); - /** - * Sets the parent to use from the specified {@code Context}. If not set, the {@link Span} or - * {@link SpanContext} in {@code Context.current()} at {@link #startSpan()} time will be used as - * parent. - * - *

If no {@link Span} nor {@link SpanContext} is availablle in the specified {@code Context}, - * the resulting {@code Span} will become a root instance, as if {@link #setNoParent()} had been - * called. - * - *

If called multiple times, only the last specified value will be used. Observe that the - * state defined by a previous call to {@link #setNoParent()} will be discarded. - * - * @param context the {@code Context}. - * @return this. - * @throws NullPointerException if {@code context} is {@code null}. - * @since 0.3.0 - */ - Builder setParent(Context context); - /** * Sets the option to become a root {@code Span} for a new trace. If not set, the value of * {@code Tracer.getCurrentSpan()} at {@link #startSpan()} time will be used as parent. diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 81dcec3c9b6..7f567c6c4af 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -141,13 +141,6 @@ public void noopContextBuilder_SetParent_DisallowsNullParent() { noopBuilder.setParent((CorrelationContext) null); } - @Test - public void noopContextBuilder_SetParent_DisallowsNullContext() { - CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); - thrown.expect(NullPointerException.class); - noopBuilder.setParent((Context) null); - } - @Test public void noopContextBuilder_Put_DisallowsNullKey() { CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 8b46fa16147..9643a8dda23 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -108,27 +108,6 @@ public void testSpanContextPropagation() { assertThat(span.getContext()).isSameInstanceAs(spanContext); } - @Test - public void testSpanContextPropagationFromSpanInContext() { - Context context = - TracingContextUtils.withSpanContext( - SpanContext.create( - new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), TraceState.getDefault()), - TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current())); - - // Span in Context has higher priority than SpanContext. - Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - assertThat(span.getContext()).isSameInstanceAs(spanContext); - } - - @Test - public void testSpanContextPropagationFromContext() { - Context context = TracingContextUtils.withSpanContext(spanContext, Context.current()); - - Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - assertThat(span.getContext()).isSameInstanceAs(spanContext); - } - @Test public void testSpanContextPropagationCurrentSpan() { DefaultSpan parent = new DefaultSpan(spanContext); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index 23c01e293c6..6179cb659b9 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -18,10 +18,8 @@ import static io.opentelemetry.internal.Utils.checkNotNull; -import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; @@ -129,18 +127,6 @@ public CorrelationContext.Builder setParent(CorrelationContext parent) { return this; } - @Override - public CorrelationContext.Builder setParent(Context context) { - Utils.checkNotNull(context, "context"); - CorrelationContext corrContext = CorrelationsContextUtils.getCorrelationContext(context); - if (corrContext != null) { - setParent(corrContext); - } else { - setNoParent(); - } - return this; - } - @Override public CorrelationContext.Builder setNoParent() { this.parent = null; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index f65565e5175..ed31705e8cb 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -103,22 +103,6 @@ public Span.Builder setParent(SpanContext remoteParent) { return this; } - @Override - public Span.Builder setParent(Context context) { - Utils.checkNotNull(context, "context"); - - Span span = TracingContextUtils.getSpanWithoutDefault(context); - SpanContext spanContext = TracingContextUtils.getSpanContextWithoutDefault(context); - if (span != null) { - setParent(span); - } else if (spanContext != null) { - setParent(spanContext); - } else { - setNoParent(); - } - return this; - } - @Override public Span.Builder setNoParent() { this.parentType = ParentType.NO_PARENT; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index f71becb6eec..3e51d607622 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -20,10 +20,8 @@ import static io.opentelemetry.sdk.correlationcontext.CorrelationContextTestUtil.listToCorrelationContext; import com.google.common.testing.EqualsTester; -import io.grpc.Context; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; -import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; @@ -134,31 +132,6 @@ public void setParent_nullValue() { contextManager.contextBuilder().setParent(parent).setParent((CorrelationContext) null).build(); } - @Test - public void setParent_nullContext() { - CorrelationContextSdk parent = listToCorrelationContext(T1); - thrown.expect(NullPointerException.class); - contextManager.contextBuilder().setParent(parent).setParent((Context) null).build(); - } - - @Test - public void setParent_fromContext() { - Context context = - CorrelationsContextUtils.withCorrelationContext( - listToCorrelationContext(T1), Context.current()); - CorrelationContext corrContext = contextManager.contextBuilder().setParent(context).build(); - assertThat(corrContext.getEntries()).containsExactly(T1); - } - - @Test - public void setParent_fromEmptyContext() { - CorrelationContextSdk parent = listToCorrelationContext(T1); - Context context = Context.current(); - CorrelationContext corrContext = - contextManager.contextBuilder().setParent(parent).setParent(context).build(); - assertThat(corrContext.getEntries()).isEmpty(); - } - @Test public void setParent_setNoParent() { CorrelationContextSdk parent = listToCorrelationContext(T1); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 5b32f9946ec..4b5ae45678a 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -409,85 +409,6 @@ public void overrideNoParent_remoteParent() { } } - @Test - public void parentExplicitContext_withSpan() { - Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = TracingContextUtils.withSpan(parent, Context.current()); - try { - RecordEventsReadableSpan span = - (RecordEventsReadableSpan) - tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - try { - assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); - } finally { - span.end(); - } - } finally { - parent.end(); - } - } - - @Test - public void parentExplicitContext_withSpanContext() { - Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = TracingContextUtils.withSpanContext(parent.getContext(), Context.current()); - try { - RecordEventsReadableSpan span = - (RecordEventsReadableSpan) - tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - try { - assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); - } finally { - span.end(); - } - } finally { - parent.end(); - } - } - - @Test - public void parentExplicitContext_withValues() { - Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = - TracingContextUtils.withSpanContext( - sampledSpanContext, TracingContextUtils.withSpan(parent, Context.current())); - try { - RecordEventsReadableSpan span = - (RecordEventsReadableSpan) - tracerSdk.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - try { - assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); - } finally { - span.end(); - } - } finally { - parent.end(); - } - } - - @Test - public void parentExplicitContext_noValues() { - Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = Context.current(); - try { - RecordEventsReadableSpan span = - (RecordEventsReadableSpan) - tracerSdk.spanBuilder(SPAN_NAME).setParent(parent).setParent(context).startSpan(); - try { - assertThat(span.getContext().getTraceId()).isNotEqualTo(parent.getContext().getTraceId()); - assertThat(span.toSpanData().getParentSpanId()) - .isNotEqualTo(parent.getContext().getSpanId()); - } finally { - span.end(); - } - } finally { - parent.end(); - } - } - @Test public void parentCurrentSpan() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); From 555095bf02f281011505ce0139a33e68484ac6e4 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 10 Mar 2020 20:41:54 +0100 Subject: [PATCH 31/42] Use DefaultSpan for the tests instead of calling getTracerProvider() --- .../propagation/HttpTraceContextTest.java | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) 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 d23c323ef4c..ba514ff686e 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -21,9 +21,9 @@ import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_STATE; import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; 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; @@ -141,16 +141,10 @@ public void inject_NotSampledContext_WithTraceState() { @Test public void inject_Span() { - // Use non-default values to verify that the Span is actually consumed. Map carrier = new LinkedHashMap(); Span span = - OpenTelemetry.getTracerProvider() - .get(null) - .spanBuilder("testSpan") - .setParent( - SpanContext.create( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)) - .startSpan(); + DefaultSpan.create( + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)); Context context = TracingContextUtils.withSpan(span, Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) @@ -163,13 +157,8 @@ public void inject_Span_SpanContextPresent() { // Span has higher priority than SpanContext. Map carrier = new LinkedHashMap(); Span span = - OpenTelemetry.getTracerProvider() - .get(null) - .spanBuilder("testSpan") - .setParent( - SpanContext.create( - TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)) - .startSpan(); + 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 = From 3c1f7817e3848d7d9461f300c9b69e735258d607 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 10 Mar 2020 20:56:49 +0100 Subject: [PATCH 32/42] Fix the sdk testbed artifact build. --- .../sdk/contrib/trace/testbed/clientserver/Server.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 221746d40a6..34cfcecf9da 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -22,7 +22,9 @@ import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; +import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; +import io.opentelemetry.trace.TracingContextUtils; import java.util.concurrent.ArrayBlockingQueue; import javax.annotation.Nullable; @@ -50,8 +52,9 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); + SpanContext spanContext = TracingContextUtils.getSpanContext(context); Span span = - tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(context).startSpan(); + tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); try (Scope ignored = tracer.withSpan(span)) { From 136f0aa232a14e377b2b8b1759132c31affad024 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Sat, 14 Mar 2020 22:12:28 +0100 Subject: [PATCH 33/42] Make the B3 propagator comply with the new propagator API. --- .../trace/propagation/B3Propagator.java | 23 +++-- .../trace/propagation/B3PropagatorTest.java | 88 ++++++++++++------- 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java b/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java index d3f7efa4bc0..54ece062798 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java @@ -18,6 +18,7 @@ import static io.opentelemetry.internal.Utils.checkNotNull; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.SpanContext; @@ -25,6 +26,7 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; +import io.opentelemetry.trace.TracingContextUtils; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -37,7 +39,7 @@ * href=https://github.com/openzipkin/b3-propagation>openzipkin/b3-propagation. */ @Immutable -public class B3Propagator implements HttpTextFormat { +public class B3Propagator implements HttpTextFormat { private static final Logger logger = Logger.getLogger(HttpTraceContext.class.getName()); static final String TRACE_ID_HEADER = "X-B3-TraceId"; @@ -79,11 +81,16 @@ public List fields() { } @Override - public void inject(SpanContext spanContext, C carrier, Setter setter) { - checkNotNull(spanContext, "spanContext"); + public void inject(Context context, C carrier, Setter setter) { + checkNotNull(context, "context"); checkNotNull(setter, "setter"); checkNotNull(carrier, "carrier"); + SpanContext spanContext = TracingContextUtils.getEffectiveSpanContext(context); + if (spanContext == null) { + return; + } + String sampled = spanContext.getTraceFlags().isSampled() ? TRUE_INT : FALSE_INT; if (singleHeader) { @@ -103,15 +110,19 @@ public void inject(SpanContext spanContext, C carrier, Setter setter) { } @Override - public SpanContext extract(C carrier, Getter getter) { + public Context extract(Context context, C carrier, Getter getter) { checkNotNull(carrier, "carrier"); checkNotNull(getter, "getter"); + SpanContext spanContext = null; + if (singleHeader) { - return getSpanContextFromSingleHeader(carrier, getter); + spanContext = getSpanContextFromSingleHeader(carrier, getter); } else { - return getSpanContextFromMultipleHeaders(carrier, getter); + spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } + + return TracingContextUtils.withSpanContext(spanContext, context); } @SuppressWarnings("StringSplitter") diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java index 059a8add7c6..33e16e1b815 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java @@ -17,7 +17,10 @@ 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; @@ -74,7 +77,9 @@ public String get(Map carrier, String key) { public void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); b3Propagator.inject( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + Context.current()), carrier, setter); assertThat(carrier).containsEntry(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); @@ -86,7 +91,9 @@ public void inject_SampledContext() { public void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); b3Propagator.inject( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + Context.current()), carrier, setter); assertThat(carrier).containsEntry(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); @@ -101,7 +108,7 @@ public void extract_SampledContext_Int() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(carrier, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -114,7 +121,7 @@ public void extract_SampledContext_Bool() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(b3Propagator.extract(carrier, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -127,7 +134,7 @@ public void extract_NotSampledContext() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.FALSE_INT); - assertThat(b3Propagator.extract(carrier, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -140,7 +147,7 @@ public void extract_SampledContext_Int_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(carrier, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -153,7 +160,7 @@ public void extract_SampledContext_Bool_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(b3Propagator.extract(carrier, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -166,7 +173,7 @@ public void extract_NotSampledContext_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.FALSE_INT); - assertThat(b3Propagator.extract(carrier, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -178,7 +185,7 @@ public void extract_InvalidTraceId() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, "abcdefghijklmnopabcdefghijklmnop"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -188,7 +195,7 @@ public void extract_InvalidTraceId_Size() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16 + "00"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -198,7 +205,7 @@ public void extract_InvalidSpanId() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "abcdefghijklmnop"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -208,7 +215,7 @@ public void extract_InvalidSpanId_Size() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "abcdefghijklmnop" + "00"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -216,7 +223,9 @@ public void extract_InvalidSpanId_Size() { public void inject_SampledContext_SingleHeader() { Map carrier = new LinkedHashMap<>(); b3PropagatorSingleHeader.inject( - SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), + Context.current()), carrier, setter); @@ -229,7 +238,9 @@ public void inject_SampledContext_SingleHeader() { public void inject_NotSampledContext_SingleHeader() { Map carrier = new LinkedHashMap<>(); b3PropagatorSingleHeader.inject( - SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + withSpanContext( + SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), + Context.current()), carrier, setter); @@ -245,7 +256,7 @@ public void extract_SampledContext_Int_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -258,7 +269,7 @@ public void extract_SampledContext_DebugFlag_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-" + "0"); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -270,7 +281,7 @@ public void extract_SampledContext_Bool_SingleHeader() { carrier.put( B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true"); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -283,7 +294,7 @@ public void extract_SampledContext_Bool_DebugFlag_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true" + "-" + "0"); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -296,7 +307,7 @@ public void extract_NotSampledContext_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.FALSE_INT); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -309,7 +320,7 @@ public void extract_SampledContext_Int_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -322,7 +333,7 @@ public void extract_SampledContext_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-" + "0"); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -334,7 +345,7 @@ public void extract_SampledContext_Bool_SingleHeader_Short_TraceId() { carrier.put( B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true"); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -347,7 +358,7 @@ public void extract_SampledContext_Bool_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true" + "-" + "0"); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -360,7 +371,7 @@ public void extract_NotSampledContext_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.FALSE_INT); - assertThat(b3PropagatorSingleHeader.extract(carrier, getter)) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -371,7 +382,9 @@ public void extract_Null_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, null); - assertThat(b3PropagatorSingleHeader.extract(invalidHeaders, getter)) + assertThat( + getSpanContext( + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -380,7 +393,9 @@ public void extract_Empty_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, ""); - assertThat(b3PropagatorSingleHeader.extract(invalidHeaders, getter)) + assertThat( + getSpanContext( + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -391,7 +406,9 @@ public void extract_InvalidTraceId_SingleHeader() { B3Propagator.COMBINED_HEADER, "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat(b3PropagatorSingleHeader.extract(invalidHeaders, getter)) + assertThat( + getSpanContext( + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -407,7 +424,9 @@ public void extract_InvalidTraceId_Size_SingleHeader() { + "-" + B3Propagator.TRUE_INT); - assertThat(b3PropagatorSingleHeader.extract(invalidHeaders, getter)) + assertThat( + getSpanContext( + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -418,7 +437,9 @@ public void extract_InvalidSpanId_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-" + B3Propagator.TRUE_INT); - assertThat(b3PropagatorSingleHeader.extract(invalidHeaders, getter)) + assertThat( + getSpanContext( + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -429,7 +450,7 @@ public void extract_InvalidSpanId_Size_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "00" + "-" + B3Propagator.TRUE_INT); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -438,7 +459,7 @@ public void extract_TooFewParts_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -449,7 +470,7 @@ public void extract_TooManyParts_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-extra"); - assertThat(b3Propagator.extract(invalidHeaders, getter)) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -470,6 +491,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(b3Propagator.extract(emptyHeaders, getter)).isEqualTo(SpanContext.getInvalid()); + assertThat(getSpanContext(b3Propagator.extract(Context.current(), emptyHeaders, getter))) + .isEqualTo(SpanContext.getInvalid()); } } From ddc370a0add081658f8151d93ea3bccb050f844e Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Sat, 14 Mar 2020 23:01:49 +0100 Subject: [PATCH 34/42] Simplify the HttpTraceContextTest tests. --- .../propagation/HttpTraceContextTest.java | 70 +++++++------------ 1 file changed, 27 insertions(+), 43 deletions(-) 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 ba514ff686e..0d20e1672fa 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -17,6 +17,9 @@ 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; @@ -30,7 +33,6 @@ 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; @@ -92,7 +94,7 @@ public void inject_Nothing() { public void inject_SampledContext() { Map carrier = new LinkedHashMap<>(); Context context = - TracingContextUtils.withSpanContext( + withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -103,7 +105,7 @@ public void inject_SampledContext() { public void inject_NotSampledContext() { Map carrier = new LinkedHashMap<>(); Context context = - TracingContextUtils.withSpanContext( + withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -114,7 +116,7 @@ public void inject_NotSampledContext() { public void inject_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); Context context = - TracingContextUtils.withSpanContext( + withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -127,7 +129,7 @@ public void inject_SampledContext_WithTraceState() { public void inject_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); Context context = - TracingContextUtils.withSpanContext( + withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT), Context.current()); httpTraceContext.inject(context, carrier, setter); @@ -145,7 +147,7 @@ public void inject_Span() { Span span = DefaultSpan.create( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)); - Context context = TracingContextUtils.withSpan(span, Context.current()); + Context context = withSpan(span, Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -161,9 +163,7 @@ public void inject_Span_SpanContextPresent() { 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 = - TracingContextUtils.withSpanContext( - spanContext, TracingContextUtils.withSpan(span, Context.current())); + Context context = withSpanContext(spanContext, withSpan(span, Context.current())); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -174,8 +174,7 @@ public void inject_Span_SpanContextPresent() { public void extract_SampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -185,8 +184,7 @@ public void extract_SampledContext() { public void extract_NotSampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -197,8 +195,7 @@ public void extract_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)); @@ -209,8 +206,7 @@ public void extract_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); @@ -220,8 +216,7 @@ public void extract_NotSampledContext_WithTraceState() { public void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -232,8 +227,7 @@ public void extract_NotSampledContext_EmptyTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, ""); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -244,8 +238,7 @@ public 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); - Context context = httpTraceContext.extract(Context.current(), carrier, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); @@ -256,8 +249,7 @@ public void extract_InvalidTraceId() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put( TRACE_PARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -265,8 +257,7 @@ public void extract_InvalidTraceId() { public void extract_InvalidTraceId_Size() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -274,8 +265,7 @@ public void extract_InvalidTraceId_Size() { public void extract_InvalidSpanId() { Map invalidHeaders = new HashMap(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -283,8 +273,7 @@ public void extract_InvalidSpanId() { public void extract_InvalidSpanId_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -292,8 +281,7 @@ public void extract_InvalidSpanId_Size() { public void extract_InvalidTraceFlags() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -301,8 +289,7 @@ public void extract_InvalidTraceFlags() { public void extract_InvalidTraceFlags_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -311,8 +298,7 @@ public 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"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -323,8 +309,7 @@ public 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"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -335,8 +320,7 @@ public 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"); - Context context = httpTraceContext.extract(Context.current(), invalidHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -356,7 +340,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - Context context = httpTraceContext.extract(Context.current(), emptyHeaders, getter); - assertThat(TracingContextUtils.getSpanContext(context)).isEqualTo(SpanContext.getInvalid()); + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), emptyHeaders, getter))) + .isSameInstanceAs(SpanContext.getInvalid()); } } From a1d93ecd6d72300279e7c9d6a984a78cc9047aca Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Sun, 15 Mar 2020 00:16:58 +0100 Subject: [PATCH 35/42] Simplify the ContextUtils* classes handling of default values. --- .../CorrelationsContextUtils.java | 9 +++++---- .../io/opentelemetry/trace/DefaultTracer.java | 3 ++- .../trace/TracingContextUtils.java | 19 +++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java index a676381bf2a..bf83538e54e 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java @@ -31,7 +31,8 @@ @Immutable public final class CorrelationsContextUtils { private static final Context.Key CORR_CONTEXT_KEY = - Context.key("opentelemetry-corr-context-key"); + Context.keyWithDefault( + "opentelemetry-corr-context-key", EmptyCorrelationContext.getInstance()); /** * Creates a new {@code Context} with the given value set. @@ -65,8 +66,7 @@ public static CorrelationContext getCurrentCorrelationContext() { * @since 0.3.0 */ public static CorrelationContext getCorrelationContext(Context context) { - CorrelationContext corrContext = CORR_CONTEXT_KEY.get(context); - return corrContext == null ? EmptyCorrelationContext.getInstance() : corrContext; + return CORR_CONTEXT_KEY.get(context); } /** @@ -79,7 +79,8 @@ public static CorrelationContext getCorrelationContext(Context context) { */ @Nullable public static CorrelationContext getCorrelationContextWithoutDefault(Context context) { - return CORR_CONTEXT_KEY.get(context); + CorrelationContext corrContext = CORR_CONTEXT_KEY.get(context); + return corrContext == EmptyCorrelationContext.getInstance() ? null : corrContext; } /** diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index da36b06730b..439f8df1275 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -21,6 +21,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; import java.util.Map; +import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; /** @@ -66,7 +67,7 @@ static NoopSpanBuilder create(String spanName) { } private boolean isRootSpan; - private SpanContext spanContext; + @Nullable private SpanContext spanContext; @Override public Span startSpan() { diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java index 30424a0eca5..b7cd7973a25 100644 --- a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java @@ -30,9 +30,10 @@ @Immutable public final class TracingContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = - Context.key("opentelemetry-trace-span-key"); + Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); private static final Context.Key CONTEXT_SPANCONTEXT_KEY = - Context.key("opentelemetry-trace-spancontext-key"); + Context.keyWithDefault( + "opentelemetry-trace-spancontext-key", SpanContext.getInvalid()); /** * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op @@ -78,8 +79,7 @@ public static Context withSpanContext(SpanContext spanContext, Context context) * @since 0.3.0 */ public static Span getSpan(Context context) { - Span span = CONTEXT_SPAN_KEY.get(context); - return span == null ? DefaultSpan.getInvalid() : span; + return CONTEXT_SPAN_KEY.get(context); } /** @@ -92,7 +92,8 @@ public static Span getSpan(Context context) { */ @Nullable public static Span getSpanWithoutDefault(Context context) { - return CONTEXT_SPAN_KEY.get(context); + Span span = CONTEXT_SPAN_KEY.get(context); + return span == DefaultSpan.getInvalid() ? null : span; } /** @@ -104,8 +105,7 @@ public static Span getSpanWithoutDefault(Context context) { * @since 0.3.0 */ public static SpanContext getSpanContext(Context context) { - SpanContext spanContext = CONTEXT_SPANCONTEXT_KEY.get(context); - return spanContext == null ? SpanContext.getInvalid() : spanContext; + return CONTEXT_SPANCONTEXT_KEY.get(context); } /** @@ -116,8 +116,10 @@ public static SpanContext getSpanContext(Context context) { * @return the {@link SpanContext} from the specified {@code Context}. * @since 0.3.0 */ + @Nullable public static SpanContext getSpanContextWithoutDefault(Context context) { - return CONTEXT_SPANCONTEXT_KEY.get(context); + SpanContext spanContext = CONTEXT_SPANCONTEXT_KEY.get(context); + return spanContext == SpanContext.getInvalid() ? null : spanContext; } /** @@ -131,6 +133,7 @@ public static SpanContext getSpanContextWithoutDefault(Context 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); From 3c966273fa8bbd06dad8ef84c0b3dadd66f35a7e Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Sun, 15 Mar 2020 00:22:47 +0100 Subject: [PATCH 36/42] Minor nit. --- .../opentelemetry/context/propagation/HttpTextFormat.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java index a2458db9646..b8bb14f0144 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java @@ -38,12 +38,12 @@ * public static final Context.Key CONCERN_KEY = Context.key("my-concern-key"); * public MyConcernPropagator implements HttpTextFormat { * public void inject(Context context, C carrier, Setter setter) { - * Object concernObj = CONCERN_KEY.get(context); - * // Use concernObj in the specified context to propagate data. + * Object concern = CONCERN_KEY.get(context); + * // Use concern in the specified context to propagate data. * } * public Context extract(Context context, C carrier, Getter setter) { * // Use setter to get the data from the carrier. - * return context.withValue(CONCERN_KEY, concernObj); + * return context.withValue(CONCERN_KEY, concern); * } * } * }

From 9a1eb5ba722fb5d5faf020239ea20e80eaa5d4ac Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Tue, 17 Mar 2020 06:53:04 +0100 Subject: [PATCH 37/42] Update api/src/main/java/io/opentelemetry/OpenTelemetry.java Co-Authored-By: Giovanni Liva --- api/src/main/java/io/opentelemetry/OpenTelemetry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 947a3dc87c6..1071bf36741 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -102,7 +102,7 @@ public static CorrelationContextManager getCorrelationContextManager() { * propagators for each supported format. * * @return registered propagators container, defaulting to a {@link ContextPropagators} object - * with {@code HttpTraceContext} registered. + * with {@link HttpTraceContext} registered. * @throws IllegalStateException if a specified manager (via system properties) could not be * found. * @since 0.3.0 From 268b5467dd358c4e31277504d08b71e6de222e5b Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Mar 2020 23:17:00 +0100 Subject: [PATCH 38/42] Annotate ContextPropagators with ThreadSafe instead of Immutable. --- .../opentelemetry/context/propagation/ContextPropagators.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index 38d2a51808e..823909985b0 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -16,7 +16,7 @@ package io.opentelemetry.context.propagation; -import javax.annotation.concurrent.Immutable; +import javax.annotation.concurrent.ThreadSafe; /** * A container of the registered propagators for every supported format. @@ -77,7 +77,7 @@ * * @since 0.3.0 */ -@Immutable +@ThreadSafe public interface ContextPropagators { /** From a0ec628baaa97965f2765ec8644c535eabe53bdb Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Thu, 19 Mar 2020 23:26:01 +0100 Subject: [PATCH 39/42] Do not use the fully qualified ContextUtils identifier. --- api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 9643a8dda23..37e5ddb8713 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -139,7 +139,7 @@ public void testSpanContextPropagationCurrentContextValues() { SpanContext.create( new TraceId(1, 1), new SpanId(1), TraceFlags.getDefault(), TraceState.getDefault()), TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current())); - Scope scope = io.opentelemetry.context.ContextUtils.withScopedContext(context); + Scope scope = ContextUtils.withScopedContext(context); // Span in Context has higher priority than SpanContext. try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); From 7a3cbeb7d4ff74c20c624b59af05eae561c37d33 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 20 Mar 2020 03:17:45 +0100 Subject: [PATCH 40/42] Remove SpanContext support from TracingContextUtils. --- .../HttpTraceContextExtractBenchmark.java | 6 +- .../HttpTraceContextInjectBenchmark.java | 4 +- .../io/opentelemetry/trace/DefaultTracer.java | 3 +- .../trace/TracingContextUtils.java | 60 +------------------ .../trace/propagation/B3Propagator.java | 9 ++- .../trace/propagation/HttpTraceContext.java | 10 ++-- .../trace/DefaultTracerTest.java | 18 +----- .../trace/propagation/B3PropagatorTest.java | 12 +++- .../propagation/HttpTraceContextTest.java | 42 +++---------- .../opentracingshim/Propagation.java | 10 ++-- .../sdk/trace/SpanBuilderSdk.java | 2 +- .../sdk/trace/SpanBuilderSdkTest.java | 45 -------------- .../trace/testbed/clientserver/Server.java | 2 +- 13 files changed, 48 insertions(+), 175 deletions(-) diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java index 4406c3fa6cb..0f53caf57e0 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java @@ -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; @@ -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)); } 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 0288fb2bbfc..733d1ac9af3 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -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; @@ -71,7 +72,8 @@ public void set(Map carrier, String key, String value) { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public Map measureInject() { - Context context = TracingContextUtils.withSpanContext(contextToTest, Context.current()); + Context context = + TracingContextUtils.withSpan(DefaultSpan.create(contextToTest), Context.current()); httpTraceContext.inject(context, carrier, setter); return carrier; } diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 439f8df1275..8b2d94df6d6 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -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; @@ -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) diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java index b7cd7973a25..d162d07c8ef 100644 --- a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java @@ -31,9 +31,6 @@ public final class TracingContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); - private static final Context.Key CONTEXT_SPANCONTEXT_KEY = - Context.keyWithDefault( - "opentelemetry-trace-spancontext-key", SpanContext.getInvalid()); /** * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op @@ -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}. @@ -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}. - * - *

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; } /** diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java b/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java index 54ece062798..11550bf6338 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/B3Propagator.java @@ -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; @@ -86,11 +88,12 @@ public void inject(Context context, C carrier, Setter 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) { @@ -122,7 +125,7 @@ public Context extract(Context context, C carrier, Getter getter) { spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } - return TracingContextUtils.withSpanContext(spanContext, context); + return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); } @SuppressWarnings("StringSplitter") 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 85949151d21..4468b6f755d 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -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; @@ -79,12 +81,12 @@ public void inject(Context context, C carrier, Setter 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 void injectImpl(SpanContext spanContext, C carrier, Setter setter) { @@ -124,7 +126,7 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter checkNotNull(getter, "getter"); SpanContext spanContext = extractImpl(carrier, getter); - return TracingContextUtils.withSpanContext(spanContext, context); + return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); } private static SpanContext extractImpl(C carrier, Getter getter) { diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 37e5ddb8713..a47fb8d3ed5 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -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); diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java index 33e16e1b815..64832036370 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/B3PropagatorTest.java @@ -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; @@ -73,6 +73,14 @@ public String get(Map 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 carrier = new LinkedHashMap<>(); 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 0d20e1672fa..40f507abee2 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -17,9 +17,6 @@ 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; @@ -27,12 +24,12 @@ 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; @@ -83,6 +80,14 @@ public String get(Map 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 carrier = new LinkedHashMap(); @@ -141,35 +146,6 @@ public void inject_NotSampledContext_WithTraceState() { TRACESTATE_NOT_DEFAULT_ENCODING); } - @Test - public void inject_Span() { - Map carrier = new LinkedHashMap(); - 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 carrier = new LinkedHashMap(); - 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 carrier = new LinkedHashMap<>(); 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 e2b134d1c6c..fdd1c3ad62c 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -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; @@ -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); @@ -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 { diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 4da045852d6..e2b759ff979 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -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: diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 3724f2e77e0..86544888753 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -19,9 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertFalse; -import io.grpc.Context; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.ContextUtils; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; @@ -34,7 +32,6 @@ 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.LinkedHashMap; import java.util.List; @@ -426,48 +423,6 @@ public void parentCurrentSpan() { } } - @Test - public void parentCurrentSpanContext() { - Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = TracingContextUtils.withSpanContext(parent.getContext(), Context.current()); - Scope scope = ContextUtils.withScopedContext(context); - try { - RecordEventsReadableSpan span = - (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try { - assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); - } finally { - span.end(); - } - } finally { - scope.close(); - parent.end(); - } - } - - @Test - public void parentCurrentContextValues() { - Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - Context context = - TracingContextUtils.withSpanContext( - sampledSpanContext, TracingContextUtils.withSpan(parent, Context.current())); - Scope scope = ContextUtils.withScopedContext(context); - try { - RecordEventsReadableSpan span = - (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try { - assertThat(span.getContext().getTraceId()).isEqualTo(parent.getContext().getTraceId()); - assertThat(span.toSpanData().getParentSpanId()).isEqualTo(parent.getContext().getSpanId()); - } finally { - span.end(); - } - } finally { - scope.close(); - parent.end(); - } - } - @Test public void parent_invalidContext() { Span parent = DefaultSpan.getInvalid(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 34cfcecf9da..3e453f054bb 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -52,7 +52,7 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = TracingContextUtils.getSpanContext(context); + SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); Span span = tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); From 88fdcbb31f5a8120ccf8a05dc8ecedb1c975a880 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 20 Mar 2020 17:36:54 +0100 Subject: [PATCH 41/42] Rever to using non-defaulted key for TracingContextUtils. --- .../trace/TracingContextUtils.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java index d162d07c8ef..9660e0d6e3c 100644 --- a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java +++ b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java @@ -30,18 +30,7 @@ @Immutable public final class TracingContextUtils { private static final Context.Key CONTEXT_SPAN_KEY = - Context.keyWithDefault("opentelemetry-trace-span-key", DefaultSpan.getInvalid()); - - /** - * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op - * {@link Span}. - * - * @return the {@link Span} from the current {@code Context}. - * @since 0.3.0 - */ - public static Span getCurrentSpan() { - return getSpan(Context.current()); - } + Context.key("opentelemetry-trace-span-key"); /** * Creates a new {@code Context} with the given {@link Span} set. @@ -55,6 +44,17 @@ public static Context withSpan(Span span, Context context) { return context.withValue(CONTEXT_SPAN_KEY, span); } + /** + * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op + * {@link Span}. + * + * @return the {@link Span} from the current {@code Context}. + * @since 0.3.0 + */ + public static Span getCurrentSpan() { + return getSpan(Context.current()); + } + /** * Returns the {@link Span} from the specified {@code Context}, falling back to a default, no-op * {@link Span}. @@ -64,7 +64,8 @@ public static Context withSpan(Span span, Context context) { * @since 0.3.0 */ public static Span getSpan(Context context) { - return CONTEXT_SPAN_KEY.get(context); + Span span = CONTEXT_SPAN_KEY.get(context); + return span == null ? DefaultSpan.getInvalid() : span; } /** @@ -77,8 +78,7 @@ public static Span getSpan(Context context) { */ @Nullable public static Span getSpanWithoutDefault(Context context) { - Span span = CONTEXT_SPAN_KEY.get(context); - return DefaultSpan.getInvalid().equals(span) ? null : span; + return CONTEXT_SPAN_KEY.get(context); } /** From 37ba00bd97e8d9021ea083b2add9d13ce2f1f961 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 20 Mar 2020 19:54:46 +0100 Subject: [PATCH 42/42] Revert the default keys for CorrelationsContextUtils. --- .../correlationcontext/CorrelationsContextUtils.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java index bf83538e54e..a676381bf2a 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java @@ -31,8 +31,7 @@ @Immutable public final class CorrelationsContextUtils { private static final Context.Key CORR_CONTEXT_KEY = - Context.keyWithDefault( - "opentelemetry-corr-context-key", EmptyCorrelationContext.getInstance()); + Context.key("opentelemetry-corr-context-key"); /** * Creates a new {@code Context} with the given value set. @@ -66,7 +65,8 @@ public static CorrelationContext getCurrentCorrelationContext() { * @since 0.3.0 */ public static CorrelationContext getCorrelationContext(Context context) { - return CORR_CONTEXT_KEY.get(context); + CorrelationContext corrContext = CORR_CONTEXT_KEY.get(context); + return corrContext == null ? EmptyCorrelationContext.getInstance() : corrContext; } /** @@ -79,8 +79,7 @@ public static CorrelationContext getCorrelationContext(Context context) { */ @Nullable public static CorrelationContext getCorrelationContextWithoutDefault(Context context) { - CorrelationContext corrContext = CORR_CONTEXT_KEY.get(context); - return corrContext == EmptyCorrelationContext.getInstance() ? null : corrContext; + return CORR_CONTEXT_KEY.get(context); } /**