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

[Do not merge] Initial prototype for context-prop overhaul. #655

72 changes: 72 additions & 0 deletions api/src/main/java/io/opentelemetry/context/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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;

public final class Context {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add this class we also need all the helpers:

  • Wrap callable/runnable
  • Wrapper for an Executor
  • Run/Call helper to run a Runnable/Callable with the context.

Also we need to make sure somehow that users are not double wrapping things with this Context and io.grpc.Context when they use both opentelemetry and io.grpc.Context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add this class we also need all the helpers:

Good point, yes. Although I wonder if we could postpone them (for a second version), or you think we would need them right from the start?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without them users cannot instrument their code. That is the main reason I actually used directly the grpc one, to not duplicate code unnecessary.

I would like to see how an RPC integration will look like with the new Injector/Extractor

private final io.grpc.Context ctx;

private Context(io.grpc.Context ctx) {
this.ctx = ctx;
}

public static Context current() {
return new Context(io.grpc.Context.current());
}

public static Scope setCurrent(Context ctx) {
return new ScopeImpl(ctx);
}

public static <T> Context.Key<T> createKey(String name) {
return new Key<T>(io.grpc.Context.<T>key(name));
}

public <T> T getValue(Context.Key<T> key) {
return key.key().get(ctx);
}

public <T> Context setValue(Context.Key<T> key, T value) {
return new Context(ctx.withValue(key.key(), value));
}

public static final class Key<T> {
io.grpc.Context.Key<T> key;

private Key(io.grpc.Context.Key<T> key) {
this.key = key;
}

io.grpc.Context.Key<T> key() {
return key;
}
}

static final class ScopeImpl implements Scope {
private final io.grpc.Context ctx;
private final io.grpc.Context prevCtx;

public ScopeImpl(Context ctx) {
this.ctx = ctx.ctx;
this.prevCtx = ctx.ctx.attach();
}

@Override
public void close() {
ctx.detach(prevCtx);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.opentelemetry.context.Context;
import javax.annotation.Nullable;

public interface HttpExtractor {
<C> Context extract(Context ctx, Getter<C> getter);

interface Getter<C> {
@Nullable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in our meeting today, This interface should have another method that provides access to all the headers:

Iterable<String> headers();

String get(C carrier, String key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.opentelemetry.context.Context;

public interface HttpInjector {
<C> void inject(Context ctx, C carrier, Setter<C> setter);

interface Setter<C> {
void put(C carrier, String key, String value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.distributedcontext;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.HttpExtractor;
import io.opentelemetry.context.propagation.HttpInjector;

public interface BaggageManager {
public Context setValue(Context ctx, String key, String value);

public String getValue(Context ctx, String key);

public void removeValue(Context ctx, String key);

public void clear(Context ctx);

public HttpInjector getHttpInjector();

public HttpExtractor getHttpExtractor();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.distributedcontext;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.HttpExtractor;
import io.opentelemetry.context.propagation.HttpInjector;
import java.util.List;

public interface CorrelationsManager {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what we called Tags in opencensus? I like Tags better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I called this Correlations as this is the used name in the related OTEPS ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please tell @tedsuo that Tags is a better name :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdandrutu you and @yurishkuro have a jello wrestling match, the winner can pick the name. :)

public Context setValue(Context ctx, CorrelationLabel label, HopLimit hopLimit);

public Context setValues(Context ctx, List<CorrelationLabel> labels, HopLimit hopLimit);

public interface CorrelationLabel {
public String key();

public String value();
}

public enum HopLimit {
NO_PROPAGATION,
UNLIMITED_PROPAGATION
}

public HttpInjector getHttpInjector();

public HttpExtractor getHttpExtractor();
}