-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces wrapper adaptor over Webhook adaptor to send logs to DataDog HTTP log ingest endpoint.
- Loading branch information
Showing
6 changed files
with
317 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
defmodule Logflare.Backends.Adaptor.DatadogAdaptor do | ||
@moduledoc """ | ||
Wrapper module for `Logflare.Backends.Adaptor.WebhookAdaptor` to provide API | ||
for DataDog logs ingestion endpoint. | ||
""" | ||
|
||
defmodule Client do | ||
use GenServer | ||
|
||
require Logger | ||
|
||
# - `queue` - list of elements in queue | ||
# - `size` - amount of elements in queue (cached to not use `length/1` all | ||
# the time) | ||
# - `threshold` - maximal amount of items in queue before sending | ||
# - `timeout` - time to wait before sending all messages in queue | ||
# - `timer` - reference to timer used | ||
defstruct queue: [], | ||
size: 0, | ||
threshold: 100, | ||
timeout: 1000, | ||
timer: nil, | ||
url: nil, | ||
method: :post, | ||
headers: [] | ||
|
||
def start_link(opts), do: GenServer.start_link(__MODULE__, Map.new(opts)) | ||
|
||
def ingest(_pid, []), do: :ok | ||
def ingest(pid, logs), do: GenServer.cast(pid, {:events, logs}) | ||
|
||
@impl GenServer | ||
def init(opts) do | ||
{:ok, | ||
%__MODULE__{ | ||
threshold: opts[:threshold] || 100, | ||
timeout: opts[:timeout] || 1000, | ||
method: opts[:method] || :post, | ||
url: Map.fetch!(opts, :url), | ||
headers: Enum.to_list(opts[:headers] || []) | ||
}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:events, events}, %__MODULE__{} = state) do | ||
len = length(events) | ||
|
||
new_state = %__MODULE__{ | ||
state | ||
| queue: events ++ state.queue, | ||
size: len + state.size, | ||
timer: start_timer(state.timer, state.timeout) | ||
} | ||
|
||
if new_state.size >= new_state.threshold do | ||
{:noreply, new_state, {:continue, :send}} | ||
else | ||
{:noreply, new_state} | ||
end | ||
end | ||
|
||
@impl GenServer | ||
def handle_info(:timeout, state) do | ||
{:noreply, state, {:continue, :send}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_continue(:send, state) do | ||
send_logs(state) | ||
{:noreply, %__MODULE__{state | queue: [], size: 0, timer: cancel_timer(state.timer)}} | ||
end | ||
|
||
defp start_timer(nil, timeout) do | ||
{:ok, ref} = :timer.send_after(timeout, :timeout) | ||
|
||
ref | ||
end | ||
|
||
defp start_timer(ref, _timeout), do: ref | ||
|
||
defp cancel_timer(nil), do: nil | ||
|
||
defp cancel_timer(ref) do | ||
:timer.cancel(ref) | ||
nil | ||
end | ||
|
||
defp send_logs(%__MODULE__{} = state) do | ||
middlewares = [ | ||
Tesla.Middleware.Telemetry, | ||
Tesla.Middleware.JSON | ||
] | ||
|
||
client = Tesla.client(middlewares) | ||
|
||
Task.start(fn -> | ||
case Tesla.request(client, | ||
url: state.url, | ||
method: state.method, | ||
headers: state.headers, | ||
body: state.queue | ||
) do | ||
{:ok, %Tesla.Env{status: status}} when status in 200..299 -> | ||
:ok | ||
|
||
{:ok, %Tesla.Env{} = resp} -> | ||
Logger.error(resp, report_cb: &__MODULE__.__report_cb__/2) | ||
:error | ||
end | ||
end) | ||
end | ||
|
||
@doc false | ||
def __report_cb__(%Tesla.Env{} = report, _opts) do | ||
"#{report.method} #{report.url} request failed with HTTP #{report.status} - #{inspect(report.body)}" | ||
end | ||
end | ||
|
||
use TypedStruct | ||
|
||
typedstruct enforce: true do | ||
field(:api_key, String.t()) | ||
end | ||
|
||
@behaviour Logflare.Backends.Adaptor | ||
|
||
@impl Logflare.Backends.Adaptor | ||
def start_link({_source, backend}) do | ||
Client.start_link( | ||
url: "https://http-intake.logs.datadoghq.com/api/v2/logs", | ||
headers: [{"dd-api-key", backend.config.api_key}] | ||
) | ||
end | ||
|
||
@impl Logflare.Backends.Adaptor | ||
def ingest(pid, log_events, _opts) do | ||
new_events = | ||
Enum.map(log_events, &translate_event/1) | ||
|
||
Client.ingest(pid, new_events) | ||
end | ||
|
||
@impl Logflare.Backends.Adaptor | ||
def execute_query(_ident, _query), do: {:error, :not_implemented} | ||
|
||
@impl Logflare.Backends.Adaptor | ||
def cast_config(params) do | ||
{%{}, %{api_key: :string}} | ||
|> Ecto.Changeset.cast(params, [:api_key]) | ||
end | ||
|
||
@impl Logflare.Backends.Adaptor | ||
def validate_config(changeset) do | ||
import Ecto.Changeset | ||
|
||
changeset | ||
|> validate_required([:api_key]) | ||
end | ||
|
||
defp translate_event(%Logflare.LogEvent{} = le) do | ||
%{ | ||
message: le.body.event_message, | ||
ddsource: "logflare", | ||
ddtags: build_tags(le.body), | ||
service: le.source.name | ||
} | ||
end | ||
|
||
defp build_tags(body) do | ||
body | ||
|> Map.drop([:event_message]) | ||
|> Enum.map_join(",", fn {key, value} -> | ||
do_build(value, key) | ||
end) | ||
end | ||
|
||
defp do_build(map, parent) when is_map(map) do | ||
for {key, value} <- map, | ||
sub <- do_build(value, "#{parent}.#{key}"), | ||
do: sub | ||
end | ||
|
||
defp do_build(list, parent) when is_list(list) do | ||
for {value, key} <- Enum.with_index(list), | ||
sub <- do_build(value, "#{parent}.#{key}"), | ||
do: sub | ||
end | ||
|
||
defp do_build(other, key), do: ["#{key}:#{other}"] | ||
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
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
Oops, something went wrong.