From f52ce48f3c23549d1b524be4fc791320a3ab72b9 Mon Sep 17 00:00:00 2001 From: jwatson Date: Fri, 19 Jun 2020 11:27:13 -0700 Subject: [PATCH 1/2] convert CC entry keys and values to simple Strings --- .../CorrelationContext.java | 22 ++--- .../DefaultCorrelationContextManager.java | 5 +- .../EmptyCorrelationContext.java | 2 +- .../correlationcontext/Entry.java | 47 ++++++++++- .../correlationcontext/EntryKey.java | 84 ------------------- .../correlationcontext/EntryValue.java | 80 ------------------ .../correlationcontext/package-info.java | 7 +- .../DefaultCorrelationContextManagerTest.java | 6 +- .../correlationcontext/EntryKeyTest.java | 80 ------------------ .../correlationcontext/EntryTest.java | 76 ++++++++++++++++- .../correlationcontext/EntryValueTest.java | 75 ----------------- .../opentracingshim/SpanContextShim.java | 11 +-- .../CorrelationContextSdk.java | 18 ++-- .../CorrelationContextSdkTest.java | 10 +-- .../ScopedCorrelationContextTest.java | 16 ++-- 15 files changed, 157 insertions(+), 382 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java delete mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java delete mode 100644 api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java delete mode 100644 api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index 93815e2d115..20bac41b37c 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -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. * *

