From 34afc937228340c25db2c5c5a562cad91d9ebd21 Mon Sep 17 00:00:00 2001 From: Muhammad Awawdi Date: Thu, 21 Nov 2024 13:15:06 +0200 Subject: [PATCH] Node - Client API for retrieving internal statistics (#2727) --------- Signed-off-by: Muhammad Awawdi --- CHANGELOG.md | 1 + node/rust-client/src/lib.rs | 12 ++++++++++ node/src/BaseClient.ts | 9 +++++++ node/tests/GlideClusterClient.test.ts | 34 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f76b4ebb3..1729c2b2dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ #### Changes +* Node: Client API for retrieving internal statistics ([#2727](https://github.com/valkey-io/valkey-glide/pull/2727)) * Python: Client API for retrieving internal statistics ([#2707](https://github.com/valkey-io/valkey-glide/pull/2707)) * Node, Python, Java: Adding support for replacing connection configured password ([#2651](https://github.com/valkey-io/valkey-glide/pull/2651), [#2659](https://github.com/valkey-io/valkey-glide/pull/2659), [#2677](https://github.com/valkey-io/valkey-glide/pull/2677)) * Python: AZ Affinity - Python Wrapper Support ([#2676](https://github.com/valkey-io/valkey-glide/pull/2676)) diff --git a/node/rust-client/src/lib.rs b/node/rust-client/src/lib.rs index 82e546d295..b15b18f521 100644 --- a/node/rust-client/src/lib.rs +++ b/node/rust-client/src/lib.rs @@ -1,3 +1,4 @@ +use glide_core::Telemetry; use redis::GlideConnectionOptions; /** * Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 @@ -484,3 +485,14 @@ impl Drop for ClusterScanCursor { glide_core::cluster_scan_container::remove_scan_state_cursor(self.cursor.clone()); } } + +#[napi] +pub fn get_statistics(env: Env) -> Result { + let total_connections = Telemetry::total_connections().to_string(); + let total_clients = Telemetry::total_clients().to_string(); + let mut stats: JsObject = env.create_object()?; + stats.set_named_property("total_connections", total_connections)?; + stats.set_named_property("total_clients", total_clients)?; + + Ok(stats) +} diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 7c2e3feb57..d3ee6a91dc 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -7,6 +7,7 @@ import { DEFAULT_TIMEOUT_IN_MILLISECONDS, Script, StartSocketConnection, + getStatistics, valueFromSplitPointer, } from "glide-rs"; import * as net from "net"; @@ -7743,4 +7744,12 @@ export class BaseClient { return response; } + /** + * Return a statistics + * + * @return Return an object that contains the statistics collected internally by GLIDE core + */ + public getStatistics(): object { + return getStatistics(); + } } diff --git a/node/tests/GlideClusterClient.test.ts b/node/tests/GlideClusterClient.test.ts index 79868c36a8..f74e137cac 100644 --- a/node/tests/GlideClusterClient.test.ts +++ b/node/tests/GlideClusterClient.test.ts @@ -2398,4 +2398,38 @@ describe("GlideClusterClient", () => { }, ); }); + describe("GlideClusterClient - Get Statistics", () => { + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + "should return valid statistics using protocol %p", + async (protocol) => { + let glideClientForTesting; + + try { + // Create a GlideClusterClient instance for testing + glideClientForTesting = + await GlideClusterClient.createClient( + getClientConfigurationOption( + cluster.getAddresses(), + protocol, + { + requestTimeout: 2000, + }, + ), + ); + + // Fetch statistics using get_statistics method + const stats = await glideClientForTesting.getStatistics(); + + // Assertions to check if stats object has correct structure + expect(typeof stats).toBe("object"); + expect(stats).toHaveProperty("total_connections"); + expect(stats).toHaveProperty("total_clients"); + expect(Object.keys(stats)).toHaveLength(2); + } finally { + // Ensure the client is properly closed + await glideClientForTesting?.close(); + } + }, + ); + }); });