-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Make it possible to inject a dependency into collect function #1837
Comments
@slukes To be honest, this seems like a very bespoke use case and I am not sure it's worth complicating the interface for it. However, I am open to a proof of concept implementation that is backwards compatible if you are willing to contribute one. I can't guarantee it gets merged simply because I think this is a super narrow use case. With that said, if the implementation is simple, elegant and backwards compatible I will consider it with an open mind. |
I also encountered a usecase where this feature would be useful:
I'm happy with this implementation, so thank you @slukes but the API you suggested would be great! Thanks for this lib @willsoto ! 🙏 |
Looks like a nice feature to me too. I have a case similar to @arthurlenoir. the problem is the api creds are injected from a service that is managing secrets. |
You can inject the secrets service and then use it by adding as an arg to the useFactory. You also can move the whole logic of creating the metric to a separate service. Here's my example of using the dataSourse to get a metric value. providers: [SomeGaugeService], import { Injectable } from '@nestjs/common';
import { Gauge } from 'prom-client';
import { DataSource } from 'typeorm';
@Injectable()
export class SomeGaugeService {
public readonly gauge: Gauge;
constructor(private readonly dataSource: DataSource) {
this.gauge = new Gauge({
name: 'some_metric_total',
help: 'Amount of some entities ion the system',
collect: async () => {
const result = await this.dataSource.query('SELECT coalesce(COUNT(*), 0) AS count FROM SOME_TABLE_NAME');
const count = +result[0].count;
this.gauge.set(count);
},
});
}
} In a constructor you can inject your secrets service too, and then consume a value, provided by a service. |
We would like to be able to inject a dependency into the collect function in order to be able to monitor resources configured by another nest provider for example a queue.
We have done a POC to work around the fact there seems to be no way to do this as so:
In an ideal world we would like to use an api such as:
The text was updated successfully, but these errors were encountered: