data_logger v0.3.1 DataLogger.Destination behaviour 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

Link to this type

options() View Source
options() :: map()

Link to this type

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

Link to this section Callbacks

Link to this callback

initialize(options) View Source (optional)
initialize(options()) :: options()

Link to this callback

on_error(error, topic, data, options) View Source (optional)
on_error(error :: term(), topic(), data :: term(), options()) :: :ok

Link to this callback

on_success(result, topic, data, options) View Source (optional)
on_success(result :: term(), topic(), data :: term(), options()) :: :ok

Link to this callback

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