Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Excluded default resolvers and introspection fields from OpenTracing
Mikail KOCAK39ea82975273
diff --git a/saleor/core/tracing.py b/saleor/core/tracing.pynew file mode 100644index 0000000000..a610b53ed9--- /dev/null+++ b/saleor/core/tracing.py@@ -0,0 +1,31 @@+from functools import partial++from graphene.types.resolver import default_resolver+from graphql import ResolveInfo+++def should_trace(info: ResolveInfo) -> bool:+ if info.field_name not in info.parent_type.fields:+ return False++ resolver = info.parent_type.fields[info.field_name].resolver+ return not (+ resolver is None+ or is_default_resolver(resolver)+ or is_introspection_field(info)+ )+++def is_introspection_field(info: ResolveInfo):+ for path in info.path:+ if isinstance(path, str) and path.startswith("__"):+ return True+ return False+++def is_default_resolver(resolver):+ while isinstance(resolver, partial):+ resolver = resolver.func+ if resolver is default_resolver:+ return True+ return resolver is default_resolverdiff --git a/saleor/graphql/middleware.py b/saleor/graphql/middleware.pyindex f25f546c3b..9ea972c2a1 100644--- a/saleor/graphql/middleware.py+++ b/saleor/graphql/middleware.py@@ -6,9 +6,11 @@ from django.conf import settingsfrom django.contrib.auth.models import AnonymousUserfrom django.utils.functional import SimpleLazyObjectfrom graphene_django.settings import graphene_settings+from graphql import ResolveInfofrom graphql_jwt.middleware import JSONWebTokenMiddlewarefrom ..account.models import ServiceAccount+from ..core.tracing import should_tracefrom .views import API_PATH, GraphQLView@@ -37,25 +39,18 @@ def jwt_middleware(get_response):return _jwt_middleware-def should_trace(info):- if info.field_name not in info.parent_type.fields:- return False-- return not info.field_name.startswith("__")--class OpentracingGrapheneMiddleware:@staticmethod- def resolve(next, root, info, **kwargs):- operation = (- f"{info.operation.operation}.{info.parent_type.name}.{info.field_name}"- )+ def resolve(next_, root, info: ResolveInfo, **kwargs):+ if not should_trace(info):+ return next_(root, info, **kwargs)+ operation = f"{info.parent_type.name}.{info.field_name}"with ot.global_tracer().start_active_span(operation_name=operation) as scope:span = scope.spanspan.set_tag(ot_tags.COMPONENT, "graphql")span.set_tag("graphql.parent_type", info.parent_type.name)span.set_tag("graphql.field_name", info.field_name)- return next(root, info, **kwargs)+ return next_(root, info, **kwargs)def get_service_account(auth_token) -> Optional[ServiceAccount]:diff --git a/saleor/product/utils/availability.py b/saleor/product/utils/availability.pyindex 910cb1d0a4..b561e59ada 100644--- a/saleor/product/utils/availability.py+++ b/saleor/product/utils/availability.py@@ -148,8 +148,6 @@ def get_product_availability()is_on_sale = product.is_visible and discount is not None- country = country if country is not None else settings.DEFAULT_COUNTRY- is_available = product.is_visible and is_product_in_stock(product, country)return ProductAvailability(on_sale=is_on_sale,price_range=discounted,