Skip to content

Commit

Permalink
Merge pull request #2 from metrics-js/return-metric
Browse files Browse the repository at this point in the history
feat: return the whole metric object from getResults()
  • Loading branch information
wkillerud authored Feb 6, 2025
2 parents 51c4e6c + ca49eb2 commit 12d9719
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)

Expand All @@ -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);
});
```

Expand Down
10 changes: 4 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ class TestConsumer {
}

/**
* Utility function returning an array of simple metric object.
* @return {Promise<Array<Partial<import("@metrics/metric")>>>} list of simplified metrics
* Utility function returning an array of metric objects.
*
* @return {Promise<Array<import("@metrics/metric")>>} list of metrics
*/
async getResults() {
return (await this.result).map((metric) => ({
name: metric.name,
labels: metric.labels,
}));
return await this.result;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 12d9719

Please sign in to comment.