Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node - Client API for retrieving internal statistics #2727

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
},
);
});
});
Loading