Upgrade to Context Propagation 1.0.3

Closes gh-718
This commit is contained in:
Brian Clozel
2023-06-19 21:12:56 +02:00
parent 2d54d77ff6
commit 0182e72fde
10 changed files with 38 additions and 97 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -27,7 +27,6 @@ import java.util.stream.Stream;
import graphql.GraphQLContext;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import reactor.core.publisher.Mono;
import org.springframework.core.CoroutinesUtils;
@@ -46,8 +45,6 @@ public abstract class InvocableHandlerMethodSupport extends HandlerMethod {
private static final Object NO_VALUE = new Object();
private static final ContextSnapshotFactory SNAPSHOT_FACTORY = ContextSnapshotFactory.builder().build();
private final boolean hasCallableReturnValue;
@@ -112,12 +109,13 @@ public abstract class InvocableHandlerMethodSupport extends HandlerMethod {
}
@Nullable
@SuppressWarnings("deprecation")
private Object handleReturnValue(GraphQLContext graphQLContext, @Nullable Object result) {
if (this.hasCallableReturnValue && result != null) {
return CompletableFuture.supplyAsync(
() -> {
try {
return SNAPSHOT_FACTORY.captureFrom(graphQLContext).wrap((Callable<?>) result).call();
return ContextSnapshot.captureFrom(graphQLContext).wrap((Callable<?>) result).call();
}
catch (Exception ex) {
throw new IllegalStateException(

View File

@@ -33,7 +33,6 @@ import graphql.schema.GraphQLTypeVisitorStub;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -60,8 +59,6 @@ final class ContextDataFetcherDecorator implements DataFetcher<Object> {
private final SubscriptionExceptionResolver subscriptionExceptionResolver;
private final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
private ContextDataFetcherDecorator(
DataFetcher<?> delegate, boolean subscription,
SubscriptionExceptionResolver subscriptionExceptionResolver) {
@@ -74,15 +71,21 @@ final class ContextDataFetcherDecorator implements DataFetcher<Object> {
}
@Override
@SuppressWarnings("deprecation")
public Object get(DataFetchingEnvironment environment) throws Exception {
ContextSnapshot snapshot;
GraphQLContext context;
// temporarily merge global and local graphql context until https://github.com/micrometer-metrics/context-propagation/pull/98
if (environment.getLocalContext() instanceof GraphQLContext localContext) {
snapshot = snapshotFactory.captureFrom(environment.getGraphQlContext(), localContext);
context = GraphQLContext.newContext()
.of(environment.getGraphQlContext())
.of(localContext)
.build();
}
else {
snapshot = snapshotFactory.captureFrom(environment.getGraphQlContext());
context = environment.getGraphQlContext();
}
ContextSnapshot snapshot = ContextSnapshot.captureFrom(context);
Object value = snapshot.wrap(() -> this.delegate.get(environment)).call();
if (this.subscription) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -22,7 +22,6 @@ import java.util.function.BiFunction;
import graphql.GraphQLError;
import graphql.schema.DataFetchingEnvironment;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import io.micrometer.context.ThreadLocalAccessor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -53,8 +52,6 @@ public abstract class DataFetcherExceptionResolverAdapter implements DataFetcher
protected final Log logger = LogFactory.getLog(getClass());
protected final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
private boolean threadLocalContextAware;
@@ -96,12 +93,13 @@ public abstract class DataFetcherExceptionResolverAdapter implements DataFetcher
}
@Nullable
@SuppressWarnings("deprecation")
private List<GraphQLError> resolveInternal(Throwable exception, DataFetchingEnvironment env) {
if (!this.threadLocalContextAware) {
return resolveToMultipleErrors(exception, env);
}
try {
return snapshotFactory.captureFrom(env.getGraphQlContext())
return ContextSnapshot.captureFrom(env.getGraphQlContext())
.wrap(() -> resolveToMultipleErrors(exception, env))
.call();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -27,7 +27,6 @@ import java.util.function.Supplier;
import graphql.GraphQLContext;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.dataloader.BatchLoaderContextProvider;
import org.dataloader.BatchLoaderEnvironment;
import org.dataloader.BatchLoaderWithContext;
@@ -53,8 +52,6 @@ import org.springframework.util.StringUtils;
*/
public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
private static final ContextSnapshotFactory SNAPSHOT_FACTORY = ContextSnapshotFactory.builder().build();
private final List<ReactorBatchLoader<?,?>> loaders = new ArrayList<>();
private final List<ReactorMappedBatchLoader<?,?>> mappedLoaders = new ArrayList<>();
@@ -62,7 +59,6 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
private final Supplier<DataLoaderOptions> defaultOptionsSupplier;
/**
* Default constructor
*/
@@ -232,9 +228,10 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
}
@Override
@SuppressWarnings("deprecation")
public CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment environment) {
GraphQLContext graphQLContext = environment.getContext();
ContextSnapshot snapshot = SNAPSHOT_FACTORY.captureFrom(graphQLContext);
ContextSnapshot snapshot = ContextSnapshot.captureFrom(graphQLContext);
try {
return snapshot.wrap(() ->
this.loader.apply(keys, environment)
@@ -282,9 +279,10 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
}
@Override
@SuppressWarnings("deprecation")
public CompletionStage<Map<K, V>> load(Set<K> keys, BatchLoaderEnvironment environment) {
GraphQLContext graphQLContext = environment.getContext();
ContextSnapshot snapshot = SNAPSHOT_FACTORY.captureFrom(graphQLContext);
ContextSnapshot snapshot = ContextSnapshot.captureFrom(graphQLContext);
try {
return snapshot.wrap(() ->
this.loader.apply(keys, environment)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -25,7 +25,6 @@ import graphql.GraphQL;
import graphql.GraphQLContext;
import graphql.execution.ExecutionIdProvider;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.dataloader.DataLoaderRegistry;
import reactor.core.publisher.Mono;
@@ -46,7 +45,6 @@ public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {
private static final BiFunction<ExecutionInput, ExecutionInput.Builder, ExecutionInput> RESET_EXECUTION_ID_CONFIGURER =
(executionInput, builder) -> builder.executionId(null).build();
private final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
private final GraphQlSource graphQlSource;
@@ -73,13 +71,14 @@ public class DefaultExecutionGraphQlService implements ExecutionGraphQlService {
@Override
@SuppressWarnings("deprecation")
public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest request) {
return Mono.deferContextual((contextView) -> {
if (!this.isDefaultExecutionIdProvider && request.getExecutionId() == null) {
request.configureExecutionInput(RESET_EXECUTION_ID_CONFIGURER);
}
ExecutionInput executionInput = request.toExecutionInput();
snapshotFactory.captureFrom(contextView).updateContext(executionInput.getGraphQLContext());
ContextSnapshot.captureFrom(contextView).updateContext(executionInput.getGraphQLContext());
ExecutionInput updatedExecutionInput = registerDataLoaders(executionInput);
return Mono.fromFuture(this.graphQlSource.graphQl().executeAsync(updatedExecutionInput))
.map(result -> new DefaultExecutionGraphQlResponse(updatedExecutionInput, result));

View File

@@ -29,7 +29,6 @@ import graphql.execution.DataFetcherExceptionHandlerResult;
import graphql.execution.ExecutionId;
import graphql.schema.DataFetchingEnvironment;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;
@@ -48,8 +47,6 @@ class ExceptionResolversExceptionHandler implements DataFetcherExceptionHandler
private static final Log logger = LogFactory.getLog(ExceptionResolversExceptionHandler.class);
private final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
private final List<DataFetcherExceptionResolver> resolvers;
/**
@@ -63,10 +60,11 @@ class ExceptionResolversExceptionHandler implements DataFetcherExceptionHandler
@Override
@SuppressWarnings("deprecation")
public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(DataFetcherExceptionHandlerParameters params) {
Throwable exception = unwrapException(params);
DataFetchingEnvironment env = params.getDataFetchingEnvironment();
ContextSnapshot snapshot = snapshotFactory.captureFrom(env.getGraphQlContext());
ContextSnapshot snapshot = ContextSnapshot.captureFrom(env.getGraphQlContext());
try {
return Flux.fromIterable(this.resolvers)
.flatMap(resolver -> resolver.resolveException(exception, env))

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-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.
@@ -72,31 +72,13 @@ public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor<O
}
@Override
public void setValue() {
this.delegate.setValue();
}
@Override
@Deprecated
@SuppressWarnings("deprecation")
public void reset() {
this.delegate.reset();
}
@Override
public void restore(Object previousValue) {
restoreInternal(previousValue);
}
@SuppressWarnings("unchecked")
public <V> void restoreInternal(Object previousValue) {
((ThreadLocalAccessor<V>) this.delegate).restore((V) previousValue);
}
@Override
public void restore() {
this.delegate.restore();
}
@SuppressWarnings("deprecation")
private static class DelegateAccessor implements ThreadLocalAccessor<Object> {
@Override
@@ -115,22 +97,6 @@ public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor<O
}
@Override
public void setValue() {
SecurityContextHolder.clearContext();
}
@Override
public void restore(Object previousValue) {
SecurityContextHolder.setContext((SecurityContext) previousValue);
}
@Override
public void restore() {
SecurityContextHolder.clearContext();
}
@Override
@Deprecated
public void reset() {
SecurityContextHolder.clearContext();
}
@@ -138,6 +104,7 @@ public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor<O
}
@SuppressWarnings("deprecation")
private static class NoOpAccessor implements ThreadLocalAccessor<Object> {
@Override
@@ -155,19 +122,6 @@ public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor<O
}
@Override
public void setValue() {
}
@Override
public void restore(Object previousValue) {
}
@Override
public void restore() {
}
@Override
@Deprecated
public void reset() {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -22,7 +22,6 @@ import java.util.function.Function;
import graphql.GraphQLError;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import io.micrometer.context.ThreadLocalAccessor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -51,8 +50,6 @@ public abstract class SubscriptionExceptionResolverAdapter implements Subscripti
protected final Log logger = LogFactory.getLog(getClass());
protected final ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
private boolean threadLocalContextAware;
@@ -81,12 +78,12 @@ public abstract class SubscriptionExceptionResolverAdapter implements Subscripti
}
@SuppressWarnings({"unused", "try"})
@SuppressWarnings({"unused", "try", "deprecation"})
@Override
public final Mono<List<GraphQLError>> resolveException(Throwable exception) {
if (this.threadLocalContextAware) {
return Mono.deferContextual(contextView -> {
ContextSnapshot snapshot = snapshotFactory.captureFrom(contextView);
ContextSnapshot snapshot = ContextSnapshot.captureFrom(contextView);
try {
List<GraphQLError> errors = snapshot.wrap(() -> resolveToMultipleErrors(exception)).call();
return Mono.justOrEmpty(errors);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.List;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import reactor.core.publisher.Mono;
import org.springframework.graphql.ExecutionGraphQlService;
@@ -71,8 +70,6 @@ class DefaultWebGraphQlHandlerBuilder implements WebGraphQlHandler.Builder {
@Override
public WebGraphQlHandler build() {
ContextSnapshotFactory snapshotFactory = ContextSnapshotFactory.builder().build();
Chain endOfChain = request -> this.service.execute(request).map(WebGraphQlResponse::new);
Chain executionChain = this.interceptors.stream()
@@ -89,8 +86,9 @@ class DefaultWebGraphQlHandlerBuilder implements WebGraphQlHandler.Builder {
}
@Override
@SuppressWarnings("deprecation")
public Mono<WebGraphQlResponse> handleRequest(WebGraphQlRequest request) {
ContextSnapshot snapshot = snapshotFactory.captureAll();
ContextSnapshot snapshot = ContextSnapshot.captureAll();
return executionChain.next(request).contextWrite(snapshot::updateContext);
}
};

View File

@@ -36,7 +36,6 @@ import graphql.ExecutionResult;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
@@ -358,14 +357,13 @@ public class GraphQlWebSocketHandler extends TextWebSocketHandler implements Sub
private static final String KEY = ContextSnapshot.class.getName();
private static final ContextSnapshotFactory SNAPSHOT_FACTORY = ContextSnapshotFactory.builder().build();
@Override
@SuppressWarnings("deprecation")
public boolean beforeHandshake(
ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) {
attributes.put(KEY, SNAPSHOT_FACTORY.captureAll());
attributes.put(KEY, ContextSnapshot.captureAll());
return true;
}