Skip to content

Add support for default labels - #137

Merged
siimon merged 10 commits into
prometheus:masterfrom
nwest1:default-labels
Jun 29, 2017
Merged

siimon merged 10 commits into
prometheus:masterfrom
nwest1:default-labels

Conversation

@nwest1

@nwest1 nwest1 commented Jun 26, 2017 •

Copy link
Copy Markdown
Contributor

I believe this is a pretty good solution for #83

It's simple, doesn't break any APIs, allows you to configure static labels per registry, and defaulted values can be overridden.

Let me know if this wasn't what you were thinking about this, or if there are any styling considerations I should be changing.

Thanks!

@SimenB SimenB left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oooh, I like this! I thought about tying them to the default metrics, but tying them to the register itself makes a lot more sense! I started on an implementation at work today, but I much prefer this version to what I had going on.

The only thing missing is also passing on default metrics when merging registries (the static merge function). And please update the changelog as well.

Other than that, this looks great!

Comment thread README.md Outdated
process_resident_memory_bytes{serviceName="api-v1"} 33853440 1498510040309
```

Default metrics will be overridden if there is a name conflict.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"labels", not "metrics", right?

Comment thread lib/registry.js Outdated
key => `${key}="${escapeLabelValue(val.labels[key])}"`
);

labels = Object.assign(this._defaultLabels, labels);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not sure Object.assign makes sense here, they are arrays not objects. should be this._defaultLables.concat(labels). Unsure how to handle dupes, though? I can see from the tests that it works, but it feels weird to me

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if you keep _defaultLabels as an object internally, then do Object.assign before the map?

@nwest1

nwest1 commented Jun 27, 2017 •

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! Glad you like it.

How should the static merge function work? I'm not sure what's intended there.

Edit:
Two ways I could understand it:

  • Apply default labels to metrics as they are registered. Question: do or don't copy default labels to new registry? The more I think about it, do not copy default values, as the end result would be the same as:
  • All default labels that exist in merging registries will be copied to merged registry. No labels will be applied to metrics contained.

@SimenB SimenB left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the index.d.ts as well? Last thing, I swear 🙂

@siimon, what do you think about the merge case?

@nwest1

nwest1 commented Jun 28, 2017

Copy link
Copy Markdown
Contributor Author

Not sure if I did the type def'n correctly. Let me know if there's anything else 😄

@SimenB

SimenB commented Jun 28, 2017

Copy link
Copy Markdown
Contributor

Looks good!

If you rebase on master the build should be green again

Comment thread README.md Outdated
Static labels may be applied to every metric emitted by a registry:
```js
var client = require('prom-client');
var defaultLabels = { serviceName: "api-v1"};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this has inconsistent spacing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and can you use const?

@nwest1

nwest1 commented Jun 28, 2017

Copy link
Copy Markdown
Contributor Author

I'm still getting local npm test failures -

/Users/z002b1g/work/prom-client/lib/metrics/osMemoryHeap.js
  28:29  error  Replace `⏎↹` with `·`  prettier/prettier
  30:2   error  Delete `↹`             prettier/prettier
  31:1   error  Delete `↹`             prettier/prettier

/Users/z002b1g/work/prom-client/lib/pushgateway.js
  44:24  error  Replace `⏎↹↹gatewayUrlParsed.pathname·&&·` with `·gatewayUrlParsed.pathname·&&⏎↹↹`  prettier/prettier
  46:3   error  Delete `↹`                                                                          prettier/prettier
  47:1   error  Delete `↹`

@SimenB

SimenB commented Jun 28, 2017

Copy link
Copy Markdown
Contributor

Have you ran npm install after rebase?

@SimenB SimenB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@siimon thoughts?

@nwest1

nwest1 commented Jun 28, 2017

Copy link
Copy Markdown
Contributor Author

Oops! That did it. Thanks.

@siimon

siimon commented Jun 28, 2017

Copy link
Copy Markdown
Contributor

I like the idea tying it together with the registry, that makes sense I think.

One thought I had earlier was to instead of providing an object which is the default labels, you could instead provide a function that in turn returns an object which is the default labels. That would make it more flexible and more powerful, however the api might be a bit more complex.

@SimenB

SimenB commented Jun 28, 2017

Copy link
Copy Markdown
Contributor

What's the use case for a function? When would you want to use it?

@nwest1

nwest1 commented Jun 28, 2017

Copy link
Copy Markdown
Contributor Author

Functional options are a great pattern to allow for expanding an API without breaking it. It would require some rework for sure.

This is for golang, but I think the pattern still applies to js:
https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

@SimenB

SimenB commented Jun 28, 2017

Copy link
Copy Markdown
Contributor

I have no strong feeling either way. Doing register.defaultMetrics({ foo: 'bar' }) or register.defaultMetrics(() => { foo: 'bar' }) doesn't matter to me. Maybe support both?

While I get the general argument "it's more extensible", I don't get what it would give default metrics. Would it receive the metric as an argument? Still, I don't see what you could really do with it.

@nwest1

nwest1 commented Jun 28, 2017

Copy link
Copy Markdown
Contributor Author

I'll let you guys decide how you'd like to do this.
I just need something that works for the edge case I'm working with right now 😄

@siimon

siimon commented Jun 29, 2017

Copy link
Copy Markdown
Contributor

Well, it's probably bait YAGNI, so lets keep it as it is and if we find another use case where a function is better we could probably live with both versions for awhile..

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.

3 participants