diff --git a/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java b/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java index b02b25e773..8bb747f328 100644 --- a/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java +++ b/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java @@ -20,6 +20,7 @@ import io.micrometer.common.lang.NonNull; import io.micrometer.common.lang.Nullable; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -941,6 +942,30 @@ public Context addHighCardinalityKeyValue(KeyValue keyValue) { return this; } + /** + * Removes a low cardinality key value by looking at its key - those will be + * removed to those fetched from the + * {@link ObservationConvention#getLowCardinalityKeyValues(Context)} method. + * @param keyName name of the key + * @return this context + */ + public Context removeLowCardinalityKeyValue(String keyName) { + this.lowCardinalityKeyValues.remove(keyName); + return this; + } + + /** + * Removes a high cardinality key value by looking at its key - those will be + * removed to those fetched from the + * {@link ObservationConvention#getHighCardinalityKeyValues(Context)} method. + * @param keyName name of the key + * @return this context + */ + public Context removeHighCardinalityKeyValue(String keyName) { + this.highCardinalityKeyValues.remove(keyName); + return this; + } + /** * Adds multiple low cardinality key values at once. * @param keyValues collection of key values @@ -961,6 +986,26 @@ public Context addHighCardinalityKeyValues(KeyValues keyValues) { return this; } + /** + * Removes multiple low cardinality key values at once. + * @param keyNames collection of key names + * @return this context + */ + public Context removeLowCardinalityKeyValues(String... keyNames) { + Arrays.stream(keyNames).forEach(this::removeLowCardinalityKeyValue); + return this; + } + + /** + * Removes multiple high cardinality key values at once. + * @param keyNames collection of key names + * @return this context + */ + public Context removeHighCardinalityKeyValues(String... keyNames) { + Arrays.stream(keyNames).forEach(this::removeHighCardinalityKeyValue); + return this; + } + @NonNull @Override public KeyValues getLowCardinalityKeyValues() { diff --git a/micrometer-observation/src/test/java/io/micrometer/observation/ObservationContextTest.java b/micrometer-observation/src/test/java/io/micrometer/observation/ObservationContextTest.java index 51c7f459e3..58a38875ac 100644 --- a/micrometer-observation/src/test/java/io/micrometer/observation/ObservationContextTest.java +++ b/micrometer-observation/src/test/java/io/micrometer/observation/ObservationContextTest.java @@ -16,6 +16,7 @@ package io.micrometer.observation; import io.micrometer.common.KeyValue; +import io.micrometer.common.KeyValues; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -131,4 +132,26 @@ void sameKeyShouldOverrideKeyValue() { assertThat(context.getHighCardinalityKeyValues()).containsExactly(newHigh); } + @Test + void removingLowCardinalityKeysShouldBePossible() { + context.addLowCardinalityKeyValues(KeyValues.of(KeyValue.of("key", "VALUE"), KeyValue.of("key2", "VALUE2"), + KeyValue.of("key3", "VALUE3"), KeyValue.of("key4", "VALUE4"))); + + context.removeLowCardinalityKeyValue("key"); + context.removeLowCardinalityKeyValues("key3", "key4"); + + assertThat(context.getLowCardinalityKeyValues()).containsExactly(KeyValue.of("key2", "VALUE2")); + } + + @Test + void removingHighCardinalityKeysShouldBePossible() { + context.addHighCardinalityKeyValues(KeyValues.of(KeyValue.of("key", "VALUE"), KeyValue.of("key2", "VALUE2"), + KeyValue.of("key3", "VALUE3"), KeyValue.of("key4", "VALUE4"))); + + context.removeHighCardinalityKeyValue("key"); + context.removeHighCardinalityKeyValues("key3", "key4"); + + assertThat(context.getHighCardinalityKeyValues()).containsExactly(KeyValue.of("key2", "VALUE2")); + } + }