Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ metrics
.increment();
// will increment 'http.requests' with 'verb:GET,path:users' tags
```
Use `tags` function to create a space with the same key and additional tags:
```js
metrics
.space('http', { verb: 'GET' })
.tags({ path: 'users' })
.increment();
// will increment 'http' with 'verb:GET,path:users' tags
```

##### Note
When the same tag is specified when creating nested spaces, the last value will be reported

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"datadog",
"DogStatsD"
],
"version": "2.0.0",
"version": "2.1.0",
"repository": {
"type": "git",
"url": "https://github.com/ysa23/metrics-reporter"
Expand Down
1 change: 1 addition & 0 deletions src/space.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export declare class Space {
meter: <T, Args extends any[]>(func: (...args:Args) => T) => (...args: Args) => T;

space: (nextKey: string, nextTags?: Tags) => Space;
tags: (nextTags: Tags) => Space;
}
7 changes: 7 additions & 0 deletions src/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ function Space({
});
};

this.tags = nextTags => {
const newTags = { ...tags, ...nextTags };
return new Space({
key, tags: newTags, reporters, errback,
});
};

function report(reportKey, start, finish) {
const duration = finish.getTime() - start.getTime();
forEachReporter(reporter => reporter.report(reportKey, duration, tags));
Expand Down
46 changes: 46 additions & 0 deletions src/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,52 @@ describe('Space', () => {
});
});

describe('tags', () => {
it('should create a report with the same key with a combination of all tags', () => {
const reports = [];
const reporter = new InMemoryReporter({ buffer: reports });
const metrics = new Metrics({ reporters: [reporter] });
const func = getSyncFunc(500);
const wrappedFunc = metrics.space('space').tags({ tag1: 'value1', tag2: 'value2' }).tags({ tag3: 'value3' })
.meter(func);

wrappedFunc(1, 1);

expect(reports).toEqual([
expect.objectContaining({
key: 'space',
tags: {
tag1: 'value1',
tag2: 'value2',
tag3: 'value3',
},
}),
]);
});

it('should override tag with the last value when same tag appears in different tags invocation', () => {
const reports = [];
const reporter = new InMemoryReporter({ buffer: reports });
const metrics = new Metrics({ reporters: [reporter] });
const func = getSyncFunc(500);
const wrappedFunc = metrics.space('space').tags({ tag1: 'value1', tag2: 'value2' }).tags({ tag3: 'value3' }).tags({ tag2: 'override-value' })
.meter(func);

wrappedFunc(1, 1);

expect(reports).toEqual([
expect.objectContaining({
key: 'space',
tags: {
tag1: 'value1',
tag2: 'override-value',
tag3: 'value3',
},
}),
]);
});
});

describe('increment', () => {
it('when value is not specify, increment by one', () => {
const reports = [];
Expand Down
Loading