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

Context propagation update (OTEP 66) #720

Merged
merged 47 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e2f642b
Initial Propagators refactor.
carlosalberto Dec 16, 2019
d5d3e02
Add tests for the new propagators changes.
carlosalberto Dec 17, 2019
b60b02d
Make the SDK compile/pass.
carlosalberto Dec 17, 2019
9160417
Make the OT shim compile/pass.
carlosalberto Dec 17, 2019
b97d700
Make contrib components compile/pass.
carlosalberto Dec 17, 2019
0b109f9
Improvement over Span/SpanContext handling in Context.
carlosalberto Dec 18, 2019
51732a9
Add Span.setParent(Context) overload.
carlosalberto Dec 19, 2019
d6894d4
Do not provide default values for Span/SpanContext keys in Context.
carlosalberto Dec 19, 2019
d5acde5
Improve the Context's active state handling.
carlosalberto Dec 19, 2019
71f04d4
Rename DistributedContext to CorrelationContext.
carlosalberto Dec 19, 2019
eb1e411
Improve names for correlationcontext's ContextUtils methods.
carlosalberto Dec 19, 2019
bbfa035
Don't provide an automatic default for current CorrelationContext.
carlosalberto Dec 19, 2019
b9f9b93
Improve the client-server example after the recent changes.
carlosalberto Dec 19, 2019
fdf644b
Adds CorrelationContext.Builder.setParent(Context) overload.
carlosalberto Dec 20, 2019
fd2b4a9
s/be/become.
carlosalberto Dec 20, 2019
24fecd1
Fix javadoc.
carlosalberto Dec 20, 2019
3f7c227
Merge branch 'master' into context_prop_update
carlosalberto Jan 9, 2020
b56ac60
No need to use diamond.
carlosalberto Jan 9, 2020
08297c6
Simply import withScopedContext().
carlosalberto Jan 9, 2020
be3dd73
Merge branch 'master' into context_prop_update
carlosalberto Jan 22, 2020
e06c32f
Fix the API/SDK build.
carlosalberto Jan 24, 2020
4c48266
Remove the builder from the Propagators interface.
carlosalberto Jan 24, 2020
1328ec5
Fix name.
carlosalberto Jan 24, 2020
d35f19e
Use Collections.emptyList() directly.
carlosalberto Jan 24, 2020
14acd5c
Rename Propagators to ContextPropagators.
carlosalberto Jan 24, 2020
b0649da
Move context/ members in api/ to context_prop/
carlosalberto Feb 20, 2020
762408e
Add check/tests for null scoped Contexts.
carlosalberto Feb 20, 2020
05b7628
Rename ContextUtils classes to better alternatives.
carlosalberto Feb 21, 2020
1113f61
Update the context* util classes.
carlosalberto Mar 9, 2020
d271bb6
Merge branch 'master' into context_prop_update
carlosalberto Mar 10, 2020
43e42d4
Make the code compile after the latest master merge.
carlosalberto Mar 10, 2020
014b3c4
Cache the fields() in our default composite propagator.
carlosalberto Mar 10, 2020
f3d29a3
Remove the overloads of setParent(Context) for now.
carlosalberto Mar 10, 2020
555095b
Use DefaultSpan for the tests instead of calling getTracerProvider()
carlosalberto Mar 10, 2020
3c1f781
Fix the sdk testbed artifact build.
carlosalberto Mar 10, 2020
64b5b1e
Merge branch 'master' into context_prop_update
carlosalberto Mar 13, 2020
136f0aa
Make the B3 propagator comply with the new propagator API.
carlosalberto Mar 14, 2020
ddc370a
Simplify the HttpTraceContextTest tests.
carlosalberto Mar 14, 2020
a1d93ec
Simplify the ContextUtils* classes handling of default values.
carlosalberto Mar 14, 2020
3c96627
Minor nit.
carlosalberto Mar 14, 2020
5f820cd
Merge branch 'master' into context_prop_update
carlosalberto Mar 15, 2020
9a1eb5b
Update api/src/main/java/io/opentelemetry/OpenTelemetry.java
carlosalberto Mar 17, 2020
268b546
Annotate ContextPropagators with ThreadSafe instead of Immutable.
carlosalberto Mar 19, 2020
a0ec628
Do not use the fully qualified ContextUtils identifier.
carlosalberto Mar 19, 2020
7a3cbeb
Remove SpanContext support from TracingContextUtils.
carlosalberto Mar 20, 2020
88fdcbb
Rever to using non-defaulted key for TracingContextUtils.
carlosalberto Mar 20, 2020
37ba00b
Revert the default keys for CorrelationsContextUtils.
carlosalberto Mar 20, 2020
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
60 changes: 48 additions & 12 deletions api/src/main/java/io/opentelemetry/OpenTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.opentelemetry;

import io.opentelemetry.distributedcontext.DefaultDistributedContextManager;
import io.opentelemetry.distributedcontext.DistributedContextManager;
import io.opentelemetry.distributedcontext.spi.DistributedContextManagerProvider;
import io.opentelemetry.context.propagation.DefaultPropagators;
import io.opentelemetry.context.propagation.Propagators;
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;
import io.opentelemetry.metrics.Meter;
Expand All @@ -28,20 +31,21 @@
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;
import javax.annotation.concurrent.ThreadSafe;

/**
* This class provides a static global accessor for telemetry objects {@link Tracer}, {@link Meter}
* and {@link DistributedContextManager}.
* and {@link CorrelationContextManager}.
*
* <p>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 {
Expand All @@ -50,7 +54,10 @@ 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();

/**
* Returns a singleton {@link TracerFactory}.
Expand All @@ -77,18 +84,47 @@ 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;
}

/**
* 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.
* @throws NullPointerException if {@code propagators} is {@code null}.
* @since 0.3.0
*/
public static void setPropagators(Propagators propagators) {
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
Utils.checkNotNull(propagators, "propagators");
getInstance().propagators = propagators;
}

/** Lazy loads an instance. */
private static OpenTelemetry getInstance() {
if (instance == null) {
Expand All @@ -113,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();
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
import io.opentelemetry.context.Scope;
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 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() {}
}
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>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.
*
* <p>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<HttpTextFormat> 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<HttpTextFormat> textPropagators) {
this.textPropagators = new HttpTextFormat[textPropagators.size()];
textPropagators.toArray(this.textPropagators);
}

@Override
public List<String> fields() {
List<String> fields = new ArrayList<>();
for (int i = 0; i < textPropagators.length; i++) {
fields.addAll(textPropagators[i].fields());
}

return fields;
}

@Override
public <C> void inject(Context context, C carrier, Setter<C> setter) {
for (int i = 0; i < textPropagators.length; i++) {
textPropagators[i].inject(context, carrier, setter);
}
}

@Override
public <C> Context extract(Context context, C carrier, Getter<C> 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<String> fields() {
return Collections.<String>emptyList();
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public <C> void inject(Context context, C carrier, Setter<C> setter) {}

@Override
public <C> Context extract(Context context, C carrier, Getter<C> getter) {
return context;
}
}
}
Loading