GH-865 Added initial test for Observability module

Resolves #865
This commit is contained in:
Oleg Zhurakousky
2022-05-10 15:23:12 +02:00
parent bc7cbf8ace
commit 3aa3c4a2c3
6 changed files with 128 additions and 3 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2022-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.observability;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
* @author Oleg Zhurakousky
*
*/
@Configuration(proxyBeanMethods = false)
public class ObservationAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ObservationFunctionAroundWrapper wrapper(ObservationRegistry registry) {
return new ObservationFunctionAroundWrapper(registry);
}
}

View File

@@ -30,6 +30,7 @@ import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry
/**
* @author Marcin Grzejszczak
* @author Oleg Zhurakousky
* @since 4.0.0
*/
public class ObservationFunctionAroundWrapper extends FunctionAroundWrapper implements Observation.KeyValuesProviderAware<FunctionTagsProvider> {
@@ -70,7 +71,7 @@ public class ObservationFunctionAroundWrapper extends FunctionAroundWrapper impl
.keyValuesProvider(tagsProvider)
.observe(() -> {
Object r = message == null ? targetFunction.get() : targetFunction.apply(invocationMessage);
context.setOutput(r);
context.setModifiedOutput(r);
return r;
});
if (result == null) {

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.observability.ObservationAutoConfiguration

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2022-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.observability;
import java.util.function.Function;
import io.micrometer.core.tck.TestObservationRegistry;
import io.micrometer.core.tck.TestObservationRegistryAssert;
import io.micrometer.observation.ObservationRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
/**
*
* @author Oleg Zhurakousky
*
*/
public class ObservationFunctionAroundWrapperTests {
@Test
public void testSingleObservation() {
try (ConfigurableApplicationContext context = SpringApplication.run(SampleConfiguration.class, "")) {
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
Function<Message<String>, Message<byte[]>> uppercase = catalog.lookup("uppercase", "application/json");
Message<byte[]> result = uppercase.apply(MessageBuilder.withPayload("\"marcin\"").build());
System.out.println("Result: " + result);
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.then(registry).hasSingleObservationThat()
.hasNameEqualTo("spring.cloud.function");
}
}
@Configuration
@EnableAutoConfiguration
public static class SampleConfiguration {
@Bean
public ObservationRegistry testRegistry() {
return TestObservationRegistry.create();
}
// @Bean
// public ObservationFunctionAroundWrapper wrapper(ObservationRegistry registry) {
// return new ObservationFunctionAroundWrapper(registry);
// }
@Bean
public Function<String, String> uppercase() {
return v -> v.toUpperCase();
}
}
}