File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,6 +15,11 @@ This release marks our first release under the Prometheus umbrella.
1515- Metric internal storage ('hashMap') changed to a separate object, LabelMap. If you have
1616 subclassed the built-in metric types you may need to adjust your code.
1717- Counter Exemplars now report the value rather than the delta
18+ - TypeScript: ` MetricType ` is now a string union matching the runtime values
19+ (` 'counter' | 'gauge' | 'histogram' | 'summary' ` ) instead of a numeric enum that had no
20+ runtime object. Value-style uses such as ` MetricType.Counter ` (which threw at runtime)
21+ no longer compile; compare against the string literals instead. Under
22+ ` verbatimModuleSyntax ` , import it with ` import type ` .
1823
1924### Changed
2025
Original file line number Diff line number Diff line change @@ -270,12 +270,11 @@ export type Metric<T extends string = NoLabelNameType> =
270270 */
271271export type Aggregator = 'omit' | 'sum' | 'first' | 'min' | 'max' | 'average' ;
272272
273- export enum MetricType {
274- Counter ,
275- Gauge ,
276- Histogram ,
277- Summary ,
278- }
273+ /**
274+ * The metric type reported in metric objects, such as those returned by
275+ * `Registry#getMetricsAsJSON()`. Matches the runtime string values.
276+ */
277+ export type MetricType = 'counter' | 'gauge' | 'histogram' | 'summary' ;
279278
280279type CollectFunction < T > = ( this : T ) => void | Promise < void > ;
281280
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import {
1818 Registry ,
1919 MetricObject ,
2020 MetricObjectWithValues ,
21+ MetricType ,
2122 MetricValue ,
2223 MetricValueWithName ,
2324} from '../index' ;
@@ -71,3 +72,38 @@ async function metricObjectTypesAreExported() {
7172 void named ;
7273}
7374void metricObjectTypesAreExported ;
75+
76+ // MetricType matches the runtime strings reported in metric metadata, so a
77+ // reported type can be compared against a literal and narrowed without casts.
78+ // The switch fails to compile if the union and this member list ever drift
79+ // apart, in either direction.
80+ async function metricTypeMatchesRuntimeStrings ( ) {
81+ const t : MetricType = 'counter' ;
82+ void t ;
83+
84+ const [ first ] = await registry . getMetricsAsJSON ( ) ;
85+ if ( first !== undefined && first . type === 'counter' ) {
86+ const narrowed : 'counter' = first . type ;
87+ void narrowed ;
88+ }
89+
90+ const lockMembers = ( type : MetricType ) : string => {
91+ switch ( type ) {
92+ case 'counter' :
93+ case 'gauge' :
94+ case 'histogram' :
95+ case 'summary' :
96+ return type ;
97+ default : {
98+ const missing : never = type ;
99+ return missing ;
100+ }
101+ }
102+ } ;
103+ void lockMembers ;
104+
105+ // @ts -expect-error MetricType is a type-only string union with no runtime
106+ // object (#336), so value-style access must not compile.
107+ void MetricType . Counter ;
108+ }
109+ void metricTypeMatchesRuntimeStrings ;
You can’t perform that action at this time.
0 commit comments