Skip to content

Commit

Permalink
legg til metrikk på graphql kall med tag for success/error
Browse files Browse the repository at this point in the history
  • Loading branch information
kenglxn committed Dec 20, 2024
1 parent a2cd5a1 commit 47731c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package no.nav.arbeidsgiver.notifikasjon.infrastruktur.graphql
import com.fasterxml.jackson.annotation.JsonTypeName
import com.fasterxml.jackson.module.kotlin.convertValue
import com.symbaloo.graphqlmicrometer.MicrometerInstrumentation
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.*
import graphql.GraphQL.newGraphQL
import graphql.GraphQLError
import graphql.GraphqlErrorException
import graphql.execution.DataFetcherResult
import graphql.schema.DataFetchingEnvironment
import graphql.schema.idl.RuntimeWiring
Expand All @@ -21,8 +18,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.future
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.Metrics.meterRegistry
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.coRecord
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.getTimer
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.json.laxObjectMapper
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.logger
import no.nav.arbeidsgiver.notifikasjon.produsent.api.ProdusentAPI
import org.intellij.lang.annotations.Language

inline fun <reified T: Any?> DataFetchingEnvironment.getTypedArgument(name: String): T {
Expand Down Expand Up @@ -134,7 +133,13 @@ data class GraphQLRequest(
@Language("GraphQL") val query: String,
val operationName: String? = null,
val variables: Map<String, Any?>? = null,
)
) {
val queryNameRegex = Regex("\\b(mutation|query).*\\{\\s*(\\w+)")
val queryName: String
get() {
return queryNameRegex.find(query)?.groups?.get(2)?.value ?: "unknown"
}
}

inline fun requireGraphql(check: Boolean, message: () -> String) {
if (!check) {
Expand All @@ -151,10 +156,9 @@ fun <T> DataFetchingEnvironment.notifikasjonContext(): T =
class TypedGraphQL<T : WithCoroutineScope>(
private val graphQL: GraphQL
) {
fun execute(request: GraphQLRequest, context: T): Any {
fun execute(request: GraphQLRequest, context: T): ExecutionResult {
val executionInput = executionInput(request, context)
val result = graphQL.execute(executionInput)
return result.toSpecification()
return graphQL.execute(executionInput)
}

private fun executionInput(
Expand All @@ -177,3 +181,20 @@ class TypedGraphQL<T : WithCoroutineScope>(
}.build()
}
}

fun <T : WithCoroutineScope> TypedGraphQL<T>.timedExecute(
request: GraphQLRequest,
context: T
): ExecutionResult = with(Timer.start(meterRegistry)) {
execute(request, context).also { result ->
if (result.errors.isNotEmpty()) {
GraphQLLogger.log.error("graphql request failed: {}", result.errors)
}
val tags = mutableSetOf(
"queryName" to (request.queryName),
"result" to if (result.errors.isEmpty()) "success" else "error"
)
if (context is ProdusentAPI.Context) tags.add("produsent" to context.appName)
stop(getTimer("graphql.timer", tags, "graphql request"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics
import io.micrometer.core.instrument.binder.logging.LogbackMetrics
import io.micrometer.core.instrument.binder.system.ProcessorMetrics
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import no.nav.arbeidsgiver.notifikasjon.bruker.BrukerAPI
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.Health
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.Metrics
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.graphql.GraphQLRequest
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.graphql.TypedGraphQL
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.graphql.WithCoroutineScope
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.graphql.timedExecute
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.json.laxObjectMapper
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.produceMetrics
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.produsenter.ProdusentRegister
Expand Down Expand Up @@ -109,8 +106,8 @@ fun <T : WithCoroutineScope> Application.graphqlSetup(
withContext(this.coroutineContext + graphQLDispatcher) {
val context = extractContext()
val request = call.receive<GraphQLRequest>()
val result = graphql.await().execute(request, context)
call.respond(result)
val result = graphql.await().timedExecute(request, context)
call.respond(result.toSpecification())
}
}
}
Expand Down Expand Up @@ -166,6 +163,10 @@ fun Application.baseSetup(
replyToHeader(HttpHeaders.XCorrelationId)
}

install(Authentication) {
configureProviders(authProviders)
}

install(CallLogging) {
disableDefaultColors()
level = Level.INFO
Expand Down Expand Up @@ -224,10 +225,6 @@ fun Application.baseSetup(
register(ContentType.Application.Json, TimedContentConverter(JacksonConverter(laxObjectMapper)))
}

install(Authentication) {
configureProviders(authProviders)
}

routing {
trace { application.log.trace(it.buildText()) }

Expand Down

0 comments on commit 47731c7

Please sign in to comment.