-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `messenger: :slack` * `messenger_channels`
- Loading branch information
Showing
18 changed files
with
295 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Config | ||
|
||
config :opentelemetry, | ||
span_processor: :batch, | ||
traces_exporter: {:otel_exporter_stdout, []} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Telemetria.Messenger do | ||
@moduledoc """ | ||
The helper allowing quick sending of the telemetry events to the messenger. | ||
""" | ||
|
||
@typedoc "The type of the message the messenger is to process and send" | ||
@type message :: %{ | ||
required(atom()) => term() | ||
} | ||
|
||
@doc "The formatter of the incoming message, producing the binary to be sent over the wire" | ||
@callback format(message(), keyword()) :: message() | String.t() | ||
|
||
@doc "The actual implementation of the message sending (`debug` level)" | ||
@callback debug(message(), keyword()) :: {:ok, term()} | {:error, term()} | ||
|
||
@doc "The actual implementation of the message sending (`info` level)" | ||
@callback info(message(), keyword()) :: {:ok, term()} | {:error, term()} | ||
|
||
@doc "The actual implementation of the message sending (`warning` level)" | ||
@callback warning(message(), keyword()) :: {:ok, term()} | {:error, term()} | ||
|
||
@doc "The actual implementation of the message sending (`error` level)" | ||
@callback error(message(), keyword()) :: {:ok, term()} | {:error, term()} | ||
|
||
@optional_callbacks format: 2 | ||
|
||
@implementation Telemetria.Messenger.Logger | ||
|
||
@doc "Routes the message to the configured messenger(s)" | ||
@spec post(message() | String.t(), impl :: atom() | module(), opts :: keyword()) :: | ||
{:ok, term()} | {:error, term()} | ||
def post(message, impl \\ @implementation, opts \\ []) | ||
|
||
def post(%{} = message, impl, opts) do | ||
impl = fix_impl_name(impl) | ||
|
||
message = | ||
if function_exported?(impl, :format, 2), | ||
do: impl.format(message, opts), | ||
else: inspect(message, opts) | ||
|
||
do_post(message, impl, opts) | ||
end | ||
|
||
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) | ||
{level, opts} = Keyword.pop(opts, :level, :info) | ||
apply(impl, level, [message, opts]) | ||
end | ||
|
||
@spec fix_impl_name(atom()) :: module() | ||
defp fix_impl_name(true), do: fix_impl_name(@implementation) | ||
|
||
defp fix_impl_name(impl) do | ||
case to_string(impl) do | ||
"Elixir." <> _ -> impl | ||
_ -> Module.concat([Telemetria, Messenger, impl |> to_string() |> Macro.camelize()]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
defmodule Telemetria.Messenger.Slack do | ||
@moduledoc false | ||
|
||
@behaviour Telemetria.Messenger | ||
|
||
@impl true | ||
# %{ | ||
# args: [a: 42], | ||
# env: %{ | ||
# function: {:half, 1}, | ||
# line: 23, | ||
# module: Test.Telemetria.Example, | ||
# file: "/home/am/Proyectos/Elixir/telemetria/test/support/telemetria_tester.ex" | ||
# }, | ||
# context: [], | ||
# result: 21.0, | ||
# locals: [], | ||
# measurements: %{ | ||
# system_time: [ | ||
# system: 1732885662078938716, | ||
# monotonic: -576460750195561, | ||
# utc: ~U[2024-11-29 13:07:42.078940Z] | ||
# ], | ||
# consumed: 3336 | ||
# }, | ||
# telemetria_group: :default | ||
# } | ||
def format(message, opts) do | ||
with {event, message} <- Map.pop(message, :event), | ||
{%{function: {f, a}} = env, message} <- Map.pop(message, :env), | ||
{level, message} <- | ||
Map.pop_lazy(message, :level, fn -> Keyword.get(opts, :level, :info) end), | ||
{icon, message} <- Map.pop(message, :icon, slack_icon(level)) do | ||
title = Enum.join(event, ".") | ||
|
||
pretext = | ||
env.module | ||
|> Function.capture(f, a) | ||
|> inspect() | ||
|> Kernel.<>("\n#{env.file}:#{env.line}") | ||
|
||
fields = | ||
message | ||
|> Estructura.Flattenable.flatten(jsonify: true) | ||
|> Enum.map(fn {k, v} -> | ||
%{ | ||
title: k, | ||
value: v, | ||
short: not is_binary(v) or String.length(v) < 32 | ||
} | ||
end) | ||
|
||
attachments = | ||
%{ | ||
color: slack_color(level), | ||
fields: fields, | ||
mrkdwn_in: ["title", "text", "pretext"] | ||
} | ||
|> Map.merge(%{pretext: "```\n" <> pretext <> "\n```"}) | ||
|
||
fallback = | ||
[title, pretext] | ||
|> Enum.reject(&is_nil/1) | ||
|> Enum.join("\n") | ||
|
||
%{ | ||
description: title, | ||
emoji_icon: icon, | ||
fallback: fallback, | ||
mrkdwn: true, | ||
attachments: [attachments] | ||
} | ||
end | ||
end | ||
|
||
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 | ||
json = | ||
message | ||
|> put_in([:emoji_icon], slack_icon(level)) | ||
|> put_in([:attachments, Access.all(), :color], slack_color(level)) | ||
|> Jason.encode!() | ||
|> :erlang.binary_to_list() | ||
|
||
url = Keyword.fetch!(opts, :url) | ||
|
||
:httpc.request(:post, {to_charlist(url), [], ~c"application/json", json}, [], []) | ||
end | ||
|
||
defp slack_icon(:debug), do: ":speaker:" | ||
defp slack_icon(:info), do: ":information_source:" | ||
defp slack_icon(:warn), do: ":warning:" | ||
defp slack_icon(:warning), do: slack_icon(:warn) | ||
defp slack_icon(:error), do: ":exclamation:" | ||
|
||
defp slack_icon(level) when is_binary(level), | ||
do: level |> String.to_existing_atom() |> slack_icon() | ||
|
||
defp slack_icon(_), do: slack_icon(:info) | ||
|
||
defp slack_color(:debug), do: "#AAAAAA" | ||
defp slack_color(:info), do: "good" | ||
defp slack_color(:warn), do: "#FF9900" | ||
defp slack_color(:warning), do: slack_color(:warn) | ||
defp slack_color(:error), do: "danger" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.