Skip to content

Commit

Permalink
add host tag to phoenix plugin to handle paths depended on a subdomain (
Browse files Browse the repository at this point in the history
#183)

* add :host tag to http_metrics_tags in phoenix plugin

* tweak phoenix plugin test: adding event, update metrics

* fix phoeni_multi_router test: event and metrics

* fix ets cron flusher test
  • Loading branch information
bangalcat authored Apr 20, 2023
1 parent a223be3 commit 6a002fb
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 213 deletions.
10 changes: 6 additions & 4 deletions lib/prom_ex/plugins/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ if Code.ensure_loaded?(Phoenix) do
defp http_events(metric_prefix, opts) do
routers = fetch_routers!(opts)
additional_routes = fetch_additional_routes!(opts)
http_metrics_tags = [:status, :method, :path, :controller, :action]
http_metrics_tags = [:status, :method, :path, :controller, :action, :host]
duration_unit = Keyword.get(opts, :duration_unit, :millisecond)
duration_unit_plural = Utils.make_plural_atom(duration_unit)

Expand Down Expand Up @@ -402,7 +402,8 @@ if Code.ensure_loaded?(Phoenix) do
|> do_get_router_info(routers, default_route_tags)
|> Map.merge(%{
status: conn.status,
method: conn.method
method: conn.method,
host: conn.host
})

_ ->
Expand All @@ -419,7 +420,8 @@ if Code.ensure_loaded?(Phoenix) do
|> do_get_router_info(routers, default_route_tags)
|> Map.merge(%{
status: conn.status,
method: conn.method
method: conn.method,
host: conn.host
})

_ ->
Expand All @@ -430,7 +432,7 @@ if Code.ensure_loaded?(Phoenix) do
defp do_get_router_info(conn, routers, default_route_tags) do
routers
|> Enum.find_value(default_route_tags, fn router ->
case Phoenix.Router.route_info(router, conn.method, conn.request_path, "") do
case Phoenix.Router.route_info(router, conn.method, conn.request_path, conn.host) do
:error ->
false

Expand Down
20 changes: 10 additions & 10 deletions test/prom_ex/ets_cron_flusher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ defmodule PromEx.ETSCronFlusherTest do

Events.execute_all(:phoenix)

assert length(get_metrics_table(DefaultPromExSetUp)) == 5
assert length(get_dist_table(DefaultPromExSetUp)) == 42
assert length(get_metrics_table(DefaultPromExSetUp)) == 6
assert length(get_dist_table(DefaultPromExSetUp)) == 44

Events.execute_all(:phoenix)

assert length(get_metrics_table(DefaultPromExSetUp)) == 5
assert length(get_dist_table(DefaultPromExSetUp)) == 84
assert length(get_metrics_table(DefaultPromExSetUp)) == 6
assert length(get_dist_table(DefaultPromExSetUp)) == 88

Process.sleep(8_000)

assert length(get_metrics_table(DefaultPromExSetUp)) == 13
assert length(get_metrics_table(DefaultPromExSetUp)) == 16
assert get_dist_table(DefaultPromExSetUp) == []

new_timer_ref = get_timer_ref(DefaultPromExSetUp)
Expand All @@ -72,17 +72,17 @@ defmodule PromEx.ETSCronFlusherTest do

Events.execute_all(:phoenix)

assert length(get_metrics_table(ManualPromExSetUp)) == 5
assert length(get_dist_table(ManualPromExSetUp)) == 42
assert length(get_metrics_table(ManualPromExSetUp)) == 6
assert length(get_dist_table(ManualPromExSetUp)) == 44

Events.execute_all(:phoenix)

assert length(get_metrics_table(ManualPromExSetUp)) == 5
assert length(get_dist_table(ManualPromExSetUp)) == 84
assert length(get_metrics_table(ManualPromExSetUp)) == 6
assert length(get_dist_table(ManualPromExSetUp)) == 88

Process.sleep(3_500)

assert length(get_metrics_table(ManualPromExSetUp)) == 13
assert length(get_metrics_table(ManualPromExSetUp)) == 16
assert get_dist_table(ManualPromExSetUp) == []

new_timer_ref = get_timer_ref(ManualPromExSetUp)
Expand Down
111 changes: 111 additions & 0 deletions test/support/events/phoenix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4120,5 +4120,116 @@
},
options: []
}
},
%{
event: [:phoenix, :endpoint, :stop],
measurements: %{duration: 335_366_105},
metadata: %{
conn: %{
__struct__: Plug.Conn,
adapter:
{Plug.Cowboy.Conn,
%{
bindings: %{},
body_length: 0,
cert: :undefined,
has_body: false,
headers: %{
"accept" =>
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding" => "gzip, deflate, br",
"accept-language" => "en-US,en;q=0.9",
"cache-control" => "no-cache",
"connection" => "keep-alive",
"cookie" =>
"_web_app_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYNjI2TEUteGtpeDcwczNzMXdiT2FUakpz.dv3_U0ixALpjrQ5DsBqexNfeQ2pPPH0v0WHUD6BU1Ik",
"host" => "otherhost:4000",
"pragma" => "no-cache",
"sec-fetch-dest" => "document",
"sec-fetch-mode" => "navigate",
"sec-fetch-site" => "none",
"sec-fetch-user" => "?1",
"sec-gpc" => "1",
"upgrade-insecure-requests" => "1",
"user-agent" =>
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
},
host: "otherhost",
host_info: :undefined,
method: "GET",
path: "/",
path_info: :undefined,
peer: {{127, 0, 0, 1}, 45816},
pid: nil,
port: 4000,
qs: "",
ref: WebAppWeb.Endpoint.HTTP,
scheme: "http",
sock: {{127, 0, 0, 1}, 4000},
streamid: 20,
version: :"HTTP/1.1"
}},
assigns: %{},
before_send: [],
body_params: %{__struct__: Plug.Conn.Unfetched, aspect: :body_params},
cookies: %{
"_web_app_key" =>
"SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYNjI2TEUteGtpeDcwczNzMXdiT2FUakpz.dv3_U0ixALpjrQ5DsBqexNfeQ2pPPH0v0WHUD6BU1Ik"
},
halted: false,
host: "otherhost",
method: "GET",
owner: nil,
params: %{},
path_info: :undefined,
path_params: %{},
port: 4000,
private: %{
phoenix_endpoint: WebAppWeb.Endpoint,
phoenix_request_logger: {"request_logger", "request_logger"}
},
query_params: %{},
query_string: "",
remote_ip: {127, 0, 0, 1},
req_cookies: %{
"_web_app_key" =>
"SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYNjI2TEUteGtpeDcwczNzMXdiT2FUakpz.dv3_U0ixALpjrQ5DsBqexNfeQ2pPPH0v0WHUD6BU1Ik"
},
req_headers: [
{"accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"},
{"accept-encoding", "gzip, deflate, br"},
{"accept-language", "en-US,en;q=0.9"},
{"cache-control", "no-cache"},
{"connection", "keep-alive"},
{"cookie",
"_web_app_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYNjI2TEUteGtpeDcwczNzMXdiT2FUakpz.dv3_U0ixALpjrQ5DsBqexNfeQ2pPPH0v0WHUD6BU1Ik"},
{"host", "otherhost:4000"},
{"pragma", "no-cache"},
{"sec-fetch-dest", "document"},
{"sec-fetch-mode", "navigate"},
{"sec-fetch-site", "none"},
{"sec-fetch-user", "?1"},
{"sec-gpc", "1"},
{"upgrade-insecure-requests", "1"},
{"user-agent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"}
],
request_path: "/",
resp_body: ["{\"", [[] | "a"], "\":", "10", 125],
resp_cookies: %{},
resp_headers: [
{"content-type", "application/json; charset=utf-8"},
{"cache-control", "max-age=0, private, must-revalidate"},
{"x-request-id", "FoZ8T-7wK6JKr24AAAYP"}
],
scheme: :http,
script_name: [],
secret_key_base: "5fBSdz+TtF5BpvdQA4BVXsADOz4AUIrUeUPDy4CUpZb37kCLgrLT0Tfhq7fBT5TC",
state: :set,
status: 200
},
options: []
}
}
]
2 changes: 1 addition & 1 deletion test/support/events/phoenix_multi_router.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@
}
},
%{
event: [:phoenix, :endpoint, :stop],
event: [:internal, :endpoint, :stop],
measurements: %{duration: 45265},
metadata: %{
conn: %{
Expand Down
Loading

0 comments on commit 6a002fb

Please sign in to comment.