Skip to content

Commit

Permalink
Adds an option to allow removal of low & high cardinality key values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Nov 8, 2022
1 parent c919224 commit 8a7c662
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

}

0 comments on commit 8a7c662

Please sign in to comment.