Skip to content

Commit

Permalink
refactor: better prom metric helper types (#6261)
Browse files Browse the repository at this point in the history
Improves typing in our Prometheus metric helpers.
  • Loading branch information
nunogois committed Feb 16, 2024
1 parent b02f800 commit 5f781b4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
16 changes: 13 additions & 3 deletions src/lib/util/metrics/createCounter.ts
@@ -1,4 +1,14 @@
import { Counter, CounterConfiguration } from 'prom-client';
import { Counter as PromCounter, CounterConfiguration } from 'prom-client';

/**
* A wrapped instance of prom-client's Counter, overriding some of its methods for enhanced functionality and type-safety.
*/
export type Counter<T extends string = string> = {
counter: PromCounter<T>;
labels: (labels: Record<T, string | number>) => PromCounter.Internal;
inc: (value?: number | undefined) => void;
increment: (labels: Record<T, string | number>, value?: number) => void;
};

/**
* Creates a wrapped instance of prom-client's Counter, overriding some of its methods for enhanced functionality and type-safety.
Expand All @@ -9,11 +19,11 @@ import { Counter, CounterConfiguration } from 'prom-client';
*/
export const createCounter = <T extends string>(
options: CounterConfiguration<T>,
) => {
): Counter<T> => {
/**
* The underlying instance of prom-client's Counter.
*/
const counter = new Counter(options);
const counter = new PromCounter<T>(options);

/**
* Applies given labels to the counter. Labels are key-value pairs.
Expand Down
16 changes: 13 additions & 3 deletions src/lib/util/metrics/createGauge.ts
@@ -1,4 +1,14 @@
import { Gauge, GaugeConfiguration } from 'prom-client';
import { Gauge as PromGauge, GaugeConfiguration } from 'prom-client';

/**
* A wrapped instance of prom-client's Gauge, overriding some of its methods for enhanced functionality and type-safety.
*/
export type Gauge<T extends string = string> = {
gauge: PromGauge<T>;
labels: (labels: Record<T, string | number>) => PromGauge.Internal<T>;
reset: () => void;
set: (value: number) => void;
};

/**
* Creates a wrapped instance of prom-client's Gauge, overriding some of its methods for enhanced functionality and type-safety.
Expand All @@ -9,11 +19,11 @@ import { Gauge, GaugeConfiguration } from 'prom-client';
*/
export const createGauge = <T extends string>(
options: GaugeConfiguration<T>,
) => {
): Gauge<T> => {
/**
* The underlying instance of prom-client's Gauge.
*/
const gauge = new Gauge(options);
const gauge = new PromGauge(options);

/**
* Applies given labels to the gauge. Labels are key-value pairs.
Expand Down
15 changes: 12 additions & 3 deletions src/lib/util/metrics/createSummary.ts
@@ -1,4 +1,13 @@
import { Summary, SummaryConfiguration } from 'prom-client';
import { Summary as PromSummary, SummaryConfiguration } from 'prom-client';

/**
* A wrapped instance of prom-client's Summary, overriding some of its methods for enhanced functionality and type-safety.
*/
export type Summary<T extends string = string> = {
summary: PromSummary<T>;
labels: (labels: Record<T, string | number>) => PromSummary.Internal<T>;
observe: (value: number) => void;
};

/**
* Creates a wrapped instance of prom-client's Summary, overriding some of its methods for enhanced functionality and type-safety.
Expand All @@ -9,11 +18,11 @@ import { Summary, SummaryConfiguration } from 'prom-client';
*/
export const createSummary = <T extends string>(
options: SummaryConfiguration<T>,
) => {
): Summary<T> => {
/**
* The underlying instance of prom-client's Summary.
*/
const summary = new Summary(options);
const summary = new PromSummary(options);

/**
* Applies given labels to the summary. Labels are key-value pairs.
Expand Down

0 comments on commit 5f781b4

Please sign in to comment.