Skip to content

Commit

Permalink
Initial relocation of ContextUtils classes. (open-telemetry#904)
Browse files Browse the repository at this point in the history
* Initial relocation of ContextUtils classes.

Now they will live within the child `propagation` package,
with a prefix based on their cross-cutting concern, to be
referenced easily.
  • Loading branch information
carlosalberto authored and MikeGoldsmith committed Mar 13, 2020
1 parent 8779aee commit 0e3911b
Show file tree
Hide file tree
Showing 22 changed files with 502 additions and 459 deletions.
Original file line number Diff line number Diff line change
@@ -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<CorrelationContext> 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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.correlationcontext.unsafe.ContextUtils;
import io.opentelemetry.internal.Utils;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -50,7 +49,7 @@ public static CorrelationContextManager getInstance() {

@Override
public CorrelationContext getCurrentContext() {
return ContextUtils.getValue();
return CorrelationsContextUtils.getCurrentCorrelationContext();
}

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

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

@Override
Expand Down

This file was deleted.

This file was deleted.

5 changes: 2 additions & 3 deletions api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.trace.propagation.HttpTraceContext;
import io.opentelemetry.trace.unsafe.ContextUtils;
import java.util.Map;
import javax.annotation.concurrent.ThreadSafe;

Expand All @@ -46,12 +45,12 @@ public static Tracer getInstance() {

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

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

@Override
Expand Down
97 changes: 97 additions & 0 deletions api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java
Original file line number Diff line number Diff line change
@@ -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<Span> CONTEXT_SPAN_KEY =
Context.<Span>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() {}
}
Loading

0 comments on commit 0e3911b

Please sign in to comment.