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

Make the baggage API fully functional in the API #1822

Merged
merged 4 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 1 addition & 18 deletions api/src/main/java/io/opentelemetry/OpenTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry;

import io.opentelemetry.baggage.BaggageManager;
import io.opentelemetry.baggage.DefaultBaggageManager;
import io.opentelemetry.baggage.spi.BaggageManagerFactory;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.DefaultContextPropagators;
Expand Down Expand Up @@ -37,13 +36,13 @@
*/
@ThreadSafe
public final class OpenTelemetry {

private static final Object mutex = new Object();

@Nullable private static volatile OpenTelemetry instance;

private final TracerProvider tracerProvider;
private final MeterProvider meterProvider;
private final BaggageManager contextManager;

private volatile ContextPropagators propagators = DefaultContextPropagators.builder().build();

Expand Down Expand Up @@ -126,17 +125,6 @@ public static Meter getMeter(String instrumentationName, String instrumentationV
return getMeterProvider().get(instrumentationName, instrumentationVersion);
}

/**
* Returns a singleton {@link BaggageManager}.
*
* @return registered manager or default via {@link DefaultBaggageManager#getInstance()}.
* @throws IllegalStateException if a specified manager (via system properties) could not be
* found.
*/
public static BaggageManager getBaggageManager() {
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

return getInstance().contextManager;
}

/**
* Returns a {@link ContextPropagators} object, which can be used to access the set of registered
* propagators for each supported format.
Expand Down Expand Up @@ -188,11 +176,6 @@ private OpenTelemetry() {
meterProviderFactory != null
? meterProviderFactory.create()
: DefaultMeterProvider.getInstance();
BaggageManagerFactory contextManagerProvider = loadSpi(BaggageManagerFactory.class);
contextManager =
contextManagerProvider != null
? contextManagerProvider.create()
: DefaultBaggageManager.getInstance();
}

/**
Expand Down
27 changes: 24 additions & 3 deletions api/src/main/java/io/opentelemetry/baggage/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
*/
@Immutable
public interface Baggage {

/** Baggage with no entries. */
static Baggage empty() {
return ImmutableBaggage.EMPTY;
}

/** Creates a new {@link Builder} for creating Baggage. */
static Builder builder() {
return ImmutableBaggage.builder();
}

/**
* Returns an immutable collection of the entries in this {@code Baggage}. Order of entries is not
* guaranteed.
Expand All @@ -39,10 +50,11 @@ public interface Baggage {

/** Builder for the {@link Baggage} class. */
interface Builder {

/**
* Sets the parent {@link Baggage} to use from the specified {@code Context}. If no parent
* {@link Baggage} is provided, the value of {@link BaggageManager#getCurrentBaggage()} at
* {@link #build()} time will be used as parent, unless {@link #setNoParent()} was called.
* {@link Baggage} is provided, the value of {@link BaggageUtils#getCurrentBaggage()} at {@link
* #build()} time will be used as parent, unless {@link #setNoParent()} was called.
*
* <p>If no parent {@link Baggage} is available in the specified {@code Context}, the resulting
* {@link Baggage} will become a root instance, as if {@link #setNoParent()} had been called.
Expand All @@ -62,7 +74,7 @@ interface Builder {
/**
* Sets the option to become a root {@link Baggage} with no parent. If <b>not</b> called, the
* value provided using {@link #setParent(Context)} or otherwise {@link
* BaggageManager#getCurrentBaggage()} at {@link #build()} time will be used as parent.
* BaggageUtils#getCurrentBaggage()} at {@link #build()} time will be used as parent.
*
* @return this.
*/
Expand All @@ -78,6 +90,15 @@ interface Builder {
*/
Builder put(String key, String value, EntryMetadata entryMetadata);

/**
* Adds the key/value pair with empty metadata regardless of whether the key is present.
*
* @param key the {@code String} key which will be set.
* @param value the {@code String} value to set for the given key.
* @return this
*/
Builder put(String key, String value);

/**
* Removes the key if it exists.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static Baggage getCurrentBaggage() {
*/
public static Baggage getBaggage(Context context) {
Baggage baggage = context.getValue(CORR_CONTEXT_KEY);
return baggage == null ? EmptyBaggage.getInstance() : baggage;
return baggage == null ? Baggage.empty() : baggage;
}

/**
Expand Down

This file was deleted.

39 changes: 0 additions & 39 deletions api/src/main/java/io/opentelemetry/baggage/EmptyBaggage.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.baggage;
package io.opentelemetry.baggage;

import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.baggage.Baggage;
import io.opentelemetry.baggage.BaggageUtils;
import io.opentelemetry.baggage.Entry;
import io.opentelemetry.baggage.EntryMetadata;
import io.opentelemetry.context.Context;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -22,24 +17,29 @@
@Immutable
// TODO: Migrate to AutoValue
// @AutoValue
class BaggageSdk implements Baggage {
class ImmutableBaggage implements Baggage {
static final Baggage EMPTY = new ImmutableBaggage.Builder().build();

// The types of the EntryKey and Entry must match for each entry.
private final Map<String, Entry> entries;
@Nullable private final Baggage parent;

/**
* Creates a new {@link BaggageSdk} with the given entries.
* Creates a new {@link ImmutableBaggage} with the given entries.
*
* @param entries the initial entries for this {@code BaggageSdk}.
* @param parent providing a default set of entries
*/
private BaggageSdk(Map<String, ? extends Entry> entries, Baggage parent) {
private ImmutableBaggage(Map<String, ? extends Entry> entries, Baggage parent) {
this.entries =
Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(entries, "entries")));
this.parent = parent;
}

public static Baggage.Builder builder() {
return new Builder();
}

@Override
public Collection<Entry> getEntries() {
Map<String, Entry> combined = new HashMap<>(entries);
Expand Down Expand Up @@ -72,16 +72,16 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof BaggageSdk)) {
if (!(o instanceof ImmutableBaggage)) {
return false;
}

BaggageSdk baggageSdk = (BaggageSdk) o;
ImmutableBaggage baggage = (ImmutableBaggage) o;

if (!entries.equals(baggageSdk.entries)) {
if (!entries.equals(baggage.entries)) {
return false;
}
return Objects.equals(parent, baggageSdk.parent);
return Objects.equals(parent, baggage.parent);
}

@Override
Expand All @@ -91,6 +91,11 @@ public int hashCode() {
return result;
}

@Override
public String toString() {
return "ImmutableBaggage{" + "entries=" + entries + ", parent=" + parent + '}';
}

// TODO: Migrate to AutoValue.Builder
// @AutoValue.Builder
static class Builder implements Baggage.Builder {
Expand Down Expand Up @@ -128,6 +133,14 @@ public Baggage.Builder put(String key, String value, EntryMetadata entryMetadata
return this;
}

@Override
public Baggage.Builder put(String key, String value) {
entries.put(
Objects.requireNonNull(key, "key"),
Entry.create(key, Objects.requireNonNull(value, "value"), EntryMetadata.EMPTY));
return this;
}

@Override
public Baggage.Builder remove(String key) {
entries.remove(Objects.requireNonNull(key, "key"));
Expand All @@ -138,11 +151,11 @@ public Baggage.Builder remove(String key) {
}

@Override
public BaggageSdk build() {
public ImmutableBaggage build() {
if (parent == null && !noImplicitParent) {
parent = OpenTelemetry.getBaggageManager().getCurrentBaggage();
parent = BaggageUtils.getCurrentBaggage();
}
return new BaggageSdk(entries, parent);
return new ImmutableBaggage(entries, parent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.opentelemetry.baggage.Baggage;
import io.opentelemetry.baggage.BaggageManager;
import io.opentelemetry.baggage.BaggageUtils;
import io.opentelemetry.baggage.EmptyBaggage;
import io.opentelemetry.baggage.Entry;
import io.opentelemetry.baggage.EntryMetadata;
import io.opentelemetry.context.Context;
Expand All @@ -26,15 +25,7 @@ public class W3CBaggagePropagator implements TextMapPropagator {

private static final String FIELD = "baggage";
private static final List<String> FIELDS = singletonList(FIELD);
private static final W3CBaggagePropagator INSTANCE =
new W3CBaggagePropagator(OpenTelemetry.getBaggageManager());

private final BaggageManager baggageManager;

// visible for testing
W3CBaggagePropagator(BaggageManager baggageManager) {
this.baggageManager = baggageManager;
}
private static final W3CBaggagePropagator INSTANCE = new W3CBaggagePropagator();

/**
* Singleton instance of the W3C Baggage Propagator. Uses the {@link BaggageManager} from the
Expand Down Expand Up @@ -77,14 +68,14 @@ public <C> Context extract(Context context, @Nullable C carrier, Getter<C> gette
return context;
}
if (baggageHeader.isEmpty()) {
return BaggageUtils.withBaggage(EmptyBaggage.getInstance(), context);
return BaggageUtils.withBaggage(Baggage.empty(), context);
}

Baggage.Builder baggageBuilder = baggageManager.baggageBuilder();
Baggage.Builder baggageBuilder = Baggage.builder();
try {
extractEntries(baggageHeader, baggageBuilder);
} catch (Exception e) {
return BaggageUtils.withBaggage(EmptyBaggage.getInstance(), context);
return BaggageUtils.withBaggage(Baggage.empty(), context);
}
return BaggageUtils.withBaggage(baggageBuilder.build(), context);
}
Expand Down
Loading