Skip to content

Commit

Permalink
fix: add get method to type definitions of metric classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jan 17, 2023
1 parent 8d6456c commit 524e786
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export class Registry {
/**
* Get all metrics as objects
*/
getMetricsAsJSON(): Promise<metric[]>;
getMetricsAsJSON(): Promise<MetricObjectWithValues<MetricValue<string>>[]>;

/**
* Get all metrics as objects
*/
getMetricsAsArray(): metric[];
getMetricsAsArray(): MetricObject[];

/**
* Remove a single metric
Expand Down Expand Up @@ -136,14 +136,28 @@ export enum MetricType {

type CollectFunction<T> = (this: T) => void | Promise<void>;

interface metric {
interface MetricObject {
name: string;
help: string;
type: MetricType;
aggregator: Aggregator;
collect: CollectFunction<any>;
}

interface MetricObjectWithValues<T extends MetricValue<string>>
extends MetricObject {
values: T[];
}

type MetricValue<T extends string> = {
value: number;
labels: LabelValues<T>;
};

type MetricValueWithName<T extends string> = MetricValue<T> & {
metricName?: string;
};

type LabelValues<T extends string> = Partial<Record<T, string | number>>;

interface MetricConfiguration<T extends string> {
Expand Down Expand Up @@ -182,6 +196,11 @@ export class Counter<T extends string = string> {
*/
inc(value?: number): void;

/**
* Get counter metric object
*/
get(): Promise<MetricObjectWithValues<MetricValue<T>>>;

/**
* Return the child for given labels
* @param values Label values
Expand Down Expand Up @@ -277,6 +296,11 @@ export class Gauge<T extends string = string> {
*/
set(value: number): void;

/**
* Get gauge metric object
*/
get(): Promise<MetricObjectWithValues<MetricValue<T>>>;

/**
* Set gauge value to current epoch time in ms
* @param labels Object with label keys and values
Expand Down Expand Up @@ -386,6 +410,11 @@ export class Histogram<T extends string = string> {
*/
observe(labels: LabelValues<T>, value: number): void;

/**
* Get histogram metric object
*/
get(): Promise<MetricObjectWithValues<MetricValueWithName<T>>>;

/**
* Start a timer. Calling the returned function will observe the duration in
* seconds in the histogram.
Expand Down Expand Up @@ -488,6 +517,11 @@ export class Summary<T extends string = string> {
*/
observe(labels: LabelValues<T>, value: number): void;

/**
* Get summary metric object
*/
get(): Promise<MetricObjectWithValues<MetricValueWithName<T>>>;

/**
* Start a timer. Calling the returned function will observe the duration in
* seconds in the summary.
Expand Down

0 comments on commit 524e786

Please sign in to comment.