From 95aafb477d46857af2a330653bc6f76e027a5329 Mon Sep 17 00:00:00 2001 From: Vinicius Brasil Date: Wed, 19 Oct 2022 09:22:52 -0300 Subject: [PATCH] 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. --- lib/plausible/google/http.ex | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/plausible/google/http.ex b/lib/plausible/google/http.ex index bb324168fd..6b32513793 100644 --- a/lib/plausible/google/http.ex +++ b/lib/plausible/google/http.ex @@ -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