@@ -4,7 +4,70 @@ const { test } = require("tap");
44const MetricsClient = require ( "@metrics/client" ) ;
55
66const 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
972test ( "has a start and stop method" , async ( t ) => {
1073 const testHelper = new TestConsumer ( new MetricsClient ( ) ) ;
0 commit comments