Parse Google Analytics failed response before reporting to Sentry (#2354)

This commit parses failed GA responses to JSON before reporting to
Sentry. This makes Sentry's PII filtering smarter, redacting only
specific keys from the response, instead of the whole string.
This commit is contained in:
Vinicius Brasil 2022-10-19 09:22:52 -03:00 committed by GitHub
parent ad10721d46
commit 95aafb477d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,8 +42,8 @@ defmodule Plausible.Google.HTTP do
{:ok, report} <- convert_to_maps(report) do
{:ok, {report, token}}
else
{:ok, %{status: _non_http_200, body: body}} ->
Sentry.Context.set_extra_context(%{google_analytics_response: body})
{:ok, %{status: _non_http_200, body: _body} = response} ->
report_failed_request_to_sentry(response)
{:error, :request_failed}
{:error, cause} ->
@ -51,6 +51,16 @@ defmodule Plausible.Google.HTTP do
end
end
defp report_failed_request_to_sentry(%{status: status, body: body}) do
case Jason.decode(body) do
{:ok, %{} = body} ->
Sentry.Context.set_extra_context(%{ga_response: %{body: body, status: status}})
_error ->
Sentry.Context.set_extra_context(%{ga_response: %{body: body, status: status}})
end
end
defp parse_report_from_response(raw_body) do
with {:ok, map} <- Jason.decode(raw_body),
%{"reports" => [report | _]} <- map do