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

Default instance labels for all custom metrics #1209

Closed
raphaabreu opened this issue Dec 31, 2021 · 3 comments
Closed

Default instance labels for all custom metrics #1209

raphaabreu opened this issue Dec 31, 2021 · 3 comments
Labels
question Further information is requested released waiting

Comments

@raphaabreu
Copy link

Hello @willsoto, awesome package!

I am trying to have all my custom metrics carry some common tags like the service name, version and the hostname but I am not finding any easy way to do this. All paths seems to require a lot of code duplication to have these common tags set on all custom metrics.

Is there a way to add a parameter to the PrometheusModule.register(PrometheusOptions) class so that a Record<string,string> could be specified with all tags that all metrics should always carry? This would be analogous to the defaultMetrics -> config -> labels property, but it should apply to both "defaultMetrics" and "customMetrics".

What do you think? I can help with a PR if you point me in the right direction.

@willsoto
Copy link
Owner

Thanks @raphaabreu

By design, I try not to add anything to this library that isn't directly supported by the prom-client library. I was browsing through their docs and it seems like even they don't really have a way to apply common labels to both default and custom metrics (I was reading here). If I am wrong on this, please correct me.

In the meantime, I'd be more than happy to look at any proposals you may have for an API. If it doesn't add a lot of surface area to the project, I'd be fine incorporating those changes in.


As far as implementation goes, you may want to look at creating an injectable with those common labels you want applied everywhere. This would allow to inject them when creating metrics (code). As far as where to get those labels, I would add an option to this interface that - ideally - matches the structure that prom-client accepts. Given that each metric accepts options, you can probably just merge the options the user provides and the common options provided at the module level. I hope that makes sense.

@willsoto willsoto added question Further information is requested waiting labels Jan 17, 2022
@gallak87
Copy link

gallak87 commented Mar 8, 2022

I also required this but found a somewhat simple work around. I have a file utils/metrics.utils.ts where I set up all my metric provider helpers and label keys. I then call client.register.setDefaultLabels:

import { Provider } from '@nestjs/common';
import { makeCounterProvider } from '@willsoto/nestjs-prometheus';
import * as client from 'prom-client';

// prometheus guidelines https://prometheus.io/docs/practices/naming/

// labels
export const WALLET_CREATED_RESULT_KEY = 'wallet_created_result';

// use default labels to separate microservice metrics
export const DEFAULT_LABELS = {
    // eslint-disable-next-line @typescript-eslint/camelcase
    app_name: process.env.APP_NAME,
};

export const METRIC_WALLET_CREATED_COUNTER = 'wallet_created_total';
export const makeWalletCounterMetric = (): Provider =>
    makeCounterProvider({
        name: METRIC_WALLET_CREATED_COUNTER,
        help: 'Count of wallets created and their status',
        labelNames: [WALLET_CREATED_RESULT_KEY],
    });

// sets the default labels for all metrics
client.register.setDefaultLabels(DEFAULT_LABELS);

Now all my metircs have app_name=wallet in them

willsoto pushed a commit that referenced this issue Mar 9, 2022
# [4.6.0](v4.5.0...v4.6.0) (2022-03-09)

### Features

* ability to set defaultLabels ([7fd289a](7fd289a)), closes [#1209](#1209)
* upgrade packages ([79a67f1](79a67f1))
@willsoto
Copy link
Owner

willsoto commented Mar 9, 2022

🎉 This issue has been resolved in version 4.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested released waiting
Projects
None yet
Development

No branches or pull requests

3 participants