Skip to content

Commit

Permalink
Use default values for missing values
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Feb 22, 2025
1 parent 464135d commit 49385e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.config.server.environment;

import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
Expand All @@ -35,21 +36,20 @@ class ObservationEnvironmentRepositoryObservationConvention
@Override
public KeyValues getLowCardinalityKeyValues(ObservationEnvironmentRepositoryContext context) {
KeyValues keyValues = KeyValues.empty();
keyValues = appendIfPresent(keyValues, DocumentedConfigObservation.LowCardinalityTags.ENVIRONMENT_CLASS,
keyValues = appendWithValueOrUseDefault(keyValues,
DocumentedConfigObservation.LowCardinalityTags.ENVIRONMENT_CLASS,
context.getEnvironmentRepositoryClass().getName());
keyValues = appendIfPresent(keyValues, DocumentedConfigObservation.LowCardinalityTags.LABEL,
keyValues = appendWithValueOrUseDefault(keyValues, DocumentedConfigObservation.LowCardinalityTags.LABEL,
context.getLabel());
keyValues = appendIfPresent(keyValues, DocumentedConfigObservation.LowCardinalityTags.PROFILE,
keyValues = appendWithValueOrUseDefault(keyValues, DocumentedConfigObservation.LowCardinalityTags.PROFILE,
context.getProfile());
return appendIfPresent(keyValues, DocumentedConfigObservation.LowCardinalityTags.APPLICATION,
return appendWithValueOrUseDefault(keyValues, DocumentedConfigObservation.LowCardinalityTags.APPLICATION,
context.getApplication());
}

private KeyValues appendIfPresent(KeyValues keyValues, KeyName profile, String value) {
if (StringUtils.hasText(value)) {
keyValues = keyValues.and(profile.withValue(value));
}
return keyValues;
private KeyValues appendWithValueOrUseDefault(KeyValues keyValues, KeyName keyName, String value) {
value = StringUtils.hasText(value) ? value : KeyValue.NONE_VALUE;
return keyValues.and(keyName.withValue(value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Arrays;

import io.micrometer.common.KeyValue;
import io.micrometer.observation.Observation;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -70,6 +71,50 @@ && contextExists(context, "spring.cloud.config.environment.class",
});
}

@Test
void shouldCreateRootObservationForCompositeWithDefaultValues() {
TestObservationRegistry registry = TestObservationRegistry.create();
EnvironmentRepository delegate = new MyEnvRepo();
EnvironmentRepository composite = new CompositeEnvironmentRepository(Arrays.asList(delegate), registry, true);
EnvironmentRepository wrapper = ObservationEnvironmentRepositoryWrapper.wrap(registry, composite);

wrapper.findOne("foo", "bar", null);

assertThat(registry).hasHandledContextsThatSatisfy(contexts -> {
contexts.stream()
.filter(context -> context.getName().equals("spring.cloud.config.environment.find")
&& contextExists(context, "spring.cloud.config.environment.class",
"org.springframework.cloud.config.server.environment.CompositeEnvironmentRepository")
&& context.getLowCardinalityKeyValue("spring.cloud.config.environment.label")
.getValue()
.equals(KeyValue.NONE_VALUE)
&& context.getLowCardinalityKeyValue("spring.cloud.config.environment.application")
.getValue()
.equals("foo")
&& context.getLowCardinalityKeyValue("spring.cloud.config.environment.profile")
.getValue()
.equals("bar"))
.findFirst()
.orElseThrow(
() -> new AssertionError("There's no observation for the Composite EnvironmentRepository"));
contexts.stream()
.filter(context -> context.getName().equals("spring.cloud.config.environment.find") && contextExists(
context, "spring.cloud.config.environment.class",
"org.springframework.cloud.config.server.environment.ObservationEnvironmentRepositoryWrapperTests$MyEnvRepo")
&& context.getLowCardinalityKeyValue("spring.cloud.config.environment.label")
.getValue()
.equals(KeyValue.NONE_VALUE)
&& context.getLowCardinalityKeyValue("spring.cloud.config.environment.application")
.getValue()
.equals("foo")
&& context.getLowCardinalityKeyValue("spring.cloud.config.environment.profile")
.getValue()
.equals("bar"))
.findFirst()
.orElseThrow(() -> new AssertionError("There's no observation for the wrapped EnvironmentRepository"));
});
}

private boolean contextExists(Observation.Context context, String tagName, String tagValue) {
return context.getLowCardinalityKeyValues()
.stream()
Expand Down

0 comments on commit 49385e7

Please # to comment.