|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import functools |
| 16 | +import sys |
| 17 | +from inspect import isawaitable |
| 18 | + |
| 19 | +from newrelic.api.error_trace import ErrorTrace |
| 20 | +from newrelic.api.graphql_trace import GraphQLOperationTrace |
| 21 | +from newrelic.api.transaction import current_transaction |
| 22 | +from newrelic.common.object_names import callable_name |
| 23 | +from newrelic.common.object_wrapper import wrap_function_wrapper |
| 24 | +from newrelic.common.package_version_utils import get_package_version |
| 25 | +from newrelic.common.signature import bind_args |
| 26 | +from newrelic.core.graphql_utils import graphql_statement |
| 27 | +from newrelic.hooks.framework_graphql import GRAPHQL_VERSION, ignore_graphql_duplicate_exception |
| 28 | + |
| 29 | +GRAPHENE_DJANGO_VERSION = get_package_version("graphene_django") |
| 30 | +graphene_django_version_tuple = tuple(map(int, GRAPHENE_DJANGO_VERSION.split("."))) |
| 31 | + |
| 32 | + |
| 33 | +# Implementation from `newrelic.hooks.framework_graphql_py3` |
| 34 | +def nr_coro_execute_graphql_request_wrapper(wrapped, trace, ignore, result): |
| 35 | + @functools.wraps(wrapped) |
| 36 | + async def _nr_coro_execute_graphql_request_wrapper(): |
| 37 | + try: |
| 38 | + with ErrorTrace(ignore=ignore): |
| 39 | + result_ = await result |
| 40 | + except: |
| 41 | + trace.__exit__(*sys.exc_info()) |
| 42 | + raise |
| 43 | + else: |
| 44 | + trace.__exit__(None, None, None) |
| 45 | + return result_ |
| 46 | + |
| 47 | + return _nr_coro_execute_graphql_request_wrapper() |
| 48 | + |
| 49 | + |
| 50 | +def wrap_GraphQLView_execute_graphql_request(wrapped, instance, args, kwargs): |
| 51 | + transaction = current_transaction() |
| 52 | + |
| 53 | + if not transaction: |
| 54 | + return wrapped(*args, **kwargs) |
| 55 | + |
| 56 | + transaction.add_framework_info(name="GraphQL", version=GRAPHQL_VERSION) |
| 57 | + transaction.add_framework_info(name="GrapheneDjango", version=GRAPHENE_DJANGO_VERSION) |
| 58 | + bound_args = bind_args(wrapped, args, kwargs) |
| 59 | + |
| 60 | + try: |
| 61 | + schema = instance.schema.graphql_schema |
| 62 | + query = bound_args.get("query", None) |
| 63 | + except TypeError: |
| 64 | + return wrapped(*args, **kwargs) |
| 65 | + |
| 66 | + transaction.set_transaction_name(callable_name(wrapped), "GraphQL", priority=11) |
| 67 | + |
| 68 | + trace = GraphQLOperationTrace() |
| 69 | + |
| 70 | + trace.statement = graphql_statement(query) |
| 71 | + trace.product = "GrapheneDjango" |
| 72 | + |
| 73 | + # Handle Schemas created from frameworks |
| 74 | + if hasattr(schema, "_nr_framework"): |
| 75 | + framework = schema._nr_framework |
| 76 | + transaction.add_framework_info(name=framework[0], version=framework[1]) |
| 77 | + |
| 78 | + # Trace must be manually started and stopped to ensure it exists prior to and during the entire duration of the query. |
| 79 | + # Otherwise subsequent instrumentation will not be able to find an operation trace and will have issues. |
| 80 | + trace.__enter__() |
| 81 | + try: |
| 82 | + with ErrorTrace(ignore=ignore_graphql_duplicate_exception): |
| 83 | + result = wrapped(*args, **kwargs) |
| 84 | + except Exception: |
| 85 | + # Execution finished synchronously, exit immediately. |
| 86 | + trace.__exit__(*sys.exc_info()) |
| 87 | + raise |
| 88 | + else: |
| 89 | + trace.set_transaction_name(priority=14) |
| 90 | + if isawaitable(result): |
| 91 | + # Asynchronous implementations |
| 92 | + # Return a coroutine that handles closing the operation trace |
| 93 | + return nr_coro_execute_graphql_request_wrapper(wrapped, trace, ignore_graphql_duplicate_exception, result) |
| 94 | + else: |
| 95 | + # Execution finished synchronously, exit immediately. |
| 96 | + trace.__exit__(None, None, None) |
| 97 | + return result |
| 98 | + |
| 99 | + |
| 100 | +def instrument_graphene_django_views(module): |
| 101 | + if hasattr(module, "GraphQLView") and hasattr(module.GraphQLView, "execute_graphql_request"): |
| 102 | + wrap_function_wrapper(module, "GraphQLView.execute_graphql_request", wrap_GraphQLView_execute_graphql_request) |
0 commit comments