-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a connection listener that emits telemetry events (#311)
* Add a connection listener that emits telemetry events * Add documentation * Address review comment * Add usage instructions * Fix wrong module name * Documentation improvements
- Loading branch information
Showing
3 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
defmodule DBConnection.TelemetryListener do | ||
@moduledoc """ | ||
A connection listener that emits telemetry events for connection and disconnection | ||
It monitors connection processes and ensures that disconnection events are | ||
always emitted. | ||
## Usage | ||
Start the listener, and pass it under the `:connection_listeners` option when | ||
starting DBConnection: | ||
{:ok, pid} = TelemetryListener.start_link() | ||
{:ok, _conn} = DBConnection.start_link(SomeModule, connection_listeners: [pid]) | ||
# Using a tag, which will be sent in telemetry metadata | ||
{:ok, _conn} = DBConnection.start_link(SomeModule, connection_listeners: {[pid], :my_tag}) | ||
# Or, with a Supervisor: | ||
Supervisor.start_link([ | ||
{TelemetryListener, [name: MyListener]}, | ||
DBConnection.child_spec(SomeModule, connection_listeners: {[MyListener], :my_tag}) | ||
]) | ||
## Telemetry events | ||
### Connected | ||
`[:db_connection, :connected]` - Executed after a connection is established. | ||
#### Measurements | ||
* `:count` - Always 1 | ||
#### Metadata | ||
* `:pid` - The connection pid | ||
* `:tag` - The connection pool tag | ||
### Disconnected | ||
`[:db_connection, :disconnected]` - Executed after a disconnect. | ||
#### Measurements | ||
* `:count` - Always 1 | ||
#### Metadata | ||
* `:pid` - The connection pid | ||
* `:tag` - The connection pool tag | ||
""" | ||
|
||
use GenServer | ||
|
||
@doc "Starts a telemetry listener" | ||
@spec start_link(GenServer.options()) :: {:ok, pid()} | ||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, nil, opts) | ||
end | ||
|
||
@impl GenServer | ||
def init(nil) do | ||
{:ok, %{monitoring: %{}}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({:connected, pid, tag}, state) do | ||
handle_connected(pid, tag, state) | ||
end | ||
|
||
def handle_info({:connected, pid}, state) do | ||
handle_connected(pid, nil, state) | ||
end | ||
|
||
def handle_info({:disconnected, pid, _}, state) do | ||
handle_disconnected(pid, state) | ||
end | ||
|
||
def handle_info({:disconnected, pid}, state) do | ||
handle_disconnected(pid, state) | ||
end | ||
|
||
def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do | ||
handle_disconnected(pid, state) | ||
end | ||
|
||
defp handle_connected(pid, tag, state) do | ||
:telemetry.execute([:db_connection, :connected], %{count: 1}, %{tag: tag, pid: pid}) | ||
ref = Process.monitor(pid) | ||
|
||
{:noreply, put_in(state.monitoring[pid], {ref, tag})} | ||
end | ||
|
||
defp handle_disconnected(pid, state) do | ||
case state.monitoring[pid] do | ||
# Already handled. We may receive two messages: one from monitor and one | ||
# from listener. For this reason, we need to handle both. | ||
nil -> | ||
{:noreply, state} | ||
|
||
{ref, tag} -> | ||
Process.demonitor(ref, [:flush]) | ||
:telemetry.execute([:db_connection, :disconnected], %{count: 1}, %{tag: tag, pid: pid}) | ||
{:noreply, %{state | monitoring: Map.delete(state.monitoring, pid)}} | ||
end | ||
end | ||
end |