Skip to content

Commit

Permalink
test for :phoenix_controller_render instrumenter
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Jul 30, 2017
1 parent 06415fe commit 33baba1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
30 changes: 24 additions & 6 deletions test/prometheus_phoenix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,38 @@ defmodule PrometheusPhoenixTest do
test "Default config" do
conn = get build_conn(), "/qwe"
assert html_response(conn, 200) =~ "qwe"
assert {buckets, sum} = Histogram.value([name: :phoenix_controller_call_duration_microseconds,
labels: ["PrometheusPhoenixTest.Controller", :qwe]])
assert (sum > 1000000 and sum < 1200000)
assert {buckets, sum} =
Histogram.value([name: :phoenix_controller_call_duration_microseconds,
labels: ["PrometheusPhoenixTest.Controller", :qwe]])
assert (sum > 1_000_000 and sum < 1_200_000)
assert 1 = Enum.reduce(buckets, fn(x, acc) -> x + acc end)
end

test "Custom config" do
conn = get build_conn(), "/qwe"
assert html_response(conn, 200) =~ "qwe"
assert {buckets, sum} = Histogram.value([name: :phoenix_controller_call_duration_seconds,
labels: ["PrometheusPhoenixTest.Controller", "custom_label"],
registry: :qwe])
assert {buckets, sum} =
Histogram.value([name: :phoenix_controller_call_duration_seconds,
labels: ["PrometheusPhoenixTest.Controller", "custom_label"],
registry: :qwe])
assert (sum > 1 and sum < 1.2)
assert 3 = length(buckets)
assert 1 = Enum.reduce(buckets, fn(x, acc) -> x + acc end)
end

test "Default config view" do
conn = get build_conn(), "/qwe_view"
assert html_response(conn, 200) =~ "Hello John Doe"
assert {buckets, sum} =
Histogram.value([name: :phoenix_controller_call_duration_microseconds,
labels: ["PrometheusPhoenixTest.Controller", :qwe_view]])
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_render_duration_microseconds,
labels: [PrometheusPhoenixTest.View, "qwe_view.html", "html"]])
assert (sum > 0 and sum < 100_000)
assert 1 = Enum.reduce(buckets, fn(x, acc) -> x + acc end)
end
end
1 change: 1 addition & 0 deletions test/templates/qwe_view.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello <%= @name %>
12 changes: 12 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ExUnit.start()
:application.ensure_all_started(:plug)
:application.ensure_all_started(:phoenix)

Application.put_env(:phoenix, PrometheusPhoenixTest.Endpoint,
[instrumenters: [TestPhoenixInstrumenter,
Expand Down Expand Up @@ -26,13 +28,18 @@ end
defmodule PrometheusPhoenixTest.Router do
use Phoenix.Router
get "/qwe", PrometheusPhoenixTest.Controller, :qwe
get "/qwe_view", PrometheusPhoenixTest.Controller, :qwe_view
end

defmodule PrometheusPhoenixTest.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix
plug PrometheusPhoenixTest.Router
end

defmodule PrometheusPhoenixTest.View do
use Phoenix.View, root: "test/templates"
end

defmodule PrometheusPhoenixTest.Controller do
use Phoenix.Controller

Expand All @@ -42,6 +49,11 @@ defmodule PrometheusPhoenixTest.Controller do
|> put_resp_content_type("text/html")
|> send_resp(200, "qwe")
end

def qwe_view(conn, _params) do
Process.sleep(1000)
render(conn, PrometheusPhoenixTest.View, "qwe_view.html", name: "John Doe", layout: false)
end
end

PrometheusPhoenixTest.Endpoint.start_link

0 comments on commit 33baba1

Please sign in to comment.