diff --git a/docs/sources/k6/next/javascript-api/k6-net-grpc/client/_index.md b/docs/sources/k6/next/javascript-api/k6-net-grpc/client/_index.md index 3d099b4355..ebefa90a8e 100644 --- a/docs/sources/k6/next/javascript-api/k6-net-grpc/client/_index.md +++ b/docs/sources/k6/next/javascript-api/k6-net-grpc/client/_index.md @@ -16,6 +16,7 @@ aliases: | [Client.loadProtoset(protosetPath)](https://grafana.com/docs/k6//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//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//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//javascript-api/k6-net-grpc/response). | +| [Client.healthCheck([service])](https://grafana.com/docs/k6//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//javascript-api/k6-net-grpc/client/client-close) | Close the connection to the gRPC service. | ### Examples @@ -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 diff --git a/docs/sources/k6/next/javascript-api/k6-net-grpc/client/client-health-check.md b/docs/sources/k6/next/javascript-api/k6-net-grpc/client/client-health-check.md new file mode 100644 index 0000000000..5e09095c7b --- /dev/null +++ b/docs/sources/k6/next/javascript-api/k6-net-grpc/client/client-health-check.md @@ -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 >}} diff --git a/docs/sources/k6/next/using-k6/protocols/grpc.md b/docs/sources/k6/next/using-k6/protocols/grpc.md index 94602640d5..7673888122 100644 --- a/docs/sources/k6/next/using-k6/protocols/grpc.md +++ b/docs/sources/k6/next/using-k6/protocols/grpc.md @@ -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 @@ -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//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.