-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial impl for connection telemetry (#3)
- Loading branch information
1 parent
049901d
commit 8f77ea4
Showing
12 changed files
with
228 additions
and
15 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
use Mix.Config | ||
|
||
if Mix.env() == :test do | ||
import_config "test.exs" | ||
end | ||
|
||
# Generated by Elixir.Gaas.Generators.Simple.ConfigConfig |
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,6 @@ | ||
use Mix.Config | ||
|
||
config :phoenix, json_library: Jason | ||
|
||
config :slipstream_honeycomb, | ||
honeycomb_sender: HoneycombSenderMock |
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,104 @@ | ||
defmodule Slipstream.Honeycomb.Connection do | ||
@moduledoc """ | ||
A GenServer that collects telemetry events from Slipstream connections and | ||
emits them to Honeycomb | ||
""" | ||
|
||
@sender Application.get_env( | ||
:slipstream_honeycomb, | ||
:honeycomb_sender, | ||
Opencensus.Honeycomb.Sender | ||
) | ||
|
||
alias Opencensus.Honeycomb.Event | ||
|
||
@event_names [ | ||
~w[slipstream connection connect stop]a, | ||
~w[slipstream connection handle stop]a | ||
] | ||
|
||
use GenServer | ||
|
||
@doc false | ||
def start_link(_args) do | ||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) | ||
end | ||
|
||
@doc false | ||
@impl GenServer | ||
def init(state) do | ||
{:ok, state, {:continue, :telemetry_attach}} | ||
end | ||
|
||
@doc false | ||
@impl GenServer | ||
def handle_continue(:telemetry_attach, state) do | ||
:telemetry.attach_many( | ||
"slipstream-honeycomb-connection-exporter", | ||
@event_names, | ||
&handle_event/4, | ||
state | ||
) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
def handle_event(event, measurements, metadata, _state) do | ||
GenServer.cast(__MODULE__, {event, measurements, metadata}) | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({event, measurements, metadata}, state) do | ||
{event, measurements, metadata} | ||
|> map_to_event() | ||
|> send_event() | ||
|
||
{:noreply, state} | ||
end | ||
|
||
defp map_to_event( | ||
{~w[slipstream connection connect stop]a, %{duration: duration}, | ||
metadata} | ||
) do | ||
%Event{ | ||
time: metadata.start_time, | ||
data: %{ | ||
state: inspect(metadata.state), | ||
traceId: metadata.trace_id, | ||
id: metadata.connection_id, | ||
durationMs: convert_time(duration) | ||
} | ||
} | ||
end | ||
|
||
defp map_to_event( | ||
{~w[slipstream connection handle stop]a, %{duration: duration}, | ||
metadata} | ||
) do | ||
%Event{ | ||
time: metadata.start_time, | ||
data: %{ | ||
state: inspect(metadata.state), | ||
traceId: metadata.trace_id, | ||
parentId: metadata.connection_id, | ||
id: metadata.span_id, | ||
durationMs: convert_time(duration), | ||
raw_message: inspect(metadata.raw_message), | ||
message: inspect(metadata.message), | ||
events: inspect(metadata.events), | ||
built_events: inspect(metadata.built_events), | ||
return: metadata.return | ||
} | ||
} | ||
end | ||
|
||
defp send_event(event) do | ||
@sender.send_batch([event]) | ||
end | ||
|
||
defp convert_time(time) do | ||
# nanoseconds but with decimals! | ||
# if we were to convert directly to msec, it'd be an integer :( | ||
System.convert_time_unit(time, :native, :microsecond) / 1_000 | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,72 @@ | ||
defmodule Slipstream.Honeycomb.ConnectionTest do | ||
use ExUnit.Case | ||
|
||
import Mox | ||
setup :verify_on_exit! | ||
@sender Application.fetch_env!(:slipstream_honeycomb, :honeycomb_sender) | ||
|
||
test "honeycomb events are emitted on telemetry events" do | ||
pid = start_supervised!(Slipstream.Honeycomb.Connection) | ||
test_proc = self() | ||
|
||
expect(@sender, :send_batch, 2, fn [event] -> | ||
send(test_proc, {:send_event, event}) | ||
|
||
{:ok, 1} | ||
end) | ||
|> allow(self(), pid) | ||
|
||
# ideally we'd have slipstream do the actual emitting here, but I don't | ||
# wanna go through all the melarky to set up a phoenix endpoint just for | ||
# this test | ||
metadata = %{ | ||
state: %{}, | ||
connection_id: "foo", | ||
trace_id: "bar", | ||
start_time: DateTime.utc_now() | ||
} | ||
|
||
duration = System.monotonic_time() - System.monotonic_time() | ||
|
||
:telemetry.execute( | ||
[:slipstream, :connection, :connect, :stop], | ||
%{duration: duration}, | ||
metadata | ||
) | ||
|
||
assert_receive {:send_event, event} | ||
|
||
assert event.time == metadata.start_time | ||
assert event.data.state == "%{}" | ||
assert event.data.traceId == metadata.trace_id | ||
assert event.data.id == metadata.connection_id | ||
|
||
metadata = %{ | ||
state: %{}, | ||
connection_id: "foo", | ||
span_id: "baz", | ||
trace_id: "bar", | ||
start_time: DateTime.utc_now(), | ||
raw_message: :connect, | ||
message: :connect, | ||
events: [], | ||
built_events: [], | ||
return: {:noreply, %{}} | ||
} | ||
|
||
:telemetry.execute( | ||
[:slipstream, :connection, :handle, :stop], | ||
%{duration: duration}, | ||
metadata | ||
) | ||
|
||
assert_receive {:send_event, event} | ||
|
||
assert event.time == metadata.start_time | ||
assert event.data.state == "%{}" | ||
assert event.data.traceId == metadata.trace_id | ||
assert event.data.parentId == metadata.connection_id | ||
assert event.data.id == metadata.span_id | ||
assert event.data.raw_message == ":connect" | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
defmodule Slipstream.Honeycomb.SenderBehaviour do | ||
@moduledoc """ | ||
A behaviour for `Opencensus.Honeycomb.Sender` that defines the `send_batch/1` | ||
function | ||
Used by mox to allow us to test emitting these events without side effects | ||
""" | ||
|
||
@callback send_batch([Opencensus.Honeycomb.Event.t()]) :: | ||
{:ok, integer()} | {:error, Exception.t()} | ||
end |
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 @@ | ||
Mox.defmock(HoneycombSenderMock, for: Slipstream.Honeycomb.SenderBehaviour) |