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

Fix setting request id #44

Merged
merged 2 commits into from
Dec 10, 2017
Merged

Fix setting request id #44

merged 2 commits into from
Dec 10, 2017

Conversation

jsumners
Copy link
Member

@jsumners jsumners commented Dec 9, 2017

While updating restify-pino-logger I learned that the request id property was not getting set properly. I'm don't know how it ever was. Consider:

  1. https://github.com/pinojs/pino-http/blame/626eede60281e460e77968836293d671d709f19a/logger.js#L53
  2. https://github.com/pinojs/pino-http/blame/626eede60281e460e77968836293d671d709f19a/logger.js#L82

First it's set to a function and then that function is assigned to the property.

This PR simply invokes the req.id function in the serializer if it is actually a function. Otherwise it assigns whatever that value is to the property.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 98.795% when pulling 03c39a7 on req-id into 39355e3 on master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage remained the same at 98.795% when pulling 03c39a7 on req-id into 39355e3 on master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 98.795% when pulling 03c39a7 on req-id into 39355e3 on master.

logger.js Outdated
@@ -121,7 +121,7 @@ function wrapReqSerializer (serializer) {
function asReqValue (req) {
var connection = req.connection
const _req = Object.create(pinoReqProto)
_req.id = req.id
_req.id = Function.prototype.isPrototypeOf(req.id) ? req.id() : req.id
Copy link
Member

Choose a reason for hiding this comment

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

why not typeof req.id === 'function'? Which one is faster?

Copy link
Member

Choose a reason for hiding this comment

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

unless there's a speed benefit.. definitely go with typeof here...

Copy link
Member Author

Choose a reason for hiding this comment

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

/shrug, I prefer using the actual API. I haven't tested for any difference in speed.

test.js Outdated
logger: pino(dest),
serializers: {
req: function (req) {
t.is(Function.isPrototypeOf(req.id), false)
Copy link
Member

Choose a reason for hiding this comment

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

prefer typeof - unless there's some really awesome reasons

test.js Outdated
server.unref()
server.listen(0, () => {
const port = server.address().port
http.get(`http://127.0.0.1:${port}`, () => {})
Copy link
Member

Choose a reason for hiding this comment

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

pointless pedantry: you could just do http.get(server.address(), () => {})

@coveralls
Copy link

Coverage Status

Coverage remained the same at 98.795% when pulling 018653d on req-id into 39355e3 on master.

1 similar comment
@coveralls
Copy link

coveralls commented Dec 10, 2017

Coverage Status

Coverage remained the same at 98.795% when pulling 018653d on req-id into 39355e3 on master.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@jsumners jsumners merged commit 1c8c812 into master Dec 10, 2017
@jsumners jsumners deleted the req-id branch December 10, 2017 13:05
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

Successfully merging this pull request may close these issues.

4 participants