Merge branch '4.2.x'

This commit is contained in:
Ryan Baxter
2025-02-25 08:43:09 -06:00
2 changed files with 54 additions and 9 deletions

View File

@@ -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;
@@ -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

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.config.server.environment;
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;
@@ -70,6 +71,50 @@ class ObservationEnvironmentRepositoryWrapperTests {
});
}
@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()