async def monitoring(handler, root, info: ResolveInfo, **kwargs):
t_start = time.perf_counter()
result = handler(root, info, **kwargs)
if not isawaitable(result):
return result
result = await result
duration = max(time.perf_counter() - t_start, 0)
if root is not None and hasattr(root, '_meta'):
parent_type = root._meta.name
else:
parent_type = '<root>'
operation_type = info.operation.operation
field_name = info.field_name
RESOLVE_LATENCY.labels(
operation_type,
f'{parent_type}.{field_name}',
).observe(duration)
return result
middleware = MiddlewareManager(monitoring, wrap_in_promise=False)
Each time the monitoring
middleware is executed, a new asyncio.Task is created, that task is then never retrieved.