poa_agent v0.1.0 POAAgent.Plugins.Transfer behaviour View Source

Defines a Transfer Plugin.

A Transfer plugin receives data from Collectors. It uses the Collector’s label in order to differenciate from multiple Collectors.

POAAgent app reads the Transfers configuration from the config.exs file when bootstrap and will create a process per each one of them. That configuration is referenced by :transfers key.

config :poa_agent,
   :transfers,
   [
     {name, module, args}
   ]

for example

config :poa_agent,
   :transfers,
   [
     {:my_transfer, POAAgent.Plugins.Transfers.MyTransfer, [ws_key: "mykey", other_stuff: "hello"]}
   ]

name, module and args must be defined in the configuration file.

  • name: Name for the new created process. Must be unique
  • module: Module which implements the Transfer behaviour
  • args: Initial args which will be passed to the init_transfer/1 function

Implementing A Transfer Plugin

In order to implement your Transfer Plugin you must implement 3 functions.

  • init_transfer/1: Called only once when the process starts
  • data_received/4: This function is called every time a Collector sends metrics to the Transfer
  • handle_message/1: This is called when the transfer process receives an Erlang message
  • terminate/1: Called just before stopping the process

This is a simple example of custom Transfer Plugin

defmodule POAAgent.Plugins.Transfers.MyTransfer do
  use POAAgent.Plugins.Transfer

  def init_transfer(args) do
    {:ok, :no_state}
  end

  def data_received(label, metric_type, data, state) do
    IO.puts "Received data from the collector referenced by label"
    {:ok, :no_state}
  end

  def handle_message(_message, state) do
    {:ok, state}
  end

  def terminate(_state) do
    :ok
  end

end

Link to this section Summary

Callbacks

In this callback is called when a Collector sends data to this Transfer

In this callback is called when the Transfer process receives an erlang message

A callback executed when the Transfer Plugin starts. The argument is retrieved from the configuration file when the Transfer is defined It must return {:ok, state}, that state will be keept as in GenServer and can be retrieved in the data_received/2 function

This callback is called just before the Process goes down. This is a good place for closing connections

Link to this section Callbacks

Link to this callback data_received(label, metric_type, data, state) View Source
data_received(
  label :: atom(),
  metric_type :: String.t(),
  data :: any(),
  state :: any()
) :: {:ok, any()}

In this callback is called when a Collector sends data to this Transfer.

Regarding the parameters, label identifies the Collector, metric_type is the metric type in String format, it is used in order to know which kind of data we are sending, the data is the real data received, and the state is the Collector’s state

It must return {:ok, state}.

Link to this callback handle_message(msg, state) View Source
handle_message(msg :: any(), state :: any()) :: {:ok, state :: any()}

In this callback is called when the Transfer process receives an erlang message.

It must return {:ok, state}.

Link to this callback init_transfer(args) View Source
init_transfer(args :: term()) :: {:ok, any()}

A callback executed when the Transfer Plugin starts. The argument is retrieved from the configuration file when the Transfer is defined It must return {:ok, state}, that state will be keept as in GenServer and can be retrieved in the data_received/2 function.

Link to this callback terminate(state) View Source
terminate(state :: term()) :: term()

This callback is called just before the Process goes down. This is a good place for closing connections.