Skip to content

Commit 6f8aa65

Browse files
committed
grpc: Docs for client.healthCheck method
1 parent 06a2b15 commit 6f8aa65

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

docs/sources/k6/next/javascript-api/k6-net-grpc/client/_index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ aliases:
1616
| [Client.loadProtoset(protosetPath)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-loadprotoset) | Loads and parses the given protoset file to be made available for RPC requests. |
1717
| [Client.connect(address [,params])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-connect) | Opens a connection to the given gRPC server. |
1818
| [Client.invoke(url, request [,params])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-invoke) | Makes an unary RPC for the given service/method and returns a [Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/response). |
19+
| [Client.healthCheck([service])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-health-check) | Check the status of the service using the [Health Checking](https://grpc.io/docs/guides/health-checking) protocol. |
1920
| [Client.close()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-close) | Close the connection to the gRPC service. |
2021

2122
### Examples
@@ -36,7 +37,7 @@ export default () => {
3637

3738
const response = client.invoke('quickpizza.GRPC/RatePizza', {
3839
ingredients: ['Tomatoes', 'Cheese'],
39-
dough: 'Thin'
40+
dough: 'Thin',
4041
});
4142
console.log(response.message.starsRating); // should print a number between 1-5
4243

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: 'Client.healthCheck()'
3+
description: 'Check the health of a gRPC endpoint.'
4+
---
5+
6+
# Client.healthCheck([service])
7+
8+
Check the health status of the gRPC service using the gRPC [Health Checking](https://grpc.io/docs/guides/health-checking/) protocol.
9+
10+
The method returns the health status of the provided service. If no service name is provided, it checks the overall server health.
11+
12+
The health check response includes a `status` field that can be one of the following constants:
13+
14+
- `HealthCheckServing`: The service is healthy and serving requests
15+
- `HealthCheckNotServing`: The service is not serving requests
16+
- `HealthCheckUnknown`: The health status is unknown
17+
18+
### Example
19+
20+
{{< code >}}
21+
22+
```javascript
23+
import grpc from 'k6/net/grpc';
24+
import { check } from 'k6';
25+
26+
const client = new grpc.Client();
27+
client.load([], 'routeguide.proto');
28+
29+
export default () => {
30+
client.connect('localhost:10000', { plaintext: true });
31+
32+
// Check overall server health
33+
const res = client.healthCheck();
34+
35+
check(res, {
36+
'server is healthy': (r) => r && r.status === grpc.StatusOK,
37+
'health status is serving': (r) => r && r.message.status === grpc.HealthCheckServing,
38+
});
39+
40+
// Check specific service health
41+
const svcRes = client.healthCheck('my.service.Name');
42+
console.log('Health status:', svcRes.message.status);
43+
44+
client.close();
45+
};
46+
```
47+
48+
{{< /code >}}

docs/sources/k6/next/using-k6/protocols/grpc.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ It's important to note how k6 handles requests and messages. First, it tries to
263263
A limitation during this process is that the object you pass as a request/message must be serializable. That means structs like `Map` don't work.
264264

265265
The benefit of using `protojson` is the canonical JSON encoding support. The [Protocol Buffers documentation](https://protobuf.dev/programming-guides/proto3/#json) describes this mapping.
266-
Also note that according to [ProtoJSON docs](https://protobuf.dev/programming-guides/json/), the JSON value of a non-numeric float (e.g. `NaN` or `Infinity`) will be their equivalent string representation.
266+
Also note that according to [ProtoJSON docs](https://protobuf.dev/programming-guides/json/), the JSON value of a non-numeric float (e.g. `NaN` or `Infinity`) will be their equivalent string representation.
267267

268268
#### Examples
269269

@@ -341,3 +341,9 @@ const respWithID = client.invoke('testing.Service/Test', { id: 123 });
341341
```
342342

343343
{{< /code >}}
344+
345+
## Health Checking protocol
346+
347+
There is a dedicated support for the [Health Checking](https://grpc.io/docs/guides/health-checking) protocol, which provides a standard way to check if a gRPC service is healthy and ready to serve requests. It eliminates the need to use the generic `invoke` method for health checks.
348+
349+
The [Client.healthCheck()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-health-check) method implements this protocol and returns the health status of the specified service. If no service name is provided, it checks the overall server health.

0 commit comments

Comments
 (0)