From d666568d63eb0c5295bf6b6d72a8b70669544b79 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Wed, 13 Sep 2023 09:29:26 +0200 Subject: [PATCH] Add type definition --- metric.d.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 6 ++-- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 metric.d.ts diff --git a/metric.d.ts b/metric.d.ts new file mode 100644 index 0000000..4920bff --- /dev/null +++ b/metric.d.ts @@ -0,0 +1,80 @@ +/** + * A hint of what type of metric this is. Each numeric value represents a different type of metric: + * + * - `0`: `unknown` + * - `1`: `gauge` + * - `2`: `counter` + * - `3`: `state_set` + * - `4`: `info` + * - `5`: `cumulative histogram` + * - `6`: `gauge histogram` + * - `7`: `summary` + */ +export type MetricType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; + +export type MetricLabel = { + /** + * The name must match the regular expression `[a-zA-Z0-9_]`. + */ + name: string; + value: string | number | boolean | null; +} + +export type MetricOptions = { + /** + * The name must match the regular expression `[a-zA-Z_:][a-zA-Z0-9_:]*`. + * + * By convention, colons are reserved for monitoring system use, and names + * beginning with underscores are reserved for monitoring-system internal use. + */ + name: string; + description: string; + /** + * @default Date.now() / 1000 + */ + timestamp?: number | null; + /** + * @deprecated Use `timestamp` instead. + */ + time?: number | null; + source?: string | null; + labels?: Array; + value?: number | null; + /** + * @default 0 (`unknown`) + */ + type?: MetricType; + /** + * Arbitrary metadata to attach to this metric. This field is not validated. + * @default {} + */ + meta?: unknown; +} + +export default class Metric { + constructor(args: MetricOptions); + + name: Readonly; + description: Readonly; + value: Readonly; + type: Readonly; + source: string | null; + timestamp: Readonly; + /** + * @deprecated Use `timestamp` instead. + */ + time: Readonly; + labels: Readonly>; + meta: Readonly; + + toJSON(): { + name: string; + description: string; + timestamp: number | null; + type: MetricType; + value: number | null; + labels: Array; + time: number | null; + meta: unknown; + }; +} diff --git a/package.json b/package.json index 63ad6a0..30a5b90 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,12 @@ { "name": "@metrics/metric", - "version": "2.3.1", + "version": "2.3.2", "description": "The metric class definition which metric objects in the @metrics library is instansiated from", "main": "lib/metric.js", + "types": "metric.d.ts", "files": [ - "lib" + "lib", + "metric.d.ts" ], "scripts": { "bench": "node benchmark/benchmark.js",