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 bindings method to logger instances #588

Merged
merged 10 commits into from
Jan 24, 2019
Merged

add bindings method to logger instances #588

merged 10 commits into from
Jan 24, 2019

Conversation

yellowbrickc
Copy link
Contributor

Step 1:
Expose tools.getChindings()
Register a symbol for the method
Reference the method in prototype.

Register a symbol for the method
Reference the method in prototype
@yellowbrickc
Copy link
Contributor Author

As you suggested @mcollina I created the PR early. I have anyway no choice, I'm stocked :(

I tried to expose the method on every logger but I'm obviously overseeing something, this is what the tests tell me. And I'm still not happy about the naming, "chindings" are used already in prototype.child() to store the result as string.

Copy link
Member

@davidmarkclements davidmarkclements left a comment

Choose a reason for hiding this comment

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

just some initial feedback

lib/proto.js Outdated Show resolved Hide resolved
lib/proto.js Outdated Show resolved Hide resolved
lib/symbols.js Outdated Show resolved Hide resolved
lib/tools.js Outdated Show resolved Hide resolved
@davidmarkclements davidmarkclements changed the title Start exposing the method to read the chindings WIP: Start exposing the method to read the chindings Jan 20, 2019
Rename the method for child bindings
@yellowbrickc
Copy link
Contributor Author

I verified and you are absolutely right, the childBindings is available. Unfortunately it does not return the expected chindings because the stream.lastLogger does not exists at that point.
This means for me that a simple refactoring won't do it, @davidmarkclements is right, we must store and return the child object. Or:
we revert everything and create a method in proto.js which could transform instance[chindingsSym] in an object, eventually strip everything not being a child information and return it:

function childBindings () {
  const chindings = this[chindingsSym]
  var chindingsObject = (chindings.indexOf(',') === 0) ? `{${chindings.substr(1)}}` : `{${chindings}}`
  var bindings = JSON.parse(chindingsObject) || {}
  // eventually strip props as hostname and pid
  return bindings
}

What do you think?

@mcollina
Copy link
Member

+1 to that proposal @yellowbrickc! I would add that we should cache that value as it's not going to change after usage.

I like it a lot for two reasons:

  1. it ensures that the data is fully cloned and serialized before accessing it back.
  2. it does not touch a hot path in this library, which is still painful to opimize.

@yellowbrickc
Copy link
Contributor Author

Ok, than I will do this.
Regarding the caching, it could be dangerous. With this solution

var bindings = null
function childBindings () {
  if (!bindings) {
    const chindings = this[chindingsSym]
    var chindingsObject = (chindings.indexOf(',') === 0) ? `{${chindings.substr(1)}}` : `{${chindings}}`
    bindings = JSON.parse(chindingsObject) || {}
    // eventually strip props as hostname and pid
  }
  return bindings
}

will the second of these tests fail, the method still returns the object w/o {foo: 'bar'}

test('WIP childBindings are exposed on instance', async ({ isA }) => {
  const instance = pino()
  isA(instance.childBindings(), 'object')
})

test('WIP childBindings are exposed on every instance', async ({ match }) => {
  const instance = pino().child({ foo: 'bar' })
  match(instance.childBindings(), { foo: 'bar' })
})

@mcollina
Copy link
Member

Let's leave caching to a later time :).

@davidmarkclements
Copy link
Member

Quick check though: would a user expect the bindings to be cloned, or would a user expect to have the same object that was passed to child (referntially)?

@mcollina
Copy link
Member

100% cloned.

@davidmarkclements
Copy link
Member

Given an input API: logger.child(bindings)
And and output API logger.childBindings() => bindings

I kind of would expect the same object back, however we definitely should clone it because we need to uphold/promote the assumption that once set, the bindings are immutable. So we'll need to make docs clear on this.

@yellowbrickc if it makes sense to clone directly from the passed in binding object, (because that may make caching easier), use http://npm.im/rfdc for cloning

Expose it in the prototype.
Extend the docs.
@yellowbrickc
Copy link
Contributor Author

Sorry, I didn't see your last comments.
I just finished what we discussed (and discovered the LsCache, I suppose, this is what you meant earlier), but I didn't touch this subject.
I have reread the #560 and I tried to fulfill his issues/wishes. I didn't returned the whole object (neither the level, nor any serializers) just the "current state of the binding props for child loggers".

I tried but I cannot imagine any situation where I would need something else from a childlogger. But it is your decision :)

Copy link
Member

@davidmarkclements davidmarkclements left a comment

Choose a reason for hiding this comment

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

nice job so far!

lib/proto.js Outdated Show resolved Hide resolved
lib/proto.js Outdated Show resolved Hide resolved
lib/proto.js Outdated Show resolved Hide resolved
@davidmarkclements
Copy link
Member

I think this also has to be intergrated with pretty wrapper as well right? we need to remove that code from the pretty wrapper and use this instead?

@yellowbrickc
Copy link
Contributor Author

The private method in the prettyWrapper does not remove any props, adds "v:1" to the output and is based on the lastLogger which is created/overwritten during the life cycle of the logger instance. It is used only for writing the logs.
The new method is a simple nice-to-have "reader" method, without side effects but with a try-catch (which I wouldn't us if errors wouldn't be allowed like in the other method) .
Also if you ask me than no, I wouldn't replace the other method.

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.

Good work!

