Skip to content

Commit

Permalink
feat(createdTimedSpan): Add tags (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkiraJ48 authored Jul 19, 2023
1 parent 9287e97 commit 81f4e7a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/createTimedSpan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ describe('timedSpan', () => {
expect(duration).toBeGreaterThan(0);
});

it('should pass along tags', async () => {
const mockIncrement = jest.spyOn(metricsClient, 'increment');
const mockTiming = jest.spyOn(metricsClient, 'timing');

await timedSpan(
'test',
// This is false but we still successfully resolved
() => Promise.resolve(false),
() => {},
['new-tags'],
);

expect(mockIncrement).toHaveBeenCalledWith('test.count', [
'success',
'new-tags',
]);

expect(mockTiming).toHaveBeenCalledWith(
'test.latency',
expect.any(Number),
['new-tags'],
);
});

it('should handle failure', async () => {
const mockIncrement = jest.spyOn(metricsClient, 'increment');
const mockTiming = jest.spyOn(metricsClient, 'timing');
Expand Down
5 changes: 3 additions & 2 deletions src/createTimedSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const createTimedSpan =
name: string,
block: () => PromiseLike<T>,
afterCompletion?: (duration: number, success: boolean) => void,
tags?: string[],
): Promise<T> => {
const startTime = process.hrtime.bigint();

Expand All @@ -27,8 +28,8 @@ export const createTimedSpan =
const successTag = success ? 'success' : 'failure';
const durationMilliseconds = Number(durationNanos) / 1e6;

metricsClient.timing(`${name}.latency`, durationMilliseconds);
metricsClient.increment(`${name}.count`, [successTag]);
metricsClient.timing(`${name}.latency`, durationMilliseconds, tags);
metricsClient.increment(`${name}.count`, [successTag, ...(tags ?? [])]);

afterCompletion?.(durationMilliseconds, success);
};
Expand Down

0 comments on commit 81f4e7a

Please sign in to comment.