From 81f4e7a315d19d7756a2387ec1475e6b2029ad9e Mon Sep 17 00:00:00 2001 From: Mattias Williams Date: Wed, 19 Jul 2023 14:31:55 +1000 Subject: [PATCH] feat(createdTimedSpan): Add `tags` (#214) --- src/createTimedSpan.test.ts | 24 ++++++++++++++++++++++++ src/createTimedSpan.ts | 5 +++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/createTimedSpan.test.ts b/src/createTimedSpan.test.ts index 895eece..b2d9818 100644 --- a/src/createTimedSpan.test.ts +++ b/src/createTimedSpan.test.ts @@ -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'); diff --git a/src/createTimedSpan.ts b/src/createTimedSpan.ts index 1670789..b9c21b6 100644 --- a/src/createTimedSpan.ts +++ b/src/createTimedSpan.ts @@ -19,6 +19,7 @@ export const createTimedSpan = name: string, block: () => PromiseLike, afterCompletion?: (duration: number, success: boolean) => void, + tags?: string[], ): Promise => { const startTime = process.hrtime.bigint(); @@ -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); };