Open mullman-mission opened 1 year ago
Update: poked around at this some more, and it seems to only occur when we send a GET request to our GraphQL server which returns GraphiQL, so I think that https://github.com/newrelic/newrelic-python-agent/blob/main/newrelic/hooks/component_graphqlserver.py#L38 should just return wrapped if query
is None
, or don't hook in to calls other than POST
.
Hello--I cannot seem to be able to replicate this, so would you be able to supply a sample app?
Hello, I too have encountered this error in my application and prepared a sample app:
from flask import Flask
from flask_graphql import GraphQLView
from graphene import ObjectType, String, Schema
class Query(ObjectType):
hello = String(first_name=String(default_value="stranger"))
goodbye = String()
def resolve_hello(root, info, first_name):
return f"Hello {first_name}!"
def resolve_goodbye(root, info):
return "See ya!"
schema = Schema(query=Query)
app = Flask(__name__)
app.add_url_rule(
"/graphql",
view_func=GraphQLView.as_view(
"graphql",
schema=schema,
graphiql=True,
),
)
if __name__ == "__main__":
app.run()
I put that in a file called example.py
inside a new virtualenv created on an M1 Mac running macOS 13.3 with Python 3.11 installed from the Python website. Inside the virtualenv, I installed dependencies with this
pip install flask flask-graphql graphene newrelic
and ran the app like this
FLASK_DEBUG=True NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program python example.py
I used a newly created New Relic application/service and a default newrelic.ini
downloaded for it from the panel. By default, Flask will serve on 127.0.0.1:5000, so I hit 127.0.0.1:5000/graphql
. On the first load, it worked (I haven't had time to investigate why it works on first load). But when I refreshed, just like with my app, it crashed with the message mentioned above
...
File "lib/python3.11/site-packages/newrelic/core/database_utils.py", line 106, in _obfuscate_sql
sql = quotes_re.sub('?', sql)
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'
The first request after startup, both in my app and in this example, is always okay. It doesn't matter whether it's a browser request to load GraphiQL or an actual GraphQL POST request containing the query. Only after that first request does the error show up on an attempt to load GraphiQL.
I hope this helps 🙂
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Is there anything I can do to help resolve this? From my limited investigation, what mullman-mission suggested seems to be the cleanest possible solution. Since May, we patch the library on our deployments to do exactly that and encountered no issues. However, patching like this is of course sub-optimal. I can prepare the PR if needed...
We're still dealing with this error and it's rather annoying. @maroskucera I'd at least love your deployment patch solution.
After each update of the library, we find the wrap_get_response
function in the newrelic/hooks/component_graphqlserver.py
file in our virtualenv's site-packages and add
if query is None:
return wrapped(*args, **kwargs)
after
transaction.set_transaction_name(callable_name(wrapped), "GraphQL", priority=10)
which is at line 40 as of v8.11.0. What is noteworthy here is that this stops before creating a trace, which is fine for us. I tried using
if query is None:
query = ""
and that ran okay as well, that should create a trace if you want/need that. However, I tested it only briefly and only locally.
Not sure if related but we are also seeing the same(ish) error message when our gunicorn workers times out in the newrelic part of the code.
We've recently bumped our newrelic python package from 7.4.0.172 to 8.7.1 and are now frequently encountering exceptions when our Flask application processes HTTP requests.
Description
Expected Behavior
For it to not throw an exception
Troubleshooting or NR Diag results
Steps to Reproduce
I unfortunately haven't had time to put together a sample app that reproduces this issue and I cannot share the code base behind this. I may be able to find some time this week to do so, though, unless you all have an idea what's going on.
Your Environment
The main players in this scenario seems to be Flask 2.2.3, Flask-SQLAlchemy 3.0.3, and SQLAlchemy 1.4.46. Python 3.11.2.
Additional context
Sorry this is an extremely dry bug report, let me know if I should include any info on dependency versions that seem related to this crash.