Move GraphQL observation support into spring-boot-graphql
This commit is contained in:
committed by
Phillip Webb
parent
a5d2272ae0
commit
f7cc6473b2
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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.boot.graphql.autoconfigure.observation;
|
||||
|
||||
import graphql.GraphQL;
|
||||
import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.graphql.observation.DataFetcherObservationConvention;
|
||||
import org.springframework.graphql.observation.DataLoaderObservationConvention;
|
||||
import org.springframework.graphql.observation.ExecutionRequestObservationConvention;
|
||||
import org.springframework.graphql.observation.GraphQlObservationInstrumentation;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for instrumentation of Spring
|
||||
* GraphQL endpoints.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@AutoConfiguration(
|
||||
afterName = "org.springframework.boot.micrometer.observation.autoconfigure.ObservationAutoConfiguration")
|
||||
@ConditionalOnBean(ObservationRegistry.class)
|
||||
@ConditionalOnClass({ GraphQL.class, GraphQlSource.class, Observation.class })
|
||||
public class GraphQlObservationAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
GraphQlObservationInstrumentation graphQlObservationInstrumentation(ObservationRegistry observationRegistry,
|
||||
ObjectProvider<ExecutionRequestObservationConvention> executionConvention,
|
||||
ObjectProvider<DataFetcherObservationConvention> dataFetcherConvention,
|
||||
ObjectProvider<DataLoaderObservationConvention> dataLoaderObservationConvention) {
|
||||
return new GraphQlObservationInstrumentation(observationRegistry, executionConvention.getIfAvailable(),
|
||||
dataFetcherConvention.getIfAvailable(), dataLoaderObservationConvention.getIfAvailable());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Auto-configuration for Spring GraphQL observations.
|
||||
*/
|
||||
package org.springframework.boot.graphql.autoconfigure.observation;
|
||||
@@ -3,6 +3,7 @@ org.springframework.boot.graphql.autoconfigure.data.GraphQlQueryByExampleAutoCon
|
||||
org.springframework.boot.graphql.autoconfigure.data.GraphQlQuerydslAutoConfiguration
|
||||
org.springframework.boot.graphql.autoconfigure.data.GraphQlReactiveQueryByExampleAutoConfiguration
|
||||
org.springframework.boot.graphql.autoconfigure.data.GraphQlReactiveQuerydslAutoConfiguration
|
||||
org.springframework.boot.graphql.autoconfigure.observation.GraphQlObservationAutoConfiguration
|
||||
org.springframework.boot.graphql.autoconfigure.reactive.GraphQlWebFluxAutoConfiguration
|
||||
org.springframework.boot.graphql.autoconfigure.rsocket.GraphQlRSocketAutoConfiguration
|
||||
org.springframework.boot.graphql.autoconfigure.rsocket.RSocketGraphQlClientAutoConfiguration
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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.boot.graphql.autoconfigure.observation;
|
||||
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import io.micrometer.observation.tck.TestObservationRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.graphql.observation.DefaultDataFetcherObservationConvention;
|
||||
import org.springframework.graphql.observation.DefaultDataLoaderObservationConvention;
|
||||
import org.springframework.graphql.observation.DefaultExecutionRequestObservationConvention;
|
||||
import org.springframework.graphql.observation.GraphQlObservationInstrumentation;
|
||||
import org.springframework.graphql.server.WebGraphQlHandler;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link GraphQlObservationAutoConfiguration}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
class GraphQlObservationAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withBean(TestObservationRegistry.class, TestObservationRegistry::create)
|
||||
.withConfiguration(AutoConfigurations.of(GraphQlObservationAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void backsOffWhenObservationRegistryIsMissing() {
|
||||
new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(GraphQlObservationAutoConfiguration.class))
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(GraphQlObservationInstrumentation.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void definesInstrumentationWhenObservationRegistryIsPresent() {
|
||||
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(GraphQlObservationInstrumentation.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void instrumentationBacksOffIfAlreadyPresent() {
|
||||
this.contextRunner.withUserConfiguration(InstrumentationConfiguration.class)
|
||||
.run((context) -> assertThat(context).hasSingleBean(GraphQlObservationInstrumentation.class)
|
||||
.hasBean("customInstrumentation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void instrumentationUsesCustomConventionsIfAvailable() {
|
||||
this.contextRunner.withUserConfiguration(CustomConventionsConfiguration.class).run((context) -> {
|
||||
GraphQlObservationInstrumentation instrumentation = context
|
||||
.getBean(GraphQlObservationInstrumentation.class);
|
||||
assertThat(instrumentation).extracting("requestObservationConvention")
|
||||
.isInstanceOf(CustomExecutionRequestObservationConvention.class);
|
||||
assertThat(instrumentation).extracting("dataFetcherObservationConvention")
|
||||
.isInstanceOf(CustomDataFetcherObservationConvention.class);
|
||||
assertThat(instrumentation).extracting("dataLoaderObservationConvention")
|
||||
.isInstanceOf(CustomDataLoaderObservationConvention.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class InstrumentationConfiguration {
|
||||
|
||||
@Bean
|
||||
GraphQlObservationInstrumentation customInstrumentation(ObservationRegistry registry) {
|
||||
return new GraphQlObservationInstrumentation(registry);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class CustomConventionsConfiguration {
|
||||
|
||||
@Bean
|
||||
CustomExecutionRequestObservationConvention customExecutionConvention() {
|
||||
return new CustomExecutionRequestObservationConvention();
|
||||
}
|
||||
|
||||
@Bean
|
||||
CustomDataFetcherObservationConvention customDataFetcherConvention() {
|
||||
return new CustomDataFetcherObservationConvention();
|
||||
}
|
||||
|
||||
@Bean
|
||||
CustomDataLoaderObservationConvention customDataLoaderConvention() {
|
||||
return new CustomDataLoaderObservationConvention();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class CustomExecutionRequestObservationConvention extends DefaultExecutionRequestObservationConvention {
|
||||
|
||||
}
|
||||
|
||||
static class CustomDataFetcherObservationConvention extends DefaultDataFetcherObservationConvention {
|
||||
|
||||
}
|
||||
|
||||
static class CustomDataLoaderObservationConvention extends DefaultDataLoaderObservationConvention {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class WebGraphQlConfiguration {
|
||||
|
||||
@Bean
|
||||
WebGraphQlHandler webGraphQlHandler() {
|
||||
return mock(WebGraphQlHandler.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user