Skip to content

Commit

Permalink
Add configuration to disable/enable resolver and query metrics. (#1665)
Browse files Browse the repository at this point in the history
* Add configuration to disable/enable resolver and query metrics.

* Fix lint errors.
  • Loading branch information
srinivasankavitha authored Oct 12, 2023
1 parent 564e82d commit 7792c41
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,6 +61,10 @@ class DgsGraphQLMetricsInstrumentation(
parameters: InstrumentationExecutionParameters,
state: InstrumentationState
): InstrumentationContext<ExecutionResult> {
if (!properties.query.enabled) {
return noOp()
}

val miState: MetricsInstrumentationState = state as MetricsInstrumentationState
miState.startTimer()

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7792c41

Please sign in to comment.