Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access plug_conn assigns and params in report_event? #113

Closed
blunckr-aj opened this issue Jan 14, 2025 · 6 comments
Closed

Access plug_conn assigns and params in report_event? #113

blunckr-aj opened this issue Jan 14, 2025 · 6 comments

Comments

@blunckr-aj
Copy link

Hello, I'm currently working on adding this package to my app with the rollbar client, and I have a question. plug_conn gets included in the event, which is very useful, but it's missing some information. My application reads some information about the user from the request body and puts in the 'assigns' on the conn object. In the report_event function, 'assigns' is empty, and other fields are not populated as they would be during the request:

assigns: %{},
body_params: %Plug.Conn.Unfetched{aspect: :body_params},
params: %Plug.Conn.Unfetched{aspect: :params},
query_params: %Plug.Conn.Unfetched{aspect: :query_params},

I don't know exactly how you're accessing the conn object, but this seems to be the conn before it has passed through the various plugs that parse and assign the body, params, and query params, and in my case, set some 'assigns' values.

Is it possible that those values will be assigned in the future, or could another plug be written in my app that would somehow make them accessible to Tower? (Seems like #94 might be relevant?)

Thanks for your work on this project, it seems like a great solution.

@grzuy
Copy link
Collaborator

grzuy commented Jan 14, 2025

Hi @blunckr-aj,

Thanks for writing.

So the event you're testing out is an exception?
Are you sure values are put on plug conn assigns before the exception is raised?

Also, are you using bandit or plug_cowboy? Which version?

Thanks.

@grzuy
Copy link
Collaborator

grzuy commented Jan 14, 2025

Are you implementing a custom rollbar client yourself or using tower_rollbar?

@grzuy
Copy link
Collaborator

grzuy commented Jan 14, 2025

Note that some Plug.Conn fields need to be fetched to be read (https://hexdocs.pm/plug/1.16.1/Plug.Conn.html#module-fetchable-fields).

FWIW, this is what we're currently doing in tower_rollbar: https://github.com/mimiquate/tower_rollbar/blob/220c1f2780952ae81d4cf01a9d87293e3470bc69/lib/tower_rollbar/rollbar/item.ex#L114-L130.

@blunckr-aj
Copy link
Author

blunckr-aj commented Jan 14, 2025

Thanks for the quick responses! I'm using plug_cowboy 2.7. I'm raising an exception in my controller action, and the assigns are set in plug just before then.

I'm using the tower_rollbar package, but was trying to build my own reporter. I want to add some more fields to the item before it goes out to rollbar. I didn't see a way to do that without writing my own reporter. I thought about including that as a feature request, but didn't want to complicate the issue.

My reporter calls TowerRollbar.Rollbar.Item.from_event, then adds additional fields. I'll have a look at your code and see if I can do something similar.

@grzuy
Copy link
Collaborator

grzuy commented Jan 14, 2025

Gotcha.
That's cool.

Most probably params (things that come from the original request) could be fetched in the report_event function, as tower_rollbar does.

I don't think we would be able to get the Plug.Conn "instance" value that has the added assigns.

Note that when you conn = Plug.Conn.assign(conn, :some, :value) you're creating a new plug conn struct different from the original one. Plug Cowboy does report the original connection as-is before calling the user plug (your application) in https://github.com/elixir-plug/plug_cowboy/blob/master/lib/plug/cowboy/handler.ex#L23.

So yes, a different approach needed.

If you're wanting to set some request context for a potential exception report you could use Logger.metadata.

After we added #98 we support you configuring config :tower, :logger_metadata, [:user_id] to get any Logger.metadata(user_id: 123) automatically picked up and reported in Tower.Event.metadata.

Sorry this is not well documented.

@blunckr-aj
Copy link
Author

Ah, ok. I think Logger.metadata will do what I need. I can set some some values at the same time I set the assigns. Then I don't have to worry about re-constructing that data from the request. Thanks!

Feel free to close the issue if you want.

@grzuy grzuy pinned this issue Jan 15, 2025
@grzuy grzuy closed this as completed Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants