-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starlette GraphQL Context Propagation (#361)
* Add graphql sync tests to fastapi * Add starlette context propagation * Move starlette tests to starlette not fastapi
- Loading branch information
1 parent
49c95c3
commit acf43cb
Showing
10 changed files
with
193 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
This module implements utilities for context propagation for tracing across threads. | ||
""" | ||
|
||
from newrelic.common.object_wrapper import function_wrapper | ||
from newrelic.core.trace_cache import trace_cache | ||
|
||
class ContextOf(object): | ||
def __init__(self, trace_cache_id): | ||
self.trace_cache = trace_cache() | ||
self.trace = self.trace_cache._cache.get(trace_cache_id) | ||
self.thread_id = None | ||
self.restore = None | ||
|
||
def __enter__(self): | ||
if self.trace: | ||
self.thread_id = self.trace_cache.current_thread_id() | ||
self.restore = self.trace_cache._cache.get(self.thread_id) | ||
self.trace_cache._cache[self.thread_id] = self.trace | ||
return self | ||
|
||
def __exit__(self, exc, value, tb): | ||
if self.restore: | ||
self.trace_cache._cache[self.thread_id] = self.restore | ||
|
||
|
||
async def context_wrapper_async(awaitable, trace_cache_id): | ||
with ContextOf(trace_cache_id): | ||
return await awaitable | ||
|
||
|
||
def context_wrapper(func, trace_cache_id): | ||
@function_wrapper | ||
def _context_wrapper(wrapped, instance, args, kwargs): | ||
with ContextOf(trace_cache_id): | ||
return wrapped(*args, **kwargs) | ||
|
||
return _context_wrapper(func) | ||
|
||
|
||
def current_thread_id(): | ||
return trace_cache().current_thread_id() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from starlette.applications import Starlette | ||
from starlette.routing import Route | ||
from testing_support.asgi_testing import AsgiTest | ||
|
||
from graphene import ObjectType, String, Schema | ||
from graphql.execution.executors.asyncio import AsyncioExecutor | ||
from starlette.graphql import GraphQLApp | ||
|
||
|
||
class Query(ObjectType): | ||
hello = String() | ||
|
||
def resolve_hello(self, info): | ||
return "Hello!" | ||
|
||
|
||
routes = [ | ||
Route("/async", GraphQLApp(executor_class=AsyncioExecutor, schema=Schema(query=Query))), | ||
Route("/sync", GraphQLApp(schema=Schema(query=Query))), | ||
] | ||
|
||
app = Starlette(routes=routes) | ||
target_application = AsgiTest(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import pytest | ||
from testing_support.fixtures import dt_enabled, validate_transaction_metrics | ||
from testing_support.validators.validate_span_events import validate_span_events | ||
|
||
@pytest.fixture(scope="session") | ||
def target_application(): | ||
import _test_graphql | ||
|
||
return _test_graphql.target_application | ||
|
||
@dt_enabled | ||
@pytest.mark.parametrize("endpoint", ("/async", "/sync")) | ||
def test_graphql_metrics_and_attrs(target_application, endpoint): | ||
from graphql import __version__ as version | ||
|
||
FRAMEWORK_METRICS = [ | ||
("Python/Framework/GraphQL/%s" % version, 1), | ||
] | ||
_test_scoped_metrics = [ | ||
("GraphQL/resolve/GraphQL/hello", 1), | ||
("GraphQL/operation/GraphQL/query/<anonymous>/hello", 1), | ||
] | ||
_test_unscoped_metrics = [ | ||
("GraphQL/all", 1), | ||
("GraphQL/GraphQL/all", 1), | ||
("GraphQL/allWeb", 1), | ||
("GraphQL/GraphQL/allWeb", 1), | ||
] + _test_scoped_metrics | ||
|
||
_expected_query_operation_attributes = { | ||
"graphql.operation.type": "query", | ||
"graphql.operation.name": "<anonymous>", | ||
"graphql.operation.query": "{ hello }", | ||
} | ||
_expected_query_resolver_attributes = { | ||
"graphql.field.name": "hello", | ||
"graphql.field.parentType": "Query", | ||
"graphql.field.path": "hello", | ||
"graphql.field.returnType": "String", | ||
} | ||
|
||
@validate_span_events(exact_agents=_expected_query_operation_attributes) | ||
@validate_span_events(exact_agents=_expected_query_resolver_attributes) | ||
@validate_transaction_metrics( | ||
"query/<anonymous>/hello", | ||
"GraphQL", | ||
scoped_metrics=_test_scoped_metrics, | ||
rollup_metrics=_test_unscoped_metrics + FRAMEWORK_METRICS, | ||
) | ||
def _test(): | ||
response = target_application.make_request("POST", endpoint, body=json.dumps({"query": "{ hello }"}), headers={"Content-Type": "application/json"}) | ||
assert response.status == 200 | ||
assert "Hello!" in response.body.decode("utf-8") | ||
|
||
_test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters