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

Add type definition #35

Merged
merged 1 commit into from
Sep 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
80 changes: 80 additions & 0 deletions metric.d.ts
Original file line number Diff line number Diff line change
@@ -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<MetricLabel>;
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<string>;
description: Readonly<string>;
value: Readonly<number | null>;
type: Readonly<MetricType>;
source: string | null;
timestamp: Readonly<number | null>;
/**
* @deprecated Use `timestamp` instead.
*/
time: Readonly<number | null>;
labels: Readonly<Array<MetricLabel>>;
meta: Readonly<unknown>;

toJSON(): {
name: string;
description: string;
timestamp: number | null;
type: MetricType;
value: number | null;
labels: Array<MetricLabel>;
time: number | null;
meta: unknown;
};
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Loading