Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind sharing more about this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I totally forgot about this PR. This was probably just for passing the test case, though I’m not sure. I will look into this again ASAP. Thanks for the comment.

By the way, I made my custom phoenix plugin for this purpose and it has been working great for my team.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I made my custom phoenix plugin for this purpose and it has been working great for my team.

🚀 🥳 write some blog post or something see we learn about it 🚀 🥳

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some investigation, here's what I've figured out.
Firstly, there are only two endpoints in the test file, as both endpoints define custom event_prefix ([:external, :endpoint], [:internal, :endpoint]). So it seems like there should be only events starting with those prefixes in this file.

Next, in the phoenix_multi_router_test.exs, it looks like that it expect 2 [:internal, :endpoint, :stop] event for "/really-cool-route" that belongs to additional_routes. Before this change, there was only one event of it when it comes to the [:internal, :endpoint, :stop] event.

So how the test could pass before? Well, in my guess, there is one more "/really-cool-route" path event, though it belongs to [:external, :endpoint, :stop]. I think it was caught with the wrong metadata before my changes.

I'm not 100% sure 'cause I didn't make that test originally but I think it would make sense..

measurements: %{duration: 45265},
metadata: %{
conn: %{
Expand Down
Loading