forked from AeroNotix/nestjs-prom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add PromService and some decorators
Add PromService to be able to create metric on the fly Add new decorators to auto generate metric for class instance and method call
- Loading branch information
Showing
7 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,46 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { getMetricToken } from './prom.utils'; | ||
import { getMetricToken, findOrCreateCounter } from './prom.utils'; | ||
import * as client from 'prom-client'; | ||
|
||
export const InjectCounterMetric = (name: string) => Inject(getMetricToken(`Counter`, name)); | ||
export const InjectGaugeMetric = (name: string) => Inject(getMetricToken(`Gauge`, name)); | ||
export const InjectHistogramMetric = (name: string) => Inject(getMetricToken(`Histogram`, name)); | ||
export const InjectSummaryMetric = (name: string) => Inject(getMetricToken(`Summary`, name)); | ||
|
||
// export const InjectRegistry = (name?: string) => | ||
// Inject(name ? getRegistryName(name) : ''); | ||
/** | ||
* Create and increment a counter when the method is called | ||
* | ||
* @param param0 | ||
*/ | ||
export const PromMethodCounter = () => { | ||
return (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<Function>) => { | ||
const className = target.constructor.name; | ||
const counterMetric = findOrCreateCounter({ | ||
name: `${className}_${propertyKey.toString()}_method_counter`, | ||
help: `${className}#${propertyKey.toString()} called counter`, | ||
}); | ||
const methodFunc = descriptor.value; | ||
descriptor.value = function (...args) { | ||
counterMetric.inc(1); | ||
return methodFunc.apply(this, args); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Create and increment a counter when a new instance is created | ||
* | ||
* @param ctor | ||
*/ | ||
export const PromInstanceCounter = <T extends { new(...args: any[]): {} }>(ctor: T) => { | ||
const counterMetric = findOrCreateCounter({ | ||
name: `${ctor.name}_instance_counter`, | ||
help: `${ctor.name} object instances counter`, | ||
}); | ||
return class extends ctor { | ||
constructor(...args) { | ||
counterMetric.inc(1); | ||
super(...args); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { findOrCreateCounter, findOrCreateGauge, findOrCreateHistogram } from './common/prom.utils'; | ||
|
||
@Injectable() | ||
export class PromService { | ||
getCounterMetric(name: string) { | ||
return findOrCreateCounter({ name }); | ||
} | ||
|
||
getGaugeMetric(name: string) { | ||
return findOrCreateGauge({ name }); | ||
} | ||
|
||
getHistogramMetric(name: string) { | ||
return findOrCreateHistogram({ name }); | ||
} | ||
|
||
getSummaryMetric(name: string) { | ||
return findOrCreateHistogram({ name }); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ | |
"node_modules", | ||
"**/*.spec.ts" | ||
] | ||
} | ||
} |