Skip to content

Commit 990e817

Browse files
marcingrzejszczakttddyy
authored andcommitted
Adds an option to allow removal of low & high cardinality key values (#3529)
(cherry picked from commit 8a7c662)
1 parent 86582a1 commit 990e817

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

micrometer-observation/src/main/java/io/micrometer/observation/Observation.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.micrometer.common.lang.NonNull;
2121
import io.micrometer.common.lang.Nullable;
2222

23+
import java.util.Arrays;
2324
import java.util.HashMap;
2425
import java.util.LinkedHashMap;
2526
import java.util.Map;
@@ -941,6 +942,30 @@ public Context addHighCardinalityKeyValue(KeyValue keyValue) {
941942
return this;
942943
}
943944

945+
/**
946+
* Removes a low cardinality key value by looking at its key - those will be
947+
* removed to those fetched from the
948+
* {@link ObservationConvention#getLowCardinalityKeyValues(Context)} method.
949+
* @param keyName name of the key
950+
* @return this context
951+
*/
952+
public Context removeLowCardinalityKeyValue(String keyName) {
953+
this.lowCardinalityKeyValues.remove(keyName);
954+
return this;
955+
}
956+
957+
/**
958+
* Removes a high cardinality key value by looking at its key - those will be
959+
* removed to those fetched from the
960+
* {@link ObservationConvention#getHighCardinalityKeyValues(Context)} method.
961+
* @param keyName name of the key
962+
* @return this context
963+
*/
964+
public Context removeHighCardinalityKeyValue(String keyName) {
965+
this.highCardinalityKeyValues.remove(keyName);
966+
return this;
967+
}
968+
944969
/**
945970
* Adds multiple low cardinality key values at once.
946971
* @param keyValues collection of key values
@@ -961,6 +986,26 @@ public Context addHighCardinalityKeyValues(KeyValues keyValues) {
961986
return this;
962987
}
963988

989+
/**
990+
* Removes multiple low cardinality key values at once.
991+
* @param keyNames collection of key names
992+
* @return this context
993+
*/
994+
public Context removeLowCardinalityKeyValues(String... keyNames) {
995+
Arrays.stream(keyNames).forEach(this::removeLowCardinalityKeyValue);
996+
return this;
997+
}
998+
999+
/**
1000+
* Removes multiple high cardinality key values at once.
1001+
* @param keyNames collection of key names
1002+
* @return this context
1003+
*/
1004+
public Context removeHighCardinalityKeyValues(String... keyNames) {
1005+
Arrays.stream(keyNames).forEach(this::removeHighCardinalityKeyValue);
1006+
return this;
1007+
}
1008+
9641009
@NonNull
9651010
@Override
9661011
public KeyValues getLowCardinalityKeyValues() {

micrometer-observation/src/test/java/io/micrometer/observation/ObservationContextTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.micrometer.observation;
1717

1818
import io.micrometer.common.KeyValue;
19+
import io.micrometer.common.KeyValues;
1920
import org.assertj.core.api.Assertions;
2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
@@ -131,4 +132,26 @@ void sameKeyShouldOverrideKeyValue() {
131132
assertThat(context.getHighCardinalityKeyValues()).containsExactly(newHigh);
132133
}
133134

135+
@Test
136+
void removingLowCardinalityKeysShouldBePossible() {
137+
context.addLowCardinalityKeyValues(KeyValues.of(KeyValue.of("key", "VALUE"), KeyValue.of("key2", "VALUE2"),
138+
KeyValue.of("key3", "VALUE3"), KeyValue.of("key4", "VALUE4")));
139+
140+
context.removeLowCardinalityKeyValue("key");
141+
context.removeLowCardinalityKeyValues("key3", "key4");
142+
143+
assertThat(context.getLowCardinalityKeyValues()).containsExactly(KeyValue.of("key2", "VALUE2"));
144+
}
145+
146+
@Test
147+
void removingHighCardinalityKeysShouldBePossible() {
148+
context.addHighCardinalityKeyValues(KeyValues.of(KeyValue.of("key", "VALUE"), KeyValue.of("key2", "VALUE2"),
149+
KeyValue.of("key3", "VALUE3"), KeyValue.of("key4", "VALUE4")));
150+
151+
context.removeHighCardinalityKeyValue("key");
152+
context.removeHighCardinalityKeyValues("key3", "key4");
153+
154+
assertThat(context.getHighCardinalityKeyValues()).containsExactly(KeyValue.of("key2", "VALUE2"));
155+
}
156+
134157
}

0 commit comments

Comments
 (0)