Skip to content

Commit

Permalink
Convert CorrelationContext entry keys and values to simple Strings (#…
Browse files Browse the repository at this point in the history
…1363)

* convert CC entry keys and values to simple Strings

* fix broken javadoc
  • Loading branch information
jkwatson authored Jun 22, 2020
1 parent 3273841 commit 24b4be9
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import javax.annotation.concurrent.Immutable;

/**
* A map from {@link EntryKey} to {@link EntryValue} and {@link EntryMetadata} that can be used to
* label anything that is associated with a specific operation.
* A map from {@link String} to {@link String} and {@link EntryMetadata} that can be used to label
* anything that is associated with a specific operation.
*
* <p>For example, {@code CorrelationContext}s can be used to label stats, log messages, or
* debugging information.
Expand All @@ -41,14 +41,14 @@ public interface CorrelationContext {
Collection<Entry> getEntries();

/**
* Returns the {@code EntryValue} associated with the given {@code EntryKey}.
* Returns the {@code String} associated with the given key.
*
* @param entryKey entry key to return the value for.
* @return the {@code EntryValue} associated with the given {@code EntryKey}, or {@code null} if
* no {@code Entry} with the given {@code entryKey} is in this {@code CorrelationContext}.
* @return the value associated with the given key, or {@code null} if no {@code Entry} with the
* given {@code entryKey} is in this {@code CorrelationContext}.
*/
@Nullable
EntryValue getEntryValue(EntryKey entryKey);
String getEntryValue(String entryKey);

/**
* Builder for the {@link CorrelationContext} class.
Expand Down Expand Up @@ -88,22 +88,22 @@ interface Builder {
/**
* Adds the key/value pair and metadata regardless of whether the key is present.
*
* @param key the {@code EntryKey} which will be set.
* @param value the {@code EntryValue} to set for the given key.
* @param key the {@code String} key which will be set.
* @param value the {@code String} value to set for the given key.
* @param entryMetadata the {@code EntryMetadata} associated with this {@link Entry}.
* @return this
* @since 0.1.0
*/
Builder put(EntryKey key, EntryValue value, EntryMetadata entryMetadata);
Builder put(String key, String value, EntryMetadata entryMetadata);

/**
* Removes the key if it exists.
*
* @param key the {@code EntryKey} which will be removed.
* @param key the {@code String} key which will be removed.
* @return this
* @since 0.1.0
*/
Builder remove(EntryKey key);
Builder remove(String key);

