Skip to content

Commit 2b287a4

Browse files
committed
fix: remove createMetric export from package
It was too tightly coupled to a specific implementation. BREAKING CHANGE: the createMetric function no longer exists. Make your own should you need it. See test/index.js in https://github.com/metrics-js/test-consumer for inspiration.
1 parent a6abe13 commit 2b287a4

File tree

2 files changed

+64
-73
lines changed

2 files changed

+64
-73
lines changed

lib/index.js

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -76,76 +76,4 @@ class TestConsumer {
7676
}
7777
}
7878

79-
/**
80-
* Helper object for creating dummy metrics for tests.
81-
*/
82-
const createMetric = {
83-
/**
84-
* Returns a metrics object for a timer
85-
* @param {object} [options={}]
86-
* @param {string} [options.uri]
87-
* @param {string} [options.method]
88-
* @param {string|number} [options.status]
89-
* @param {string|object} [options.type]
90-
* @return {{name, labels: *[]}}
91-
*/
92-
timer: (options = {}) => {
93-
const { uri, method, status, type } = options;
94-
return createMetric.base({
95-
name: "http_request_duration_seconds",
96-
uri,
97-
method,
98-
status,
99-
type,
100-
});
101-
},
102-
103-
/**
104-
* Returns a metrics object for a counter
105-
* @param {object} [options={}]
106-
* @param {string} [options.uri]
107-
* @param {string} [options.method]
108-
* @param {string|number} [options.status]
109-
* @param {object} [options.type]
110-
* @return {{name, labels: *[]}}
111-
*/
112-
counter: (options = {}) => {
113-
const { uri, method, status, type } = options;
114-
115-
return createMetric.base({
116-
name: "http_requests_total",
117-
uri,
118-
method,
119-
status,
120-
type,
121-
});
122-
},
123-
124-
/**
125-
* @param {object} options
126-
* @param {string} options.name
127-
* @param {string} options.uri=undefined
128-
* @param {string} options.method="GET"
129-
* @param {string|number} options.status=204
130-
* @param {object} options.type=undefined
131-
* @return {{name, labels: *[]}}
132-
* @private
133-
*/
134-
base: ({ name, uri = undefined, method = "GET", status = 204, type = undefined }) => ({
135-
name,
136-
labels: [
137-
...(method ? [{ name: "method", value: method }] : []),
138-
...[
139-
{
140-
name: "type",
141-
value: type,
142-
},
143-
],
144-
...(uri ? [{ name: "uri", value: uri }] : []),
145-
...(status ? [{ name: "status", value: status }] : []),
146-
],
147-
}),
148-
};
149-
15079
module.exports = TestConsumer;
151-
module.exports.createMetric = createMetric;

test/index.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,70 @@ const { test } = require("tap");
44
const MetricsClient = require("@metrics/client");
55

66
const TestConsumer = require("../lib/index");
7-
const { createMetric } = TestConsumer;
7+
8+
/**
9+
* Helper object for creating dummy metrics for tests.
10+
*/
11+
const createMetric = {
12+
/**
13+
* Returns a metrics object for a timer
14+
* @param {object} [options={}]
15+
* @param {string} [options.uri]
16+
* @param {string} [options.method]
17+
* @param {string|number} [options.status]
18+
* @param {string|object} [options.type]
19+
* @return {{name, labels: *[]}}
20+
*/
21+
timer: (options = {}) => {
22+
const { uri, method, status, type } = options;
23+
return createMetric.base({
24+
name: "http_request_duration_seconds",
25+
uri,
26+
method,
27+
status,
28+
type,
29+
});
30+
},
31+
32+
/**
33+
* Returns a metrics object for a counter
34+
* @param {object} [options={}]
35+
* @param {string} [options.uri]
36+
* @param {string} [options.method]
37+
* @param {string|number} [options.status]
38+
* @param {object} [options.type]
39+
* @return {{name, labels: *[]}}
40+
*/
41+
counter: (options = {}) => {
42+
const { uri, method, status, type } = options;
43+
44+
return createMetric.base({
45+
name: "http_requests_total",
46+
uri,
47+
method,
48+
status,
49+
type,
50+
});
51+
},
52+
53+
/**
54+
* @param {object} options
55+
* @param {string} options.name
56+
* @param {string} options.uri=undefined
57+
* @param {string} options.method="GET"
58+
* @param {string|number} options.status=204
59+
* @return {{name, labels: *[]}}
60+
* @private
61+
*/
62+
base: ({ name, uri = undefined, method = "GET", status = 204 }) => ({
63+
name,
64+
labels: [
65+
...(method ? [{ name: "method", value: method }] : []),
66+
...(uri ? [{ name: "uri", value: uri }] : []),
67+
...(status ? [{ name: "status", value: status }] : []),
68+
],
69+
}),
70+
};
871

972
test("has a start and stop method", async (t) => {
1073
const testHelper = new TestConsumer(new MetricsClient());

0 commit comments

Comments
 (0)