Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an option to allow removal of low & high cardinality key values #3529

Merged
merged 1 commit into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"));
}

}