Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F113499
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/prometheus_phx.ex b/lib/prometheus_phx.ex
index d928242..c3f0c39 100644
--- a/lib/prometheus_phx.ex
+++ b/lib/prometheus_phx.ex
@@ -1,145 +1,145 @@
defmodule PrometheusPhx do
use Prometheus.Metric
require Logger
require Prometheus.Contrib.HTTP
alias Prometheus.Contrib.HTTP
@duration_unit :microseconds
def setup do
events = [
[:phoenix, :endpoint, :stop],
[:phoenix, :error_rendered],
[:phoenix, :channel_joined],
[:phoenix, :channel_handled_in]
]
Logger.info "Attaching the phoenix telemetry events: #{inspect events}"
:telemetry.attach_many(
"telemetry_web__event_handler",
events,
&handle_event/4,
nil
)
Histogram.declare(
name: :"phoenix_controller_call_duration_#{@duration_unit}",
help: "Whole controller pipeline execution time in #{@duration_unit}.",
labels: [:action, :controller, :status],
buckets: HTTP.microseconds_duration_buckets(),
duration_unit: @duration_unit,
registry: :default
)
Histogram.declare(
name: :"phoenix_controller_error_rendered_duration_#{@duration_unit}",
help: "View error rendering time in #{@duration_unit}.",
labels: [:action, :controller, :status],
buckets: HTTP.microseconds_duration_buckets(),
duration_unit: @duration_unit,
registry: :default
)
Histogram.declare(
name: :"phoenix_channel_join_duration_#{@duration_unit}",
help: "Phoenix channel join handler time in #{@duration_unit}",
labels: [:channel, :topic, :transport],
buckets: HTTP.microseconds_duration_buckets(),
duration_unit: @duration_unit,
registry: :default
)
Histogram.declare(
name: :"phoenix_channel_receive_duration_#{@duration_unit}",
help: "Phoenix channel receive handler time in #{@duration_unit}",
labels: [:channel, :topic, :transport, :event],
buckets: HTTP.microseconds_duration_buckets(),
duration_unit: @duration_unit,
registry: :default
)
end
def handle_event([:phoenix, :endpoint, :stop], %{duration: duration}, metadata, _config) do
labels = labels(metadata)
Logger.info "Recording event phoenix_controller_call_duration_#{@duration_unit} for #{inspect labels}"
Histogram.observe(
[
name: :"phoenix_controller_call_duration_#{@duration_unit}",
labels: labels,
registry: :default
],
duration
)
end
def handle_event([:phoenix, :error_rendered], %{duration: duration}, metadata, _config) do
labels = labels(metadata)
Histogram.observe(
[
name: :"phoenix_controller_error_rendered_duration_#{@duration_unit}",
labels: labels,
registry: :default
],
duration
)
end
def handle_event([:phoenix, :channel_joined], %{duration: duration}, metadata, _config) do
labels = labels(metadata)
Histogram.observe(
[
name: :"phoenix_channel_join_duration_#{@duration_unit}",
labels: labels,
registry: :default
],
duration
)
end
def handle_event([:phoenix, :channel_handled_in], %{duration: duration}, metadata, _config) do
labels = labels(metadata)
Histogram.observe(
[
name: :"phoenix_channel_receive_duration_#{@duration_unit}",
labels: labels,
registry: :default
],
duration
)
end
def labels(%{
status: status,
conn: %{private: %{phoenix_action: action, phoenix_controller: controller}}
}) do
- [controller, action, status]
+ [action, controller, status]
end
def labels(%{
conn: %{
status: status,
private: %{phoenix_action: action, phoenix_controller: controller}
}
}) do
- [controller, action, status]
+ [action, controller, status]
end
def labels(%{status: status, stacktrace: [{module, function, _, _} | _]}) do
- [module, function, status]
+ [function, module, status]
end
def labels(%{event: event, socket: %{channel: channel, topic: topic, transport: transport}}) do
[channel, topic, transport, event]
end
def labels(%{socket: %{channel: channel, topic: topic, transport: transport}}) do
[channel, topic, transport]
end
end
diff --git a/test/prometheus_phx_test.exs b/test/prometheus_phx_test.exs
index 88b2a84..a9419b2 100644
--- a/test/prometheus_phx_test.exs
+++ b/test/prometheus_phx_test.exs
@@ -1,105 +1,105 @@
defmodule PrometheusPhxTest do
use PrometheusPhxTestWeb.ConnCase
use PrometheusPhxTestWeb.ChannelCase
import ExUnit.CaptureLog, only: [capture_log: 1]
require Prometheus.Registry
require Logger
alias Prometheus.Metric.Histogram
describe "Channel tests" do
test "joining a channel" do
socket = socket(PrometheusPhxTestWeb.TestSocket)
assert {:ok, _payload, socket} =
subscribe_and_join(socket, PrometheusPhxTestWeb.TestChannel, "qwe:qwa")
assert {buckets, sum} =
Histogram.value(
name: :phoenix_channel_join_duration_microseconds,
labels: [PrometheusPhxTestWeb.TestChannel, "qwe:qwa", :channel_test]
)
assert sum > 200_000 and sum < 300_000
assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
assert ref = Phoenix.ChannelTest.push(socket, "invite", %{"name" => "Bob Dobbs"})
assert_reply(ref, :ok, %{"name" => "Bob Dobbs"}, 1000)
assert {buckets, sum} =
Histogram.value(
name: :phoenix_channel_receive_duration_microseconds,
labels: [
PrometheusPhxTestWeb.TestChannel,
"qwe:qwa",
:channel_test,
"invite"
]
)
assert sum > 500_000 and sum < 600_000
assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
end
end
describe "Controller tests" do
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Welcome to PrometheusPhx!"
assert {buckets, sum} =
Histogram.value(
name: :phoenix_controller_call_duration_microseconds,
- labels: [PrometheusPhxTestWeb.PageController, :index, 200]
+ labels: [:index, PrometheusPhxTestWeb.PageController, 200]
)
assert sum > 1_000_000 and sum < 1_200_000
assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
end
test "GET /error-422", %{conn: conn} do
conn = get(conn, "/error-422")
assert html_response(conn, 422) =~ "Bad Request"
assert {buckets, sum} =
Histogram.value(
name: :phoenix_controller_call_duration_microseconds,
- labels: [PrometheusPhxTestWeb.PageController, :error, 422]
+ labels: [:error, PrometheusPhxTestWeb.PageController, 422]
)
assert sum > 1_000_000 and sum < 1_200_000
assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
end
test "GET /raise-error", %{conn: conn} do
assert capture_log(fn ->
try do
get(conn, "/raise-error")
rescue
_e in RuntimeError ->
Logger.error("Internal Server Error")
end
end) =~ "Internal Server Error"
assert {buckets, sum} =
Histogram.value(
name: :phoenix_controller_call_duration_microseconds,
- labels: [PrometheusPhxTestWeb.PageController, :raise_error, 500]
+ labels: [:raise_error, PrometheusPhxTestWeb.PageController, 500]
)
assert sum > 1_000_000 and sum < 1_200_000
assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
assert {buckets, sum} =
Histogram.value(
name: :phoenix_controller_error_rendered_duration_microseconds,
- labels: [PrometheusPhxTestWeb.PageController, :raise_error, 500]
+ labels: [:raise_error, PrometheusPhxTestWeb.PageController, 500]
)
assert sum > 1 and sum < 5_000
assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
end
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 6:51 AM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39702
Default Alt Text
(8 KB)
Attached To
Mode
R25 prometheus-phx
Attached
Detach File
Event Timeline
Log In to Comment