Plugs and their "contract" in a pipeline

49 views
Skip to first unread message

easco

unread,
Apr 15, 2015, 2:29:31 PM4/15/15
to elixir-l...@googlegroups.com
For an HTTP-based, server-side API I have a need to log all the incoming requests, including the request bodies.

My first thought was to use a Plug in the API Pipeline and have that plug do the logging.

The tricky part is logging the request body.  From what I can tell you can only retrieve the body from the Plug.Conn once.  That means that I would have to retrieve the body in the logging component (using Plug.Conn.read_body/2) and then add the data to the Conn as a custom key or something. This seems "wrong" because it seemingly breaks a contract for any plugs farther down.  They can no longer retrieve the body using the same functions.  They have to know to look for the body of the request somewhere else.

By the same token, this must be how Plug.Parsers work right?  A Plug parser must read the body to parse it.  That would imply that any plugs that follow the parser in a pipeline must "know" that the body is no longer available through Plug.Conn.read_body/2.

I guess my question is... is this "normal".  Is it common for a Plug to modify the connection in some way that only plugs that are aware of the modification can follow it in a pipeline?

Scott

P.S.  It could be the case that handling this level of logging in a Plug is just the Wrong Thing™ to do.  I'm certainly open to that possibility too.

José Valim

unread,
Apr 15, 2015, 2:44:21 PM4/15/15
to elixir-l...@googlegroups.com
Hello Easco!

The reason we discard the body is because the body can be very large. For example, you could do a 100MB file upload. In your case, are you expecting only some particular body format? Or are you receiving many but you should log just one of them? If so, which?

Depending on your reply, a custom plug may indeed be the best solution.



José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/5e372fee-c01f-4f9e-aef8-d839acaec80c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Valim

unread,
Apr 15, 2015, 2:45:18 PM4/15/15
to elixir-l...@googlegroups.com
s/Easco/Scott

Sorry, I went with whatever my gmail client was showing me.



José Valim
Skype: jv.ptec
Founder and Lead Developer

easco

unread,
Apr 15, 2015, 4:08:04 PM4/15/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Oh I understand why the body is discarded.  I am more wondering if it's OK for a plug to grab the body and what the consequences might be for plugs later in the pipeline.  It seems unusual that the later plugs would have to know that an earlier plug grabbed the body and where it put the information... but that be rather normal.

José Valim

unread,
Apr 15, 2015, 4:12:26 PM4/15/15
to elixir-l...@googlegroups.com
Yes, I would say that is completely OK. At least with how everything is set up today.



José Valim
Skype: jv.ptec
Founder and Lead Developer

On Wed, Apr 15, 2015 at 10:08 PM, easco <eas...@gmail.com> wrote:
Oh I understand why the body is discarded.  I am more wondering if it's OK for a plug to grab the body and what the consequences might be for plugs later in the pipeline.  It seems unusual that the later plugs would have to know that an earlier plug grabbed the body and where it put the information... but that be rather normal.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages