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

Logging outbound request from client #1418

Closed
oa-metaswitch opened this issue Jan 16, 2018 · 4 comments
Closed

Logging outbound request from client #1418

oa-metaswitch opened this issue Jan 16, 2018 · 4 comments

Comments

@oa-metaswitch
Copy link

Hi. I'm trying to write a tracer which logs exactly what requests get sent over the wire, and preferably including local and remote ip/port. I realize hyper is agnostic to transport, but is there some way of logging the request and response struct, and getting passed a reference to the transport for diagnostics purposes?

Has anyone done anything like that before?

@seanmonstar
Copy link
Member

You can sort of do what you want, but not all of it is especially easy.

For logging request and response, that you can do by logging whatever you want just before calling client.request (or client.call), and you can log the response returned by the future, for example:

debug!("req = {:?}", req);
client.request(req)
    .map(|res| {
        debug!("res = {:?}", res);
        res
    })

In order to get a reference to the transport being used, you'd probably need to write a custom Connect so that you can return a transport that can log some instances about itself.

Probably this is also related to #1402.

(As this seems more like a question around how to do something in hyper, and not a bug that something doesn't work, I'm going to close for now.)

@oa-metaswitch
Copy link
Author

Hi Sean.

Thanks for the quick reply, and sorry: I should have been more specific. I've already attempted something like what you describe above, however: hyper tacks on a few headers (transfer encoding, Date header, etc) before sending, and I'd like to log the request as it actually goes out over the wire, including those headers.

I currently have a tracer that works by implementing a custom connect and wrapping the TcpStream, but to sensibly log the request I then find I have to re-parse it to get the method, headers, body, etc.

It would be nice to have a logging callback, maybe somewhere around https://docs.rs/hyper/0.11.12/src/hyper/client/mod.rs.html#193, that could pass a reference to the transport and the request after the extra headers have been added. Is that sensible? Otherwise, is there a way of doing this already?

Thanks.

@sfackler
Copy link
Contributor

Do you know what these kinds of APIs look like in other HTTP client libraries?

@oa-metaswitch
Copy link
Author

@sfackler sorry for taking a while to respond.

I don't really have any examples of this in other client libraries, but I don't think there are many other libraries that would have a need for it. Put differently, I think hyper is a bit more opaque than other HTTP client libraries I've worked with. In particular

  • It's agnostic to the transport layer, which makes it hard to access information about the TCP connection.
  • It's also opaque in its request processing, in that it does not give you easy access to the request just before it's sent on the wire.

Don't get me wrong, I think it's great that it works like this, I would just really like some way to special-case for TCP connections when I have a need to access the transport layer.

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

3 participants