Skip to content

Commit

Permalink
Node - Client API for retrieving internal statistics (#2727)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Muhammad Awawdi <[email protected]>
  • Loading branch information
Muhammad-awawdi-amazon authored Nov 21, 2024
1 parent 475dba7 commit 34afc93
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
12 changes: 12 additions & 0 deletions node/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use glide_core::Telemetry;
use redis::GlideConnectionOptions;
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
Expand Down Expand Up @@ -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<JsObject> {
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)
}
9 changes: 9 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DEFAULT_TIMEOUT_IN_MILLISECONDS,
Script,
StartSocketConnection,
getStatistics,
valueFromSplitPointer,
} from "glide-rs";
import * as net from "net";
Expand Down Expand Up @@ -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();
}
}
34 changes: 34 additions & 0 deletions node/tests/GlideClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},
);
});
});

0 comments on commit 34afc93

Please sign in to comment.