|
| 1 | +### Starting Prometheus and Grafana Services |
| 2 | + |
| 3 | +**(1) Prometheus Service** |
| 4 | + |
| 5 | +Here is the [script for starting the Prometheus service](https://github.com/zhufuyi/sponge/tree/main/test/server/monitor/prometheus). Start the Prometheus service: |
| 6 | + |
| 7 | +```bash |
| 8 | +docker-compose up -d |
| 9 | +``` |
| 10 | + |
| 11 | +Access the Prometheus homepage in your browser at [http://localhost:9090](http://localhost:9090/). |
| 12 | + |
| 13 | +<br> |
| 14 | + |
| 15 | +**(2) Grafana Service** |
| 16 | + |
| 17 | +Here is the [script for starting the Grafana service](https://github.com/zhufuyi/sponge/tree/main/test/server/monitor/grafana). Start the Grafana service: |
| 18 | + |
| 19 | +```bash |
| 20 | +docker-compose up -d |
| 21 | +``` |
| 22 | + |
| 23 | +Access the main Grafana page in your browser at [http://localhost:33000](http://localhost:33000), and configure the Prometheus data source to be `http://localhost:9090`. |
| 24 | + |
| 25 | +> [!attention] When importing JSON dashboards into Grafana, the **datasource** value in the JSON must match the name of the Prometheus data source you set in Grafana (in this case, **Prometheus**), or the graphs won't display data. |
| 26 | +
|
| 27 | +<br> |
| 28 | + |
| 29 | +### Web Service Monitoring Example |
| 30 | + |
| 31 | +Taking the code for the `⓵ Web Service Based on SQL` as an example, it provides a default metrics interface at [http://localhost:8080/metrics](http://localhost:8080/metrics). |
| 32 | + |
| 33 | +**(1) Adding Monitoring Targets to Prometheus** |
| 34 | + |
| 35 | +Open the Prometheus configuration file `prometheus.yml` and add scraping targets: |
| 36 | + |
| 37 | +```bash |
| 38 | + - job_name: 'http-edusys' |
| 39 | + scrape_interval: 10s |
| 40 | + static_configs: |
| 41 | + - targets: ['localhost:8080'] |
| 42 | +``` |
| 43 | + |
| 44 | +> [!attention] Before starting the Prometheus service, make sure to change the permissions of the `prometheus.yml` file to `0777`. Otherwise, changes made to `prometheus.yml` using `vim` won't be synchronized with the container. |
| 45 | +
|
| 46 | +Trigger the Prometheus configuration to take effect by executing `curl -X POST http://localhost:9090/-/reload`. Wait for a moment, then access [http://localhost:9090/targets](http://localhost:9090/targets) in your browser to check if the newly added scraping target is active. |
| 47 | + |
| 48 | +<br> |
| 49 | + |
| 50 | +**(2) Adding Monitoring Dashboards to Grafana** |
| 51 | + |
| 52 | +Import the [HTTP monitoring dashboard](https://github.com/zhufuyi/sponge/blob/main/pkg/gin/middleware/metrics/gin_grafana.json) into Grafana. If the monitoring interface does not display data, check if the data source name in the JSON matches the Prometheus data source name in Grafana's configuration. |
| 53 | + |
| 54 | +<br> |
| 55 | + |
| 56 | +**(3) Load Testing the API and Observing Monitoring Data** |
| 57 | + |
| 58 | +Use the [wrk](https://github.com/wg/wrk) tool to perform load testing on the API: |
| 59 | + |
| 60 | +```bash |
| 61 | +# test 1 |
| 62 | +wrk -t2 -c10 -d10s http://192.168.3.27:8080/api/v1/teacher/1 |
| 63 | + |
| 64 | +# test 2 |
| 65 | +wrk -t2 -c10 -d10s http://192.168.3.27:8080/api/v1/course/1 |
| 66 | +``` |
| 67 | + |
| 68 | +The monitoring interface will look like the following image: |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +<br> |
| 73 | + |
| 74 | +### GRPC Service Monitoring Example |
| 75 | + |
| 76 | +Taking the code for the `⓶Create grpc service based on sql` as an example, it provides a default metrics interface at [http://localhost:8283/metrics](http://localhost:8283/metrics). |
| 77 | + |
| 78 | +**(1) Adding Monitoring Targets to Prometheus** |
| 79 | + |
| 80 | +Open the Prometheus configuration file `prometheus.yml` and add scraping targets: |
| 81 | + |
| 82 | +```yaml |
| 83 | + - job_name: 'rpc-server-user' |
| 84 | + scrape_interval: 10s |
| 85 | + static_configs: |
| 86 | + - targets: ['localhost:8283'] |
| 87 | +``` |
| 88 | +
|
| 89 | +> [!attention] Before starting the Prometheus service, make sure to change the permissions of the `prometheus.yml` file to `0777`. Otherwise, changes made to `prometheus.yml` using `vim` won't be synchronized with the container. |
| 90 | + |
| 91 | +Trigger the Prometheus configuration to take effect by executing `curl -X POST http://localhost:9090/-/reload`. Wait for a moment, then access [http://localhost:9090/targets](http://localhost:9090/targets) in your browser to check if the newly added scraping target is active. |
| 92 | + |
| 93 | +<br> |
| 94 | + |
| 95 | +**(2) Adding Monitoring Dashboards to Grafana** |
| 96 | + |
| 97 | +Import the [grpc service monitoring dashboard](https://github.com/zhufuyi/sponge/blob/main/pkg/grpc/metrics/server_grafana.json) into Grafana. If the monitoring interface does not display data, check if the data source name in the JSON matches the Prometheus data source name in Grafana's configuration. |
| 98 | + |
| 99 | +<br> |
| 100 | + |
| 101 | +**(3) Load Testing GRPC Methods and Observing Monitoring Data** |
| 102 | + |
| 103 | +Open the `internal/service/teacher_client_test.go` file using the `Goland` IDE and test various methods under **Test_teacherService_methods** or **Test_teacherService_benchmark**. |
| 104 | + |
| 105 | +The monitoring interface will look like the following image: |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +<br> |
| 110 | + |
| 111 | +The monitoring for the grpc client is similar to the server monitoring, and you can find the [grpc client monitoring dashboard](https://github.com/zhufuyi/sponge/blob/main/pkg/grpc/metrics/client_grafana.json) here. |
| 112 | + |
| 113 | +<br> |
| 114 | + |
| 115 | +### Automatically Adding and Removing Monitoring Targets in Prometheus |
| 116 | + |
| 117 | +In real-world scenarios, managing monitoring targets in Prometheus manually can be cumbersome and error-prone, especially when dealing with a large number of services. Prometheus supports dynamic configuration using service discovery with tools like `Consul` to automatically add and remove monitoring targets. |
| 118 | + |
| 119 | +Start a local Consul service using the [Consul service start script](https://github.com/zhufuyi/sponge/tree/main/test/server/consul): |
| 120 | + |
| 121 | +```bash |
| 122 | +docker-compose up -d |
| 123 | +``` |
| 124 | + |
| 125 | +Open the Prometheus configuration file `prometheus.yml` and add Consul configuration: |
| 126 | + |
| 127 | +```yaml |
| 128 | + - job_name: 'consul-micro-exporter' |
| 129 | + consul_sd_configs: |
| 130 | + - server: 'localhost:8500' |
| 131 | + services: [] |
| 132 | + relabel_configs: |
| 133 | + - source_labels: [__meta_consul_tags] |
| 134 | + regex: .*user.* |
| 135 | + action: keep |
| 136 | + - regex: __meta_consul_service_metadata_(.+) |
| 137 | + action: labelmap |
| 138 | +``` |
| 139 | + |
| 140 | +Trigger the Prometheus configuration to take effect by executing `curl -X POST http://localhost:9090/-/reload`. |
| 141 | + |
| 142 | +Once you've configured Prometheus with Consul service discovery, you can push service address information to Consul. Push the information using a JSON file named `user_exporter.json` with content like this: |
| 143 | + |
| 144 | +```json |
| 145 | +{ |
| 146 | + "ID": "user-exporter", |
| 147 | + "Name": "user", |
| 148 | + "Tags": [ |
| 149 | + "user-exporter" |
| 150 | + ], |
| 151 | + "Address": "localhost", |
| 152 | + "Port": 8283, |
| 153 | + "Meta": { |
| 154 | + "env": "dev", |
| 155 | + "project": "user" |
| 156 | + }, |
| 157 | + "EnableTagOverride": false, |
| 158 | + "Check": { |
| 159 | + "HTTP": "http://localhost:8283/metrics", |
| 160 | + "Interval": "10s" |
| 161 | + }, |
| 162 | + "Weights": { |
| 163 | + "Passing": 10, |
| 164 | + "Warning": 1 |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +> curl -XPUT --data @user_exporter.json http://localhost:8500/v1/agent/service/register |
| 170 | + |
| 171 | +Wait for a moment, then open [http://localhost:9090/targets](http://localhost:9090/targets) in your browser to check if the newly added scraping target is active. Then, stop the service and wait for a moment to check if the target is automatically removed. |
| 172 | + |
| 173 | +> [!tip] In web or grpc service, the JSON information is usually submitted to Consul automatically via program code, not through commands. After the web or grpc service starts normally, Prometheus can dynamically discover monitoring targets. When the web or grpc service stops, Prometheus automatically removes the monitoring target. |
| 174 | + |
| 175 | +<br> |
0 commit comments