Skip to content

Search: do not record invalid queries #9349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions readthedocs/search/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import structlog

from dateutil.parser import parse
from django.apps import apps
from django.conf import settings
Expand Down Expand Up @@ -161,11 +160,21 @@ def delete_old_search_queries_from_db():
@app.task(queue='web')
def record_search_query(project_slug, version_slug, query, total_results, time_string):
"""Record/update a search query for analytics."""
if not project_slug or not version_slug or not query:

log.bind(
project_slug=project_slug,
version_slug=version_slug,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log object is for the entire module --There are also log functions in other tasks, so I wonder: couldn't this produce log messages with project_slug and version_slug in unrelated calls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how structulog works (https://github.com/jrobichaud/django-structlog). Every log instance is tied to each request. Take a look at https://www.structlog.org/en/stable/getting-started.html#building-ctx

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be log = log.bind ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is required when running a normal Python application. However, we also have django-structlog and we are installing a middleware from there that I understand that "reset" the logger for each different thread/request: https://django-structlog.readthedocs.io/en/latest/getting_started.html

The same happens with the Celery integration we are using from the same module.

I don't remember from the top of my mind exactly how this works, but I think what I'm saying it's pretty close 😄 . I haven't seen log entries with invalid/inconsistent key/values pairs, so I'd assume it's working fine so far; but maybe we just haven't hit the issue yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense that it's something that this middleware would provide 👍 Thanks for a great explanation 👍


# Ignore some queries that generate errors on our code.
# I found some of these on Sentry
ignored_queries = [
"\x00",
]

if not project_slug or not version_slug or not query or query in ignored_queries:
log.debug(
'Not recording the search query.',
project_slug=project_slug,
version_slug=version_slug,
query=query,
total_results=total_results,
time=time_string,
Expand Down Expand Up @@ -199,8 +208,6 @@ def record_search_query(project_slug, version_slug, query, total_results, time_s
if not version:
log.debug(
'Not recording the search query because project does not exist.',
project_slug=project_slug,
version_slug=version_slug,
)
return

Expand Down