For example, {@code CorrelationContext}s can be used to label stats, log messages, or * debugging information. @@ -41,14 +41,14 @@ public interface CorrelationContext { Collection 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. @@ -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. diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 8d47920faf1..d1852134724 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -71,8 +71,7 @@ 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"); @@ -80,7 +79,7 @@ public CorrelationContext.Builder put( } @Override - public CorrelationContext.Builder remove(EntryKey key) { + public CorrelationContext.Builder remove(String key) { Utils.checkNotNull(key, "key"); return this; } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java index 384dce01d9c..e290157f7bd 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/EmptyCorrelationContext.java @@ -45,7 +45,7 @@ public Collection getEntries() { @Nullable @Override - public EntryValue getEntryValue(EntryKey entryKey) { + public String getEntryValue(String entryKey) { return null; } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java b/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java index be90f03bb8c..7618e3b8db2 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java @@ -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_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 = @@ -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); } @@ -54,7 +71,7 @@ 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. @@ -62,7 +79,7 @@ public static Entry create(EntryKey key, EntryValue value, EntryMetadata entryMe * @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}. @@ -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); + } } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java b/api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java deleted file mode 100644 index 6f762094182..00000000000 --- a/api/src/main/java/io/opentelemetry/correlationcontext/EntryKey.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 com.google.auto.value.AutoValue; -import io.opentelemetry.internal.StringUtils; -import io.opentelemetry.internal.Utils; -import javax.annotation.concurrent.Immutable; - -/** - * A key to a value stored in a {@link CorrelationContext}. - * - *

Each {@code EntryKey} has a {@code String} name. Names have a maximum length of {@link - * #MAX_LENGTH} and contain only printable ASCII characters. - * - *

{@code EntryKey}s are designed to be used as constants. Declaring each key as a constant - * prevents key names from being validated multiple times. - * - * @since 0.1.0 - */ -@Immutable -@AutoValue -public abstract class EntryKey { - /** - * The maximum length for an entry key name. The value is {@value #MAX_LENGTH}. - * - * @since 0.1.0 - */ - public static final int MAX_LENGTH = 255; - - EntryKey() {} - - /** - * Constructs an {@code EntryKey} with the given name. - * - *

The name must meet the following requirements: - * - *

    - *
  1. It cannot be longer than {@link #MAX_LENGTH}. - *
  2. It can only contain printable ASCII characters. - *
- * - * @param name the name of the key. - * @return an {@code EntryKey} with the given name. - * @throws IllegalArgumentException if the name is not valid. - * @since 0.1.0 - */ - public static EntryKey create(String name) { - Utils.checkArgument(isValid(name), "Invalid EntryKey name: %s", name); - return new AutoValue_EntryKey(name); - } - - /** - * Returns the name of the key. - * - * @return the name of the key. - * @since 0.1.0 - */ - public abstract String getName(); - - /** - * 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 isValid(String name) { - return !name.isEmpty() && name.length() <= MAX_LENGTH && StringUtils.isPrintableString(name); - } -} diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java b/api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java deleted file mode 100644 index 8742c16c778..00000000000 --- a/api/src/main/java/io/opentelemetry/correlationcontext/EntryValue.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 com.google.auto.value.AutoValue; -import io.opentelemetry.internal.StringUtils; -import io.opentelemetry.internal.Utils; -import javax.annotation.concurrent.Immutable; - -/** - * A validated entry value. - * - *

Validation ensures that the {@code String} has a maximum length of {@link #MAX_LENGTH} and - * contains only printable ASCII characters. - * - * @since 0.1.0 - */ -@Immutable -@AutoValue -public abstract class EntryValue { - /** - * The maximum length for a entry value. The value is {@value #MAX_LENGTH}. - * - * @since 0.1.0 - */ - public static final int MAX_LENGTH = 255; - - EntryValue() {} - - /** - * Constructs an {@code EntryValue} from the given string. The string must meet the following - * requirements: - * - *

    - *
  1. It cannot be longer than {@link #MAX_LENGTH}. - *
  2. It can only contain printable ASCII characters. - *
- * - * @param value the entry value. - * @return an {@code EntryValue} from the given string. - * @throws IllegalArgumentException if the {@code String} is not valid. - * @since 0.1.0 - */ - public static EntryValue create(String value) { - Utils.checkArgument(isValid(value), "Invalid EntryValue: %s", value); - return new AutoValue_EntryValue(value); - } - - /** - * Returns the entry value as a {@code String}. - * - * @return the entry value as a {@code String}. - * @since 0.1.0 - */ - public abstract String asString(); - - /** - * 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 isValid(String value) { - return value.length() <= MAX_LENGTH && StringUtils.isPrintableString(value); - } -} diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java index cafe13b13b3..9fc8b574d40 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java @@ -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. * - *

{@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}. + *

{@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}. * *

Note that entries are independent of the tracing data that is propagated in the {@code * io.grpc.Context}, such as trace ID. diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 5b993f362e3..b326b7f7d10 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -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() { @@ -46,7 +46,7 @@ public Collection getEntries() { } @Override - public EntryValue getEntryValue(EntryKey entryKey) { + public String getEntryValue(String entryKey) { return VALUE; } }; diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java deleted file mode 100644 index 73f7a712a9f..00000000000 --- a/api/src/test/java/io/opentelemetry/correlationcontext/EntryKeyTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import java.util.Arrays; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for {@link EntryKey}. */ -@RunWith(JUnit4.class) -public final class EntryKeyTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - - @Test - public void testMaxLength() { - assertThat(EntryKey.MAX_LENGTH).isEqualTo(255); - } - - @Test - public void testGetName() { - assertThat(EntryKey.create("foo").getName()).isEqualTo("foo"); - } - - @Test - public void create_AllowEntryKeyNameWithMaxLength() { - char[] chars = new char[EntryKey.MAX_LENGTH]; - Arrays.fill(chars, 'k'); - String key = new String(chars); - assertThat(EntryKey.create(key).getName()).isEqualTo(key); - } - - @Test - public void create_DisallowEntryKeyNameOverMaxLength() { - char[] chars = new char[EntryKey.MAX_LENGTH + 1]; - Arrays.fill(chars, 'k'); - String key = new String(chars); - thrown.expect(IllegalArgumentException.class); - EntryKey.create(key); - } - - @Test - public void create_DisallowUnprintableChars() { - thrown.expect(IllegalArgumentException.class); - EntryKey.create("\2ab\3cd"); - } - - @Test - public void createString_DisallowEmpty() { - thrown.expect(IllegalArgumentException.class); - EntryKey.create(""); - } - - @Test - public void testEntryKeyEquals() { - new EqualsTester() - .addEqualityGroup(EntryKey.create("foo"), EntryKey.create("foo")) - .addEqualityGroup(EntryKey.create("bar")) - .testEquals(); - } -} diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java index 013fd0dc48a..54bb0874023 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/EntryTest.java @@ -20,18 +20,22 @@ import com.google.common.testing.EqualsTester; import io.opentelemetry.correlationcontext.EntryMetadata.EntryTtl; +import java.util.Arrays; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Tests for {@link Entry}. */ @RunWith(JUnit4.class) public final class EntryTest { + @Rule public final ExpectedException thrown = ExpectedException.none(); - private static final EntryKey KEY = EntryKey.create("KEY"); - private static final EntryKey KEY_2 = EntryKey.create("KEY2"); - private static final EntryValue VALUE = EntryValue.create("VALUE"); - private static final EntryValue VALUE_2 = EntryValue.create("VALUE2"); + private static final String KEY = "KEY"; + private static final String KEY_2 = "KEY2"; + private static final String VALUE = "VALUE"; + private static final String VALUE_2 = "VALUE2"; private static final EntryMetadata METADATA_UNLIMITED_PROPAGATION = EntryMetadata.create(EntryTtl.UNLIMITED_PROPAGATION); private static final EntryMetadata METADATA_NO_PROPAGATION = @@ -59,4 +63,68 @@ public void testEntryEquals() { .addEqualityGroup(Entry.create(KEY, VALUE, METADATA_NO_PROPAGATION)) .testEquals(); } + + @Test + public void testKeyMaxLength() { + assertThat(Entry.MAX_KEY_LENGTH).isEqualTo(255); + } + + @Test + public void create_AllowEntryKeyNameWithMaxLength() { + char[] chars = new char[Entry.MAX_KEY_LENGTH]; + Arrays.fill(chars, 'k'); + String key = new String(chars); + assertThat(Entry.create(key, "value", Entry.METADATA_UNLIMITED_PROPAGATION)).isNotNull(); + } + + @Test + public void create_DisallowEntryKeyNameOverMaxLength() { + char[] chars = new char[Entry.MAX_KEY_LENGTH + 1]; + Arrays.fill(chars, 'k'); + String key = new String(chars); + thrown.expect(IllegalArgumentException.class); + Entry.create(key, "value", Entry.METADATA_UNLIMITED_PROPAGATION); + } + + @Test + public void create_DisallowKeyUnprintableChars() { + thrown.expect(IllegalArgumentException.class); + Entry.create("\2ab\3cd", "value", Entry.METADATA_UNLIMITED_PROPAGATION); + } + + @Test + public void createString_DisallowKeyEmpty() { + thrown.expect(IllegalArgumentException.class); + Entry.create("", "value", Entry.METADATA_UNLIMITED_PROPAGATION); + } + + @Test + public void testValueMaxLength() { + assertThat(Entry.MAX_VALUE_LENGTH).isEqualTo(255); + } + + @Test + public void create_AllowEntryValueWithMaxLength() { + char[] chars = new char[Entry.MAX_VALUE_LENGTH]; + Arrays.fill(chars, 'v'); + String value = new String(chars); + assertThat(Entry.create("key", value, Entry.METADATA_UNLIMITED_PROPAGATION).getValue()) + .isEqualTo(value); + } + + @Test + public void create_DisallowEntryValueOverMaxLength() { + char[] chars = new char[Entry.MAX_VALUE_LENGTH + 1]; + Arrays.fill(chars, 'v'); + String value = new String(chars); + thrown.expect(IllegalArgumentException.class); + Entry.create("key", value, Entry.METADATA_UNLIMITED_PROPAGATION); + } + + @Test + public void disallowEntryValueWithUnprintableChars() { + String value = "\2ab\3cd"; + thrown.expect(IllegalArgumentException.class); + Entry.create("key", value, Entry.METADATA_UNLIMITED_PROPAGATION); + } } diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java deleted file mode 100644 index 19786c3ab98..00000000000 --- a/api/src/test/java/io/opentelemetry/correlationcontext/EntryValueTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 static com.google.common.truth.Truth.assertThat; - -import com.google.common.testing.EqualsTester; -import java.util.Arrays; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for {@link EntryValue}. */ -@RunWith(JUnit4.class) -public final class EntryValueTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - - @Test - public void testMaxLength() { - assertThat(EntryValue.MAX_LENGTH).isEqualTo(255); - } - - @Test - public void testAsString() { - assertThat(EntryValue.create("foo").asString()).isEqualTo("foo"); - } - - @Test - public void create_AllowEntryValueWithMaxLength() { - char[] chars = new char[EntryValue.MAX_LENGTH]; - Arrays.fill(chars, 'v'); - String value = new String(chars); - assertThat(EntryValue.create(value).asString()).isEqualTo(value); - } - - @Test - public void create_DisallowEntryValueOverMaxLength() { - char[] chars = new char[EntryValue.MAX_LENGTH + 1]; - Arrays.fill(chars, 'v'); - String value = new String(chars); - thrown.expect(IllegalArgumentException.class); - EntryValue.create(value); - } - - @Test - public void disallowEntryValueWithUnprintableChars() { - String value = "\2ab\3cd"; - thrown.expect(IllegalArgumentException.class); - EntryValue.create(value); - } - - @Test - public void testEntryValueEquals() { - new EqualsTester() - .addEqualityGroup(EntryValue.create("foo"), EntryValue.create("foo")) - .addEqualityGroup(EntryValue.create("bar")) - .testEquals(); - } -} diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java index 8bb2a046741..692f249335b 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanContextShim.java @@ -18,9 +18,7 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.Entry; -import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; -import io.opentelemetry.correlationcontext.EntryValue; import io.opentracing.SpanContext; import java.util.Iterator; import java.util.Map; @@ -54,7 +52,7 @@ public SpanContextShim( SpanContextShim newWithKeyValue(String key, String value) { CorrelationContext.Builder builder = contextManager().contextBuilder().setParent(distContext); - builder.put(EntryKey.create(key), EntryValue.create(value), DEFAULT_ENTRY_METADATA); + builder.put(key, value, DEFAULT_ENTRY_METADATA); return new SpanContextShim(telemetryInfo(), context, builder.build()); } @@ -85,8 +83,7 @@ public Iterable> baggageItems() { @SuppressWarnings("ReturnMissingNullable") String getBaggageItem(String key) { - EntryValue value = distContext.getEntryValue(EntryKey.create(key)); - return value == null ? null : value.asString(); + return distContext.getEntryValue(key); } static class BaggageIterable implements Iterable> { @@ -124,12 +121,12 @@ static class BaggageEntry implements Map.Entry { @Override public String getKey() { - return entry.getKey().getName(); + return entry.getKey(); } @Override public String getValue() { - return entry.getValue().asString(); + return entry.getValue(); } @Override diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index e4b03cb21be..e3084b1ef99 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -19,9 +19,7 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.Entry; -import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; -import io.opentelemetry.correlationcontext.EntryValue; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -37,7 +35,7 @@ class CorrelationContextSdk implements CorrelationContext { // The types of the EntryKey and Entry must match for each entry. - private final Map entries; + private final Map entries; @Nullable private final CorrelationContext parent; /** @@ -46,8 +44,7 @@ class CorrelationContextSdk implements CorrelationContext { * @param entries the initial entries for this {@code CorrelationContextSdk}. * @param parent providing a default set of entries */ - private CorrelationContextSdk( - Map entries, CorrelationContext parent) { + private CorrelationContextSdk(Map entries, CorrelationContext parent) { this.entries = Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(entries, "entries"))); this.parent = parent; @@ -55,7 +52,7 @@ private CorrelationContextSdk( @Override public Collection getEntries() { - Map combined = new HashMap<>(entries); + Map combined = new HashMap<>(entries); if (parent != null) { for (Entry entry : parent.getEntries()) { if (!combined.containsKey(entry.getKey())) { @@ -75,7 +72,7 @@ public Collection getEntries() { @Nullable @Override - public EntryValue getEntryValue(EntryKey entryKey) { + public String getEntryValue(String entryKey) { Entry entry = entries.get(entryKey); if (entry != null) { return entry.getValue(); @@ -113,7 +110,7 @@ public int hashCode() { static class Builder implements CorrelationContext.Builder { @Nullable private CorrelationContext parent; private boolean noImplicitParent; - private final Map entries; + private final Map entries; /** Create a new empty CorrelationContext builder. */ Builder() { @@ -134,8 +131,7 @@ 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) { entries.put( Objects.requireNonNull(key, "key"), Entry.create( @@ -146,7 +142,7 @@ public CorrelationContext.Builder put( } @Override - public CorrelationContext.Builder remove(EntryKey key) { + public CorrelationContext.Builder remove(String key) { entries.remove(Objects.requireNonNull(key, "key")); if (parent != null && parent.getEntryValue(key) != null) { entries.put(key, null); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index 9b1709c0f0c..d0a96c24918 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -23,9 +23,7 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.Entry; -import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; -import io.opentelemetry.correlationcontext.EntryValue; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -45,11 +43,11 @@ public class CorrelationContextSdkTest { private static final EntryMetadata TMD = EntryMetadata.create(EntryMetadata.EntryTtl.UNLIMITED_PROPAGATION); - private static final EntryKey K1 = EntryKey.create("k1"); - private static final EntryKey K2 = EntryKey.create("k2"); + private static final String K1 = "k1"; + private static final String K2 = "k2"; - private static final EntryValue V1 = EntryValue.create("v1"); - private static final EntryValue V2 = EntryValue.create("v2"); + private static final String V1 = "v1"; + private static final String V2 = "v2"; private static final Entry T1 = Entry.create(K1, V1, TMD); private static final Entry T2 = Entry.create(K2, V2, TMD); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java index d7179a79b6d..abf6bd13312 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java @@ -23,9 +23,7 @@ import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.EmptyCorrelationContext; import io.opentelemetry.correlationcontext.Entry; -import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; -import io.opentelemetry.correlationcontext.EntryValue; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -36,14 +34,14 @@ */ @RunWith(JUnit4.class) public class ScopedCorrelationContextTest { - private static final EntryKey KEY_1 = EntryKey.create("key 1"); - private static final EntryKey KEY_2 = EntryKey.create("key 2"); - private static final EntryKey KEY_3 = EntryKey.create("key 3"); + private static final String KEY_1 = "key 1"; + private static final String KEY_2 = "key 2"; + private static final String KEY_3 = "key 3"; - private static final EntryValue VALUE_1 = EntryValue.create("value 1"); - private static final EntryValue VALUE_2 = EntryValue.create("value 2"); - private static final EntryValue VALUE_3 = EntryValue.create("value 3"); - private static final EntryValue VALUE_4 = EntryValue.create("value 4"); + private static final String VALUE_1 = "value 1"; + private static final String VALUE_2 = "value 2"; + private static final String VALUE_3 = "value 3"; + private static final String VALUE_4 = "value 4"; private static final EntryMetadata METADATA_UNLIMITED_PROPAGATION = EntryMetadata.create(EntryMetadata.EntryTtl.UNLIMITED_PROPAGATION); From b9251fd5f64574bfc474cbe3f161b324707b2610 Mon Sep 17 00:00:00 2001 From: jwatson Date: Fri, 19 Jun 2020 13:26:43 -0700 Subject: [PATCH 2/2] fix broken javadoc --- .../main/java/io/opentelemetry/correlationcontext/Entry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java b/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java index 7618e3b8db2..3bdf0329d32 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/Entry.java @@ -38,7 +38,7 @@ public abstract class Entry { public static final int MAX_KEY_LENGTH = 255; /** - * The maximum length for a entry value. The value is {@value #MAX_LENGTH}. + * The maximum length for a entry value. The value is {@value #MAX_VALUE_LENGTH}. * * @since 0.1.0 */