Skip to content

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

Fix App data in tracing (#13472)

Filip Owczarek450e8be51727
diff --git a/saleor/graphql/tests/test_tracing.py b/saleor/graphql/tests/test_tracing.py
index 2f30e1282f..8b35417ae6 100644
--- a/saleor/graphql/tests/test_tracing.py
+++ b/saleor/graphql/tests/test_tracing.py
@@ -19,6 +19,7 @@ def test_tracing_query_hashing(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -34,8 +35,12 @@ def test_tracing_query_hashing(
}
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
hash = hashlib.md5(span.tags["graphql.query"].encode("utf-8")).hexdigest()
assert span.tags["graphql.query_fingerprint"] == f"query:test:{hash}"
@@ -46,6 +51,7 @@ def test_tracing_query_hashing_with_fragment(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -67,8 +73,12 @@ def test_tracing_query_hashing_with_fragment(
}
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
hash = hashlib.md5(span.tags["graphql.query"].encode("utf-8")).hexdigest()
assert span.tags["graphql.query_fingerprint"] == f"query:test:{hash}"
@@ -79,6 +89,7 @@ def test_tracing_query_hashing_different_vars_same_checksum(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -95,8 +106,12 @@ def test_tracing_query_hashing_different_vars_same_checksum(
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
QUERIES = 5
+
+ # when
for i in range(QUERIES):
staff_api_client.post_graphql(query, {"first": i + 1})
+
+ # then
fingerprints = list(
map(
lambda span: span.tags["graphql.query_fingerprint"],
@@ -113,6 +128,7 @@ def test_tracing_query_hashing_unnamed_query(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -128,8 +144,12 @@ def test_tracing_query_hashing_unnamed_query(
}
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
hash = hashlib.md5(span.tags["graphql.query"].encode("utf-8")).hexdigest()
assert span.tags["graphql.query_fingerprint"] == f"query:{hash}"
@@ -140,6 +160,7 @@ def test_tracing_query_hashing_unnamed_query_no_query_spec(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -155,8 +176,12 @@ def test_tracing_query_hashing_unnamed_query_no_query_spec(
}
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
hash = hashlib.md5(span.tags["graphql.query"].encode("utf-8")).hexdigest()
assert span.tags["graphql.query_fingerprint"] == f"query:{hash}"
@@ -168,6 +193,7 @@ def test_tracing_mutation_hashing(
permission_manage_orders,
order_with_lines,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
order = order_with_lines
@@ -186,10 +212,14 @@ def test_tracing_mutation_hashing(
}
}
"""
+
+ # when
staff_api_client.post_graphql(
mutation, variables, permissions=[permission_manage_orders]
)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
hash = hashlib.md5(span.tags["graphql.query"].encode("utf-8")).hexdigest()
assert span.tags["graphql.query_fingerprint"] == f"mutation:cancelOrder:{hash}"
@@ -200,6 +230,7 @@ def test_tracing_query_identifier_for_query(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -256,8 +287,12 @@ def test_tracing_query_identifier_with_fragment(
}
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
assert span.tags["graphql.query_identifier"] == "products"
@@ -266,6 +301,7 @@ def test_tracing_query_identifier_for_unnamed_mutation(
tracing_mock,
staff_api_client,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -275,8 +311,12 @@ def test_tracing_query_identifier_for_unnamed_mutation(
}
}
"""
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
assert span.tags["graphql.query_identifier"] == "tokenCreate"
@@ -285,6 +325,7 @@ def test_tracing_query_identifier_for_named_mutation(
tracing_mock,
staff_api_client,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -294,8 +335,12 @@ def test_tracing_query_identifier_for_named_mutation(
}
}
"""
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
assert span.tags["graphql.query_identifier"] == "tokenCreate"
@@ -304,6 +349,7 @@ def test_tracing_query_identifier_for_many_mutations(
tracing_mock,
staff_api_client,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -320,8 +366,12 @@ def test_tracing_query_identifier_for_many_mutations(
}
}
"""
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
assert span.tags["graphql.query_identifier"] == "deleteWarehouse, tokenCreate"
@@ -331,6 +381,7 @@ def test_tracing_query_identifier_undefined(
staff_api_client,
permission_manage_products,
):
+ # given
tracer = MockTracer()
tracing_mock.return_value = tracer
query = """
@@ -339,6 +390,75 @@ def test_tracing_query_identifier_undefined(
}
"""
staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
staff_api_client.post_graphql(query)
span = _get_graphql_span(tracer.finished_spans())
+
+ # then
assert span.tags["graphql.query_identifier"] == "undefined"
+
+
+@patch("saleor.graphql.views.opentracing.global_tracer")
+def test_tracing_dont_have_app_data_staff_as_requestor(
+ tracing_mock,
+ staff_api_client,
+ permission_manage_products,
+):
+ # given
+ tracer = MockTracer()
+ tracing_mock.return_value = tracer
+ query = """
+ query test {
+ products(first:5) {
+ edges{
+ node{
+ id
+ name
+ }
+ }
+ }
+ }
+ """
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
+ staff_api_client.post_graphql(query)
+ span = _get_graphql_span(tracer.finished_spans())
+
+ # then
+ assert "app.name" not in span.tags
+ assert "app.id" not in span.tags
+
+
+@patch("saleor.graphql.views.opentracing.global_tracer")
+def test_tracing_have_app_data_app_as_requestor(
+ tracing_mock,
+ app_api_client,
+ permission_manage_products,
+):
+ # given
+ tracer = MockTracer()
+ tracing_mock.return_value = tracer
+ query = """
+ query test {
+ products(first:5) {
+ edges{
+ node{
+ id
+ name
+ }
+ }
+ }
+ }
+ """
+ app = app_api_client.app
+ app.permissions.add(permission_manage_products)
+
+ # when
+ app_api_client.post_graphql(query)
+ span = _get_graphql_span(tracer.finished_spans())
+
+ # then
+ assert span.tags["app.name"] == app.name
+ assert span.tags["app.id"] == app.id
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index bde0b0836b..ba875b5863 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -307,6 +307,12 @@ class GraphQLView(View):
# We only include it optionally since
# executor is not a valid argument in all backends
extra_options["executor"] = self.executor
+
+ context = get_context_value(request)
+ if app := getattr(request, "app", None):
+ span.set_tag("app.id", app.id)
+ span.set_tag("app.name", app.name)
+
try:
with connection.execute_wrapper(tracing_wrapper):
response = None
@@ -322,22 +328,17 @@ class GraphQLView(View):
root=self.get_root_value(),
variables=variables,
operation_name=operation_name,
- context=get_context_value(request),
+ context=context,
middleware=self.middleware,
**extra_options,
)
if should_use_cache_for_scheme:
cache.set(key, response)
- if app := getattr(request, "app", None):
- span.set_tag("app.name", app.name)
-
return set_query_cost_on_result(response, query_cost)
except Exception as e:
span.set_tag(opentracing.tags.ERROR, True)
- if app := getattr(request, "app", None):
- span.set_tag("app.id", app.id)
- span.set_tag("app.name", app.name)
+
# In the graphql-core version that we are using,
# the Exception is raised for too big integers value.
# As it's a validation error we want to raise GraphQLError instead.