-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NullPointerException in Tracing support
Prior to this commit, using the tracing support could result in an NullPointerException since the instrumentation expected the tracing propagation information to be always available from the request extensions. This commit improves the strategy for extracing the inbound propagation information. First, the instrumentation now looks up the propagation information in the GraphQL context first, then in the request extensions as a fallback. We ensure that no NPE can be raised if the information is missing. Some clients might change the propagation information in the request extensions, but in the HTTP world, most clients will send those as HTTP headers. We are adding a new `PropagationWebGraphQlInterceptor` that can be configured on the application to copy the relevant HTTP headers from the HTTP request to the GraphQL context. For other transports, we currently advise the request extensions - both WebSocket and RSocket multiplex over the same transport message, making this approach impossible. Fixes gh-547
- Loading branch information
Showing
7 changed files
with
336 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...c/main/java/org/springframework/graphql/observation/PropagationWebGraphQlInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2020-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.graphql.observation; | ||
|
||
import io.micrometer.tracing.propagation.Propagator; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.graphql.server.WebGraphQlInterceptor; | ||
import org.springframework.graphql.server.WebGraphQlRequest; | ||
import org.springframework.graphql.server.WebGraphQlResponse; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* {@link WebGraphQlInterceptor} that copies {@link Propagator propagation} headers | ||
* from the HTTP request to the {@link graphql.GraphQLContext}. | ||
* This makes it possible to propagate tracing information sent by HTTP clients. | ||
* | ||
* @author Brian Clozel | ||
* @since 1.1.1 | ||
*/ | ||
public class PropagationWebGraphQlInterceptor implements WebGraphQlInterceptor { | ||
|
||
private final Propagator propagator; | ||
|
||
/** | ||
* Create an interceptor that leverages the field names used by the given | ||
* {@link Propagator} instance. | ||
* | ||
* @param propagator the propagator that will be used for tracing support | ||
*/ | ||
public PropagationWebGraphQlInterceptor(Propagator propagator) { | ||
Assert.notNull(propagator, "propagator should not be null"); | ||
this.propagator = propagator; | ||
} | ||
|
||
@Override | ||
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) { | ||
request.configureExecutionInput((input, inputBuilder) -> { | ||
HttpHeaders headers = request.getHeaders(); | ||
for (String field : this.propagator.fields()) { | ||
if (headers.containsKey(field)) { | ||
inputBuilder.graphQLContext(contextBuilder -> contextBuilder.of(field, headers.getFirst(field))); | ||
} | ||
} | ||
return inputBuilder.build(); | ||
}); | ||
return chain.next(request); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...java/org/springframework/graphql/observation/ExecutionRequestObservationContextTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2020-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.graphql.observation; | ||
|
||
|
||
import java.util.Map; | ||
|
||
import graphql.ExecutionInput; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ExecutionRequestObservationContext}. | ||
* | ||
* @author Brian Clozel | ||
*/ | ||
class ExecutionRequestObservationContextTests { | ||
|
||
@Test | ||
void readPropagationFieldFromGraphQlContext() { | ||
ExecutionInput executionInput = ExecutionInput | ||
.newExecutionInput("{ notUsed }") | ||
.graphQLContext(builder -> builder.of("X-Tracing-Test", "traceId")) | ||
.build(); | ||
ExecutionRequestObservationContext context = new ExecutionRequestObservationContext(executionInput); | ||
assertThat(context.getGetter().get(executionInput, "X-Tracing-Test")).isEqualTo("traceId"); | ||
} | ||
|
||
@Test | ||
void readPropagationFieldFromExtensions() { | ||
ExecutionInput executionInput = ExecutionInput | ||
.newExecutionInput("{ notUsed }") | ||
.extensions(Map.of("X-Tracing-Test", "traceId")) | ||
.build(); | ||
ExecutionRequestObservationContext context = new ExecutionRequestObservationContext(executionInput); | ||
assertThat(context.getGetter().get(executionInput, "X-Tracing-Test")).isEqualTo("traceId"); | ||
} | ||
|
||
@Test | ||
void doesNotFailIsMissingPropagationField() { | ||
ExecutionInput executionInput = ExecutionInput | ||
.newExecutionInput("{ notUsed }") | ||
.build(); | ||
ExecutionRequestObservationContext context = new ExecutionRequestObservationContext(executionInput); | ||
assertThat(context.getGetter().get(executionInput, "X-Tracing-Test")).isNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.