diff --git a/ddtrace/contrib/graphql/__init__.py b/ddtrace/contrib/graphql/__init__.py index 89350191ef1..85d33c4a02c 100644 --- a/ddtrace/contrib/graphql/__init__.py +++ b/ddtrace/contrib/graphql/__init__.py @@ -35,6 +35,15 @@ Enabling instrumentation for resolvers will produce a ``graphql.resolve`` span for every graphql field. For complex graphql queries this could produce large traces. +.. py:data:: ddtrace.config.graphql["simplify_resources"] + + To enable this option, set ``DD_TRACE_GRAPHQL_SIMPLIFY_RESOURCES`` to True. + + Default: ``False`` + + When True, resource names on GraphQL spans will be reduced to just the query or mutation name, if provided. Otherwise + the entire query will be used. + To configure the graphql integration using the ``Pin`` API:: @@ -44,6 +53,7 @@ Pin.override(graphql, service="mygraphql") """ + from ddtrace.internal.utils.importlib import require_modules diff --git a/ddtrace/contrib/internal/graphql/patch.py b/ddtrace/contrib/internal/graphql/patch.py index b54df97e520..4e5cd5dabc3 100644 --- a/ddtrace/contrib/internal/graphql/patch.py +++ b/ddtrace/contrib/internal/graphql/patch.py @@ -61,6 +61,7 @@ def get_version(): dict( _default_service=schematize_service_name("graphql"), resolvers_enabled=asbool(os.getenv("DD_TRACE_GRAPHQL_RESOLVERS_ENABLED", default=False)), + simplify_resource=asbool(os.getenv("DD_TRACE_GRAPHQL_SIMPLIFY_RESOURCES", default=False)), ), ) @@ -168,10 +169,22 @@ def _traced_execute(func, args, kwargs): else: document = get_argument_value(args, kwargs, 1, "document") source_str = _get_source_str(document) + resource = source_str + if config.graphql.simplify_resource: + # queries can look like: + # query Name(args) {... + # query Name {... + # query {... + # simplified names only make sense for the first two. the last one should just default to the full query + # split on either the query body brace or the arg parens + resource = re.split("[({]", resource, maxsplit=1)[0] + resource = resource.strip() + if resource in ("query", "mutation"): + resource = source_str with pin.tracer.trace( name="graphql.execute", - resource=source_str, + resource=resource, service=trace_utils.int_service(pin, config.graphql), span_type=SpanTypes.GRAPHQL, ) as span: