Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add port monitoring #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions lib/statix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ defmodule Statix do
end

This will make `MyApp.Statix` a Statix connection that implements the `Statix`
behaviour. This connection can be started with the `MyApp.Statix.connect/1`
function (see the `c:connect/1` callback) and a few functions can be called on
it to report metrics to the StatsD-compatible server read from the
configuration. Usually, `connect/1` is called in your application's
`c:Application.start/2` callback:
behaviour. A few functions can be called on it to report metrics to the
StatsD-compatible server read from the configuration. The connection can be
started by including `MyApp.Statix` in your application's `c:Application.start/2`
callback's list of supervised children:

def start(_type, _args) do
:ok = MyApp.Statix.connect()
children = [
# ...
MyApp.Statix
]

# ...
Supervisor.start_link(children, strategy: :one_for_one)
end

## Configuration
Expand Down Expand Up @@ -306,6 +309,10 @@ defmodule Statix do
end

quote location: :keep do
use GenServer

require Logger

@behaviour Statix

unquote(current_statix)
Expand Down Expand Up @@ -342,6 +349,21 @@ defmodule Statix do
Statix.transmit(current_statix(), :set, key, val, options)
end

def init(_init_arg) do
Process.flag(:trap_exit, true)
connect()
{:ok, []}
end

def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts)
end

def handle_info({:EXIT, port, reason}, %Statix.Conn{sock: __MODULE__} = state) do
Logger.error("Port #{inspect(port)} exited with reason #{reason}")
{:stop, :normal, state}
end

defoverridable(
increment: 3,
decrement: 3,
Expand Down