Skip to content

Commit 21c8c85

Browse files
authored
Merge pull request #786 from milcho0604/fix/export-metric-object-types
fix(types): export MetricObjectWithValues, MetricValue and related types
2 parents 8085ab2 + b200ecd commit 21c8c85

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This release marks our first release under the Prometheus umbrella.
4040
- perf: Stat aggregation uses similar strategy to collection. 60% faster aggregation
4141
- chore: Add copyright license headers and test
4242
- Make cluster and worker-thread metric aggregation order deterministic
43+
- Export `MetricObject`, `MetricObjectWithValues`, `MetricValue` and `MetricValueWithName` from the TypeScript definitions
4344

4445
### Added
4546

index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,26 +279,26 @@ export enum MetricType {
279279

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

282-
interface MetricObject {
282+
export interface MetricObject {
283283
name: string;
284284
help: string;
285285
type: MetricType;
286286
aggregator: Aggregator;
287287
collect: CollectFunction<any>;
288288
}
289289

290-
interface MetricObjectWithValues<
290+
export interface MetricObjectWithValues<
291291
T extends MetricValue<string>,
292292
> extends MetricObject {
293293
values: T[];
294294
}
295295

296-
type MetricValue<T extends string> = {
296+
export type MetricValue<T extends string> = {
297297
value: number;
298298
labels: LabelValues<T>;
299299
};
300300

301-
type MetricValueWithName<T extends string> = MetricValue<T> & {
301+
export type MetricValueWithName<T extends string> = MetricValue<T> & {
302302
metricName?: string;
303303
};
304304

test/typescript.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Counter, Pushgateway, Registry } from '../index';
15+
import {
16+
Counter,
17+
Pushgateway,
18+
Registry,
19+
MetricObject,
20+
MetricObjectWithValues,
21+
MetricValue,
22+
MetricValueWithName,
23+
} from '../index';
1624

1725
const registry = new Registry();
1826
const counter = new Counter({
@@ -38,3 +46,28 @@ void metricsText;
3846
void gatewayWithRegistry;
3947
void gatewayWithOptionsAndRegistry;
4048
void gatewayWithNullOptionsAndRegistry;
49+
50+
// The metric-object types are exported, so consumers can name the return
51+
// types of Registry#getMetricsAsJSON()/getMetricsAsArray() and Metric#get()
52+
// instead of re-deriving them. These annotations fail to compile if the types
53+
// are removed, renamed, or reshaped.
54+
async function metricObjectTypesAreExported() {
55+
const asJson: MetricObjectWithValues<MetricValue<string>>[] =
56+
await registry.getMetricsAsJSON();
57+
void asJson;
58+
59+
const asArray: MetricObject[] = registry.getMetricsAsArray();
60+
void asArray;
61+
62+
const counterSnapshot: MetricObjectWithValues<MetricValue<string>> =
63+
await counter.get();
64+
void counterSnapshot;
65+
66+
const named: MetricValueWithName<string> = {
67+
value: 1,
68+
labels: {},
69+
metricName: 'typescript_test_counter',
70+
};
71+
void named;
72+
}
73+
void metricObjectTypesAreExported;

0 commit comments

Comments
 (0)