Skip to content

Commit

Permalink
feat: Tower.Event includes timestmap (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy authored Jul 30, 2024
1 parent 04e310f commit 6bb230c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
6 changes: 3 additions & 3 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ defmodule Tower do
:ok = Tower.LoggerHandler.detach()
end

def handle_exception(exception, stacktrace, meta \\ %{})
def handle_exception(exception, stacktrace, meta)
when is_exception(exception) and is_list(stacktrace) do
Event.from_exception(exception, stacktrace, meta)
|> report_event()
end

def handle_throw(reason, stacktrace, metadata \\ %{}) do
def handle_throw(reason, stacktrace, metadata) do
Event.from_throw(reason, stacktrace, metadata)
|> report_event()
end

def handle_exit(reason, stacktrace, metadata \\ %{}) do
def handle_exit(reason, stacktrace, metadata) do
Event.from_exit(reason, stacktrace, metadata)
|> report_event()
end
Expand Down
40 changes: 10 additions & 30 deletions lib/tower/ephemeral_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,21 @@ defmodule Tower.EphemeralReporter do
end

@impl true
def report_event(%Event{
kind: :error,
reason: exception,
stacktrace: stacktrace,
log_event_meta: log_event_meta
}) do
add_error(exception.__struct__, Exception.message(exception), stacktrace, log_event_meta)
def report_event(%Event{time: time, kind: :error, reason: exception, stacktrace: stacktrace}) do
add_error(time, exception.__struct__, Exception.message(exception), stacktrace)
end

def report_event(%Event{
kind: :exit,
reason: reason,
stacktrace: stacktrace,
log_event_meta: log_event_meta
}) do
add_error(:exit, reason, stacktrace, log_event_meta)
def report_event(%Event{time: time, kind: :exit, reason: reason, stacktrace: stacktrace}) do
add_error(time, :exit, reason, stacktrace)
end

def report_event(%Event{
kind: :throw,
reason: reason,
stacktrace: stacktrace,
log_event_meta: log_event_meta
}) do
add_error(:throw, reason, stacktrace, log_event_meta)
def report_event(%Event{time: time, kind: :throw, reason: reason, stacktrace: stacktrace}) do
add_error(time, :throw, reason, stacktrace)
end

def report_event(%Event{
kind: :message,
level: level,
reason: message,
log_event_meta: log_event_meta
}) do
def report_event(%Event{time: time, kind: :message, level: level, reason: message}) do
add(%{
time: Map.get(log_event_meta, :time, :logger.timestamp()),
time: time,
level: level,
kind: nil,
reason: message,
Expand All @@ -56,9 +36,9 @@ defmodule Tower.EphemeralReporter do
Agent.get(__MODULE__, & &1)
end

defp add_error(kind, reason, stacktrace, metadata) do
defp add_error(time, kind, reason, stacktrace) do
add(%{
time: Map.get(metadata, :time, :logger.timestamp()),
time: time,
level: :error,
kind: kind,
reason: reason,
Expand Down
13 changes: 9 additions & 4 deletions lib/tower/event.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
defmodule Tower.Event do
defstruct [:level, :kind, :reason, :stacktrace, :log_event_meta]
defstruct [:time, :level, :kind, :reason, :stacktrace, :log_event_meta]

@type t :: %__MODULE__{
time: :logger.timestamp(),
level: :logger.level(),
kind: :error | :exit | :throw | :message,
reason: Exception.t() | term(),
stacktrace: Exception.stacktrace(),
log_event_meta: :logger.metadata()
}

def from_exception(exception, stacktrace, log_event_meta) do
def from_exception(exception, stacktrace, %{time: time} = log_event_meta) do
%__MODULE__{
time: time,
level: :error,
kind: :error,
reason: exception,
Expand All @@ -19,8 +21,9 @@ defmodule Tower.Event do
}
end

def from_exit(reason, stacktrace, log_event_meta) do
def from_exit(reason, stacktrace, %{time: time} = log_event_meta) do
%__MODULE__{
time: time,
level: :error,
kind: :exit,
reason: reason,
Expand All @@ -29,8 +32,9 @@ defmodule Tower.Event do
}
end

def from_throw(reason, stacktrace, log_event_meta) do
def from_throw(reason, stacktrace, %{time: time} = log_event_meta) do
%__MODULE__{
time: time,
level: :error,
kind: :throw,
reason: reason,
Expand All @@ -41,6 +45,7 @@ defmodule Tower.Event do

def from_message(level, message, log_event_meta) do
%__MODULE__{
time: Map.get(log_event_meta, :time, :logger.timestamp()),
level: level,
kind: :message,
reason: message,
Expand Down
34 changes: 24 additions & 10 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: ArithmeticError,
reason: "bad argument in arithmetic expression",
Expand All @@ -35,6 +35,7 @@ defmodule TowerTest do
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

Expand All @@ -47,7 +48,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: RuntimeError,
reason: "error inside process",
Expand All @@ -56,6 +57,7 @@ defmodule TowerTest do
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

Expand All @@ -68,7 +70,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: :throw,
reason: "error",
Expand All @@ -77,6 +79,7 @@ defmodule TowerTest do
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

Expand All @@ -89,7 +92,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: :throw,
reason: [something: "here"],
Expand All @@ -98,6 +101,7 @@ defmodule TowerTest do
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

Expand All @@ -118,7 +122,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: :exit,
reason: :abnormal,
Expand All @@ -127,6 +131,7 @@ defmodule TowerTest do
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

Expand All @@ -139,7 +144,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: :exit,
reason: :kill,
Expand All @@ -148,6 +153,7 @@ defmodule TowerTest do
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
assert is_list(stacktrace)
end

Expand All @@ -173,14 +179,16 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: nil,
reason: "Something went wrong here",
stacktrace: []
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
end

@tag capture_log: true
Expand All @@ -202,7 +210,7 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :error,
kind: nil,
reason:
Expand All @@ -211,6 +219,8 @@ defmodule TowerTest do
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
end

@tag capture_log: true
Expand All @@ -223,14 +233,16 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :critical,
kind: nil,
reason: [something: :reported, this: :critical],
stacktrace: []
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
end

test "reports message manually" do
Expand All @@ -239,14 +251,16 @@ defmodule TowerTest do
assert_eventually(
[
%{
time: _,
time: time,
level: :info,
kind: nil,
reason: "Something interesting",
stacktrace: []
}
] = reported_events()
)

assert_in_delta(time, :logger.timestamp(), 100_000)
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
Expand Down

0 comments on commit 6bb230c

Please sign in to comment.