Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add query_identifier to tracing (#10922)
Filip Owczarek9064bf0ddea8
diff --git a/saleor/graphql/tests/test_tracing.py b/saleor/graphql/tests/test_tracing.pyindex 5288bc3f8d..2f30e1282f 100644--- a/saleor/graphql/tests/test_tracing.py+++ b/saleor/graphql/tests/test_tracing.py@@ -192,3 +192,153 @@ def test_tracing_mutation_hashing(span = _get_graphql_span(tracer.finished_spans())hash = hashlib.md5(span.tags["graphql.query"].encode("utf-8")).hexdigest()assert span.tags["graphql.query_fingerprint"] == f"mutation:cancelOrder:{hash}"+++@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_query_identifier_for_query(+ tracing_mock,+ staff_api_client,+ permission_manage_products,+):+ tracer = MockTracer()+ tracing_mock.return_value = tracer+ query = """+ query test {+ products(first: 5, channel:"default-channel") {+ edges {+ node {+ id+ }+ }+ }+ otherProducts: products(first: 5, channel:"default-channel") {+ edges {+ node {+ id+ }+ }+ }+ me{+ id+ }+ }+ """+ staff_api_client.user.user_permissions.add(permission_manage_products)+ staff_api_client.post_graphql(query)+ span = _get_graphql_span(tracer.finished_spans())+ assert span.tags["graphql.query_identifier"] == "me, products"+++@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_query_identifier_with_fragment(+ tracing_mock,+ staff_api_client,+ permission_manage_products,+):+ tracer = MockTracer()+ tracing_mock.return_value = tracer+ query = """+ fragment ProductVariant on ProductVariant {+ id+ }+ query test {+ products(first:5) {+ edges{+ node{+ id+ name+ variants {+ ...ProductVariant+ }+ }+ }+ }+ }+ """+ staff_api_client.user.user_permissions.add(permission_manage_products)+ staff_api_client.post_graphql(query)+ span = _get_graphql_span(tracer.finished_spans())+ assert span.tags["graphql.query_identifier"] == "products"+++@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_query_identifier_for_unnamed_mutation(+ tracing_mock,+ staff_api_client,+):+ tracer = MockTracer()+ tracing_mock.return_value = tracer+ query = """+ mutation{+ tokenCreate(email: "[email protected]", password:"admin"){+ token+ }+ }+ """+ staff_api_client.post_graphql(query)+ span = _get_graphql_span(tracer.finished_spans())+ assert span.tags["graphql.query_identifier"] == "tokenCreate"+++@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_query_identifier_for_named_mutation(+ tracing_mock,+ staff_api_client,+):+ tracer = MockTracer()+ tracing_mock.return_value = tracer+ query = """+ mutation MutationName{+ tokenCreate(email: "[email protected]", password:"admin"){+ token+ }+ }+ """+ staff_api_client.post_graphql(query)+ span = _get_graphql_span(tracer.finished_spans())+ assert span.tags["graphql.query_identifier"] == "tokenCreate"+++@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_query_identifier_for_many_mutations(+ tracing_mock,+ staff_api_client,+):+ tracer = MockTracer()+ tracing_mock.return_value = tracer+ query = """+ mutation {+ tokenCreate(email: "[email protected]", password:"admin"){+ token+ refreshToken+ csrfToken+ }+ deleteWarehouse(id:""){+ errors{+ field+ }+ }+ }+ """+ staff_api_client.post_graphql(query)+ span = _get_graphql_span(tracer.finished_spans())+ assert span.tags["graphql.query_identifier"] == "deleteWarehouse, tokenCreate"+++@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_query_identifier_undefined(+ tracing_mock,+ staff_api_client,+ permission_manage_products,+):+ tracer = MockTracer()+ tracing_mock.return_value = tracer+ query = """+ fragment ProductVariant on ProductVariant {+ id+ }+ """+ staff_api_client.user.user_permissions.add(permission_manage_products)+ staff_api_client.post_graphql(query)+ span = _get_graphql_span(tracer.finished_spans())+ assert span.tags["graphql.query_identifier"] == "undefined"diff --git a/saleor/graphql/utils/__init__.py b/saleor/graphql/utils/__init__.pyindex a2a408487f..cbdacf2293 100644--- a/saleor/graphql/utils/__init__.py+++ b/saleor/graphql/utils/__init__.py@@ -178,6 +178,53 @@ def requestor_is_superuser(requestor):return getattr(requestor, "is_superuser", False)+def query_identifier(document: GraphQLDocument) -> str:+ """Generate a fingerprint for a GraphQL query.++ For queries identifier is sorted set of all root objects separated by `,`.+ e.g+ query AnyQuery {+ product {+ id+ }+ order {+ id+ }+ Product2: product {+ id+ }+ Myself: me {+ }+ }+ identifier: me, order, product++ For mutations identifier is mutation type name.+ e.g.+ mutation CreateToken{+ tokenCreate(...){+ token+ }+ deleteWarehouse(...){+ ...+ }+ }+ identifier: deleteWarehouse, tokenCreate+ """+ labels = []+ for definition in document.document_ast.definitions:+ if getattr(definition, "operation", None) in {+ "query",+ "mutation",+ }:+ selections = definition.selection_set.selections+ for selection in selections:+ labels.append(selection.name.value)+ if not labels:+ return "undefined"+ return ", ".join(sorted(set(labels)))++def query_fingerprint(document: GraphQLDocument) -> str:"""Generate a fingerprint for a GraphQL query."""label = "unknown"diff --git a/saleor/graphql/views.py b/saleor/graphql/views.pyindex c5cb98c0e0..26a2cad685 100644--- a/saleor/graphql/views.py+++ b/saleor/graphql/views.py@@ -27,7 +27,7 @@ from .api import API_PATH, schemafrom .context import get_context_valuefrom .core.validators.query_cost import validate_query_costfrom .query_cost_map import COST_MAP-from .utils import format_error, query_fingerprint+from .utils import format_error, query_fingerprint, query_identifierINT_ERROR_MSG = "Int cannot represent non 32-bit signed integer value"@@ -295,6 +295,7 @@ class GraphQLView(View):if document is not None:raw_query_string = document.document_stringspan.set_tag("graphql.query", raw_query_string)+ span.set_tag("graphql.query_identifier", query_identifier(document))span.set_tag("graphql.query_fingerprint", query_fingerprint(document))try:query_contains_schema = self.check_if_query_contains_only_schema(