Skip to content

Commit

Permalink
fix metrics typing
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Dec 1, 2023
1 parent c8b9780 commit 0fb79ab
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 50 deletions.
3 changes: 3 additions & 0 deletions src/metrics/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const METRIC_LABEL_REGEXP = /^[a-zA-Z_][a-zA-Z0-9-_.]*$/;
* @typedef {import("./registry").MetricListOptions} MetricListOptions
* @typedef {import("./types/base")} BaseMetric
* @typedef {import("./types/base").BaseMetricOptions} BaseMetricOptions
* @typedef {import("./types/gauge")} GaugeMetric
*
* @typedef {import("../service-broker")} ServiceBroker
*/
Expand Down Expand Up @@ -202,6 +203,7 @@ class MetricRegistry {
increment(name, labels, value = 1, timestamp) {
if (!this.opts.enabled) return null;

/** @type {GaugeMetric} */
const item = this.getMetric(name);
if (!isFunction(item.increment))
throw new Error(
Expand All @@ -224,6 +226,7 @@ class MetricRegistry {
decrement(name, labels, value = 1, timestamp) {
if (!this.opts.enabled) return null;

/** @type {GaugeMetric} */
const item = this.getMetric(name);
if (!isFunction(item.decrement))
throw new Error("Invalid metric type. Decrementing works only with gauge metric type.");
Expand Down
14 changes: 5 additions & 9 deletions src/metrics/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ declare namespace BaseMetric {
}

declare abstract class BaseMetric<TValue = unknown> {
type: string;
registry: MetricRegistry;

type: string;
name: string;

description?: string;

labelNames: string[];

unit?: string;

aggregator: string;

lastSnapshot: Record<string, any> | null;

dirty: boolean;

values: Map<string, Record<string, any>>;
Expand All @@ -48,9 +44,9 @@ declare abstract class BaseMetric<TValue = unknown> {

get(labels?: Record<string, any>): Record<string, any> | null;

reset(labels?: Record<string, any>, timestamp?: number): Record<string, any> | null;
abstract reset(labels?: Record<string, any>, timestamp?: number): void;

resetAll(timestamp?: number): Record<string, any> | null;
abstract resetAll(timestamp?: number): void;

clear(): void;

Expand All @@ -60,7 +56,7 @@ declare abstract class BaseMetric<TValue = unknown> {

abstract generateSnapshot(): TValue[];

changed(value: any | null, labels?: Record<string, any>, timestamp?: number): void;
changed(value?: any | null, labels?: Record<string, any>, timestamp?: number): void;

toObject(): BaseMetric.BaseMetricPOJO<TValue>;
}
Expand Down
32 changes: 24 additions & 8 deletions src/metrics/types/base.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
/*
* moleculer
* Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

/* eslint-disable no-unused-vars */

"use strict";

/**
* Import types
*
* @typedef {import("../registry")} MetricRegistry
* @typedef {import("./base")} BaseMetricClass
* @typedef {import("./base").BaseMetricOptions} BaseMetricOptions
*/

/**
* Abstract Base Metric class.
*
* @class BaseMetric
* @implements {BaseMetricClass}
*/
class BaseMetric {
/**
* Creates an instance of BaseMetric.
*
* @param {Object} opts
* @param {BaseMetricOptions} opts
* @param {MetricRegistry} registry
* @memberof BaseMetric
*/
Expand Down Expand Up @@ -67,19 +78,23 @@ class BaseMetric {
/**
* Reset item by labels
*
* @param {Object?} labels
* @param {Number?} timestamp
*
* @memberof BaseMetric
*/
reset(/*labels, timestamp*/) {
reset(labels, timestamp) {
/* istanbul ignore next */
throw new Error("Not implemented");
}

/**
* Reset all items
*
* @param {Number?} timestamp
* @memberof BaseMetric
*/
resetAll(/*timestamp*/) {
resetAll(timestamp) {
/* istanbul ignore next */
throw new Error("Not implemented");
}
Expand All @@ -91,7 +106,7 @@ class BaseMetric {
*/
clear() {
this.values = new Map();
this.changed();
this.changed(null, null, null);
}

/**
Expand Down Expand Up @@ -119,7 +134,7 @@ class BaseMetric {
/**
* Get a snapshot.
*
* @returns {Object}
* @returns {Object|null}
* @memberof BaseMetric
*/
snapshot() {
Expand All @@ -134,6 +149,7 @@ class BaseMetric {
/**
* Generate a snapshot.
*
* @returns {Array}
* @memberof BaseMetric
*/
generateSnapshot() {
Expand All @@ -143,8 +159,8 @@ class BaseMetric {

/**
* Metric has been changed.
* @param {any} value
* @param {Object} labels
* @param {any?} value
* @param {Object?} labels
* @param {Number?} timestamp
*/
changed(value, labels, timestamp) {
Expand Down
18 changes: 2 additions & 16 deletions src/metrics/types/counter.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
import BaseMetric = require("./base");
import GaugeMetric = require("./gauge");

declare namespace CounterMetric {
export interface CounterMetricSnapshot {
value: number;
labels: Record<string, any>;
timestamp: number;
}
}

declare class CounterMetric extends BaseMetric<CounterMetric.CounterMetricSnapshot> {
increment(labels?: Record<string, any>, value?: number, timestamp?: number): void;

set(value: number, labels?: Record<string, any>, timestamp?: number): void;

generateSnapshot(): CounterMetric.CounterMetricSnapshot[];
}
declare class CounterMetric extends GaugeMetric {}
export = CounterMetric;
13 changes: 11 additions & 2 deletions src/metrics/types/counter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand All @@ -9,16 +9,25 @@
const GaugeMetric = require("./gauge");
const METRIC = require("../constants");

/**
* Import types
*
* @typedef {import("../registry")} MetricRegistry
* @typedef {import("./counter")} CounterMetricClass
* @typedef {import("./base").BaseMetricOptions} BaseMetricOptions
*/

/**
* Counter metric class.
*
* @class CounterMetric
* @extends {GaugeMetric}
* @implements {CounterMetricClass}
*/
class CounterMetric extends GaugeMetric {
/**
* Creates an instance of CounterMetric.
* @param {Object} opts
* @param {BaseMetricOptions} opts
* @param {MetricRegistry} registry
* @memberof CounterMetric
*/
Expand Down
8 changes: 8 additions & 0 deletions src/metrics/types/gauge.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import BaseMetric = require("./base");
import type MetricRegistry = require("../registry");

declare namespace GaugeMetric {
export interface GaugeMetricSnapshot {
key: string;
value: number;
labels: Record<string, any>;
timestamp: number;
}
}

declare class GaugeMetric extends BaseMetric<GaugeMetric.GaugeMetricSnapshot> {
constructor(opts: BaseMetric.BaseMetricOptions, registry: MetricRegistry);

increment(labels?: Record<string, any>, value?: number, timestamp?: number): void;

decrement(labels?: Record<string, any>, value?: number, timestamp?: number): void;

set(value: number, labels?: Record<string, any>, timestamp?: number): void;

reset(labels?: Record<string, any>, timestamp?: number): void;

resetAll(timestamp?: number): void;

generateSnapshot(): GaugeMetric.GaugeMetricSnapshot[];
}
export = GaugeMetric;
18 changes: 14 additions & 4 deletions src/metrics/types/gauge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand All @@ -11,16 +11,26 @@ const BaseMetric = require("./base");
const METRIC = require("../constants");
const MetricRate = require("../rates");

/**
* Import types
*
* @typedef {import("../registry")} MetricRegistry
* @typedef {import("./gauge")} GaugeMetricClass
* @typedef {import("./gauge").GaugeMetricSnapshot} GaugeMetricSnapshot
* @typedef {import("./base").BaseMetricOptions} BaseMetricOptions
*/

/**
* Gauge metric class.
*
* @class GaugeMetric
* @extends {BaseMetric}
* @implements {GaugeMetricClass}
*/
class GaugeMetric extends BaseMetric {
/**
* Creates an instance of GaugeMetric.
* @param {Object} opts
* @param {BaseMetricOptions} opts
* @param {MetricRegistry} registry
* @memberof GaugeMetric
*/
Expand Down Expand Up @@ -105,7 +115,7 @@ class GaugeMetric extends BaseMetric {
/**
* Reset item by labels.
*
* @param {Object} labels
* @param {Object?} labels
* @param {Number?} timestamp
* @returns
* @memberof GaugeMetric
Expand All @@ -131,7 +141,7 @@ class GaugeMetric extends BaseMetric {
/**
* Generate a snapshot.
*
* @returns {Array<Object>}
* @returns {Array<GaugeMetricSnapshot>}
* @memberof GaugeMetric
*/
generateSnapshot() {
Expand Down
35 changes: 35 additions & 0 deletions src/metrics/types/histogram.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import BaseMetric = require("./base");

declare namespace HistogramMetric {

export interface HistogramMetricOptions extends BaseMetric.BaseMetricOptions {
linearBuckets?: {
start: number;
width: number;
count: number;
};
exponentialBuckets?: {
start: number;
factor: number;
count: number;
};
buckets?: boolean|number[];
quantiles?: boolean|number[];

maxAgeSeconds?: number;
ageBuckets?: number;
rate?: boolean;
}

export interface HistogramMetricSnapshot {
labels: Record<string, any>;
count: number;
Expand Down Expand Up @@ -31,6 +51,21 @@ declare class HistogramMetric extends BaseMetric<HistogramMetric.HistogramMetric

observe(value: number, labels?: Record<string, any>, timestamp?: number): void;

createBucketValues(): Record<string, number>;

generateSnapshot(): HistogramMetric.HistogramMetricSnapshot[];

generateItemSnapshot(item: Record<string, any>, key: string): HistogramMetric.HistogramMetricSnapshot;

resetItem(item: Record<string, any>, timestamp?: number): Record<string, any>;

reset(labels?: Record<string, any>, timestamp?: number): void;

resetAll(timestamp?: number): void;

static generateLinearBuckets(start: number, width: number, count: number): number[];

static generateExponentialBuckets(start: number, factor: number, count: number): number[];

}
export = HistogramMetric;
Loading

0 comments on commit 0fb79ab

Please sign in to comment.