Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Trace the full span of dataloaders and report the number of objects returned (#17931)

Patryk Zawadzki8d0cb16c5c08
diff --git a/saleor/core/telemetry/saleor_attributes.py b/saleor/core/telemetry/saleor_attributes.py
index 29fffd96f8..42b4582108 100644
--- a/saleor/core/telemetry/saleor_attributes.py
+++ b/saleor/core/telemetry/saleor_attributes.py
@@ -8,10 +8,11 @@ OPERATION_NAME: Final = "operation.name"
# GraphQL
GRAPHQL_DOCUMENT_FINGERPRINT: Final = "graphql.document_fingerprint"
-GRAPHQL_OPERATION_IDENTIFIER: Final = "graphql.operation.identifier"
+GRAPHQL_FIELD_NAME: Final = "graphql.field_name"
GRAPHQL_OPERATION_COST: Final = "graphql.operation.cost"
+GRAPHQL_OPERATION_IDENTIFIER: Final = "graphql.operation.identifier"
GRAPHQL_PARENT_TYPE: Final = "graphql.parent_type"
-GRAPHQL_FIELD_NAME: Final = "graphql.field_name"
+GRAPHQL_RESOLVER_ROW_COUNT: Final = "graphql.resolver.row_count"
# Http
SALEOR_SOURCE_SERVICE_NAME: Final = "saleor.source.service.name"
diff --git a/saleor/graphql/core/dataloaders.py b/saleor/graphql/core/dataloaders.py
index 30086e927d..7aafe8c68e 100644
--- a/saleor/graphql/core/dataloaders.py
+++ b/saleor/graphql/core/dataloaders.py
@@ -42,7 +42,9 @@ class DataLoader[K, R](BaseLoader):
def batch_load_fn( # pylint: disable=method-hidden
self, keys: Iterable[K]
) -> Promise[list[R]]:
- with tracer.start_as_current_span(self.__class__.__name__) as span:
+ with tracer.start_as_current_span(
+ self.__class__.__name__, end_on_exit=False
+ ) as span:
span.set_attribute(
saleor_attributes.OPERATION_NAME, "dataloader.batch_load"
)
@@ -51,8 +53,24 @@ class DataLoader[K, R](BaseLoader):
results = self.batch_load(keys)
if not isinstance(results, Promise):
+ span.set_attribute(
+ saleor_attributes.GRAPHQL_RESOLVER_ROW_COUNT, len(results)
+ )
+ span.end()
return Promise.resolve(results)
- return results
+
+ def did_fulfill(results: list[R]) -> list[R]:
+ span.set_attribute(
+ saleor_attributes.GRAPHQL_RESOLVER_ROW_COUNT, len(results)
+ )
+ span.end()
+ return results
+
+ def did_reject(error: Exception) -> list[R]:
+ span.end()
+ raise error
+
+ return results.then(did_fulfill, did_reject)
def batch_load(self, keys: Iterable[K]) -> Promise[list[R]] | list[R]:
raise NotImplementedError()