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

Fix typescript definitions for using with Registry<OpenMetricsContentType> #571

Merged
merged 4 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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