Skip to content

Commit

Permalink
feat: better Tower.Event metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Jul 30, 2024
1 parent f86ee7a commit 5182279
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
16 changes: 8 additions & 8 deletions lib/tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ defmodule Tower do
:ok = Tower.LoggerHandler.detach()
end

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

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

def handle_exit(reason, stacktrace, metadata \\ %{}) do
Event.from_exit(reason, stacktrace, metadata)
def handle_exit(reason, stacktrace, log_event \\ %{}) do
Event.from_exit(reason, stacktrace, log_event)
|> report_event()
end

def handle_message(level, message, metadata \\ %{}) do
Event.from_message(level, message, metadata)
def handle_message(level, message, log_event \\ %{}) do
Event.from_message(level, message, log_event)
|> report_event()
end

Expand Down
38 changes: 24 additions & 14 deletions lib/tower/event.ex
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
defmodule Tower.Event do
defstruct [:time, :level, :kind, :reason, :stacktrace, :log_event_meta]
defstruct [:time, :level, :kind, :reason, :stacktrace, :metadata]

@type metadata :: %{log_event: :logger.log_event()}

@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()
metadata: metadata()
}

def from_exception(exception, stacktrace, log_event_meta) do
def from_exception(exception, stacktrace, log_event) do
%__MODULE__{
time: Map.get(log_event_meta, :time, :logger.timestamp()),
time: log_event[:meta][:time] || :logger.timestamp(),
level: :error,
kind: :error,
reason: exception,
stacktrace: stacktrace,
log_event_meta: log_event_meta
metadata: %{
log_event: log_event
}
}
end

def from_exit(reason, stacktrace, log_event_meta) do
def from_exit(reason, stacktrace, log_event) do
%__MODULE__{
time: Map.get(log_event_meta, :time, :logger.timestamp()),
time: log_event[:meta][:time] || :logger.timestamp(),
level: :error,
kind: :exit,
reason: reason,
stacktrace: stacktrace,
log_event_meta: log_event_meta
metadata: %{
log_event: log_event
}
}
end

def from_throw(reason, stacktrace, log_event_meta) do
def from_throw(reason, stacktrace, log_event) do
%__MODULE__{
time: Map.get(log_event_meta, :time, :logger.timestamp()),
time: log_event[:meta][:time] || :logger.timestamp(),
level: :error,
kind: :throw,
reason: reason,
stacktrace: stacktrace,
log_event_meta: log_event_meta
metadata: %{
log_event: log_event
}
}
end

def from_message(level, message, log_event_meta) do
def from_message(level, message, log_event) do
%__MODULE__{
time: Map.get(log_event_meta, :time, :logger.timestamp()),
time: log_event[:meta][:time] || :logger.timestamp(),
level: level,
kind: :message,
reason: message,
log_event_meta: log_event_meta
metadata: %{
log_event: log_event
}
}
end
end
50 changes: 20 additions & 30 deletions lib/tower/logger_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,83 +21,73 @@ defmodule Tower.LoggerHandler do
end

# elixir 1.15+
def log(%{level: :error, meta: %{crash_reason: {exception, stacktrace}} = meta}, _config)
def log(%{level: :error, meta: %{crash_reason: {exception, stacktrace}}} = log_event, _config)
when is_exception(exception) and is_list(stacktrace) do
Tower.handle_exception(exception, stacktrace, meta)
Tower.handle_exception(exception, stacktrace, log_event)
end

# elixir 1.15+
def log(
%{level: :error, meta: %{crash_reason: {{:nocatch, reason}, stacktrace}} = meta},
%{level: :error, meta: %{crash_reason: {{:nocatch, reason}, stacktrace}}} = log_event,
_config
)
when is_list(stacktrace) do
Tower.handle_throw(reason, stacktrace, meta)
Tower.handle_throw(reason, stacktrace, log_event)
end

# elixir 1.15+
def log(%{level: :error, meta: %{crash_reason: {exit_reason, stacktrace}} = meta}, _config)
def log(%{level: :error, meta: %{crash_reason: {exit_reason, stacktrace}}} = log_event, _config)
when is_list(stacktrace) do
Tower.handle_exit(exit_reason, stacktrace, meta)
Tower.handle_exit(exit_reason, stacktrace, log_event)
end

# elixir 1.14
def log(
%{
level: :error,
msg: {:report, %{report: %{reason: {exception, stacktrace}}}},
meta: meta
},
%{level: :error, msg: {:report, %{report: %{reason: {exception, stacktrace}}}}} =
log_event,
_config
)
when is_exception(exception) and is_list(stacktrace) do
Tower.handle_exception(exception, stacktrace, meta)
Tower.handle_exception(exception, stacktrace, log_event)
end

# elixir 1.14
def log(
%{
level: :error,
msg: {:report, %{report: %{reason: {{:nocatch, reason}, stacktrace}}}},
meta: meta
},
%{level: :error, msg: {:report, %{report: %{reason: {{:nocatch, reason}, stacktrace}}}}} =
log_event,
_config
)
when is_list(stacktrace) do
Tower.handle_throw(reason, stacktrace, meta)
Tower.handle_throw(reason, stacktrace, log_event)
end

# elixir 1.14
def log(
%{
level: :error,
msg: {:report, %{report: %{reason: {reason, stacktrace}}}},
meta: meta
},
%{level: :error, msg: {:report, %{report: %{reason: {reason, stacktrace}}}}} = log_event,
_config
)
when is_list(stacktrace) do
case Exception.normalize(:error, reason) do
%ErlangError{} ->
Tower.handle_exit(reason, stacktrace, meta)
Tower.handle_exit(reason, stacktrace, log_event)

e when is_exception(e) ->
Tower.handle_exception(e, stacktrace, meta)
Tower.handle_exception(e, stacktrace, log_event)

_ ->
Tower.handle_exit(reason, stacktrace, meta)
Tower.handle_exit(reason, stacktrace, log_event)
end
end

def log(%{level: level, msg: {:string, reason_chardata}, meta: meta}, _config) do
def log(%{level: level, msg: {:string, reason_chardata}} = log_event, _config) do
if should_handle?(level) do
Tower.handle_message(level, IO.chardata_to_string(reason_chardata), meta)
Tower.handle_message(level, IO.chardata_to_string(reason_chardata), log_event)
end
end

def log(%{level: level, msg: {:report, report}, meta: meta}, _config) do
def log(%{level: level, msg: {:report, report}} = log_event, _config) do
if should_handle?(level) do
Tower.handle_message(level, report, meta)
Tower.handle_message(level, report, log_event)
end
end

Expand Down

0 comments on commit 5182279

Please sign in to comment.