gRPC Health Check

An implementation of gRPC health checks, written in typescript.
It is assumed that you are using the grpc
library.
Installation
npm install grpc-ts-health-check --save
Install the grpc
library:
npm install grpc --save
Dependencies
- Google Protobuf: Protocol Buffers - Google's data interchange format.
- gRPC Boom: A zero dependency library to help create gRPC-friendly error objects.
Usage
import * as grpc from 'grpc';
import { GrpcHealthCheck, HealthCheckResponse, HealthService } from 'grpc-ts-health-check';
const serviceName = 'auth.Authenticator';
const healthCheckStatusMap = {
serviceName: HealthCheckResponse.ServingStatus.UNKNOWN
};
function start(): grpc.Server {
const server: grpc.Server = new grpc.Server();
const grpcHealthCheck = new GrpcHealthCheck(healthCheckStatusMap);
server.addService(HealthService, grpcHealthCheck);
server.bind('localhost:9090', grpc.ServerCredentials.createInsecure());
server.start();
const healthClient = new HealthClient(`${host}:${port}`, grpc.credentials.createInsecure());
const request = new HealthCheckRequest();
request.setService(serviceName);
healthClient.check(request, (error: Error | null, response: HealthCheckResponse) => {
if (error) {
AppLogger.logger.error('Contact Service: Health Check Failed', error);
} else {
AppLogger.logger.debug(`Contact Service: Health Check Status: ${response.getStatus()}`);
}
});
const healthStream = healthClient.watch(request);
healthStream.on('data', (response: HealthCheckResponse) => {
AppLogger.logger.debug(`Contact Service: Health Check Status: ${response.getStatus()}`);
});
return server;
}