diff --git a/README.md b/README.md index 986c3b8..05c9ab5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This module is a memory based consumer for metrics streams to be used in tests. The purpose of the module is to make writing tests and asserting metrics easier. -It takes a metric stream generated by @metrics/client and makes the collected metrics available as an array. +It takes a metric stream generated by @metrics/client and makes the collected metrics available as an array. ⚠️ You should _never_ use this in produciton code, however it is very convenient when writing tests which produce metrics using [`@metrics/client`](https://metrics-js.github.io/reference/client/) @@ -16,29 +16,28 @@ It takes a metric stream generated by @metrics/client and makes the collected me Below is a sample test showing how this could be used: ```js -const test = require('tap'); -const Metrics = require('@metrics/client'); -const TestConsumer = require('@metrics/test-consumer'); - -test('some test case', async () => { - const metrics = new Metrics(); - // This sets up the metrics client to be used - const testHelper = new TestConsumer(metrics) - // .start sets up the stream - testHelper.start(); - - // Code which triggers a count metric - - testHelper.stop(); // .stop ends the streams and now we can get the result. - - testHelper.getResults().then(result => { - t.equal(result.name, "some_counter"); // Validate our metrics was collected - res.labels.forEach((metricLabel) => { - if (metricLabel.name === "value") { - t.equal(metricLabel.value, 2); // We expect two counts to have happened - } - }); - }); +const assert = require("node:assert"); +const { test } = require("node:test"); +const MetricsClient = require("@metrics/client"); +const TestConsumer = require("@metrics/test-consumer"); + +test("some test case", async () => { + // Pass in the metrics client you want to consume. + const metrics = new MetricsClient(); + const testHelper = new TestConsumer(metrics); + // Sets up the consumer to capture events. + testHelper.start(); + + // 👋 Your code which produces some kind of metric goes here. + + // Ends the streams, now we can get the result. + testHelper.stop(); + + const result = await testHelper.getResults(); + const metric = result.find((m) => m.name === "some_counter"); + + assert.ok(metric, "Expected to find metric some_counter"); + assert.equal(metric.value, 2); }); ``` diff --git a/lib/index.js b/lib/index.js index 58253ce..23b4d93 100644 --- a/lib/index.js +++ b/lib/index.js @@ -59,14 +59,12 @@ class TestConsumer { } /** - * Utility function returning an array of simple metric object. - * @return {Promise>>} list of simplified metrics + * Utility function returning an array of metric objects. + * + * @return {Promise>} list of metrics */ async getResults() { - return (await this.result).map((metric) => ({ - name: metric.name, - labels: metric.labels, - })); + return await this.result; } /** diff --git a/test/index.js b/test/index.js index 4f6610a..afc911b 100644 --- a/test/index.js +++ b/test/index.js @@ -43,3 +43,26 @@ test("can create dummy metric for counter & timer", async (t) => { const timer = createMetric.timer({ uri: "/lol2" }); t.equal(timer.name, "http_request_duration_seconds"); }); + +test("lets you get the value of a counter", async (t) => { + // Pass in the metrics client you want to consume. + const metrics = new MetricsClient(); + const testHelper = new TestConsumer(metrics); + // Sets up the consumer to capture events. + testHelper.start(); + + const counter = metrics.counter({ + name: "a_custom_counter_metric", + description: "A custom metric", + }); + counter.inc(2, { labels: { type: "some_label" } }); + + // Ends the streams, now we can get the result. + testHelper.stop(); + + const result = await testHelper.getResults(); + const metric = result.find((m) => m.name === "a_custom_counter_metric"); + + t.ok(metric, "Expected to find metric a_custom_counter_metric"); + t.equal(metric.value, 2); +});