data_logger v0.3.0 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
options()
View Source
options() :: map()
options() :: map()
prefix() View Source
send_result() View Source
topic() View Source
Link to this section Callbacks
initialize(options) View Source (optional)
on_error(error, topic, data, options) View Source (optional)
on_success(result, topic, data, options) View Source (optional)
send_data(topic, data, options)
View Source
send_data(topic(), data :: term(), options()) :: send_result()
send_data(topic(), data :: term(), options()) :: send_result()