Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ This release marks our first release under the Prometheus umbrella.
- Metric internal storage ('hashMap') changed to a separate object, LabelMap. If you have
subclassed the built-in metric types you may need to adjust your code.
- Counter Exemplars now report the value rather than the delta
- TypeScript: `MetricType` is now a string union matching the runtime values
(`'counter' | 'gauge' | 'histogram' | 'summary'`) instead of a numeric enum that had no
runtime object. Value-style uses such as `MetricType.Counter` (which threw at runtime)
no longer compile; compare against the string literals instead. Under
`verbatimModuleSyntax`, import it with `import type`.

### Changed

Expand Down
11 changes: 5 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,11 @@ export type Metric<T extends string = NoLabelNameType> =
*/
export type Aggregator = 'omit' | 'sum' | 'first' | 'min' | 'max' | 'average';

export enum MetricType {
Counter,
Gauge,
Histogram,
Summary,
}
/**
* The metric type reported in metric objects, such as those returned by
* `Registry#getMetricsAsJSON()`. Matches the runtime string values.
*/
export type MetricType = 'counter' | 'gauge' | 'histogram' | 'summary';

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

Expand Down
36 changes: 36 additions & 0 deletions test/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Registry,
MetricObject,
MetricObjectWithValues,
MetricType,
MetricValue,
MetricValueWithName,
} from '../index';
Expand Down Expand Up @@ -71,3 +72,38 @@ async function metricObjectTypesAreExported() {
void named;
}
void metricObjectTypesAreExported;

// MetricType matches the runtime strings reported in metric metadata, so a
// reported type can be compared against a literal and narrowed without casts.
// The switch fails to compile if the union and this member list ever drift
// apart, in either direction.
async function metricTypeMatchesRuntimeStrings() {
const t: MetricType = 'counter';
void t;

const [first] = await registry.getMetricsAsJSON();
if (first !== undefined && first.type === 'counter') {
const narrowed: 'counter' = first.type;
void narrowed;
}

const lockMembers = (type: MetricType): string => {
switch (type) {
case 'counter':
case 'gauge':
case 'histogram':
case 'summary':
return type;
default: {
const missing: never = type;
return missing;
}
}
};
void lockMembers;

// @ts-expect-error MetricType is a type-only string union with no runtime
// object (#336), so value-style access must not compile.
void MetricType.Counter;
}
void metricTypeMatchesRuntimeStrings;
Loading