Skip to content

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

Opentracing graphene middleware

Tomasz Szymańskid893c6f73ea8
diff --git a/.isort.cfg b/.isort.cfg
index d4e80f6709..c3d4d0f6fd 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,bleach,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,jwt,markdown,measurement,mptt,oauthlib,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
+known_third_party = PIL,babel,bleach,braintree,celery,ddtrace,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,jwt,markdown,measurement,mptt,oauthlib,opentracing,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
diff --git a/requirements.txt b/requirements.txt
index 87897b6bbb..93ad26cd2f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -16,6 +16,7 @@ certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
cssselect2==0.2.2
+ddtrace==0.32.2
defusedxml==0.6.0
dj-database-url==0.5.0
dj-email-url==0.2.0
@@ -71,6 +72,7 @@ maxminddb-geolite2==2018.703
measurement==3.2.0
mpmath==1.1.0
oauthlib==3.1.0
+opentracing==2.3.0
phonenumberslite==8.11.1
pillow==6.2.1
prices==1.0.0
diff --git a/saleor/graphql/middleware.py b/saleor/graphql/middleware.py
index ec819b73a2..d7b4b742a3 100644
--- a/saleor/graphql/middleware.py
+++ b/saleor/graphql/middleware.py
@@ -1,5 +1,6 @@
from typing import Optional
+import opentracing
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.utils.functional import SimpleLazyObject
@@ -35,6 +36,24 @@ 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):
+ if settings.ENABLE_OPENTRACING and should_trace(info):
+ with opentracing.tracer.start_span(operation_name=info.field_name) as span:
+ span.set_tag("parent-type", info.parent_type.name)
+ return next(root, info, **kwargs)
+
+ return next(root, info, **kwargs)
+
+
def get_service_account(auth_token) -> Optional[ServiceAccount]:
qs = ServiceAccount.objects.filter(tokens__auth_token=auth_token, is_active=True)
return qs.first()
diff --git a/saleor/settings.py b/saleor/settings.py
index bffd7afb5f..92e89c1d85 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -5,6 +5,7 @@ import warnings
import dj_database_url
import dj_email_url
import sentry_sdk
+from ddtrace.opentracer import Tracer, set_global_tracer
from django.contrib.messages import constants as messages
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import gettext_lazy as _, pgettext_lazy
@@ -12,6 +13,22 @@ from django_prices.utils.formatting import get_currency_fraction
from sentry_sdk.integrations.django import DjangoIntegration
+def init_tracer(service_name):
+ config = {
+ "agent_hostname": "localhost",
+ "agent_port": 8126,
+ }
+ tracer = Tracer(service_name, config=config)
+ set_global_tracer(tracer)
+ return tracer
+
+
+init_tracer("saleor")
+
+
+ENABLE_OPENTRACING = True
+
+
def get_list(text):
return [item.strip() for item in text.split(",")]
@@ -185,6 +202,7 @@ TEMPLATES = [
SECRET_KEY = os.environ.get("SECRET_KEY")
MIDDLEWARE = [
+ # 'django_opentracing.OpenTracingMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.middleware.common.CommonMiddleware",
"saleor.core.middleware.discounts",
@@ -526,6 +544,7 @@ if SENTRY_DSN:
sentry_sdk.init(dsn=SENTRY_DSN, integrations=[DjangoIntegration()])
GRAPHENE = {
+ "MIDDLEWARE": ("saleor.graphql.middleware.OpentracingGrapheneMiddleware",),
"RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST": True,
"RELAY_CONNECTION_MAX_LIMIT": 100,
}