Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add source.service.name span tag (#17250)
Maciej Korycinski438595033c60
diff --git a/saleor/graphql/tests/test_tracing.py b/saleor/graphql/tests/test_tracing.pyindex dd6e1945b1..1fe176de61 100644--- a/saleor/graphql/tests/test_tracing.py+++ b/saleor/graphql/tests/test_tracing.py@@ -2,6 +2,7 @@ import hashlibfrom unittest.mock import patchimport graphene+import pytestfrom opentracing.mocktracer import MockTracer@@ -460,3 +461,55 @@ def test_tracing_have_app_data_app_as_requestor(# thenassert span.tags["app.name"] == app.nameassert span.tags["app.id"] == app.id+++ ("header_source", "expected_result"),+ [+ ("saleor.dashboard", "saleor.dashboard"),+ ("saleor.dashboard.playground", "saleor.dashboard.playground"),+ ("saleor.playground", "saleor.playground"),+ ("saleor.DASHBOARD", "saleor.dashboard"),+ ("SALEOR.dashboard", "saleor.dashboard"),+ ("saleor.dashboard.Playground", "saleor.dashboard.playground"),+ ("saleor.playgrounD", "saleor.playground"),+ ("incorrect-value", "unknown_service"),+ (None, "unknown_service"),+ ],+)+@patch("saleor.graphql.views.opentracing.global_tracer")+def test_tracing_have_source_service_name_set(+ tracing_mock,+ header_source,+ expected_result,+ 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, headers={"source-service-name": header_source})++ # then+ spans = list(+ filter(lambda s: s.tags.get("source.service.name"), tracer.finished_spans())+ )+ assert len(spans) == 1+ span = spans[0]+ assert span.tags["source.service.name"] == expected_resultdiff --git a/saleor/graphql/tests/test_utils.py b/saleor/graphql/tests/test_utils.pyindex b6a4f21d2e..2e459dd59b 100644--- a/saleor/graphql/tests/test_utils.py+++ b/saleor/graphql/tests/test_utils.py@@ -6,7 +6,12 @@ from graphql import GraphQLErrorfrom graphql.utils import schema_printerfrom ..api import backend, schema-from ..utils import ALLOWED_ERRORS, INTERNAL_ERROR_MESSAGE, format_error+from ..utils import (+ ALLOWED_ERRORS,+ INTERNAL_ERROR_MESSAGE,+ format_error,+ get_source_service_name_value,+)from ..utils.validators import check_if_query_contains_only_schemafrom .utils import get_graphql_content@@ -332,3 +337,22 @@ def test_check_if_query_contains_only_schema_with_introspection():# thenassert result is True+++ ("header_source", "expected_result"),+ [+ ("saleor.DASHBOARD", "saleor.dashboard"),+ ("SALEOR.dashboard", "saleor.dashboard"),+ ("saleor.dashboard.PLAYGROUND", "saleor.dashboard.playground"),+ ("saleor.PLAYGROUND", "saleor.playground"),+ ("incorrect-value", "unknown_service"),+ (None, "unknown_service"),+ ],+)+def test_get_source_service_name_value(header_source, expected_result):+ # when+ result = get_source_service_name_value(header_source)++ # then+ assert result == expected_resultdiff --git a/saleor/graphql/utils/__init__.py b/saleor/graphql/utils/__init__.pyindex c06d6cc5d9..9ebb41a5f3 100644--- a/saleor/graphql/utils/__init__.py+++ b/saleor/graphql/utils/__init__.py@@ -17,10 +17,7 @@ from jwt import InvalidTokenErrorfrom ...account.models import Userfrom ...app.models import App-from ...core.exceptions import (- CircularSubscriptionSyncEvent,- PermissionDenied,-)+from ...core.exceptions import CircularSubscriptionSyncEvent, PermissionDeniedfrom ..core.enums import PermissionEnumfrom ..core.types import TYPES_WITH_DOUBLE_ID_AVAILABLE, Permissionfrom ..core.utils import from_global_id_or_error@@ -51,6 +48,12 @@ ALLOWED_ERRORS = [QueryCostError,]+AVAILABLE_SOURCE_SERVICE_NAMES_FOR_SPAN_TAG = {+ "saleor.dashboard",+ "saleor.dashboard.playground",+ "saleor.playground",+}+INTERNAL_ERROR_MESSAGE = "Internal Server Error"@@ -309,3 +312,12 @@ def format_error(error, handled_exceptions, query=None):lines.extend(line.rstrip().splitlines())result["extensions"]["exception"]["stacktrace"] = linesreturn result+++def get_source_service_name_value(header_source: str | None) -> str | None:+ default_value = "unknown_service"+ if not header_source:+ return default_value+ if header_source.lower() in AVAILABLE_SOURCE_SERVICE_NAMES_FOR_SPAN_TAG:+ return header_source.lower()+ return default_valuediff --git a/saleor/graphql/views.py b/saleor/graphql/views.pyindex 0af1ae45ba..45d202ad08 100644--- a/saleor/graphql/views.py+++ b/saleor/graphql/views.py@@ -28,7 +28,12 @@ from .api import API_PATH, schemafrom .context import clear_context, get_context_valuefrom .core.validators.query_cost import validate_query_costfrom .query_cost_map import COST_MAP-from .utils import format_error, query_fingerprint, query_identifier+from .utils import (+ format_error,+ get_source_service_name_value,+ query_fingerprint,+ query_identifier,+)from .utils.validators import check_if_query_contains_only_schemaINT_ERROR_MSG = "Int cannot represent non 32-bit signed integer value"@@ -181,6 +186,11 @@ class GraphQLView(View):span.set_tag("http.useragent", request.headers.get("user-agent", ""))span.set_tag("span.type", "web")+ source_service_name = get_source_service_name_value(+ request.headers.get("source-service-name")+ )+ span.set_tag("source.service.name", source_service_name)+main_ip_header = settings.REAL_IP_ENVIRON[0]additional_ip_headers = settings.REAL_IP_ENVIRON[1:]