Skip to content

Commit

Permalink
Fix typescript definitions for using with Registry<OpenMetricsContent…
Browse files Browse the repository at this point in the history
…Type> (#571)
  • Loading branch information
FreeRiderBysik committed May 13, 2023
1 parent 1df34ed commit 992d5a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

- Cleanup code and refactor to be more efficient

### Breaking

- drop support for Node.js versions 10, 12 and 17
Expand All @@ -23,6 +19,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
avoid failures from the server when using `Content-Encoding: gzip` header.
- Refactor `escapeString` helper in `lib/registry.js` to improve performance and
avoid an unnecessarily complex regex.
- Cleanup code and refactor to be more efficient
- Correct TS types for working with OpenMetrics

### Added

Expand Down
35 changes: 24 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export type PrometheusMetricsVersion = '0.0.4';
export type OpenMetricsMIME = 'application/openmetrics-text';
export type OpenMetricsVersion = '1.0.0';

export type PrometheusContentType =
`${OpenMetricsMIME}; version=${OpenMetricsVersion}; charset=${Charset}`;
export type OpenMetricsContentType =
`${OpenMetricsMIME}; version=${OpenMetricsVersion}; charset=${Charset}`;
export type PrometheusContentType =
`${PrometheusMIME}; version=${PrometheusMetricsVersion}; charset=${Charset}`;

export type RegistryContentType =
Expand Down Expand Up @@ -119,7 +119,9 @@ export const prometheusContentType: PrometheusContentType;
*/
export const openMetricsContentType: OpenMetricsContentType;

export class AggregatorRegistry extends Registry {
export class AggregatorRegistry<
T extends RegistryContentType,
> extends Registry<T> {
/**
* Gets aggregated metrics for all workers.
* @return {Promise<string>} Promise that resolves with the aggregated
Expand All @@ -136,7 +138,9 @@ export class AggregatorRegistry extends Registry {
* `registry.getMetricsAsJSON()`.
* @return {Registry} aggregated registry.
*/
static aggregate(metricsArr: Array<Object>): Registry; // TODO Promise?
static aggregate<T extends RegistryContentType>(
metricsArr: Array<Object>,
): Registry<T>; // TODO Promise?

/**
* Sets the registry or registries to be aggregated. Call from workers to
Expand All @@ -145,7 +149,14 @@ export class AggregatorRegistry extends Registry {
* aggregated.
* @return {void}
*/
static setRegistries(regs: Array<Registry> | Registry): void;
static setRegistries(
regs:
| Array<
Registry<PrometheusContentType> | Registry<OpenMetricsContentType>
>
| Registry<PrometheusContentType>
| Registry<OpenMetricsContentType>,
): void;
}

/**
Expand Down Expand Up @@ -655,13 +666,13 @@ export namespace Summary {
/**
* Push metrics to a Pushgateway
*/
export class Pushgateway {
export class Pushgateway<T extends RegistryContentType> {
/**
* @param url Complete url to the Pushgateway. If port is needed append url with :port
* @param options Options
* @param registry Registry
*/
constructor(url: string, options?: any, registry?: Registry);
constructor(url: string, options?: any, registry?: Registry<T>);

/**
* Add metric and overwrite old ones
Expand Down Expand Up @@ -729,8 +740,10 @@ export function exponentialBuckets(
count: number,
): number[];

export interface DefaultMetricsCollectorConfiguration {
register?: Registry;
export interface DefaultMetricsCollectorConfiguration<
T extends RegistryContentType,
> {
register?: Registry<T>;
prefix?: string;
gcDurationBuckets?: number[];
eventLoopMonitoringPrecision?: number;
Expand All @@ -741,8 +754,8 @@ export interface DefaultMetricsCollectorConfiguration {
* Configure default metrics
* @param config Configuration object for default metrics collector
*/
export function collectDefaultMetrics(
config?: DefaultMetricsCollectorConfiguration,
export function collectDefaultMetrics<T extends RegistryContentType>(
config?: DefaultMetricsCollectorConfiguration<T>,
): void;

export interface defaultMetrics {
Expand Down

0 comments on commit 992d5a2

Please sign in to comment.