posts: fix search queries not being logged to NewRelic in some cases (#4900)

Fix the /posts index controller not logging the normalized search query
to NewRelic when the search failed, either because of a tag limit error,
a search timeout, or a RSS feed rate limit error.

Also don't log the number of search results when it's an API request or
failed search. This is to avoid doing a potentially slow full post count
when it's not otherwise needed.
This commit is contained in:
evazion
2022-01-11 13:22:24 -06:00
parent 698d732667
commit 1518c3c4be
5 changed files with 30 additions and 28 deletions

View File

@@ -4,6 +4,8 @@ class PostsController < ApplicationController
respond_to :html, :xml, :json, :js respond_to :html, :xml, :json, :js
layout "sidebar" layout "sidebar"
before_action :log_search_query, only: :index
after_action :log_search_count, only: :index, if: -> { request.format.html? && response.successful? }
rate_limit :index, rate: 1.0/2.seconds, burst: 50, if: -> { request.format.atom? }, key: "posts:index.atom" rate_limit :index, rate: 1.0/2.seconds, burst: 50, if: -> { request.format.atom? }, key: "posts:index.atom"
def index def index
@@ -13,16 +15,11 @@ class PostsController < ApplicationController
format.html { redirect_to(@post) } format.html { redirect_to(@post) }
end end
elsif params[:random].to_s.truthy? elsif params[:random].to_s.truthy?
post_set = PostSets::Post.new(params[:tags], params[:page], params[:limit], format: request.format.symbol)
query = "#{post_set.normalized_query.to_s} random:#{post_set.per_page}".strip query = "#{post_set.normalized_query.to_s} random:#{post_set.per_page}".strip
redirect_to posts_path(tags: query, page: params[:page], limit: params[:limit], format: request.format.symbol) redirect_to posts_path(tags: query, page: params[:page], limit: params[:limit], format: request.format.symbol)
else else
tag_query = params[:tags] || params.dig(:post, :tags)
@show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy?
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, show_votes: @show_votes)
@preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostGalleryComponent::DEFAULT_SIZE @preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostGalleryComponent::DEFAULT_SIZE
@posts = authorize @post_set.posts, policy_class: PostPolicy @posts = authorize post_set.posts, policy_class: PostPolicy
@post_set.log!
respond_with(@posts) do |format| respond_with(@posts) do |format|
format.atom format.atom
end end
@@ -118,6 +115,29 @@ class PostsController < ApplicationController
private private
def post_set
@post_set ||= begin
tag_query = params[:tags] || params.dig(:post, :tags)
show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy?
PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, show_votes: show_votes)
end
end
def log_search_query
DanbooruLogger.add_attributes("search", {
query: post_set.normalized_query.to_s,
page: post_set.current_page,
limit: post_set.per_page,
term_count: post_set.normalized_query.terms.count,
tag_count: post_set.normalized_query.tags.count,
metatag_count: post_set.normalized_query.metatags.count,
})
end
def log_search_count
DanbooruLogger.add_attributes("search", { count: post_set.post_count, })
end
def respond_with_post_after_update(post) def respond_with_post_after_update(post)
respond_with(post) do |format| respond_with(post) do |format|
format.html do format.html do

View File

@@ -968,7 +968,7 @@ class PostQueryBuilder
concerning :CountMethods do concerning :CountMethods do
def post_count def post_count
fast_count @post_count ||= fast_count
end end
# Return an estimate of the number of posts returned by the search. By # Return an estimate of the number of posts returned by the search. By

View File

@@ -137,24 +137,6 @@ module PostSets
end end
end end
def search_stats
{
query: normalized_query.to_s,
count: post_count,
page: current_page,
limit: per_page,
term_count: normalized_query.terms.count,
tag_count: normalized_query.tags.count,
metatag_count: normalized_query.metatags.count,
censored_posts: censored_posts.count,
hidden_posts: hidden_posts.count,
}
end
def log!
DanbooruLogger.add_attributes("search", search_stats)
end
concerning :TagListMethods do concerning :TagListMethods do
def related_tags def related_tags
if query.is_wildcard_search? if query.is_wildcard_search?

View File

@@ -134,7 +134,7 @@
<%= render PopupMenuComponent.new(classes: "post-preview-options-menu") do |menu| %> <%= render PopupMenuComponent.new(classes: "post-preview-options-menu") do |menu| %>
<% menu.item do %> <% menu.item do %>
<% if @show_votes %> <% if @post_set.show_votes %>
<%= link_to "Hide scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: nil, size: params[:size]), class: "post-preview-hide-votes", rel: "nofollow" %> <%= link_to "Hide scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: nil, size: params[:size]), class: "post-preview-hide-votes", rel: "nofollow" %>
<% else %> <% else %>
<%= link_to "Show scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: true, size: params[:size]), class: "post-preview-show-votes", rel: "nofollow" %> <%= link_to "Show scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: true, size: params[:size]), class: "post-preview-show-votes", rel: "nofollow" %>

View File

@@ -458,7 +458,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "for a search that times out" do context "for a search that times out" do
context "during numbered pagination" do context "during numbered pagination" do
should "show the search timeout error page" do should "show the search timeout error page" do
Post::const_get(:ActiveRecord_Relation).any_instance.stubs(:records).raises(ActiveRecord::QueryCanceled) PostSets::Post.any_instance.stubs(:posts).raises(ActiveRecord::QueryCanceled)
get posts_path(page: "1") get posts_path(page: "1")
assert_response 500 assert_response 500
@@ -468,7 +468,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "during sequential pagination" do context "during sequential pagination" do
should "show the search timeout error page" do should "show the search timeout error page" do
Post::const_get(:ActiveRecord_Relation).any_instance.stubs(:records).raises(ActiveRecord::QueryCanceled) PostSets::Post.any_instance.stubs(:posts).raises(ActiveRecord::QueryCanceled)
get posts_path(page: "a0") get posts_path(page: "a0")
assert_response 500 assert_response 500