Skip to content

grpc: Docs for client.healthCheck method #2022

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ aliases:
| [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. |
| [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. |
| [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). |
| [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. |
| [Client.close()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/client/client-close) | Close the connection to the gRPC service. |

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: 'Client.healthCheck()'
description: 'Check the health of a gRPC endpoint.'
---

# Client.healthCheck([service])

Check the health status of the gRPC service using the gRPC [Health Checking](https://grpc.io/docs/guides/health-checking/) protocol.

The method returns the health status of the provided service. If no service name is provided, it checks the overall server health.

The health check response includes a `status` field that can be one of the following constants:

- `HealthCheckServing`: The service is healthy and serving requests
- `HealthCheckNotServing`: The service is not serving requests
- `HealthCheckUnknown`: The health status is unknown

### Example

{{< code >}}

```javascript
import grpc from 'k6/net/grpc';
import { check } from 'k6';

const client = new grpc.Client();
client.load([], 'routeguide.proto');

export default () => {
client.connect('localhost:10000', { plaintext: true });

// Check overall server health
const res = client.healthCheck();

check(res, {
'server is healthy': (r) => r && r.status === grpc.StatusOK,
'health status is serving': (r) => r && r.message.status === grpc.HealthCheckServing,
});

// Check specific service health
const svcRes = client.healthCheck('my.service.Name');
console.log('Health status:', svcRes.message.status);

client.close();
};
```

{{< /code >}}
8 changes: 7 additions & 1 deletion docs/sources/k6/next/using-k6/protocols/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ It's important to note how k6 handles requests and messages. First, it tries to
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.

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.
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.
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.

#### Examples

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

{{< /code >}}

## Health Checking protocol

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.

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.
Loading