Packages
logflare_ex
0.1.1
0.2.0-dev.b339a6b8
0.2.0-dev.980340db
0.2.0-dev.5d01deca
0.2.0-dev.236c1c6b
0.2.0-dev.082a725d
0.2.0-dev.1718089325.ea5f751
0.2.0-dev.1718053351.fca7edd
0.2.0-dev.1718049151.46c51d9
0.2.0-dev.1718049098.9ad6f45
0.2.0-dev.1715303639.8521594
0.2.0-dev.1701249644.103466d
0.1.1
0.1.1-dev-5d6bc7c9
0.1.0
0.1.0-dev-a70912b8
0.1.0-dev-79b32387
Logflare Elixir SDK
Current section
Files
Jump to
Current section
Files
lib/logflare_ex.ex
defmodule LogflareEx do
alias LogflareEx.Client
@moduledoc """
Documentation for `LogflareEx`.
"""
@doc """
Creates a client for interacting with Logflare.
Options:
- `:api_key`, required - api key obtained from dashboard
- `:api_url`, optional - Logflare instance url, defaults to Logflare service at https://api.logflare.app
- `:source_token`, required for ingest - A source uuid obtained from dashboard
- `:adapter`, optional - Tesla client adapter
## Examples
iex> LogflareEx.client(%{...})
%LogflareEx.Client{...}
"""
defdelegate client(opts), to: __MODULE__.Client, as: :new
@doc """
Send a singular event to Logflare.
### Example
Pipe the client into the desired function
iex> client |> LogflareEx.send_event(%{my: "event"})
{:ok, %{"message"=> "Logged!}}
If an error is encountered, the tesla result will be returned
iex> client |> LogflareEx.send_event(%{my: "event"})
{:error, %Tesla.Env{...}}
"""
@spec send_event(Client.t(), map()) :: {:ok, map()} | {:error, Tesla.Env.t()}
def send_event(client, %{} = event) do
send_events(client, [event])
end
@spec send_events(Client.t(), [map()]) :: {:ok, map()} | {:error, Tesla.Env.t()}
def send_events(_client, []), do: {:error, :no_events}
def send_events(%Client{source_token: nil}, _batch), do: {:error, :no_source}
def send_events(client, [%{} | _] = batch) do
body = Bertex.encode(%{"batch" => batch, "source" => client.source_token})
case Tesla.post(client.tesla_client, "/api/logs", body) do
%Tesla.Env{status: 201, body: body} ->
{:ok, Jason.decode!(body)}
%Tesla.Env{} = result ->
# on_error callback
case Map.get(client, :on_error) do
{m, f, 1} -> apply(m, f, [result])
cb when is_function(cb) -> cb.(result)
_ -> :noop
end
{:error, result}
end
end
end