/**
* Creates a {@code CorrelationContext} from this builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ public CorrelationContext.Builder setNoParent() {
}

@Override
public CorrelationContext.Builder put(
EntryKey key, EntryValue value, EntryMetadata entryMetadata) {
public CorrelationContext.Builder put(String key, String value, EntryMetadata entryMetadata) {
Utils.checkNotNull(key, "key");
Utils.checkNotNull(value, "value");
Utils.checkNotNull(entryMetadata, "entryMetadata");
return this;
}

@Override
public CorrelationContext.Builder remove(EntryKey key) {
public CorrelationContext.Builder remove(String key) {
Utils.checkNotNull(key, "key");
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Collection<Entry> getEntries() {

@Nullable
@Override
public EntryValue getEntryValue(EntryKey entryKey) {
public String getEntryValue(String entryKey) {
return null;
}

Expand Down
47 changes: 43 additions & 4 deletions api/src/main/java/io/opentelemetry/correlationcontext/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,31 @@

import com.google.auto.value.AutoValue;
import io.opentelemetry.correlationcontext.EntryMetadata.EntryTtl;
import io.opentelemetry.internal.StringUtils;
import io.opentelemetry.internal.Utils;
import javax.annotation.concurrent.Immutable;

/**
* {@link EntryKey} paired with a {@link EntryValue}.
* String-String key-value pair, along with {@link EntryMetadata}.
*
* @since 0.1.0
*/
@Immutable
@AutoValue
public abstract class Entry {
/**
* The maximum length for an entry key name. The value is {@value #MAX_KEY_LENGTH}.
*
* @since 0.1.0
*/
public static final int MAX_KEY_LENGTH = 255;

/**
* The maximum length for a entry value. The value is {@value #MAX_VALUE_LENGTH}.
*
* @since 0.1.0
*/
public static final int MAX_VALUE_LENGTH = 255;

/** Default propagation metadata - unlimited propagation. */
public static final EntryMetadata METADATA_UNLIMITED_PROPAGATION =
Expand All @@ -44,7 +59,9 @@ public abstract class Entry {
* @return a {@code Entry}.
* @since 0.1.0
*/
public static Entry create(EntryKey key, EntryValue value, EntryMetadata entryMetadata) {
public static Entry create(String key, String value, EntryMetadata entryMetadata) {
Utils.checkArgument(keyIsValid(key), "Invalid entry key name: %s", key);
Utils.checkArgument(isValueValid(value), "Invalid entry value: %s", value);
return new AutoValue_Entry(key, value, entryMetadata);
}

Expand All @@ -54,15 +71,15 @@ public static Entry create(EntryKey key, EntryValue value, EntryMetadata entryMe
* @return the entry's key.
* @since 0.1.0
*/
public abstract EntryKey getKey();
public abstract String getKey();

/**
* Returns the entry's value.
*
* @return the entry's value.
* @since 0.1.0
*/
public abstract EntryValue getValue();
public abstract String getValue();

/**
* Returns the {@link EntryMetadata} associated with this {@link Entry}.
Expand All @@ -71,4 +88,26 @@ public static Entry create(EntryKey key, EntryValue value, EntryMetadata entryMe
* @since 0.1.0
*/
public abstract EntryMetadata getEntryMetadata();

/**
* Determines whether the given {@code String} is a valid entry key.
*
* @param name the entry key name to be validated.
* @return whether the name is valid.
*/
private static boolean keyIsValid(String name) {
return !name.isEmpty()
&& name.length() <= MAX_KEY_LENGTH
&& StringUtils.isPrintableString(name);
}

/**
* Determines whether the given {@code String} is a valid entry value.
*
* @param value the entry value to be validated.
* @return whether the value is valid.
*/
private static boolean isValueValid(String value) {
return value.length() <= MAX_VALUE_LENGTH && StringUtils.isPrintableString(value);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
* to label anything that is associated with a specific operation. For example, the {@code
* opentelemetry.stats} package labels all stats with the current entries.
*
* <p>{@link io.opentelemetry.correlationcontext.Entry Entrys} are key-value pairs. The {@link
* io.opentelemetry.correlationcontext.EntryKey keys} and {@link
* io.opentelemetry.correlationcontext.EntryValue values} are wrapped {@code String}s. They are
* stored as a map in a {@link io.opentelemetry.correlationcontext.CorrelationContext}.
* <p>{@link io.opentelemetry.correlationcontext.Entry Entrys} are key-value pairs of {@link
* java.lang.String}s. They are stored as a map in a {@link
* io.opentelemetry.correlationcontext.CorrelationContext}.
*
* <p>Note that entries are independent of the tracing data that is propagated in the {@code
* io.grpc.Context}, such as trace ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
public final class DefaultCorrelationContextManagerTest {
private static final CorrelationContextManager defaultCorrelationContextManager =
DefaultCorrelationContextManager.getInstance();
private static final EntryKey KEY = EntryKey.create("key");
private static final EntryValue VALUE = EntryValue.create("value");
private static final String KEY = "key";
private static final String VALUE = "value";

private static final CorrelationContext DIST_CONTEXT =
new CorrelationContext() {
Expand All @@ -46,7 +46,7 @@ public Collection<Entry> getEntries() {
}

@Override
public EntryValue getEntryValue(EntryKey entryKey) {
public String getEntryValue(String entryKey) {
return VALUE;
}
};
Expand Down
Loading

0 comments on commit 24b4be9

Please sign in to comment.