Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial relocation of ContextUtils classes. #904

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

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.CorrelationsContextUtils;
import io.opentelemetry.internal.Utils;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static CorrelationContextManager getInstance() {

@Override
public CorrelationContext getCurrentContext() {
return ContextUtils.getValue();
return CorrelationsContextUtils.getCorrelationContextWithDefault(Context.current());
}

@Override
Expand All @@ -63,7 +64,7 @@ public CorrelationContext.Builder contextBuilder() {

@Override
public Scope withContext(CorrelationContext distContext) {
return ContextUtils.withCorrelationContext(distContext);
return CorrelationsContextUtils.withScopedCorrelationContext(distContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

package io.opentelemetry.correlationcontext.unsafe;
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;
Expand All @@ -32,10 +33,10 @@
* @since 0.1.0
*/
@Immutable
public final class ContextUtils {
public final class CorrelationsContextUtils {
private static final Context.Key<CorrelationContext> DIST_CONTEXT_KEY =
Context.keyWithDefault(
"opentelemetry-dist-context-key", EmptyCorrelationContext.getInstance());
Context.key("opentelemetry-dist-context-key");
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
private static final CorrelationContext DEFAULT_VALUE = EmptyCorrelationContext.getInstance();

/**
* Creates a new {@code Context} with the given value set.
Expand All @@ -44,7 +45,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);
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -56,7 +57,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);
}

Expand All @@ -66,7 +67,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();
}

Expand All @@ -77,10 +78,23 @@ 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);
}

/**
* 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
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
*/
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}.
Expand All @@ -89,9 +103,10 @@ 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) {
return CorrelationContextInScope.create(distContext);
public static Scope withScopedCorrelationContext(CorrelationContext distContext) {
Context context = withCorrelationContext(distContext);
return ContextUtils.withScopedContext(context);
}

private ContextUtils() {}
private CorrelationsContextUtils() {}
}

This file was deleted.

7 changes: 4 additions & 3 deletions api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package io.opentelemetry.trace;

import io.grpc.Context;
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.TracingContextUtils;
import java.util.Map;
import javax.annotation.concurrent.ThreadSafe;

Expand All @@ -49,12 +50,12 @@ public static Tracer getInstance() {

@Override
public Span getCurrentSpan() {
return ContextUtils.getValue();
return TracingContextUtils.getSpanWithDefault(Context.current());
}

@Override
public Scope withSpan(Span span) {
return ContextUtils.withSpan(span);
return TracingContextUtils.withScopedSpan(span);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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;
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

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}.
*
* <p>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<Span> CONTEXT_SPAN_KEY =
Context.<Span>key("opentelemetry-trace-span-key");
private static final Context.Key<SpanContext> CONTEXT_SPANCONTEXT_KEY =
Context.<SpanContext>key("opentelemetry-trace-spancontext-key");
private static final Span DEFAULT_SPAN = DefaultSpan.getInvalid();

/**
* Creates a new {@code Context} with the given value set.
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
*
* @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());
}
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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}.
*
* <p>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() {}
}
Loading