Skip to content

Commit f3a1a9d

Browse files
ruslandogaaerosol
andauthored
derive jason encoder for request (#3182)
* derive jason encoder for request * test didn't test --------- Co-authored-by: hq1 <[email protected]>
1 parent 9286911 commit f3a1a9d

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
66
### Fixed
77
- Fixed weekly/monthly e-mail report [rendering issues](https://github.com/plausible/analytics/issues/284)
88
- Fixed [long URLs display](https://github.com/plausible/analytics/issues/3158) in Outbound Link breakdown view
9+
- Fixed [Sentry reports](https://github.com/plausible/analytics/discussions/3166) for ingestion requests plausible/analytics#3182
910

1011
## v2.0.0 - 2023-07-12
1112

lib/plausible/ingestion/request.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ defmodule Plausible.Ingestion.Request do
2727

2828
@max_url_size 2_000
2929

30+
@primary_key false
3031
embedded_schema do
3132
field :remote_ip, :string
3233
field :user_agent, :string
@@ -312,3 +313,16 @@ defmodule Plausible.Ingestion.Request do
312313
nil
313314
end
314315
end
316+
317+
defimpl Jason.Encoder, for: URI do
318+
def encode(uri, _opts), do: [?", URI.to_string(uri), ?"]
319+
end
320+
321+
defimpl Jason.Encoder, for: Plausible.Ingestion.Request do
322+
@fields Plausible.Ingestion.Request.__schema__(:fields)
323+
def encode(request, opts) do
324+
request
325+
|> Map.take(@fields)
326+
|> Jason.Encode.map(opts)
327+
end
328+
end

lib/plausible_web/controllers/api/external_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule PlausibleWeb.Api.ExternalController do
1212

1313
def event(conn, _params) do
1414
with {:ok, request} <- Ingestion.Request.build(conn),
15-
_ <- Sentry.Context.set_extra_context(%{request: inspect(request)}) do
15+
_ <- Sentry.Context.set_extra_context(%{request: request}) do
1616
case Ingestion.Event.build_and_buffer(request) do
1717
{:ok, %{dropped: [], buffered: _buffered}} ->
1818
conn

lib/sentry_filter.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ defmodule Plausible.SentryFilter do
3636
%{event | fingerprint: ["db_connection", reason]}
3737
end
3838

39+
def before_send(%{extra: %{request: %Plausible.Ingestion.Request{}}} = event) do
40+
%{event | fingerprint: ["ingestion_request"]}
41+
end
42+
3943
def before_send(event) do
4044
event
4145
end

test/plausible/ingestion/request_test.exs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,43 @@ defmodule Plausible.Ingestion.RequestTest do
422422
assert {:error, changeset} = Request.build(conn)
423423
assert changeset.errors[:request]
424424
end
425+
426+
test "encodable" do
427+
params = %{
428+
name: "pageview",
429+
domain: "dummy.site",
430+
url: "https://dummy.site/pictures/index.html?foo=bar&baz=bam",
431+
referrer: "https://example.com",
432+
props: %{"abc" => "qwerty", "hello" => "world"},
433+
hashMode: 1,
434+
revenue: %{
435+
"amount" => "12.3",
436+
"currency" => "USD"
437+
}
438+
}
439+
440+
assert {:ok, request} =
441+
build_conn(:post, "/api/events", params)
442+
|> put_req_header("user-agent", "Mozilla")
443+
|> Request.build()
444+
445+
request = request |> Jason.encode!() |> Jason.decode!()
446+
447+
assert Map.drop(request, ["timestamp"]) == %{
448+
"domains" => ["dummy.site"],
449+
"event_name" => "pageview",
450+
"hash_mode" => 1,
451+
"hostname" => "dummy.site",
452+
"pathname" => "/pictures/index.html",
453+
"props" => %{"abc" => "qwerty", "hello" => "world"},
454+
"query_params" => %{"baz" => "bam", "foo" => "bar"},
455+
"referrer" => "https://example.com",
456+
"remote_ip" => "127.0.0.1",
457+
"revenue_source" => %{"amount" => "12.3", "currency" => "USD"},
458+
"uri" => "https://dummy.site/pictures/index.html?foo=bar&baz=bam",
459+
"user_agent" => "Mozilla"
460+
}
461+
462+
assert %NaiveDateTime{} = NaiveDateTime.from_iso8601!(request["timestamp"])
463+
end
425464
end

test/plausible/sentry_filter_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Plausible.SentryFilterTest do
2+
use ExUnit.Case
3+
4+
test "enforces fingerprint for ingestion request" do
5+
event =
6+
Sentry.Event.create_event(
7+
message: "oof",
8+
fingerprint: ["to be", " replaced"],
9+
extra: %{request: %Plausible.Ingestion.Request{}}
10+
)
11+
12+
assert %Sentry.Event{fingerprint: ["ingestion_request"]} =
13+
Plausible.SentryFilter.before_send(event)
14+
end
15+
end

0 commit comments

Comments
 (0)