From 7792c417e2c28ccc2fa5439ba5476321bd732a53 Mon Sep 17 00:00:00 2001 From: Kavitha Srinivasan <41588701+srinivasankavitha@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:00:53 -0700 Subject: [PATCH] Add configuration to disable/enable resolver and query metrics. (#1665) * Add configuration to disable/enable resolver and query metrics. * Fix lint errors. --- .../DgsGraphQLMetricsInstrumentation.kt | 8 +++++- .../micrometer/DgsGraphQLMetricsProperties.kt | 17 +++++++++++- .../DgsGraphQLMetricsPropertiesTest.kt | 27 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsInstrumentation.kt b/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsInstrumentation.kt index 1c44fe7c3..ffefbe853 100644 --- a/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsInstrumentation.kt +++ b/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsInstrumentation.kt @@ -17,6 +17,7 @@ import graphql.analysis.QueryVisitorStub import graphql.execution.instrumentation.InstrumentationContext import graphql.execution.instrumentation.InstrumentationState import graphql.execution.instrumentation.SimpleInstrumentationContext +import graphql.execution.instrumentation.SimpleInstrumentationContext.noOp import graphql.execution.instrumentation.SimplePerformantInstrumentation import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters @@ -60,6 +61,10 @@ class DgsGraphQLMetricsInstrumentation( parameters: InstrumentationExecutionParameters, state: InstrumentationState ): InstrumentationContext { + if (!properties.query.enabled) { + return noOp() + } + val miState: MetricsInstrumentationState = state as MetricsInstrumentationState miState.startTimer() @@ -120,7 +125,8 @@ class DgsGraphQLMetricsInstrumentation( if (parameters.isTrivialDataFetcher || miState.isIntrospectionQuery || TagUtils.shouldIgnoreTag(gqlField) || - !schemaProvider.isFieldMetricsInstrumentationEnabled(gqlField) + !schemaProvider.isFieldMetricsInstrumentationEnabled(gqlField) || + !properties.resolver.enabled ) { return dataFetcher } diff --git a/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsProperties.kt b/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsProperties.kt index 58852777a..05d888b34 100644 --- a/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsProperties.kt +++ b/graphql-dgs-spring-boot-micrometer/src/main/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsProperties.kt @@ -15,7 +15,12 @@ data class DgsGraphQLMetricsProperties( var autotime: PropertiesAutoTimer = PropertiesAutoTimer(autotimeProperties), /** Settings that can be used to limit some of the tag metrics used by DGS. */ @NestedConfigurationProperty - var tags: TagsProperties = TagsProperties() + var tags: TagsProperties = TagsProperties(), + /** Settings to selectively enable/disable gql timers.*/ + @NestedConfigurationProperty + var resolver: ResolverMetricProperties = ResolverMetricProperties(), + var query: QueryMetricProperties = QueryMetricProperties() + ) { data class TagsProperties( @@ -42,6 +47,16 @@ data class DgsGraphQLMetricsProperties( var enabled: Boolean = true ) + data class ResolverMetricProperties( + @DefaultValue("true") + var enabled: Boolean = true + ) + + data class QueryMetricProperties( + @DefaultValue("true") + var enabled: Boolean = true + ) + enum class CardinalityLimiterKind { /** Restrict the cardinality of the input to the first n values that are seen. */ FIRST, diff --git a/graphql-dgs-spring-boot-micrometer/src/test/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsPropertiesTest.kt b/graphql-dgs-spring-boot-micrometer/src/test/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsPropertiesTest.kt index d91ce8536..5c207ce50 100644 --- a/graphql-dgs-spring-boot-micrometer/src/test/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsPropertiesTest.kt +++ b/graphql-dgs-spring-boot-micrometer/src/test/kotlin/com/netflix/graphql/dgs/metrics/micrometer/DgsGraphQLMetricsPropertiesTest.kt @@ -43,6 +43,9 @@ internal class DgsGraphQLMetricsPropertiesTest { assertThat(props.tags.limiter.kind).isEqualTo(DgsGraphQLMetricsProperties.CardinalityLimiterKind.FIRST) assertThat(props.tags.limiter.limit).isEqualTo(100) assertThat(props.tags.complexity.enabled).isEqualTo(true) + + assertThat(props.resolver.enabled).isTrue() + assertThat(props.query.enabled).isTrue() } } @@ -73,6 +76,30 @@ internal class DgsGraphQLMetricsPropertiesTest { } } + @Test + fun `Can disable resolver metric`() { + contextRunner + .withPropertyValues( + "management.metrics.dgs-graphql.resolver.enabled=false" + ).run { ctx -> + val props = ctx.getBean(DgsGraphQLMetricsProperties::class.java) + + assertThat(props.resolver.enabled).isEqualTo(false) + } + } + + @Test + fun `Can disable query metric`() { + contextRunner + .withPropertyValues( + "management.metrics.dgs-graphql.query.enabled=false" + ).run { ctx -> + val props = ctx.getBean(DgsGraphQLMetricsProperties::class.java) + + assertThat(props.query.enabled).isEqualTo(false) + } + } + @Configuration @EnableConfigurationProperties(DgsGraphQLMetricsProperties::class) open class TestConfiguration