DataLogger.Destination behaviour (data_logger v0.4.0) View Source

A behaviour, representing a destination for data logging.

The mandatory callback to implement is DataLogger.Destination.send_data/3. An implementation should handle errors and retries by using the optional callbacks DataLogger.Destination.on_error/4 or/and DataLogger.Destination.on_success/4. These functions are called with the result of the call to the DataLogger.Destination.send_data/4 function. Also an implementation can have some custom initialization, using the optional callback DataLogger.Destination.init/1 which gets the configured options from the config and can return modified options. By default it returns what is passed to it.

A possible implementation could look like this:

defmodule RelationalDBDestination do
  use DataLogger.Destination

  @impl true
  def send_data(topic, data, options) do
    connection = ConnectionToDBImpl.connect(options)

    query_to_insert_data = transform_data_to_query(topic, data)

    case ConnectionToDBImpl.execute(connection, query_to_insert_data, data) do
      :ok -> :ok
      {:error, reason} -> {:error, reason}
    end
  end
end

The implementation can also define the DataLogger.Destination.on_error/4 function and retry on error or log some message. The default implementations of the DataLogger.Destination.on_error/4 and DataLogger.Destination.on_success/4 callbacks, do nothing.

The above example implementation can be configured in the application configuration like this:

config :data_logger,
  destinations: [
    {RelationalDBDestination, %{host: "localhost", user: "inflowmatix", password: "secret", send_async: true}}
  ]

Link to this section Summary

Link to this section Types

Specs

options() :: map()

Specs

prefix() :: atom() | String.t()

Specs

send_result() ::
  :ok
  | {:ok, result :: term()}
  | {:error, reason :: term()}
  | {:ok, result :: term(), options()}
  | {:error, reason :: term(), options()}

Specs

topic() :: atom() | String.t()

Link to this section Callbacks

Link to this callback

initialize(options)

View Source (optional)

Specs

initialize(options()) :: options()
Link to this callback

on_error(error, topic, data, options)

View Source (optional)

Specs

on_error(error :: term(), topic(), data :: term(), options()) :: :ok
Link to this callback

on_success(result, topic, data, options)

View Source (optional)

Specs

on_success(result :: term(), topic(), data :: term(), options()) :: :ok
Link to this callback

send_data(topic, data, options)

View Source

Specs

send_data(topic(), data :: term(), options()) :: send_result()