docs/api.md Outdated Show resolved Hide resolved
lib/proto.js Outdated Show resolved Hide resolved
Co-Authored-By: yellowbrickc <yellowbrickc@users.noreply.github.com>
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

@yellowbrickc
Copy link
Contributor Author

Finally I passed your 100% code coverage requirement 😆
I cannot find any open conversations, I think @davidmarkclements you must accept them maybe?

@mcollina
Copy link
Member

Thanks for keeping up with us @yellowbrickc! Good work!

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

LGTM

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

@davidmarkclements
Copy link
Member

sorry for the delay, I'm just thinking about the name,

if the logger is a child, then the returned object isn't child bindings, it's just bindings

I think we should rename childBindings to bindings

@yellowbrickc
Copy link
Contributor Author

Done.

@davidmarkclements
Copy link
Member

amazing thanks @yellowbrickc

@davidmarkclements davidmarkclements merged commit e2ec9ff into pinojs:master Jan 24, 2019
@davidmarkclements davidmarkclements changed the title WIP: Start exposing the method to read the chindings add bindings method to logger instances Jan 24, 2019
@davidmarkclements
Copy link
Member

v5.11.0 released 👍

@yellowbrickc yellowbrickc deleted the issue-560 branch January 24, 2019 20:32
@yellowbrickc
Copy link
Contributor Author

It was a pleasure.
It would be very helpful for me, if we could have short "retro" ;-) I want to gather more experience in working on OSS.

  1. At the end this was a very small PR (less then 20 lines) but we still had a lot of reworking and a lot of communication. My question is: this is the usual way or you would like more or less planing before?

  2. Is this anything else what you would have to run somehow different?

@jsumners
Copy link
Member

I suspect some of the back and forth was due to a small language barrier. Regardless if that is true or not, it’s not the ultimate delta in code size that dictates the conversation around its implementation. The goal of the conversation is to make sure the right code gets checked in.

@yellowbrickc
Copy link
Contributor Author

I have no problem at all with back and forth communication. I rather build something what fulfills some needs than something else. This is why I liked the last change request from @davidmarkclements very much because it shows that you really care not only about coding but also about naming.

Maybe I can give my answers:

  1. I don't know if this is the usual way in OSS but this is the usual way for me to build the right features.
  2. I like the way how this PR evolved.
  3. Just feedback from me regarding the repo: I made very good experience with a separate test file for edge cases. This is where I would start with recreating bugs and it would be not a unit test but a behavior test set.

BTW: thank you for the "fireworks" on twitter, I was really flattered - because we know that it wasn't that much what I did.

@mcollina
Copy link
Member

Pino does not receive many contributions, and this was exceptionally well done.
This PR is an example of why OSS is awesome 👏 .

wagnerand pushed a commit to mozilla/dispensary that referenced this pull request Jan 25, 2019
## The dependency [pino](https://github.com/pinojs/pino) was updated from `5.10.10` to `5.11.0`.
This version is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

---

<details>
<summary>Release Notes for v5.11.0</summary>

<ul>
<li>add <code>bindings</code> method to logger instances <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="401113555" data-permission-text="Issue title is private" data-url="pinojs/pino#588" data-hovercard-type="pull_request" data-hovercard-url="/pinojs/pino/pull/588/hovercard" href="https://urls.greenkeeper.io/pinojs/pino/pull/588">#588</a></li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 12 commits.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/fd96fd983432338f52a3811ce928a52321f23400"><code>fd96fd9</code></a> <code>5.11.0</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/e2ec9ff02937b99d4276ad145c51125bde847555"><code>e2ec9ff</code></a> <code>Merge pull request #588 from yellowbrickc/issue-560</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/694da028230b8c2a3a3f223843bb4d855ba654ac"><code>694da02</code></a> <code>Rename childBindings to bindings.</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/5c8834f3a0fe0627456e5955176fb18d7bae8285"><code>5c8834f</code></a> <code>Remove unnecessary security net, cannot happen.</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/e17d319d31a33b4027101d6c6f3efd1bb030d944"><code>e17d319</code></a> <code>Remove try-catch (cannot be tested), even the logger is minimal configured, the method works.</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/b90ac8f17062c364221a9bb3a6efcbe742203587"><code>b90ac8f</code></a> <code>Small refactoring</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/e952417b15c5c673876f29449fcca7d09906fcc7"><code>e952417</code></a> <code>Update docs/api.md, logger.childBindings</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/52ae8cac8e07cd2046dca2383cdcab62fee7e3d3"><code>52ae8ca</code></a> <code>Create a method to retrieve all current bindings.</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/1246ac49089fa3fcc07fbde64ff902b43dfa7c79"><code>1246ac4</code></a> <code>Revert "Remove the new symbol and the reference Rename the method for child bindings"</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/ede794c3ff0fa018c339e0891278e75570ed67ce"><code>ede794c</code></a> <code>Revert "Remove the new symbol and the reference Rename the method for child bindings"</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/1569e4654854191f1986217f727fed7f2779b2e3"><code>1569e46</code></a> <code>Remove the new symbol and the reference</code></li>
<li><a href="https://urls.greenkeeper.io/pinojs/pino/commit/d8e9847c384b5075ec2e6904191e6b349220d95d"><code>d8e9847</code></a> <code>Expose tools.getChindings()</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/pinojs/pino/compare/2ba03f244021aed566b9f95b8cca2509bcdd2464...fd96fd983432338f52a3811ce928a52321f23400">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴
@github-actions
Copy link

github-actions bot commented Feb 5, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants