|
| 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 | +import pytest |
| 17 | +from testing_support.fixtures import dt_enabled, validate_transaction_metrics |
| 18 | +from testing_support.validators.validate_span_events import validate_span_events |
| 19 | + |
| 20 | +@pytest.fixture(scope="session") |
| 21 | +def target_application(): |
| 22 | + import _test_graphql |
| 23 | + |
| 24 | + return _test_graphql.target_application |
| 25 | + |
| 26 | +@dt_enabled |
| 27 | +@pytest.mark.parametrize("endpoint", ("/async", "/sync")) |
| 28 | +def test_graphql_metrics_and_attrs(target_application, endpoint): |
| 29 | + from graphql import __version__ as version |
| 30 | + |
| 31 | + FRAMEWORK_METRICS = [ |
| 32 | + ("Python/Framework/GraphQL/%s" % version, 1), |
| 33 | + ] |
| 34 | + _test_scoped_metrics = [ |
| 35 | + ("GraphQL/resolve/GraphQL/hello", 1), |
| 36 | + ("GraphQL/operation/GraphQL/query/<anonymous>/hello", 1), |
| 37 | + ] |
| 38 | + _test_unscoped_metrics = [ |
| 39 | + ("GraphQL/all", 1), |
| 40 | + ("GraphQL/GraphQL/all", 1), |
| 41 | + ("GraphQL/allWeb", 1), |
| 42 | + ("GraphQL/GraphQL/allWeb", 1), |
| 43 | + ] + _test_scoped_metrics |
| 44 | + |
| 45 | + _expected_query_operation_attributes = { |
| 46 | + "graphql.operation.type": "query", |
| 47 | + "graphql.operation.name": "<anonymous>", |
| 48 | + "graphql.operation.query": "{ hello }", |
| 49 | + } |
| 50 | + _expected_query_resolver_attributes = { |
| 51 | + "graphql.field.name": "hello", |
| 52 | + "graphql.field.parentType": "Query", |
| 53 | + "graphql.field.path": "hello", |
| 54 | + "graphql.field.returnType": "String", |
| 55 | + } |
| 56 | + |
| 57 | + @validate_span_events(exact_agents=_expected_query_operation_attributes) |
| 58 | + @validate_span_events(exact_agents=_expected_query_resolver_attributes) |
| 59 | + @validate_transaction_metrics( |
| 60 | + "query/<anonymous>/hello", |
| 61 | + "GraphQL", |
| 62 | + scoped_metrics=_test_scoped_metrics, |
| 63 | + rollup_metrics=_test_unscoped_metrics + FRAMEWORK_METRICS, |
| 64 | + ) |
| 65 | + def _test(): |
| 66 | + response = target_application.make_request("POST", endpoint, body=json.dumps({"query": "{ hello }"}), headers={"Content-Type": "application/json"}) |
| 67 | + assert response.status == 200 |
| 68 | + assert "Hello!" in response.body.decode("utf-8") |
| 69 | + |
| 70 | + _test() |
0 commit comments