|
| 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 | +from newrelic.api.asgi_application import wrap_asgi_application |
| 16 | +from newrelic.api.error_trace import ErrorTrace |
| 17 | +from newrelic.api.graphql_trace import GraphQLOperationTrace |
| 18 | +from newrelic.api.transaction import current_transaction |
| 19 | +from newrelic.api.transaction_name import TransactionNameWrapper |
| 20 | +from newrelic.common.object_names import callable_name |
| 21 | +from newrelic.common.object_wrapper import wrap_function_wrapper |
| 22 | +from newrelic.core.graphql_utils import graphql_statement |
| 23 | +from newrelic.hooks.framework_graphql import ( |
| 24 | + framework_version as graphql_framework_version, |
| 25 | +) |
| 26 | +from newrelic.hooks.framework_graphql import ignore_graphql_duplicate_exception |
| 27 | + |
| 28 | + |
| 29 | +def framework_details(): |
| 30 | + import strawberry |
| 31 | + |
| 32 | + return ("Strawberry", getattr(strawberry, "__version__", None)) |
| 33 | + |
| 34 | + |
| 35 | +def bind_execute(query, *args, **kwargs): |
| 36 | + return query |
| 37 | + |
| 38 | + |
| 39 | +def wrap_execute_sync(wrapped, instance, args, kwargs): |
| 40 | + transaction = current_transaction() |
| 41 | + |
| 42 | + if not transaction: |
| 43 | + return wrapped(*args, **kwargs) |
| 44 | + |
| 45 | + try: |
| 46 | + query = bind_execute(*args, **kwargs) |
| 47 | + except TypeError: |
| 48 | + return wrapped(*args, **kwargs) |
| 49 | + |
| 50 | + framework = framework_details() |
| 51 | + transaction.add_framework_info(name=framework[0], version=framework[1]) |
| 52 | + transaction.add_framework_info(name="GraphQL", version=graphql_framework_version()) |
| 53 | + |
| 54 | + if hasattr(query, "body"): |
| 55 | + query = query.body |
| 56 | + |
| 57 | + transaction.set_transaction_name(callable_name(wrapped), "GraphQL", priority=10) |
| 58 | + |
| 59 | + with GraphQLOperationTrace() as trace: |
| 60 | + trace.statement = graphql_statement(query) |
| 61 | + with ErrorTrace(ignore=ignore_graphql_duplicate_exception): |
| 62 | + return wrapped(*args, **kwargs) |
| 63 | + |
| 64 | + |
| 65 | +async def wrap_execute(wrapped, instance, args, kwargs): |
| 66 | + transaction = current_transaction() |
| 67 | + |
| 68 | + if not transaction: |
| 69 | + return await wrapped(*args, **kwargs) |
| 70 | + |
| 71 | + try: |
| 72 | + query = bind_execute(*args, **kwargs) |
| 73 | + except TypeError: |
| 74 | + return await wrapped(*args, **kwargs) |
| 75 | + |
| 76 | + framework = framework_details() |
| 77 | + transaction.add_framework_info(name=framework[0], version=framework[1]) |
| 78 | + transaction.add_framework_info(name="GraphQL", version=graphql_framework_version()) |
| 79 | + |
| 80 | + if hasattr(query, "body"): |
| 81 | + query = query.body |
| 82 | + |
| 83 | + transaction.set_transaction_name(callable_name(wrapped), "GraphQL", priority=10) |
| 84 | + |
| 85 | + with GraphQLOperationTrace() as trace: |
| 86 | + trace.statement = graphql_statement(query) |
| 87 | + with ErrorTrace(ignore=ignore_graphql_duplicate_exception): |
| 88 | + return await wrapped(*args, **kwargs) |
| 89 | + |
| 90 | + |
| 91 | +def bind_from_resolver(field, *args, **kwargs): |
| 92 | + return field |
| 93 | + |
| 94 | + |
| 95 | +def wrap_from_resolver(wrapped, instance, args, kwargs): |
| 96 | + result = wrapped(*args, **kwargs) |
| 97 | + |
| 98 | + try: |
| 99 | + field = bind_from_resolver(*args, **kwargs) |
| 100 | + except TypeError: |
| 101 | + pass |
| 102 | + else: |
| 103 | + if hasattr(field, "base_resolver"): |
| 104 | + if hasattr(field.base_resolver, "wrapped_func"): |
| 105 | + resolver_name = callable_name(field.base_resolver.wrapped_func) |
| 106 | + result = TransactionNameWrapper(result, resolver_name, "GraphQL", priority=13) |
| 107 | + |
| 108 | + return result |
| 109 | + |
| 110 | + |
| 111 | +def instrument_strawberry_schema(module): |
| 112 | + if hasattr(module, "Schema"): |
| 113 | + if hasattr(module.Schema, "execute"): |
| 114 | + wrap_function_wrapper(module, "Schema.execute", wrap_execute) |
| 115 | + if hasattr(module.Schema, "execute_sync"): |
| 116 | + wrap_function_wrapper(module, "Schema.execute_sync", wrap_execute_sync) |
| 117 | + |
| 118 | + |
| 119 | +def instrument_strawberry_asgi(module): |
| 120 | + if hasattr(module, "GraphQL"): |
| 121 | + wrap_asgi_application(module, "GraphQL.__call__", framework=framework_details()) |
| 122 | + |
| 123 | + |
| 124 | +def instrument_strawberry_schema_converter(module): |
| 125 | + if hasattr(module, "GraphQLCoreConverter"): |
| 126 | + if hasattr(module.GraphQLCoreConverter, "from_resolver"): |
| 127 | + wrap_function_wrapper(module, "GraphQLCoreConverter.from_resolver", wrap_from_resolver) |
0 commit comments