Monitoring gRPC Uptime
Tags: node, devops, testing
Published: 2020-07-09 12:33:01

Uptime monitoring for gRPC can't be done with traditional HTTP checks. With Asserted you can write sophisticated external health checks using the gRPC client and Mocha.

> Example on [GitHub](https://github.com/assertedio/grpc-uptime)

gRPC is an open source, high performance RPC framework that uses protocol buffers to efficiently serialize structured data between servers and clients in dozens of languages.

The specialized interchange format that makes it highly performant is also means that a gRPC server usually requires a specialized client to communicate, regular HTTP libraries won't work. As a result, Asserted is uniquely suited to providing the custom environment needed to externally monitor gRPC uptime and stability.

The example code used in this walk-through is heavily based on the Node example provided [here](https://github.com/grpc/grpc/tree/v1.30.0/examples/node).

## Example Server

The proto definitions that this server will use are shown below, and are taken directly from the official gRPC Node example mentioned above.

{% gist https://gist.github.com/ehacke/f09fb731052c13e84c3fe59344ea4fb7.js %}

The gRPC server that the example tests will run against is described in [this file](https://github.com/assertedio/grpc-uptime/blob/master/route_guide/route_guide_server.js). It's too large to show here in it's entirety, but I'll summarize the major elements.

The server exposes four RPCs:

- a simple RPC for getting a single object
- a server-side streaming RPC to retrieve a list
- a client-side streaming RPC to record a series of events
- and a bi-directional RPC to provide a simple chat function

## Routine Configuration

The routine.json makes use of [custom dependencies](https://docs.asserted.io/reference/included-dependencies#custom-dependencies). Custom dependencies are available on paid plans, and here we're using that option to include the Socket.IO client library in our tests.

{% gist https://gist.github.com/ehacke/c1073497f7ea960e6218e8faa77b32c9.js %}

## Routine package.json

The package.json for the routine (inside the .asserted directory) is slightly different than the default in this case because of the custom dependencies. In this case we're adding a few convenience libraries as well as @grpc/proto-loader, google-protobuf, and grpc.

{% gist https://gist.github.com/ehacke/2222589af841eaa5cf40078353690674.js %}

## Continuous Integration Tests

To start off, we create a gRPC client based on the same proto as the server.

{% gist https://gist.github.com/ehacke/9f8f751baa4c7d8d516fd842d57ac059.js %}

Then we use that client to perform tests against each of the four RPCs we defined earlier.

The simple RPC just retrieves a single object and asserts that it matches what is expected.

{% gist https://gist.github.com/ehacke/4205fa408961fb6a73159301f1a089e6.js %}

From the client side, the list RPC (streaming on the server-side) looks fairly similar to the simple RPC above.

{% gist https://gist.github.com/ehacke/e60669bcfb49e08e529edaabefc95497.js %}

The client-side streaming RPC sends a number of points to the server, and asserts the result.

{% gist https://gist.github.com/ehacke/eab2d983318a2d6505bbbe39d1115f53.js %}

And finally the bidirectional RPC sends a series of notes to the server which responds once the call is ended.

{% gist https://gist.github.com/ehacke/4992ce8c4454a06d8bcb7c287dc5aa79.js %}

## Next Steps

While the example shown [here](https://github.com/assertedio/grpc-uptime) can be cloned and run locally without an account, you'll need to do a few extra steps if you want to create your own Asserted routine to integration test your API in production.

1. Create an [Asserted](https://asserted.io) account. It's free and easy.
2. Complete the 2 minute onboarding to ensure that your environment is ready. You can also reference the docs [here](https://docs.asserted.io).
3. Start writing and running tests in prod!