forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial relocation of ContextUtils classes. (open-telemetry#904)
* 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
1 parent
8779aee
commit 0e3911b
Showing
22 changed files
with
502 additions
and
459 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 0 additions & 97 deletions
97
api/src/main/java/io/opentelemetry/correlationcontext/unsafe/ContextUtils.java
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
api/src/main/java/io/opentelemetry/correlationcontext/unsafe/CorrelationContextInScope.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
Oops, something went wrong.