diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/method/InvocableHandlerMethodSupport.java b/spring-graphql/src/main/java/org/springframework/graphql/data/method/InvocableHandlerMethodSupport.java index 65d51b0f..24764560 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/method/InvocableHandlerMethodSupport.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/method/InvocableHandlerMethodSupport.java @@ -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( diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java index 1037803b..644e38a2 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java @@ -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 { 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 { } @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) { diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolverAdapter.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolverAdapter.java index 5f73637d..24e5c58e 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolverAdapter.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolverAdapter.java @@ -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 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(); } diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java index 1373fc1c..891c7edc 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java @@ -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> loaders = new ArrayList<>(); private final List> mappedLoaders = new ArrayList<>(); @@ -62,7 +59,6 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry { private final Supplier defaultOptionsSupplier; - /** * Default constructor */ @@ -232,9 +228,10 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry { } @Override + @SuppressWarnings("deprecation") public CompletionStage> load(List 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> load(Set 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) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultExecutionGraphQlService.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultExecutionGraphQlService.java index 3434c8f8..4a206bb0 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultExecutionGraphQlService.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultExecutionGraphQlService.java @@ -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 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 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)); diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/ExceptionResolversExceptionHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/ExceptionResolversExceptionHandler.java index 6782ee5e..8cbc88c9 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/ExceptionResolversExceptionHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/ExceptionResolversExceptionHandler.java @@ -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 resolvers; /** @@ -63,10 +60,11 @@ class ExceptionResolversExceptionHandler implements DataFetcherExceptionHandler @Override + @SuppressWarnings("deprecation") public CompletableFuture 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)) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/SecurityContextThreadLocalAccessor.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/SecurityContextThreadLocalAccessor.java index 34022e13..ed2ca52f 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/SecurityContextThreadLocalAccessor.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/SecurityContextThreadLocalAccessor.java @@ -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 void restoreInternal(Object previousValue) { - ((ThreadLocalAccessor) this.delegate).restore((V) previousValue); - } - - @Override - public void restore() { - this.delegate.restore(); - } - + + @SuppressWarnings("deprecation") private static class DelegateAccessor implements ThreadLocalAccessor { @Override @@ -115,22 +97,6 @@ public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor { @Override @@ -155,19 +122,6 @@ public class SecurityContextThreadLocalAccessor implements ThreadLocalAccessor> resolveException(Throwable exception) { if (this.threadLocalContextAware) { return Mono.deferContextual(contextView -> { - ContextSnapshot snapshot = snapshotFactory.captureFrom(contextView); + ContextSnapshot snapshot = ContextSnapshot.captureFrom(contextView); try { List errors = snapshot.wrap(() -> resolveToMultipleErrors(exception)).call(); return Mono.justOrEmpty(errors); diff --git a/spring-graphql/src/main/java/org/springframework/graphql/server/DefaultWebGraphQlHandlerBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/server/DefaultWebGraphQlHandlerBuilder.java index 33d93b3c..f38e6954 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/server/DefaultWebGraphQlHandlerBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/server/DefaultWebGraphQlHandlerBuilder.java @@ -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 handleRequest(WebGraphQlRequest request) { - ContextSnapshot snapshot = snapshotFactory.captureAll(); + ContextSnapshot snapshot = ContextSnapshot.captureAll(); return executionChain.next(request).contextWrite(snapshot::updateContext); } }; diff --git a/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlWebSocketHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlWebSocketHandler.java index f481205d..d02ab52d 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlWebSocketHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlWebSocketHandler.java @@ -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 attributes) { - attributes.put(KEY, SNAPSHOT_FACTORY.captureAll()); + attributes.put(KEY, ContextSnapshot.captureAll()); return true; }