Skip to content

Commit

Permalink
messenger_channels
Browse files Browse the repository at this point in the history
  • Loading branch information
am-kantox committed Nov 30, 2024
1 parent 45d0191 commit fee88bd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ config :telemetria,
# metadata: :all

if Mix.env() == :test do
config :telemetria, :messenger, :mox
config :telemetria, :messenger_channels, %{mox: {:mox, []}}
end
5 changes: 0 additions & 5 deletions lib/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ defmodule Telemetria.Options do
doc: "The backend to be used as an actual implementation",
default: Telemetria.Backend.Telemetry
],
messenger: [
type: :atom,
doc: "The messenger to be used as an actual implementation",
default: nil
],
messenger_channels: [
type: :map,
doc: "The messenger channels as a map `%{name => {impl, opts}}`",
Expand Down
11 changes: 7 additions & 4 deletions lib/telemetria.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ defmodule Telemetria do
- **`reshape: (map() -> map())`** — the function to be called on the resulting attributes
to reshape them before sending to the actual telemetry handler; the default application-wide
reshaper might be set in `:telemetria, :reshaper` config
- **`messenger: true | false | module()`** — when `true` or `module`, the instant message
is to be sent to the desired destination (like slack)
- **`messenger_channels: %{optional(atom()) => {module, keyword()}`** — more handy messenger
management, several channels config with channels names associated with their
implementations and properties
### Example
Expand Down Expand Up @@ -427,8 +428,10 @@ defmodule Telemetria do
defp variablize({var, _, _} = val), do: {var, val}

defp get_channel_info(channel, level) do
{mod, opts} = Map.get(@messenger_channels, channel, {channel, []})
{mod, Keyword.put(opts, :level, level)}
case Map.get(@messenger_channels, channel, {channel, []}) do
{mod, opts} -> {mod, Keyword.put(opts, :level, level)}
mod when is_atom(mod) -> {mod, level: level}
end
end

defp extract_guards([]), do: []
Expand Down
5 changes: 3 additions & 2 deletions lib/telemetria/messenger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Telemetria.Messenger do

@optional_callbacks format: 2

@implementation Application.compile_env(:telemetria, :messenger, Logger)
@implementation Telemetria.Messenger.Logger

@doc "Routes the message to the configured messenger(s)"
@spec post(message() | String.t(), impl :: atom() | module(), opts :: keyword()) ::
Expand All @@ -43,7 +43,8 @@ defmodule Telemetria.Messenger do
do_post(message, impl, opts)
end

def post(message, impl, opts), do: do_post(message, impl, opts)
def post(message, impl, opts) when is_binary(message),
do: do_post(message, impl, opts)

defp do_post(message, impl, opts) do
impl = fix_impl_name(impl)
Expand Down
18 changes: 18 additions & 0 deletions lib/telemetria/messenger/logger.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Telemetria.Messenger.Logger do
@moduledoc false

require Logger

@behaviour Telemetria.Messenger

@impl true
def format(message, opts), do: inspect(message, opts)

Enum.each(~w|debug info warning error|a, fn level ->
@impl true
def unquote(level)(message, opts),
do: post(unquote(level), message, opts)
end)

defp post(level, message, opts), do: {Logger.log(level, message), opts}
end
4 changes: 1 addition & 3 deletions lib/telemetria/messenger/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule Telemetria.Messenger.Slack do

@behaviour Telemetria.Messenger

@default_url Application.compile_env(:telemetria, :messenger_default_url)

@impl true
# %{
# args: [a: 42],
Expand Down Expand Up @@ -89,7 +87,7 @@ defmodule Telemetria.Messenger.Slack do
|> Jason.encode!()
|> :erlang.binary_to_list()

url = Keyword.get(opts, :url, @default_url)
url = Keyword.fetch!(opts, :url)

:httpc.request(:post, {to_charlist(url), [], ~c"application/json", json}, [], [])
end
Expand Down
20 changes: 4 additions & 16 deletions lib/telemetria/throttler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,10 @@ defmodule Telemetria.Throttler do
|> Map.put(:measurements, measurements)
|> Map.pop(:context, %{})

case messenger do
false ->
:ok

nil ->
:ok

impl when is_atom(impl) ->
updates
|> Map.put(:event, event)
|> Telemetria.Messenger.post(impl)

{impl, opts} when is_atom(impl) ->
updates
|> Map.put(:event, event)
|> Telemetria.Messenger.post(impl, opts)
with {impl, opts} when is_atom(impl) and is_list(opts) <- messenger do
updates
|> Map.put(:event, event)
|> Telemetria.Messenger.post(impl, opts)
end

updates = if is_function(reshaper, 1), do: reshaper.(updates), else: updates
Expand Down
2 changes: 1 addition & 1 deletion test/support/telemetria_tester.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Test.Telemetria.Example do
t(&(&1 / 2), suffix: :foo).(a)
end

@telemetria level: :warning, messenger: true, locals: [:result]
@telemetria level: :warning, messenger: :mox, locals: [:result]
def third(a) do
result = a / 3
result
Expand Down

0 comments on commit fee88bd

Please sign in to comment.