apm: record only select http headers in the apm.

Don't record most HTTP request and response headers in the APM, except
for the User-Agent, Referer, Save-Data, X-Forwarded-For, Accept-Language,
and Content-Type headers. Recording every HTTP header for every request
takes up a lot of space and most of them aren't very useful.
This commit is contained in:
evazion
2022-04-19 03:19:35 -05:00
parent d3294364e1
commit c187d56cce

View File

@@ -1,11 +1,20 @@
#if Danbooru.config.elastic_apm_server_url.present?
# require "elastic_apm"
#
# # https://www.elastic.co/guide/en/apm/agent/ruby/4.x/api.html#api-agent-start
# # https://www.elastic.co/guide/en/apm/agent/ruby/4.x/configuration.html
# ElasticAPM::Rails.start(
# server_url: Danbooru.config.elastic_apm_server_url,
# service_name: Danbooru.config.app_name,
# service_version: Rails.application.config.x.git_hash,
# )
#end
ENV["ELASTIC_APM_ENABLED"] = "false" unless ENV["ELASTIC_APM_SERVER_URL"].present?
Rails.application.config.to_prepare do
if ElasticAPM.running?
ElasticAPM.agent.config.service_version ||= Rails.application.config.x.git_hash
ElasticAPM.agent.config.service_node_name ||= ENV["NODE_NAME"]
ElasticAPM.add_filter(:transaction_filter) do |event|
name, _ = event.first
if name == :transaction
event.dig(:transaction, :context, :request, :url).except!(:protocol, :port)
event.dig(:transaction, :context, :request, :headers).slice!(*%w[User-Agent Referer Save-Data X-Forwarded-For Accept-Language])
event.dig(:transaction, :context, :response, :headers).slice!("Content-Type")
else
event
end
end
end
end