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 6 commits
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
@@ -0,0 +1,113 @@
/*
* 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}.
*
* <p>Most code should interact with the current context via the public APIs in {@link
* CorrelationContext} and avoid accessing this class directly.
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.
* @return a new context with the given value set.
* @since 0.1.0
*/
public static Context withCorrelationContext(CorrelationContext corrContext) {
return withCorrelationContext(corrContext, Context.current());
}
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 a
* default, no-op {@link CorrelationContext}.
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the {@link CorrelationContext} from the current {@code Context}.
* @since 0.3.0
*/
public static CorrelationContext getCorrelationContext() {
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
return getCorrelationContext(Context.current());
}

/**
* Returns the {@link CorrelationContext} from the specified {@code Context}, falling back to a
* default, no-op {@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);
return ContextUtils.withScopedContext(context);
}
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

private CorrelationsContextUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package io.opentelemetry.correlationcontext;

import io.grpc.Context;
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 +50,7 @@ public static CorrelationContextManager getInstance() {

@Override
public CorrelationContext getCurrentContext() {
return ContextUtils.getValue();
return CorrelationsContextUtils.getCorrelationContext(Context.current());
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
}
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

@Override
Expand All @@ -60,7 +60,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.

6 changes: 3 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,11 +16,11 @@

package io.opentelemetry.trace;

import io.grpc.Context;
import io.opentelemetry.context.Scope;
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 +46,12 @@ public static Tracer getInstance() {

@Override
public Span getCurrentSpan() {
return ContextUtils.getValue();
return TracingContextUtils.getSpan(Context.current());
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Override
Expand Down
Loading