Zarathustra2

Zarathustra2

Say we have the following router definition

get "/user/:id", UserController, :show

and we have the api request /user/some-id. We can get already the current path from the Plug connection but we cannot get the matched routed from my knowledge.

Getting the matched routed would be nice so that if we persist all requests for analytical purpose we could also save the matched route with the actual route, something like:

route | matched_route | duration_ms | created_at | status | method

That would allow for some nice analytics.

If that is already possible, I would appreciate if someone can point me in that correct direction otherwise I would happily take a look at implementing it.

Showing Posts 1 to 3

Eiji

Eiji

You can fetch most if not all of those data. First of all Phoenix somehow needs to know what should be applied and there is no magic here. Simply Phoenix stores those data somewhere. Where exactly … it’s private - I mean the field in Plug.Conn struct. :wink:

Following naming private you should not access those data unless some public function do that for you as there is no guarantee that the structure of private data would be kept in next releases.

I would recommend to check the documentation, just for example we have: Phoenix.Controller.action_name/1. If you take a look at source code it fetches a value of phoenix_action from private data:

https://github.com/phoenixframework/phoenix/blob/1d8874760c45bbb885f9ebbb2bf49e7fa6a3745d/lib/phoenix/controller.ex#L320-L324

In same module there are functions to fetch Controller, Endpoint and Router modules.

The Router also allows you to fetch routing information using Phoenix.Router.routes/1

Take a deep look at Phoenix documentation and find things you think would be useful for your use case.

Zarathustra2

Zarathustra2 OP

I see, Phoenix.Router.route_info(conn.private[:phoenix_router], conn.method, conn.request_path, "") should do the trick, thanks.

LostKobrakai

LostKobrakai

Instead of doing the lookup twice you could also use Plug.Conn.register_before_send and pull out the information you need from the conn, which should then be available.

— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews