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

Add tracking request-id example #82

Merged
merged 1 commit into from Oct 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Expand Up @@ -67,6 +67,29 @@ Notice that in this case it is important to use the `prefix` option to tell the

For other examples, see `example.js`.

## Request tracking

`fastify-http-proxy` can track and pipe the `request-id` across the upstreams. Using the [`hyperid`](https://www.npmjs.com/package/hyperid) module and the [`fastify-reply-from`](https://github.com/fastify/fastify-reply-from) built in options a fairly simple example would look like this:

```js
const Fastify = require('fastify')
const proxy = require('fastify-http-proxy')
const hyperid = require('hyperid')

const server = Fastify()
const uuid = hyperid()

server.register(proxy, {
upstream: 'http://localhost:4001',
replyOptions: {
rewriteRequestHeaders: (originalReq, headers) => ({...headers, 'request-id': uuid()})
Copy link
Member

Choose a reason for hiding this comment

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

I think this might be a bit more complex than that. This example override the request-id, however we should 1) change the fastify id generator to use the unique id and 2) take it from req.id instead of generating a new one each time.

It would be really nice to have a unit test for this.

Copy link
Member

Choose a reason for hiding this comment

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

The Fastify instance should be configured to read the request id from the incoming header. https://www.fastify.io/docs/v2.12.x/Server/#requestidheader

}
})


server.listen(3000);
```

## Options

This `fastify` plugin supports _all_ the options of
Expand Down Expand Up @@ -121,8 +144,6 @@ The results where gathered on the second run of `autocannon -c 100 -d 5
URL`.

## TODO

* [ ] Generate unique request ids and implement request tracking
* [ ] Perform validations for incoming data

## License
Expand Down