Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Tracing of database queries
Tomasz Szymański752d94529c8f
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.pyindex 8610e3eb46..706392212e 100644--- a/saleor/graphql/views.py+++ b/saleor/graphql/views.py@@ -1,9 +1,12 @@import jsonimport logging+import timeimport tracebackfrom typing import Any, Dict, List, Optional, Tuple, Union+import opentracingfrom django.conf import settings+from django.db import connectionfrom django.http import HttpRequest, HttpResponseNotAllowed, JsonResponsefrom django.shortcuts import render_to_responsefrom django.urls import reverse@@ -26,6 +29,27 @@ unhandled_errors_logger = logging.getLogger("saleor.graphql.errors.unhandled")handled_errors_logger = logging.getLogger("saleor.graphql.errors.handled")+def tracing_wrapper(execute, sql, params, many, context):+ if not settings.ENABLE_OPENTRACING:+ return execute(sql, params, many, context)++ start = time.time()+ with opentracing.tracer.start_span(operation_name="query") as span:+ span.set_tag("sql", sql)+ span.set_tag("many", many)+ try:+ result = execute(sql, params, many, context)+ except Exception as e:+ span.set_tag("status", "error")+ span.set_tag("exception", e)+ raise+ else:+ span.set_tag("status", "ok")+ return result+ finally:+ span.set_tag("duration", time.time() - start)++class GraphQLView(View):# This class is our implementation of `graphene_django.views.GraphQLView`,# which was extended to support the following features:@@ -164,14 +188,15 @@ class GraphQLView(View):# executor is not a valid argument in all backendsextra_options["executor"] = self.executortry:- return document.execute( # type: ignore- root=self.get_root_value(),- variables=variables,- operation_name=operation_name,- context=request,- middleware=self.middleware,- **extra_options,- )+ with connection.execute_wrapper(tracing_wrapper):+ return document.execute( # type: ignore+ root=self.get_root_value(),+ variables=variables,+ operation_name=operation_name,+ context=request,+ middleware=self.middleware,+ **extra_options,+ )except Exception as e:return ExecutionResult(errors=[e], invalid=True)