GH-766 Initial support to expose FunctionCatalog as actuator endpoint

This commit is contained in:
Oleg Zhurakousky
2021-11-30 18:53:58 +01:00
parent 56092df2c5
commit 24847cfffa
7 changed files with 148 additions and 12 deletions

View File

@@ -440,7 +440,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
public boolean isPrototype() {
return this.isPrototype();
return !this.isSingleton;
}
public void setSkipInputConversion(boolean skipInputConversion) {

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2021-2021 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.context.config;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.endpoint.FunctionsEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Oleg Zhurakousky
* @since 3.2
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = {
"org.springframework.boot.actuate.endpoint.annotation.Endpoint" })
@ConditionalOnBean(FunctionCatalog.class)
@AutoConfigureAfter(EndpointAutoConfiguration.class)
public class FunctionsEndpointAutoConfiguration {
@Bean
@ConditionalOnAvailableEndpoint
public FunctionsEndpoint bindingsEndpoint(FunctionCatalog functionCatalog) {
return new FunctionsEndpoint(functionCatalog);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2021-2021 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.endpoint;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.json.JsonMapper;
/**
*
* Actuator endpoint to access {@link FunctionCatalog}
*
* @author Oleg Zhurakousky
* @since 3.2
*/
@Endpoint(id = "functions")
public class FunctionsEndpoint {
private final FunctionCatalog functionCatalog;
public FunctionsEndpoint(FunctionCatalog functionCatalog) {
this.functionCatalog = functionCatalog;
}
@ReadOperation
public Map<String, Map<String, Object>> listAll() {
Map<String, Map<String, Object>> allFunctions = new TreeMap<>();
Set<String> names = functionCatalog.getNames(null);
for (String name : names) {
FunctionInvocationWrapper function = functionCatalog.lookup(name);
Map<String, Object> functionMap = new LinkedHashMap<>();
if (function.isFunction()) {
functionMap.put("type", "FUNCTION");
functionMap.put("input-type", function.getInputType().toString());
functionMap.put("output-type", function.getOutputType().toString());
}
else if (function.isConsumer()) {
functionMap.put("type", "CONSUMER");
functionMap.put("input-type", function.getInputType().toString());
}
else {
functionMap.put("type", "SUPPLIER");
functionMap.put("output-type", function.getOutputType().toString());
}
allFunctions.put(name, functionMap);
}
return allFunctions;
}
}

View File

@@ -1,7 +1,8 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration,\
org.springframework.cloud.function.cloudevent.CloudEventsFunctionExtensionConfiguration,\
org.springframework.cloud.function.context.config.KotlinLambdaToFunctionAutoConfiguration
org.springframework.cloud.function.context.config.KotlinLambdaToFunctionAutoConfiguration,\
org.springframework.cloud.function.context.config.FunctionsEndpointAutoConfiguration
org.springframework.cloud.function.context.WrapperDetector=\
org.springframework.cloud.function.context.config.FluxWrapperDetector
org.springframework.context.ApplicationContextInitializer=\