Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of ApolloFederatedTracing wrapper #2167

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/scala/caliban/wrappers/Wrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sealed trait Wrapper[-R] extends GraphQLAspect[Nothing, R] { self =>
that.withWrapper(self)

// Disables tracing only for wrappers in the caliban package
final private[caliban] def trace: Trace = Trace.empty
final private[caliban] implicit def trace: Trace = Trace.empty
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this wasn't an implicit 🤦‍♂️

}

object Wrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,41 +98,50 @@ object ApolloFederatedTracing {
wrapPureValues: Boolean
): FieldWrapper[Any] =
new FieldWrapper[Any](wrapPureValues) {

private def updateState(startTime: Long, fieldInfo: FieldInfo)(result: Either[ExecutionError, ResponseValue]) =
ZQuery.succeed {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need to wrap this? We only execute it within a foldQuery. Could we just execute purely and then lift the result?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that's better actually. I'll do the change

val endTime = nanoTime
val path = (PathValue.Key(fieldInfo.name) :: fieldInfo.path).toVector
val _ = ref.updateAndGet(state =>
state.copy(
root = state.root.insert(
path,
Node(
id = Node.Id.ResponseName(fieldInfo.name),
startTime = startTime - state.startTime,
endTime = endTime - state.startTime,
`type` = fieldInfo.details.fieldType.toType().toString,
parentType = fieldInfo.details.parentType.map(_.toType().toString) getOrElse "",
originalFieldName = fieldInfo.details.alias.map(_ => fieldInfo.details.name) getOrElse "",
error = result.left.toOption.collectFirst { case e: ExecutionError =>
Error(
e.getMessage(),
location = e.locationInfo.map(l => Location(l.line, l.column)).toSeq
)
}.toSeq
)
)
)
)
result match {
case Right(value) => value
case _ => null
}
}

def wrap[R1](
query: ZQuery[R1, CalibanError.ExecutionError, ResponseValue],
fieldInfo: FieldInfo
): ZQuery[R1, CalibanError.ExecutionError, ResponseValue] =
if (enabled.get())
ZQuery.suspend {
val startTime = nanoTime
query.either.flatMap { result =>
ZQuery.fromEither {
val endTime = nanoTime
val path = (PathValue.Key(fieldInfo.name) :: fieldInfo.path).toVector
val _ = ref.updateAndGet(state =>
state.copy(
root = state.root.insert(
path,
Node(
id = Node.Id.ResponseName(fieldInfo.name),
startTime = startTime - state.startTime,
endTime = endTime - state.startTime,
`type` = fieldInfo.details.fieldType.toType().toString,
parentType = fieldInfo.details.parentType.map(_.toType().toString) getOrElse "",
originalFieldName = fieldInfo.details.alias.map(_ => fieldInfo.details.name) getOrElse "",
error = result.left.toOption.collectFirst { case e: ExecutionError =>
Error(
e.getMessage(),
location = e.locationInfo.map(l => Location(l.line, l.column)).toSeq
)
}.toSeq
)
)
)
)
result
}
}
val update0 = updateState(startTime, fieldInfo) _
query.foldQuery(
error => update0(Left(error)) *> ZQuery.fail(error),
value => update0(Right(value))
)
}
else query

Expand Down