Skip to content

Commit

Permalink
feat: Tower.Event similarity_id
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Sep 10, 2024
1 parent c713ec4 commit 3517aac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/tower/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Tower.Event do

defstruct [
:id,
:similarity_id,
:datetime,
:level,
:kind,
Expand All @@ -31,6 +32,7 @@ defmodule Tower.Event do
"""
@type t :: %__MODULE__{
id: Uniq.UUID.t(),
similarity_id: non_neg_integer(),
datetime: DateTime.t(),
level: level(),
kind: error_kind() | non_error_kind(),
Expand All @@ -41,6 +43,7 @@ defmodule Tower.Event do
metadata: map()
}

@similarity_source_attributes [:level, :kind, :reason, :stacktrace, :metadata]
@logger_time_unit :microsecond

@doc false
Expand Down Expand Up @@ -120,6 +123,7 @@ defmodule Tower.Event do
|> Map.merge(map)
|> Map.merge(attributes_from_options(options))
)
|> put_similarity_id()
end

defp attributes_from_options(options) do
Expand Down Expand Up @@ -150,4 +154,8 @@ defmodule Tower.Event do
defp plug_conn(options) do
Keyword.get(options, :plug_conn, Keyword.get(options, :log_event)[:meta][:conn])
end

defp put_similarity_id(%__MODULE__{} = event) do
struct!(event, similarity_id: :erlang.phash2(Map.take(event, @similarity_source_attributes)))
end
end
39 changes: 39 additions & 0 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,45 @@ defmodule TowerTest do
assert datetime1 > datetime2
end

test "protects reporters from repeated events" do
capture_log(fn ->
for _ <- 1..2 do
in_unlinked_process(fn ->
1 / 0

Check warning on line 559 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 559 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 559 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 559 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 559 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 559 in test/tower_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 24.3.4.17)

the call to //2 will fail with ArithmeticError
end)
end

in_unlinked_process(fn ->
raise "something else"
end)
end)

assert_eventually(
[
%{
similarity_id: other_similarity_id,
level: :error,
kind: :error,
reason: %RuntimeError{message: "something else"}
},
%{
similarity_id: similarity_id,
level: :error,
kind: :error,
reason: %ArithmeticError{message: "bad argument in arithmetic expression"}
},
%{
similarity_id: similarity_id,
level: :error,
kind: :error,
reason: %ArithmeticError{message: "bad argument in arithmetic expression"}
}
] = reported_events()
)

assert similarity_id != other_similarity_id
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
{:ok, pid} = Task.Supervisor.start_link()

Expand Down

0 comments on commit 3517aac

Please sign in to comment.