Skip to content

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

Include Saleor version and query name in error logs (#16339)

Tomasz Szymański9fb77500d33a
diff --git a/saleor/core/logging.py b/saleor/core/logging.py
index f8f9a18d52..c186351cc6 100644
--- a/saleor/core/logging.py
+++ b/saleor/core/logging.py
@@ -4,6 +4,8 @@ import time
from celery._state import get_current_task as get_current_celery_task
from pythonjsonlogger.jsonlogger import JsonFormatter as BaseFormatter
+from .. import __version__ as saleor_version
+
class JsonFormatter(BaseFormatter):
converter = time.gmtime
@@ -11,6 +13,11 @@ class JsonFormatter(BaseFormatter):
def add_fields(self, log_record, record, message_dict):
super().add_fields(log_record, record, message_dict)
log_record["hostname"] = platform.node()
+ try:
+ log_record["query"] = record.exc_info[1]._exc_query
+ log_record["version"] = saleor_version
+ except (TypeError, IndexError, AttributeError):
+ pass
class JsonCeleryFormatter(JsonFormatter):
diff --git a/saleor/graphql/utils/__init__.py b/saleor/graphql/utils/__init__.py
index a0749f3f7c..841e01efc4 100644
--- a/saleor/graphql/utils/__init__.py
+++ b/saleor/graphql/utils/__init__.py
@@ -269,7 +269,7 @@ def query_fingerprint(document: GraphQLDocument) -> str:
return f"{label}:{query_hash}"
-def format_error(error, handled_exceptions):
+def format_error(error, handled_exceptions, query=None):
result: dict[str, Any]
if isinstance(error, GraphQLError):
result = format_graphql_error(error)
@@ -284,6 +284,8 @@ def format_error(error, handled_exceptions):
exc = exc.original_error
if isinstance(exc, AssertionError):
exc = GraphQLError(str(exc))
+ if query:
+ exc._exc_query = query
if isinstance(exc, handled_exceptions):
handled_errors_logger.info("A query had an error", exc_info=exc)
else:
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index cc5b0b14e5..b90ac6e71e 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -61,6 +61,7 @@ class GraphQLView(View):
middleware = None
root_value = None
backend: GraphQLBackend = None # type: ignore[assignment]
+ _query: Optional[str] = None
HANDLED_EXCEPTIONS = (
GraphQLError,
@@ -265,7 +266,6 @@ class GraphQLView(View):
)
query, variables, operation_name = self.get_graphql_params(request, data)
-
document, error = self.parse_query(query)
with observability.report_gql_operation() as operation:
operation.query = document
@@ -274,9 +274,11 @@ class GraphQLView(View):
if error or document is None:
return error
+ _query_identifier = query_identifier(document)
+ self._query = _query_identifier
raw_query_string = document.document_string
span.set_tag("graphql.query", raw_query_string)
- span.set_tag("graphql.query_identifier", query_identifier(document))
+ span.set_tag("graphql.query_identifier", _query_identifier)
span.set_tag("graphql.query_fingerprint", query_fingerprint(document))
try:
query_contains_schema = check_if_query_contains_only_schema(document)
@@ -374,9 +376,8 @@ class GraphQLView(View):
variables = operations.get("variables")
return query, variables, operation_name
- @classmethod
- def format_error(cls, error):
- return format_error(error, cls.HANDLED_EXCEPTIONS)
+ def format_error(self, error):
+ return format_error(error, self.HANDLED_EXCEPTIONS, self._query)
def get_key(key):