GH-456 Fix Azure logging
Added static delegate context which holds target context and is also refreshed with new context on each request Resolves #456
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.cloud.function.adapter.azure;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
import com.microsoft.azure.functions.OutputBinding;
|
||||
@@ -40,6 +41,8 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
|
||||
|
||||
private String functionName;
|
||||
|
||||
private final static ExecutionContextDelegate EXECUTION_CTX_DELEGATE = new ExecutionContextDelegate();
|
||||
|
||||
public AzureSpringBootRequestHandler(Class<?> configurationClass) {
|
||||
super(configurationClass);
|
||||
}
|
||||
@@ -60,6 +63,7 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public O handleRequest(I input, ExecutionContext context) {
|
||||
EXECUTION_CTX_DELEGATE.targetContext = context;
|
||||
String name = "";
|
||||
try {
|
||||
if (context != null) {
|
||||
@@ -73,7 +77,7 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
|
||||
* see https://github.com/spring-cloud/spring-cloud-function/issues/425
|
||||
*/
|
||||
if (thisInitializer == null || !thisInitializer.functionName.equals(name)) {
|
||||
initialize(context);
|
||||
initialize(EXECUTION_CTX_DELEGATE);
|
||||
this.functionName = name;
|
||||
thisInitializer = this;
|
||||
return (O) thisInitializer.handleRequest(input, context);
|
||||
@@ -118,10 +122,6 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
|
||||
return Flux.just(input);
|
||||
}
|
||||
|
||||
// @SuppressWarnings("unchecked")
|
||||
// protected O convertOutput(I input, Object output) {
|
||||
// return (O) output;
|
||||
// }
|
||||
|
||||
protected boolean isSingleInput(Function<?, ?> function, Object input) {
|
||||
if (!(input instanceof Collection)) {
|
||||
@@ -145,4 +145,31 @@ public class AzureSpringBootRequestHandler<I, O> extends AbstractSpringFunctionA
|
||||
return ((Collection<?>) output).size() <= 1;
|
||||
}
|
||||
|
||||
private static class ExecutionContextDelegate implements ExecutionContext {
|
||||
|
||||
ExecutionContext targetContext;
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
if (targetContext == null || targetContext.getLogger() == null) {
|
||||
return Logger.getAnonymousLogger();
|
||||
}
|
||||
return targetContext.getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInvocationId() {
|
||||
return targetContext.getInvocationId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFunctionName() {
|
||||
return targetContext.getFunctionName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ExecutionContextDelegate over: " + this.targetContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,13 +232,20 @@ public class AzureSpringBootRequestHandlerTests {
|
||||
protected static class MultiConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Foo, Bar> uppercase() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
public Function<Foo, Bar> uppercase(ExecutionContext context) {
|
||||
|
||||
return foo -> {
|
||||
context.getLogger().info("Executing uppercase function");
|
||||
return new Bar(foo.getValue().toUpperCase());
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Bar, Foo> lowercase() {
|
||||
return bar -> new Foo(bar.getValue().toLowerCase());
|
||||
public Function<Bar, Foo> lowercase(ExecutionContext context) {
|
||||
return bar -> {
|
||||
context.getLogger().info("Executing lowercase function");
|
||||
return new Foo(bar.getValue().toLowerCase());
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-dependencies</artifactId>
|
||||
<version>3.0.1.BUILD-SNAPSHOT</version>
|
||||
<version>3.0.4.BUILD-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -22,6 +22,8 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Config {
|
||||
|
||||
@@ -30,58 +32,12 @@ public class Config {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Foo, Bar> uppercase() {
|
||||
return foo -> new Bar(foo.getValue().toUpperCase());
|
||||
public Function<String, String> uppercase(ExecutionContext context) {
|
||||
return value -> {
|
||||
context.getLogger().info("Uppercasing " + value);
|
||||
return value.toUpperCase();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Foo {
|
||||
|
||||
private String value;
|
||||
|
||||
Foo() {
|
||||
}
|
||||
|
||||
Foo(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String lowercase() {
|
||||
return this.value.toLowerCase();
|
||||
}
|
||||
|
||||
public String uppercase() {
|
||||
return this.value.toUpperCase();
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Bar {
|
||||
|
||||
private String value;
|
||||
|
||||
Bar() {
|
||||
}
|
||||
|
||||
Bar(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 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.
|
||||
@@ -30,11 +30,11 @@ import org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHa
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
public class FooHandler extends AzureSpringBootRequestHandler<Foo, Bar> {
|
||||
public class UppercaseHandler extends AzureSpringBootRequestHandler<String, String> {
|
||||
|
||||
@FunctionName("uppercase")
|
||||
public Bar execute(@HttpTrigger(name = "req", methods = {HttpMethod.GET,
|
||||
HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<Foo>> request,
|
||||
public String execute(@HttpTrigger(name = "req", methods = {HttpMethod.GET,
|
||||
HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
|
||||
ExecutionContext context) {
|
||||
return handleRequest(request.getBody().get(), context);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 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.
|
||||
@@ -28,19 +28,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class MapTests {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Bar result = new Config().uppercase().apply(new Foo("foo"));
|
||||
assertThat(result.getValue()).isEqualTo("FOO");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void start() throws Exception {
|
||||
AzureSpringBootRequestHandler<Foo, Bar> handler = new AzureSpringBootRequestHandler<>(
|
||||
AzureSpringBootRequestHandler<String, String> handler = new AzureSpringBootRequestHandler<>(
|
||||
Config.class);
|
||||
Bar result = handler.handleRequest(new Foo("foo"), null);
|
||||
String result = handler.handleRequest("foo", null);
|
||||
handler.close();
|
||||
assertThat(result.getValue()).isEqualTo("FOO");
|
||||
assertThat(result).isEqualTo("FOO");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user