Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F114040
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
22 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/.formatter.exs b/.formatter.exs
new file mode 100644
index 0000000..d2cda26
--- /dev/null
+++ b/.formatter.exs
@@ -0,0 +1,4 @@
+# Used by "mix format"
+[
+ inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
+]
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..49f0bff
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,24 @@
+# The directory Mix will write compiled artifacts to.
+/_build/
+
+# If you run "mix test --cover", coverage assets end up here.
+/cover/
+
+# The directory Mix downloads your dependencies sources to.
+/deps/
+
+# Where third-party dependencies like ExDoc output generated docs.
+/doc/
+
+# Ignore .fetch files in case you like to edit your project deps locally.
+/.fetch
+
+# If the VM crashes, it generates a dump, let's ignore it too.
+erl_crash.dump
+
+# Also ignore archive artifacts (built via "mix archive.build").
+*.ez
+
+# Ignore package tarball (built via "mix hex.build").
+prometheus_phx-*.tar
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c6d0cda
--- /dev/null
+++ b/README.md
@@ -0,0 +1,21 @@
+# PrometheusPhx
+
+**TODO: Add description**
+
+## Installation
+
+If [available in Hex](https://hex.pm/docs/publish), the package can be installed
+by adding `prometheus_phx` to your list of dependencies in `mix.exs`:
+
+```elixir
+def deps do
+ [
+ {:prometheus_phx, "~> 0.1.0"}
+ ]
+end
+```
+
+Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
+and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
+be found at [https://hexdocs.pm/prometheus_phx](https://hexdocs.pm/prometheus_phx).
+
diff --git a/config/config.exs b/config/config.exs
new file mode 100644
index 0000000..3d6e6bd
--- /dev/null
+++ b/config/config.exs
@@ -0,0 +1,7 @@
+use Mix.Config
+
+config :logger, :console,
+ format: "$time $metadata[$level] $message\n",
+ metadata: [:request_id]
+
+import_config "#{Mix.env()}.exs"
diff --git a/config/dev.exs b/config/dev.exs
new file mode 100644
index 0000000..e472324
--- /dev/null
+++ b/config/dev.exs
@@ -0,0 +1,4 @@
+use Mix.Config
+
+# Do not include metadata nor timestamps in development logs
+config :logger, :console, format: "[$level] $message\n"
diff --git a/config/test.exs b/config/test.exs
new file mode 100644
index 0000000..9ca6e97
--- /dev/null
+++ b/config/test.exs
@@ -0,0 +1,7 @@
+use Mix.Config
+
+# Use Jason for JSON parsing in Phoenix
+config :phoenix, :json_library, Jason
+
+# Print only warnings and errors during test
+config :logger, level: :warn
diff --git a/lib/prometheus_phx.ex b/lib/prometheus_phx.ex
new file mode 100644
index 0000000..a8827b6
--- /dev/null
+++ b/lib/prometheus_phx.ex
@@ -0,0 +1,141 @@
+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]
+ ]
+
+ :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)
+
+ 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]
+ end
+
+ def labels(%{
+ conn: %{
+ status: status,
+ private: %{phoenix_action: action, phoenix_controller: controller}
+ }
+ }) do
+ [controller, action, status]
+ end
+
+ def labels(%{status: status, stacktrace: [{module, function, _, _} | _]}) do
+ [module, function, 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/mix.exs b/mix.exs
new file mode 100644
index 0000000..f10cfbb
--- /dev/null
+++ b/mix.exs
@@ -0,0 +1,33 @@
+defmodule PrometheusPhx.MixProject do
+ use Mix.Project
+
+ def project do
+ [
+ app: :prometheus_phx,
+ version: "0.1.0",
+ elixir: "~> 1.10",
+ start_permanent: Mix.env() == :prod,
+ elixirc_paths: elixirc_paths(Mix.env()),
+ compilers: compilers(Mix.env()),
+ deps: deps()
+ ]
+ end
+
+ defp elixirc_paths(:test), do: ["lib", "test/support"]
+ defp elixirc_paths(_), do: ["lib"]
+
+ defp deps do
+ [
+ {:prometheus_ex, "~> 3.0"},
+ {:phoenix, "~> 1.5.1", only: [:test]},
+ {:phoenix_html, "~> 2.11", only: [:test]},
+ {:telemetry_metrics, "~> 0.4", only: [:test]},
+ {:telemetry_poller, "~> 0.4", only: [:test]},
+ {:jason, "~> 1.0", only: [:test]},
+ {:plug_cowboy, "~> 2.0", only: [:test]}
+ ]
+ end
+
+ def compilers(:test), do: [:phoenix] ++ Mix.compilers()
+ def compilers(_), do: Mix.compilers()
+end
diff --git a/mix.lock b/mix.lock
new file mode 100644
index 0000000..4c753bd
--- /dev/null
+++ b/mix.lock
@@ -0,0 +1,18 @@
+%{
+ "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"},
+ "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"},
+ "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
+ "mime": {:hex, :mime, "1.4.0", "5066f14944b470286146047d2f73518cf5cca82f8e4815cf35d196b58cf07c47", [:mix], [], "hexpm", "75fa42c4228ea9a23f70f123c74ba7cece6a03b1fd474fe13f6a7a85c6ea4ff6"},
+ "phoenix": {:hex, :phoenix, "1.5.4", "0fca9ce7e960f9498d6315e41fcd0c80bfa6fbeb5fa3255b830c67fdfb7e703f", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4e516d131fde87b568abd62e1b14aa07ba7d5edfd230bab4e25cc9dedbb39135"},
+ "phoenix_html": {:hex, :phoenix_html, "2.14.2", "b8a3899a72050f3f48a36430da507dd99caf0ac2d06c77529b1646964f3d563e", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "58061c8dfd25da5df1ea0ca47c972f161beb6c875cd293917045b92ffe1bf617"},
+ "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"},
+ "plug": {:hex, :plug, "1.10.4", "41eba7d1a2d671faaf531fa867645bd5a3dce0957d8e2a3f398ccff7d2ef017f", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad1e233fe73d2eec56616568d260777b67f53148a999dc2d048f4eb9778fe4a0"},
+ "plug_cowboy": {:hex, :plug_cowboy, "2.3.0", "149a50e05cb73c12aad6506a371cd75750c0b19a32f81866e1a323dda9e0e99d", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bc595a1870cef13f9c1e03df56d96804db7f702175e4ccacdb8fc75c02a7b97e"},
+ "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"},
+ "prometheus": {:hex, :prometheus, "4.6.0", "20510f381db1ccab818b4cf2fac5fa6ab5cc91bc364a154399901c001465f46f", [:mix, :rebar3], [], "hexpm", "4905fd2992f8038eccd7aa0cd22f40637ed618c0bed1f75c05aacec15b7545de"},
+ "prometheus_ex": {:hex, :prometheus_ex, "3.0.5", "fa58cfd983487fc5ead331e9a3e0aa622c67232b3ec71710ced122c4c453a02f", [:mix], [{:prometheus, "~> 4.0", [hex: :prometheus, repo: "hexpm", optional: false]}], "hexpm", "9fd13404a48437e044b288b41f76e64acd9735fb8b0e3809f494811dfa66d0fb"},
+ "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
+ "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
+ "telemetry_metrics": {:hex, :telemetry_metrics, "0.5.0", "1b796e74add83abf844e808564275dfb342bcc930b04c7577ab780e262b0d998", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31225e6ce7a37a421a0a96ec55244386aec1c190b22578bd245188a4a33298fd"},
+ "telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"},
+}
diff --git a/test/prometheus_phx_test.exs b/test/prometheus_phx_test.exs
new file mode 100644
index 0000000..88b2a84
--- /dev/null
+++ b/test/prometheus_phx_test.exs
@@ -0,0 +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]
+ )
+
+ 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]
+ )
+
+ 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]
+ )
+
+ 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]
+ )
+
+ assert sum > 1 and sum < 5_000
+ assert 1 = Enum.reduce(buckets, fn x, acc -> x + acc end)
+ end
+ end
+end
diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex
new file mode 100644
index 0000000..645e3ff
--- /dev/null
+++ b/test/support/channel_case.ex
@@ -0,0 +1,34 @@
+defmodule PrometheusPhxTestWeb.ChannelCase do
+ @moduledoc """
+ This module defines the test case to be used by
+ channel tests.
+
+ Such tests rely on `Phoenix.ChannelTest` and also
+ import other functionality to make it easier
+ to build common data structures and query the data layer.
+
+ Finally, if the test case interacts with the database,
+ we enable the SQL sandbox, so changes done to the database
+ are reverted at the end of every test. If you are using
+ PostgreSQL, you can even run database tests asynchronously
+ by setting `use PrometheusPhxTestWeb.ChannelCase, async: true`, although
+ this option is not recommended for other databases.
+ """
+
+ use ExUnit.CaseTemplate
+
+ using do
+ quote do
+ # Import conveniences for testing with channels
+ import Phoenix.ChannelTest
+ import PrometheusPhxTestWeb.ChannelCase
+
+ # The default endpoint for testing
+ @endpoint PrometheusPhxTestWeb.Endpoint
+ end
+ end
+
+ setup _tags do
+ :ok
+ end
+end
diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex
new file mode 100644
index 0000000..0857c41
--- /dev/null
+++ b/test/support/conn_case.ex
@@ -0,0 +1,37 @@
+defmodule PrometheusPhxTestWeb.ConnCase do
+ @moduledoc """
+ This module defines the test case to be used by
+ tests that require setting up a connection.
+
+ Such tests rely on `Phoenix.ConnTest` and also
+ import other functionality to make it easier
+ to build common data structures and query the data layer.
+
+ Finally, if the test case interacts with the database,
+ we enable the SQL sandbox, so changes done to the database
+ are reverted at the end of every test. If you are using
+ PostgreSQL, you can even run database tests asynchronously
+ by setting `use PrometheusPhxTestWeb.ConnCase, async: true`, although
+ this option is not recommended for other databases.
+ """
+
+ use ExUnit.CaseTemplate
+
+ using do
+ quote do
+ # Import conveniences for testing with connections
+ import Plug.Conn
+ import Phoenix.ConnTest
+ import PrometheusPhxTestWeb.ConnCase
+
+ alias PrometheusPhxTestWeb.Router.Helpers, as: Routes
+
+ # The default endpoint for testing
+ @endpoint PrometheusPhxTestWeb.Endpoint
+ end
+ end
+
+ setup _tags do
+ {:ok, conn: Phoenix.ConnTest.build_conn()}
+ end
+end
diff --git a/test/support/prometheus_phx_test_web.ex b/test/support/prometheus_phx_test_web.ex
new file mode 100644
index 0000000..f5e84b4
--- /dev/null
+++ b/test/support/prometheus_phx_test_web.ex
@@ -0,0 +1,111 @@
+defmodule PrometheusPhxTestWeb.Router do
+ use Phoenix.Router
+
+ import Plug.Conn
+ import Phoenix.Controller
+
+ scope "/", PrometheusPhxTestWeb do
+ get("/", PageController, :index)
+ get("/error-422", PageController, :error)
+ get("/raise-error", PageController, :raise_error)
+ end
+end
+
+defmodule PrometheusPhxTestWeb.Endpoint do
+ use Phoenix.Endpoint, otp_app: :prometheus_phx_test
+
+ socket("/socket", PrometheusPhxTestWeb.TestSocket,
+ websocket: true,
+ longpoll: false
+ )
+
+ plug(Plug.RequestId)
+ plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
+ plug(PrometheusPhxTestWeb.Router)
+end
+
+defmodule PrometheusPhxTestWeb.TestSocket do
+ use Phoenix.Socket
+
+ channel("qwe:*", PrometheusPhxTestWeb.TestChannel)
+
+ @impl true
+ def connect(_params, socket, _connect_info) do
+ {:ok, socket}
+ end
+
+ @impl true
+ def id(_socket), do: nil
+end
+
+defmodule PrometheusPhxTestWeb.TestChannel do
+ use Phoenix.Channel
+
+ def join("qwe:qwa", _payload, socket) do
+ Process.sleep(200)
+ {:ok, socket}
+ end
+
+ def handle_in("invite", payload, socket) do
+ Process.sleep(500)
+ {:reply, {:ok, payload}, socket}
+ end
+end
+
+defmodule PrometheusPhxTestWeb.PageController do
+ use Phoenix.Controller, namespace: PrometheusPhxTestWeb
+
+ def index(conn, _params) do
+ Process.sleep(1000)
+ render(conn, "index.html")
+ end
+
+ def error(conn, _params) do
+ Process.sleep(1000)
+
+ conn
+ |> put_status(422)
+ |> put_view(PrometheusPhxTestWeb.ErrorView)
+ |> render("422.html")
+ end
+
+ def raise_error(_conn, _params) do
+ Process.sleep(1000)
+ raise "Internal Server Error"
+ end
+end
+
+defmodule PrometheusPhxTestWeb.PageView do
+ use Phoenix.View,
+ root: "test/support/templates"
+end
+
+defmodule PrometheusPhxTestWeb.LayoutView do
+ use Phoenix.View,
+ root: "test/support/templates"
+end
+
+defmodule PrometheusPhxTestWeb.ErrorView do
+ use Phoenix.View,
+ root: "test/support/templates"
+
+ def render("404.html", _assigns), do: "Not Found"
+ def render("422.html", _assigns), do: "Bad Request"
+ def render("500.html", _assigns), do: "Internal Server Error"
+end
+
+defmodule PrometheusPhxTest.Application do
+ use Application
+
+ def start(_type, _args) do
+ children = [
+ {Phoenix.PubSub, name: PrometheusPhxTest.PubSub},
+ PrometheusPhxTestWeb.Endpoint
+ ]
+
+ PrometheusPhx.setup()
+
+ opts = [strategy: :one_for_one, name: PrometheusPhxTest.Supervisor]
+ Supervisor.start_link(children, opts)
+ end
+end
diff --git a/test/support/templates/layout/app.html.eex b/test/support/templates/layout/app.html.eex
new file mode 100644
index 0000000..602ff47
--- /dev/null
+++ b/test/support/templates/layout/app.html.eex
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8"/>
+ <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+ <title>PrometheusPhx Test </title>
+ </head>
+ <body>
+ <main role="main" class="container">
+ <%= @inner_content %>
+ </main>
+ </body>
+</html>
diff --git a/test/support/templates/page/index.html.eex b/test/support/templates/page/index.html.eex
new file mode 100644
index 0000000..d164001
--- /dev/null
+++ b/test/support/templates/page/index.html.eex
@@ -0,0 +1,4 @@
+<section class="phx-hero">
+ <h1>Welcome to PrometheusPhx!</h1>
+ <p>Writing your phoenix telemetry events to prometheus since 1.5</p>
+</section>
diff --git a/test/test_helper.exs b/test/test_helper.exs
new file mode 100644
index 0000000..c1eb7ef
--- /dev/null
+++ b/test/test_helper.exs
@@ -0,0 +1,21 @@
+ExUnit.start()
+
+# Set the config for the prometheus-phx test web application
+Application.put_env(:prometheus_phx_test, PrometheusPhxTestWeb.Endpoint,
+ pubsub_server: PrometheusPhxTest.PubSub,
+ render_errors: [
+ view: PrometheusPhxTestWeb.ErrorView,
+ accepts: ~w(html json),
+ layout: false
+ ],
+ server: false,
+ debug_errors: false
+)
+
+# Make sure everything is started
+Application.ensure_started(:logger)
+Application.ensure_started(:plug)
+Application.ensure_started(:phoenix)
+
+# Start the web application
+PrometheusPhxTest.Application.start(nil, nil)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 6:49 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40011
Default Alt Text
(22 KB)
Attached To
Mode
R25 prometheus-phx
Attached
Detach File
Event Timeline
Log In to Comment