Skip to content

Commit

Permalink
FormatHelper handles missing fields, moved webhook to smallest scope
Browse files Browse the repository at this point in the history
  • Loading branch information
whossname committed Feb 4, 2022
1 parent a77b3f0 commit 77148ec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
44 changes: 22 additions & 22 deletions lib/slack_logger_backend/format_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ defmodule SlackLoggerBackend.FormatHelper do
Formats a log event for Slack.
"""
def format_event(detail) do
fields = [
field("Module", detail.module),
field("Function", detail.function),
field("File", detail.file),
field("Line", detail.line),
field("Count", detail.count)
]

fields =
if Map.has_key?(detail, :application) do
[
field("Level", detail.level),
field("Application", detail.application) | fields
]
else
[field("Level", detail.level) | fields]
end
[
field("Level", detail, :level),
field("Application", detail, :application),
field("Module", detail, :module),
field("Function", detail, :function),
field("File", detail, :file),
field("Line", detail, :line),
field("Count", detail, :count)
]
|> Enum.reject(&is_nil/1)

{:ok, json} =
%{
Expand All @@ -42,11 +36,17 @@ defmodule SlackLoggerBackend.FormatHelper do
json
end

defp field(title, value) do
%{
title: title,
value: value,
short: true
}
defp field(title, map, key) do
case map[key] do
nil ->
nil

value ->
%{
title: title,
value: value,
short: true
}
end
end
end
11 changes: 1 addition & 10 deletions lib/slack_logger_backend/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ defmodule SlackLoggerBackend.Pool do
use Supervisor
alias SlackLoggerBackend.PoolWorker

@env_webhook "SLACK_LOGGER_WEBHOOK_URL"

@doc false
def start_link(pool_size) do
Supervisor.start_link(__MODULE__, pool_size, name: __MODULE__)
Expand Down Expand Up @@ -40,16 +38,9 @@ defmodule SlackLoggerBackend.Pool do
:poolboy.transaction(
:message_pool,
fn pid ->
PoolWorker.post(pid, get_url(), json)
PoolWorker.post(pid, json)
end,
:infinity
)
end

defp get_url() do
case System.get_env(@env_webhook) do
nil -> Application.get_env(:slack_logger_backend, :slack_webhook, nil)
url -> url
end
end
end
19 changes: 14 additions & 5 deletions lib/slack_logger_backend/pool_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule SlackLoggerBackend.PoolWorker do

use GenServer

@env_webhook "SLACK_LOGGER_WEBHOOK_URL"

@doc false
def start_link([]) do
GenServer.start_link(__MODULE__, [], [])
Expand All @@ -16,16 +18,23 @@ defmodule SlackLoggerBackend.PoolWorker do
end

@doc false
def handle_call({:post, url, json}, _from, worker_state) do
result = HTTPoison.post(url, json)
def handle_call({:post, json}, _from, worker_state) do
result = HTTPoison.post(get_url(), json)
{:reply, result, worker_state}
end

@doc """
Gets a message.
"""
@spec post(pid, String.t(), String.t()) :: atom
def post(pid, url, json) do
GenServer.call(pid, {:post, url, json}, :infinity)
@spec post(pid, String.t()) :: atom
def post(pid, json) do
GenServer.call(pid, {:post, json}, :infinity)
end

defp get_url() do
case System.get_env(@env_webhook) do
nil -> Application.get_env(:slack_logger_backend, :slack_webhook, nil)
url -> url
end
end
end
19 changes: 9 additions & 10 deletions test/slack_logger_backend/pool_worker_test.exs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
ExUnit.start
ExUnit.start()

defmodule SlackLoggerBackend.PoolWorkerTest do
use ExUnit.Case
alias SlackLoggerBackend.{PoolWorker}

setup do
bypass = Bypass.open
bypass = Bypass.open()
url = "http://localhost:#{bypass.port}/hook"
{:ok, %{bypass: bypass, url: url}}
Application.put_env(:slack_logger_backend, :slack_webhook, url)
{:ok, %{bypass: bypass}}
end

test "posts the error to the Slack incoming webhook", %{
bypass: bypass,
url: url
} do
Bypass.expect bypass, fn conn ->
bypass: bypass
} do
Bypass.expect(bypass, fn conn ->
assert "/hook" == conn.request_path
assert "POST" == conn.method
Plug.Conn.resp(conn, 200, "ok")
end
end)

{:ok, pid} = PoolWorker.start_link([])
{:ok, _} = PoolWorker.post(pid, url, "test")
{:ok, _} = PoolWorker.post(pid, "test")
end

end

0 comments on commit 77148ec

Please sign in to comment.