Skip to content

Commit

Permalink
feat: adapt to tower better handling/reporting (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy authored Jun 20, 2024
1 parent 73a5907 commit c04c953
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
29 changes: 27 additions & 2 deletions lib/tower_rollbar/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,36 @@ defmodule TowerRollbar.Reporter do
end
end

def report_message(message, options \\ []) when is_binary(message) do
@impl true
def report_throw(reason, stacktrace, metadata \\ %{}) when is_list(stacktrace) do
if enabled?() do
Rollbar.Client.post(
"/item",
Rollbar.Item.from_throw(reason, stacktrace, plug_conn: plug_conn(metadata))
)
else
IO.puts("Tower.Rollbar NOT enabled, ignoring...")
end
end

@impl true
def report_exit(reason, stacktrace, metadata \\ %{}) when is_list(stacktrace) do
if enabled?() do
Rollbar.Client.post(
"/item",
Rollbar.Item.from_exit(reason, stacktrace, plug_conn: plug_conn(metadata))
)
else
IO.puts("Tower.Rollbar NOT enabled, ignoring...")
end
end

@impl true
def report_message(level, message, _metadata \\ %{}) when is_binary(message) do
if enabled?() do
Rollbar.Client.post(
"/item",
Rollbar.Item.from_message(message, options)
Rollbar.Item.from_message(message, level: level)
)
else
IO.puts("Tower.Rollbar NOT enabled, ignoring...")
Expand Down
32 changes: 22 additions & 10 deletions lib/tower_rollbar/rollbar/item.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
defmodule TowerRollbar.Rollbar.Item do
def from_exception(exception, stacktrace, options \\ [])
when is_exception(exception) and is_list(stacktrace) do
%{
"trace" => %{
"frames" => frames(stacktrace),
"exception" => %{
"class" => inspect(exception.__struct__),
"message" => Exception.message(exception)
}
}
}
|> item_from_body(Keyword.merge([level: :error], options))
trace(inspect(exception.__struct__), Exception.message(exception), stacktrace, options)
end

def from_throw(reason, stacktrace, options \\ []) when is_list(stacktrace) do
trace("uncaught throw", reason, stacktrace, options)
end

def from_exit(reason, stacktrace, options \\ []) when is_list(stacktrace) do
trace("exit", reason, stacktrace, options)
end

def from_message(message, options \\ []) when is_binary(message) do
Expand All @@ -22,6 +21,19 @@ defmodule TowerRollbar.Rollbar.Item do
|> item_from_body(Keyword.merge([level: :info], options))
end

defp trace(class, reason, stacktrace, options) do
%{
"trace" => %{
"frames" => frames(stacktrace),
"exception" => %{
"class" => class,
"message" => reason
}
}
}
|> item_from_body(Keyword.merge([level: :error], options))
end

defp item_from_body(body, options) when is_map(body) do
plug_conn = Keyword.get(options, :plug_conn)
level = Keyword.get(options, :level)
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"plug": {:hex, :plug, "1.16.0", "1d07d50cb9bb05097fdf187b31cf087c7297aafc3fed8299aac79c128a707e47", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cbf53aa1f5c4d758a7559c0bd6d59e286c2be0c6a1fac8cc3eee2f638243b93e"},
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"tower": {:git, "https://github.com/mimiquate/tower.git", "3722967c814dea3fdd36b09448c44f7b085543a0", []},
"tower": {:git, "https://github.com/mimiquate/tower.git", "24ba0465e334ef71e22a242ca007324fc1e07463", []},
}

0 comments on commit c04c953

Please sign in to comment.