strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.87k stars 511 forks source link

Datadog Extension AssertionError #3150

Open ramanyeutukh opened 9 months ago

ramanyeutukh commented 9 months ago

If you make request with wrong Accept header, instead of MissingQueryError you will get AssertionError error from Datadog extension.

Describe the Bug

Call without Datadog extension

curl -H 'Accept: application/json' http://localhost:8000/graphql -i
HTTP/1.1 400 Bad Request
date: Fri, 13 Oct 2023 13:16:33 GMT
server: uvicorn
content-length: 37
content-type: text/plain; charset=utf-8

No GraphQL query found in the request⏎

And with Datadog extension

curl -H 'Accept: application/json' http://localhost:8000/graphql -i
HTTP/1.1 500 Internal Server Error
date: Fri, 13 Oct 2023 12:42:25 GMT
server: uvicorn
content-length: 21
content-type: text/plain; charset=utf-8

Internal Server Error

Error trace:

finishing span name='fastapi.request' id=9469625826906337360 trace_id=14479422442390738536 parent_id=None service='backend2.0' resource='GET /graphql' type='web' start=1697134642.916571 end=1697134642.9209309 duration=0.004359981 error=1 tags={'_dd.p.dm': '-1', 'asgi.spec_version': '2.3', 'asgi.version': '3.0', 'component': 'fastapi', 'env': <Environment.DEV: 'dev'>, 'error.message': '', 'error.stack': 'Traceback (most recent call last):
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/ddtrace/contrib/asgi/middleware.py", line 265, in __call__
    return await self.app(scope, receive, wrapped_send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/middleware/cors.py", line 83, in __call__
    await self.app(scope, receive, send)
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 273, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 190, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/fastapi/router.py", line 207, in handle_http_get
    return await self.run(
           ^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/http/async_base_view.py", line 176, in run
    result = await self.execute_operation(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/http/async_base_view.py", line 115, in execute_operation
    return await self.schema.execute(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/schema/schema.py", line 256, in execute
    result = await execute(
             ^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/schema/execute.py", line 86, in execute
    async with extensions_runner.operation():
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/extensions/context.py", line 191, in __aenter__
    await self.run_hooks_async()
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/extensions/context.py", line 177, in run_hooks_async
    hook.initialized_hook.__next__()  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/extensions/tracing/datadog.py", line 76, in on_operation
    resource=self._resource_name,
             ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/opt/pysetup/.venv/lib/python3.11/site-packages/strawberry/extensions/tracing/datadog.py", line 30, in _resource_name
    assert self.execution_context.query
AssertionError
', 'error.type': 'builtins.AssertionError', 'http.method': 'GET', 'http.route': '/graphql', 'http.status_code': '500', 'http.url': 'http://3fzc6of2ae.execute-api.us-east-1.amazonaws.com/graphql', 'http.useragent': 'Amazon CloudFront', 'http.version': '1.1', 'language': 'python', 'runtime-id': 'e2e59925f37a43c5980826344e50d1c3', 'span.kind': 'server', 'version': '0.0.0'} metrics={'_dd.agent_psr': 1.0, '_dd.top_level': 1, '_dd.tracer_kr': 1.0, '_sampling_priority_v1': 1, 'process_id': 1} (enabled:True)

System Information

Additional Context

Line with assert https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/extensions/tracing/datadog.py#L30 Possible solution: move that check before context manager https://github.com/strawberry-graphql/strawberry/blob/a4aa93e6be75c806457f97c7a60bcb3d446ca422/strawberry/schema/execute.py#L89

Upvote & Fund

Fund with Polar

aiguofer commented 3 months ago

We have randomly seen these exceptions on our server logs. I just tested this and can confirm we're seeing the same issue.

patrick91 commented 3 months ago

@aiguofer happy to review a PR if you have time 😊