Skip to content

Commit

Permalink
Avoid using deprecated instrumentation parameters for metrcs.
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasankavitha committed Dec 5, 2022
1 parent 471dc4f commit 711cc08
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 27 deletions.
Expand Up @@ -16,13 +16,12 @@

package com.netflix.graphql.dgs.metrics.micrometer

import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.metrics.micrometer.tagging.DgsContextualTagCustomizer
import com.netflix.graphql.dgs.metrics.micrometer.tagging.DgsExecutionTagCustomizer
import com.netflix.graphql.dgs.metrics.micrometer.tagging.DgsFieldFetchTagCustomizer
import com.netflix.graphql.dgs.metrics.micrometer.tagging.DgsGraphQLMetricsTagsProvider
import graphql.ExecutionResult
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters
import io.micrometer.core.instrument.Tag
import io.micrometer.core.instrument.Tags
import java.util.*
Expand All @@ -38,14 +37,19 @@ class DgsGraphQLCollatedMetricsTagsProvider(
}

override fun getExecutionTags(
parameters: InstrumentationExecutionParameters,
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
result: ExecutionResult,
exception: Throwable?
): Iterable<Tag> {
return Tags.of(executionTagCustomizer.flatMap { it.getExecutionTags(parameters, result, exception) })
return Tags.of(executionTagCustomizer.flatMap { it.getExecutionTags(state, context, result, exception) })
}

override fun getFieldFetchTags(parameters: InstrumentationFieldFetchParameters, exception: Throwable?): Iterable<Tag> {
return Tags.of(fieldFetchTagCustomizer.flatMap { it.getFieldFetchTags(parameters, exception) })
override fun getFieldFetchTags(
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
exception: Throwable?
): Iterable<Tag> {
return Tags.of(fieldFetchTagCustomizer.flatMap { it.getFieldFetchTags(state, context, exception) })
}
}
@@ -1,6 +1,7 @@
package com.netflix.graphql.dgs.metrics.micrometer

import com.netflix.graphql.dgs.Internal
import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.internal.DgsSchemaProvider
import com.netflix.graphql.dgs.metrics.DgsMetrics.GqlMetric
import com.netflix.graphql.dgs.metrics.DgsMetrics.GqlTag
Expand Down Expand Up @@ -61,6 +62,7 @@ class DgsGraphQLMetricsInstrumentation(
state: InstrumentationState
): InstrumentationContext<ExecutionResult> {
val miState: MetricsInstrumentationState = state as MetricsInstrumentationState
val context: DgsContext = parameters.getContext()
miState.startTimer()

miState.operationName = Optional.ofNullable(parameters.operation)
Expand All @@ -77,7 +79,7 @@ class DgsGraphQLMetricsInstrumentation(
properties.autotime
.builder(GqlMetric.QUERY.key)
.tags(tagsProvider.getContextualTags())
.tags(tagsProvider.getExecutionTags(parameters, result, exc))
.tags(tagsProvider.getExecutionTags(miState, context, result, exc))
.tags(miState.tags())
)
}
Expand All @@ -90,10 +92,11 @@ class DgsGraphQLMetricsInstrumentation(
state: InstrumentationState
): CompletableFuture<ExecutionResult> {
val miState: MetricsInstrumentationState = state as MetricsInstrumentationState
val context: DgsContext = parameters.getContext()
val tags =
Tags.empty()
.and(tagsProvider.getContextualTags())
.and(tagsProvider.getExecutionTags(parameters, executionResult, null))
.and(tagsProvider.getExecutionTags(miState, context, executionResult, null))
.and(miState.tags())

ErrorUtils
Expand All @@ -118,6 +121,7 @@ class DgsGraphQLMetricsInstrumentation(
state: InstrumentationState
): DataFetcher<*> {
val miState: MetricsInstrumentationState = state as MetricsInstrumentationState
val context: DgsContext = parameters.environment.getContext()
val gqlField = TagUtils.resolveDataFetcherTagValue(parameters)

if (parameters.isTrivialDataFetcher ||
Expand All @@ -143,17 +147,18 @@ class DgsGraphQLMetricsInstrumentation(
recordDataFetcherMetrics(
registry,
sampler,
parameters,
miState,
context,
error,
baseTags
)
}
} else {
recordDataFetcherMetrics(registry, sampler, parameters, null, baseTags)
recordDataFetcherMetrics(registry, sampler, state, context, null, baseTags)
}
result
} catch (throwable: Throwable) {
recordDataFetcherMetrics(registry, sampler, parameters, throwable, baseTags)
recordDataFetcherMetrics(registry, sampler, state, context, throwable, baseTags)
throw throwable
}
}
Expand Down Expand Up @@ -197,13 +202,14 @@ class DgsGraphQLMetricsInstrumentation(
private fun recordDataFetcherMetrics(
registry: MeterRegistry,
timerSampler: Timer.Sample,
parameters: InstrumentationFieldFetchParameters,
state: MetricsInstrumentationState,
context: DgsContext,
error: Throwable?,
baseTags: Iterable<Tag>
) {
val recordedTags = Tags
.of(baseTags)
.and(tagsProvider.getFieldFetchTags(parameters, error))
.and(tagsProvider.getFieldFetchTags(state, context, error))

timerSampler.stop(
properties
Expand Down
Expand Up @@ -16,15 +16,17 @@

package com.netflix.graphql.dgs.metrics.micrometer.tagging

import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.metrics.micrometer.DgsGraphQLMetricsInstrumentation
import graphql.ExecutionResult
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import io.micrometer.core.instrument.Tag

@FunctionalInterface
fun interface DgsExecutionTagCustomizer {

fun getExecutionTags(
parameters: InstrumentationExecutionParameters,
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
result: ExecutionResult,
exception: Throwable?
): Iterable<Tag>
Expand Down
Expand Up @@ -16,11 +16,16 @@

package com.netflix.graphql.dgs.metrics.micrometer.tagging

import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters
import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.metrics.micrometer.DgsGraphQLMetricsInstrumentation
import io.micrometer.core.instrument.Tag

@FunctionalInterface
fun interface DgsFieldFetchTagCustomizer {

fun getFieldFetchTags(parameters: InstrumentationFieldFetchParameters, error: Throwable?): Iterable<Tag>
fun getFieldFetchTags(
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
error: Throwable?
): Iterable<Tag>
}
Expand Up @@ -16,9 +16,9 @@

package com.netflix.graphql.dgs.metrics.micrometer.tagging

import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.metrics.micrometer.DgsGraphQLMetricsInstrumentation
import graphql.ExecutionResult
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters
import io.micrometer.core.instrument.Tag
import io.micrometer.core.instrument.Tags

Expand All @@ -27,10 +27,15 @@ interface DgsGraphQLMetricsTagsProvider {
fun getContextualTags(): Iterable<Tag> = Tags.empty()

fun getExecutionTags(
parameters: InstrumentationExecutionParameters,
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
result: ExecutionResult,
exception: Throwable?
): Iterable<Tag> = Tags.empty()

fun getFieldFetchTags(parameters: InstrumentationFieldFetchParameters, exception: Throwable?): Iterable<Tag> = Tags.empty()
fun getFieldFetchTags(
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
exception: Throwable?
): Iterable<Tag> = Tags.empty()
}
Expand Up @@ -16,17 +16,18 @@

package com.netflix.graphql.dgs.metrics.micrometer.tagging

import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.metrics.DgsMetrics.CommonTags.*
import com.netflix.graphql.dgs.metrics.micrometer.DgsGraphQLMetricsInstrumentation
import graphql.ExecutionResult
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters
import io.micrometer.core.instrument.Tag
import io.micrometer.core.instrument.Tags

class SimpleGqlOutcomeTagCustomizer : DgsExecutionTagCustomizer, DgsFieldFetchTagCustomizer {

override fun getExecutionTags(
parameters: InstrumentationExecutionParameters,
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
result: ExecutionResult,
exception: Throwable?
): Iterable<Tag> {
Expand All @@ -38,7 +39,8 @@ class SimpleGqlOutcomeTagCustomizer : DgsExecutionTagCustomizer, DgsFieldFetchTa
}

override fun getFieldFetchTags(
parameters: InstrumentationFieldFetchParameters,
state: DgsGraphQLMetricsInstrumentation.MetricsInstrumentationState,
context: DgsContext,
error: Throwable?
): Iterable<Tag> {
return if (error == null) {
Expand Down
Expand Up @@ -792,12 +792,12 @@ class MicrometerServletSmokeTest {

@Bean
open fun executionTagCustomizer(): DgsExecutionTagCustomizer {
return DgsExecutionTagCustomizer { _, _, _ -> Tags.of("execution-tag", "foo") }
return DgsExecutionTagCustomizer { _, _, _, _ -> Tags.of("execution-tag", "foo") }
}

@Bean
open fun fieldFetchTagCustomizer(): DgsFieldFetchTagCustomizer {
return DgsFieldFetchTagCustomizer { _, _ -> Tags.of("field-fetch-tag", "foo") }
return DgsFieldFetchTagCustomizer { _, _, _ -> Tags.of("field-fetch-tag", "foo") }
}
}

Expand Down

0 comments on commit 711cc08

Please sign in to comment.