|
| 1 | +# gRPC service built with Tonic, http server build with warp, and Instrumented with Autometrics |
| 2 | + |
| 3 | + This code example has been adapted and modified from a Blog post by Mies Hernandez van Leuffen: [Adding observability to Rust gRPC services using Tonic and Autometrics](https://autometrics.dev/blog/adding-observability-to-rust-grpc-services-using-tonic-and-autometrics). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This example shows how to: |
| 8 | +* Add observability to a gRPC services |
| 9 | +* Add a http service |
| 10 | +* Start both, the grpc and http server |
| 11 | +* Add a graceful shutdown to both, grpc and http server |
| 12 | +* Closes a DB connection during graceful shutdown |
| 13 | + |
| 14 | +### Install the protobuf compiler |
| 15 | + |
| 16 | +The protobuf compiler (protoc) compiles protocol buffers into Rust code. |
| 17 | +Cargo will call protoc automatically during the build process, but you will |
| 18 | +get an error when protoc is not installed. Therefore, ensure protoc is installed. |
| 19 | + |
| 20 | +The recommended installation for macOS is via [Homebrew](https://brew.sh/): |
| 21 | + |
| 22 | +```bash |
| 23 | +brew install protobuf |
| 24 | +``` |
| 25 | +Check if the installation worked correctly: |
| 26 | + |
| 27 | +```bash |
| 28 | +protoc --version |
| 29 | +``` |
| 30 | + |
| 31 | +## Local Observability Development |
| 32 | + |
| 33 | +The easiest way to get up and running with this application is to clone the repo and get a local Prometheus setup using the [Autometrics CLI](https://github.com/autometrics-dev/am). |
| 34 | + |
| 35 | +Read more about Autometrics in Rust [here](https://github.com/autometrics-dev/autometrics-rs) and general docs [here](https://docs.autometrics.dev/). |
| 36 | + |
| 37 | + |
| 38 | +### Install the Autometrics CLI |
| 39 | + |
| 40 | +The recommended installation for macOS is via [Homebrew](https://brew.sh/): |
| 41 | + |
| 42 | +``` |
| 43 | +brew install autometrics-dev/tap/am |
| 44 | +``` |
| 45 | + |
| 46 | +Alternatively, you can download the latest version from the [releases page](https://github.com/autometrics-dev/am/releases) |
| 47 | + |
| 48 | +Spin up local Prometheus and start scraping your application that listens on port :8080. |
| 49 | + |
| 50 | +``` |
| 51 | +am start :8080 |
| 52 | +``` |
| 53 | + |
| 54 | +If you now inspect the Autometrics explorer on `http://localhost:6789` you will see your metrics. However, upon first start, all matrics are |
| 55 | +empty because no request has been sent yet. |
| 56 | + |
| 57 | +Now you can test your endpoints and generate some traffic and refresh the autometrics explorer to see you metrics. |
| 58 | + |
| 59 | +### Starting the Service |
| 60 | + |
| 61 | +```bash |
| 62 | +cargo run |
| 63 | +``` |
| 64 | + |
| 65 | +Expected output: |
| 66 | + |
| 67 | +``` |
| 68 | +Started gRPC server on port 50051 |
| 69 | +Started metrics on port 8080 |
| 70 | +Explore autometrics at http://127.0.0.1:6789 |
| 71 | +``` |
| 72 | + |
| 73 | +### Stopping the Service |
| 74 | + |
| 75 | +You can stop the service either via ctrl-c ore by sending a SIGTERM signal to kill the process. This has been implemented for Windows, Linux, Mac, and should also work on Docker and Kubernetes. |
| 76 | + |
| 77 | +On Windows, Linux, or Mac, just hit Ctrl-C |
| 78 | + |
| 79 | +Alternatively, you can send a SIGTERM signal from another process |
| 80 | +using the kill command on Linux or Mac. |
| 81 | + |
| 82 | +In a second terminal, run |
| 83 | + |
| 84 | +```bash |
| 85 | + ps | grep grpc-http |
| 86 | +``` |
| 87 | + |
| 88 | +Sample output: |
| 89 | + |
| 90 | +``` |
| 91 | +73014 ttys002 0:00.25 /Users/.../autometrics-rs/target/debug/grpc-http |
| 92 | +``` |
| 93 | + |
| 94 | +In this example, the service runs on PID 73014. Let's send a sigterm signal to shutdown the service. On you system, a different PID will be returned so please use that one instead. |
| 95 | + |
| 96 | +```bash |
| 97 | +kill 73014 |
| 98 | +``` |
| 99 | + |
| 100 | +Expected output: |
| 101 | + |
| 102 | +``` |
| 103 | +Received SIGTERM |
| 104 | +DB connection closed |
| 105 | +gRPC shutdown complete |
| 106 | +http shutdown complete |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +## Testing the GRPC endpoints |
| 111 | + |
| 112 | +Easiest way to test the endpoints is with `grpcurl` (`brew install grpcurl`). |
| 113 | + |
| 114 | +```bash |
| 115 | +grpcurl -plaintext -import-path ./proto -proto job.proto -d '{"name": "Tonic"}' 'localhost:50051' job.JobRunner.SendJob |
| 116 | +``` |
| 117 | + |
| 118 | +returns |
| 119 | + |
| 120 | +``` |
| 121 | +{ |
| 122 | + "message": "Hello Tonic!" |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Getting the list of jobs (currently hardcoded to return one job) |
| 127 | + |
| 128 | +```bash |
| 129 | +grpcurl -plaintext -import-path ./proto -proto job.proto -d '{}' 'localhost:50051' job.JobRunner.ListJobs |
| 130 | +``` |
| 131 | + |
| 132 | +returns: |
| 133 | + |
| 134 | +``` |
| 135 | +{ |
| 136 | + "job": [ |
| 137 | + { |
| 138 | + "id": 1, |
| 139 | + "name": "test" |
| 140 | + } |
| 141 | + ] |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## Viewing the metrics |
| 146 | + |
| 147 | +When you inspect the Autometrics explorer on `http://localhost:6789` you will see your metrics and SLOs. The explorer shows four tabs: |
| 148 | + |
| 149 | +1) Dashboard: Aggregated overview of all metrics |
| 150 | +2) Functions: Detailed metrics for each instrumented API function |
| 151 | +3) SLO's: Service Level Agreements for each instrumented API function |
| 152 | +4) Alerts: Notifications of violated SLO's or any other anomaly. |
| 153 | + |
0 commit comments