Skip to content

Commit 9948fc3

Browse files
committed
Use GraphQL Observation instrumentation
This commit migrates the existing metrics support (added in #29140) to the new `Observation` instrumentation contributed in spring-projects/spring-graphql#501. We cannot have a smoother migration path here as the instrumentation does not use the same context information for metadata extraction. Closes gh-32794
1 parent 09f3d45 commit 9948fc3

File tree

16 files changed

+153
-918
lines changed

16 files changed

+153
-918
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java

-20
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public class MetricsProperties {
6363

6464
private final Data data = new Data();
6565

66-
private final Graphql graphql = new Graphql();
67-
6866
private final System system = new System();
6967

7068
private final Distribution distribution = new Distribution();
@@ -93,10 +91,6 @@ public Data getData() {
9391
return this.data;
9492
}
9593

96-
public Graphql getGraphql() {
97-
return this.graphql;
98-
}
99-
10094
public System getSystem() {
10195
return this.system;
10296
}
@@ -249,20 +243,6 @@ public AutoTimeProperties getAutotime() {
249243

250244
}
251245

252-
public static class Graphql {
253-
254-
/**
255-
* Auto-timed queries settings.
256-
*/
257-
@NestedConfigurationProperty
258-
private final AutoTimeProperties autotime = new AutoTimeProperties();
259-
260-
public AutoTimeProperties getAutotime() {
261-
return this.autotime;
262-
}
263-
264-
}
265-
266246
public static class System {
267247

268248
private final Diskspace diskspace = new Diskspace();

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/graphql/GraphQlMetricsAutoConfiguration.java

-68
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation.graphql;
18+
19+
import graphql.GraphQL;
20+
import io.micrometer.observation.Observation;
21+
import io.micrometer.observation.ObservationRegistry;
22+
23+
import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.AutoConfiguration;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.graphql.execution.GraphQlSource;
31+
import org.springframework.graphql.observation.GraphQlObservationInstrumentation;
32+
33+
/**
34+
* {@link EnableAutoConfiguration Auto-configuration} for instrumentation of Spring
35+
* GraphQL endpoints.
36+
*
37+
* @author Brian Clozel
38+
* @since 3.0.0
39+
*/
40+
@AutoConfiguration(after = ObservationAutoConfiguration.class)
41+
@ConditionalOnBean(ObservationRegistry.class)
42+
@ConditionalOnClass({ GraphQL.class, GraphQlSource.class, Observation.class })
43+
@SuppressWarnings("removal")
44+
public class GraphQlObservationAutoConfiguration {
45+
46+
@Bean
47+
@ConditionalOnMissingBean
48+
public GraphQlObservationInstrumentation graphQlObservationInstrumentation(
49+
ObservationRegistry observationRegistry) {
50+
return new GraphQlObservationInstrumentation(observationRegistry);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
/**
18-
* Auto-configuration for Spring GraphQL metrics.
18+
* Auto-configuration for Spring GraphQL observations.
1919
*/
20-
package org.springframework.boot.actuate.autoconfigure.metrics.graphql;
20+
package org.springframework.boot.actuate.autoconfigure.observation.graphql;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+26
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,32 @@
18851885
"replacement": "management.wavefront.uri"
18861886
}
18871887
},
1888+
{
1889+
"name": "management.metrics.graphql.autotime.enabled",
1890+
"description": "Whether to automatically time web client requests.",
1891+
"defaultValue": true,
1892+
"deprecation": {
1893+
"level": "error",
1894+
"reason": "Should be applied at the ObservationRegistry level."
1895+
}
1896+
},
1897+
{
1898+
"name": "management.metrics.graphql.autotime.percentiles",
1899+
"description": "Computed non-aggregable percentiles to publish.",
1900+
"deprecation": {
1901+
"level": "error",
1902+
"reason": "Should be applied at the ObservationRegistry level."
1903+
}
1904+
},
1905+
{
1906+
"name": "management.metrics.graphql.autotime.percentiles-histogram",
1907+
"description": "Whether percentile histograms should be published.",
1908+
"defaultValue": false,
1909+
"deprecation": {
1910+
"level": "error",
1911+
"reason": "Should be applied at the ObservationRegistry level."
1912+
}
1913+
},
18881914
{
18891915
"name": "management.metrics.mongo.command.enabled",
18901916
"description": "Whether to enable Mongo client command metrics.",

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetri
6767
org.springframework.boot.actuate.autoconfigure.metrics.export.stackdriver.StackdriverMetricsExportAutoConfiguration
6868
org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration
6969
org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront.WavefrontMetricsExportAutoConfiguration
70-
org.springframework.boot.actuate.autoconfigure.metrics.graphql.GraphQlMetricsAutoConfiguration
70+
org.springframework.boot.actuate.autoconfigure.observation.graphql.GraphQlObservationAutoConfiguration
7171
org.springframework.boot.actuate.autoconfigure.metrics.integration.IntegrationMetricsAutoConfiguration
7272
org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration
7373
org.springframework.boot.actuate.autoconfigure.metrics.jersey.JerseyServerMetricsAutoConfiguration

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/graphql/GraphQlMetricsAutoConfigurationTests.java

-98
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.observation.graphql;
18+
19+
import io.micrometer.observation.ObservationRegistry;
20+
import io.micrometer.observation.tck.TestObservationRegistry;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.graphql.observation.GraphQlObservationInstrumentation;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link GraphQlObservationAutoConfiguration}.
33+
*
34+
* @author Brian Clozel
35+
*/
36+
class GraphQlObservationAutoConfigurationTests {
37+
38+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
39+
.withBean(TestObservationRegistry.class, TestObservationRegistry::create)
40+
.withConfiguration(AutoConfigurations.of(GraphQlObservationAutoConfiguration.class));
41+
42+
@Test
43+
void backsOffWhenObservationRegistryIsMissing() {
44+
new ApplicationContextRunner()
45+
.withConfiguration(AutoConfigurations.of(GraphQlObservationAutoConfiguration.class))
46+
.run((context) -> assertThat(context).doesNotHaveBean(GraphQlObservationInstrumentation.class));
47+
}
48+
49+
@Test
50+
void definesInstrumentationWhenObservationRegistryIsPresent() {
51+
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(GraphQlObservationInstrumentation.class));
52+
}
53+
54+
@Test
55+
void instrumentationBacksOffIfAlreadyPresent() {
56+
this.contextRunner.withUserConfiguration(InstrumentationConfiguration.class)
57+
.run((context) -> assertThat(context).hasSingleBean(GraphQlObservationInstrumentation.class)
58+
.hasBean("customInstrumentation"));
59+
}
60+
61+
@Configuration(proxyBeanMethods = false)
62+
static class InstrumentationConfiguration {
63+
64+
@Bean
65+
GraphQlObservationInstrumentation customInstrumentation(ObservationRegistry registry) {
66+
return new GraphQlObservationInstrumentation(registry);
67+
}
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)