How to log latency for suspend function #1502
-
In am new in GraphQL and Kotlin, so pardon me in advance if I am not able to articulate my question well! Assume: type Classroom { type Student { Now, we have suspend function for each field to compute it. class ClassroomQuery {private val classroom: Classroom class Classroom { class Student {...} As in graphql kotlin our classroomquery class not returns the actual response but the functions that will be invoked only when it is being called. So how can we log actual latency for classroomQuery class? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hello, you can rely on Instrumentations to do this specifically the |
Beta Was this translation helpful? Give feedback.
-
Example @Component
class LogInstrumentation : Instrumentation {
override fun beginFieldFetch(
parameters: InstrumentationFieldFetchParameters,
state: InstrumentationState?
): InstrumentationContext<Any>? {
val start = System.currentTimeMillis();
println("begin field fetch ${parameters.field.name}")
return object : InstrumentationContext<Any>
override fun onDispatched(result: CompletableFuture<Any>?) {
}
override fun onCompleted(result: Any?, t: Throwable?) {
val finish = System.currentTimeMillis();
println("completed field fetch ${parameters.field.name}")
println("elapsed time: ${finish - start}")
}
}
}
} GraphQL Koltin Server will automatically detect your instrumentation and apply it to the operations that you will execute |
Beta Was this translation helpful? Give feedback.
-
As @samuelAndalon mentioned you can use instrumentation for capturing this info (keep in mind there is some overhead for calculating this). Instead of custom instrumentation you could use graphql-java Apollo Tracing Instrumentation or Federated Tracing Instrumentation to calculate it for you. |
Beta Was this translation helpful? Give feedback.
Example