From 6d1cfec83e8ca19331aa114fe7f85b4ea5a179f0 Mon Sep 17 00:00:00 2001 From: Noemi Date: Mon, 10 Jan 2022 22:27:39 +0100 Subject: [PATCH] Implement unregister method for probes Before this change, it was possible to disable a probe by registering an empty probe with the same name, but this is neither obvious nor documented. This commit adds a `probes.unregister()` method that removes a probe in a more explicit manner. --- .../implement-unregister-method-for-probes.md | 7 +++++++ packages/nodejs/src/__tests__/probes.test.ts | 10 ++++++++++ packages/nodejs/src/interfaces/probes.ts | 5 +++++ packages/nodejs/src/probes/index.ts | 13 +++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 packages/nodejs/.changesets/implement-unregister-method-for-probes.md diff --git a/packages/nodejs/.changesets/implement-unregister-method-for-probes.md b/packages/nodejs/.changesets/implement-unregister-method-for-probes.md new file mode 100644 index 00000000..ac0bbf7f --- /dev/null +++ b/packages/nodejs/.changesets/implement-unregister-method-for-probes.md @@ -0,0 +1,7 @@ +--- +bump: "patch" +type: "add" +--- + +Implement unregister method for probes. You can now call the +`probes.unregister(name)` method to unregister a probe by name. diff --git a/packages/nodejs/src/__tests__/probes.test.ts b/packages/nodejs/src/__tests__/probes.test.ts index 402d6b78..ea497567 100644 --- a/packages/nodejs/src/__tests__/probes.test.ts +++ b/packages/nodejs/src/__tests__/probes.test.ts @@ -21,6 +21,16 @@ describe("Probes", () => { probes.register("test_metric", fn) jest.runOnlyPendingTimers() expect(fn).toHaveBeenCalled() + expect(probes.count).toEqual(1) + }) + + it("unregisters a probe", () => { + const fn = jest.fn() + probes.register("test_metric", fn) + probes.unregister("test_metric") + jest.runOnlyPendingTimers() + expect(fn).not.toHaveBeenCalled() + expect(probes.count).toEqual(0) }) describe("v8 probe", () => { diff --git a/packages/nodejs/src/interfaces/probes.ts b/packages/nodejs/src/interfaces/probes.ts index f9e07826..2eaecd60 100644 --- a/packages/nodejs/src/interfaces/probes.ts +++ b/packages/nodejs/src/interfaces/probes.ts @@ -13,6 +13,11 @@ export interface Probes { */ register(name: string, fn: () => void): this + /** + * Unregisters an existing minutely probe. + */ + unregister(name: string): this + /** * Unregisters all probes and clears the timers. */ diff --git a/packages/nodejs/src/probes/index.ts b/packages/nodejs/src/probes/index.ts index 6d8dab13..5af87b59 100644 --- a/packages/nodejs/src/probes/index.ts +++ b/packages/nodejs/src/probes/index.ts @@ -31,12 +31,25 @@ export class BaseProbes extends EventEmitter implements Probes { return this.on(name, fn) } + public unregister(name: string): this { + const timer = this.#timers.get(name) + + if (typeof timer !== "undefined") { + clearInterval(timer) + this.#timers.delete(name) + this.removeAllListeners(name) + } + + return this + } + /** * Unregisters all probes and clears the timers. */ public clear(): this { this.#timers.forEach(t => clearInterval(t)) this.#timers = new Map() + this.removeAllListeners() return this } }