|
| 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 json |
| 16 | + |
| 17 | +import pytest |
| 18 | +from testing_support.fixtures import dt_enabled, validate_transaction_metrics |
| 19 | +from testing_support.validators.validate_span_events import validate_span_events |
| 20 | +from testing_support.validators.validate_transaction_count import ( |
| 21 | + validate_transaction_count, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="session") |
| 26 | +def target_application(): |
| 27 | + import _test_graphql |
| 28 | + |
| 29 | + return _test_graphql.target_application |
| 30 | + |
| 31 | + |
| 32 | +@dt_enabled |
| 33 | +def test_graphql_metrics_and_attrs(target_application): |
| 34 | + from graphql import __version__ as graphql_version |
| 35 | + from graphql_server import __version__ as graphql_server_version |
| 36 | + from sanic import __version__ as sanic_version |
| 37 | + |
| 38 | + FRAMEWORK_METRICS = [ |
| 39 | + ("Python/Framework/GraphQL/%s" % graphql_version, 1), |
| 40 | + ("Python/Framework/GraphQLServer/%s" % graphql_server_version, 1), |
| 41 | + ("Python/Framework/Sanic/%s" % sanic_version, 1), |
| 42 | + ] |
| 43 | + _test_scoped_metrics = [ |
| 44 | + ("GraphQL/resolve/GraphQLServer/hello", 1), |
| 45 | + ("GraphQL/operation/GraphQLServer/query/<anonymous>/hello", 1), |
| 46 | + ("Function/graphql_server.sanic.graphqlview:GraphQLView.post", 1), |
| 47 | + ] |
| 48 | + _test_unscoped_metrics = [ |
| 49 | + ("GraphQL/all", 1), |
| 50 | + ("GraphQL/GraphQLServer/all", 1), |
| 51 | + ("GraphQL/allWeb", 1), |
| 52 | + ("GraphQL/GraphQLServer/allWeb", 1), |
| 53 | + ] + _test_scoped_metrics |
| 54 | + |
| 55 | + _expected_query_operation_attributes = { |
| 56 | + "graphql.operation.type": "query", |
| 57 | + "graphql.operation.name": "<anonymous>", |
| 58 | + "graphql.operation.query": "{ hello }", |
| 59 | + } |
| 60 | + _expected_query_resolver_attributes = { |
| 61 | + "graphql.field.name": "hello", |
| 62 | + "graphql.field.parentType": "Query", |
| 63 | + "graphql.field.path": "hello", |
| 64 | + "graphql.field.returnType": "String", |
| 65 | + } |
| 66 | + |
| 67 | + @validate_span_events(exact_agents=_expected_query_operation_attributes) |
| 68 | + @validate_span_events(exact_agents=_expected_query_resolver_attributes) |
| 69 | + @validate_transaction_metrics( |
| 70 | + "query/<anonymous>/hello", |
| 71 | + "GraphQL", |
| 72 | + scoped_metrics=_test_scoped_metrics, |
| 73 | + rollup_metrics=_test_unscoped_metrics + FRAMEWORK_METRICS, |
| 74 | + ) |
| 75 | + def _test(): |
| 76 | + response = target_application.make_request( |
| 77 | + "POST", "/graphql", body=json.dumps({"query": "{ hello }"}), headers={"Content-Type": "application/json"} |
| 78 | + ) |
| 79 | + assert response.status == 200 |
| 80 | + assert "Hello!" in response.body.decode("utf-8") |
| 81 | + |
| 82 | + _test() |
| 83 | + |
| 84 | + |
| 85 | +@validate_transaction_count(0) |
| 86 | +def test_ignored_introspection_transactions(target_application): |
| 87 | + response = target_application.make_request( |
| 88 | + "POST", "/graphql", body=json.dumps({"query": "{ __schema { types { name } } }"}), headers={"Content-Type": "application/json"} |
| 89 | + ) |
| 90 | + assert response.status == 200 |
0 commit comments