当前位置:网站首页>Grpc: how to add API Prometheus monitoring interceptors / Middleware?
Grpc: how to add API Prometheus monitoring interceptors / Middleware?
2022-06-24 03:10:00 【Trespass 】
Introduce
This article will show you how to gRPC Add in microservice API Prometheus( Prometheus ) Interceptor / middleware . That is to say, it can be in Grafana Made in API monitor .
What is? API Prometheus( Prometheus ) Interceptor / middleware ?
Prometheus( Prometheus ) The interceptor will API Request record Prometheus( Prometheus ) monitor .
We will use rk-boot To start up gRPC service .
Please visit the following address for a complete tutorial :https://rkdev.info/cnhttps://rkdocs.netlify.app/cn ( spare )
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
Quick start
rk-boot By default, the following two open source libraries are integrated .
- rk-prom As Prometheus (Prometheus) Client startup Library .
Be careful ! In order for the example to proceed smoothly , Please be sure to in go.mod In the document ,module The suffix of is set to rk-demo. for example :module github.com/your-repo/rk-demo
1. establish boot.yaml
In order to verify , We launched the following options :
- commonService:commonService It contains a series of general API. details
- prom:Prometheus( Prometheus ) client .
- grpc-gateway:grpc It starts by default grpc-gateway To provide Restful API service . At the time of verification , We can send... Directly Restful request .
- prometheus middleware : start-up prometheus middleware .
---
grpc:
- name: greeter # Name of grpc entry
port: 8080 # Port of grpc entry
enabled: true # Enable grpc entry
commonService:
enabled: true # Enable common service for testing
prom:
enabled: true # Enable prometheus client
interceptors:
metricsProm:
enabled: true # Enable prometheus interceptor2. establish main.go
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-grpc/boot"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}3. Folder structure
$ tree . ├── boot.yaml ├── go.mod ├── go.sum └── main.go 0 directories, 4 files
4. verification
$ go run main.go
Send a request
$ curl -X GET localhost:8080/rk/v1/healthy
{"healthy":true}visit Prometheus client : http://localhost:8080/metrics
Visual monitoring
We have started in the local process prometheus monitor , The rest is how to be in a 【 beautiful 】 Check the monitoring on our website .
There are many tools on the market , But we choose 【 Simple 】,【 popular 】,【 free 】 The way , That is to say Prometheus + Grafana.
Architecture diagram
Let's take a look at the whole process .
In fact, the principle is very simple , Namely 【 hijacked 】API request , And record 【 Time 】,【 Error code 】 Etc . after , Give Way Prometheus The service takes the initiative from 【 Services created 】 in , Pull data . Last , Give Way Grafana Service from Prometheus Middle pull data , Show data sheet .
Quick start
1. establish prometheus.yml
Let's create prometheus.yml The configuration file , Give Way prometheus Services from localhost:8080/metrics Pull data .
In the following configuration , We didn't specify /metrics, because prometheus By default /metrics To pull data .
Be careful ! We put targets Set up a host.docker.internal:8080 instead of localhost:8080, This is because prometheus In the container , Our service is local .
Want to access the port of the local machine from the container , This is a convenient way . explain
global:
scrape_interval: 1s # Make scrape interval to 1s for testing.
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'rk-demo'
scrape_interval: 1s
static_configs:
- targets: ['host.docker.internal:8080']2. start-up Prometheus
We use docker To start up .
Prometheus By default 9090 port .
$ docker run \
-p 9090:9090 \
-v /<your path>/rk-demo/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus3. verification Prometheus
Please follow the above 【 verification 】, start-up main.go, And send a /rk/v1/healthy request .
then , Let's take a look prometheus Data in the service .
visit :localhost:9090, And search rk_demo_greeter_resCode, We can see that the data is already prometheus Inside the .
visit :localhost:9090/targets, We can see ,prometheus Data has been pulled every second .
4. start-up Grafana
Grafana By default 3000 port .
$ docker run -p 3000:3000 --name grafana grafana/grafana
visit :localhost:3000
Now ,grafana Will let you log in , The default user name and password are as follows .
user name :admin password :admin
5. stay Grafana Add in Prometheus data source
Grafana Just one. Web UI Tools , In order to see the data report , We tell Grafana Where to find Prometheus.
choice Prometheus As a data source .
Fill in Prometheus Address , The same reason as above , because Grafana Running on the Docker in , therefore , We don't use localhost:9090, It is ,host.docker.internal:9090.
6. Import Dashboard
We can edit it ourselves Grafana Dashboard, however , This is not an easy task . For use rk-boot Services started , We provide default 【 free 】 Of Grafana Dashboard Template .
Be careful , The imported Dashboard Only match 【 The service created according to the above logic 】.
Why? because rk-boot By default <App name >_<Entry name >_xxx As prometheus Of metrics name .
If the user uses a different module, Or different Entry name , It needs to be changed Dashboard Inside Variable. We'll see that in a later article , How to use Grafana.
Move to Dashboard Import page
Import 15111 Number Dashboard, The definition is :https://grafana.com/grafana/dashboards/15111
Appoint Prometheus data source , This data source is configured above Prometheus.
Start monitoring
Be careful ! If the number of requests is too small , It will be displayed as 0, Please send a few more requests .
Concept
We can start with Grafana Got surveillance data in the , Now look at rk-boot Middleware in , What type of monitoring data has been added .
The monitor interceptor will record the following by default .
Monitoring item | data type | details |
|---|---|---|
elapsedNano | Summary | RPC Time consuming |
resCode | Counter | be based on RPC Counter of return code |
errors | Counter | be based on RPC Wrong counter |
The above three monitoring , They all have the following labels .
label | details |
|---|---|
entryName | gRPC entry name |
entryType | gRPC entry type |
realm | environment variable : REALM, eg: rk |
region | environment variable : REGION, eg: beijing |
az | environment variable : AZ, eg: beijing-1 |
domain | environment variable : DOMAIN, eg: prod |
instance | Local Hostname |
appVersion | from AppInfoEntry obtain |
appName | from AppInfoEntry obtain |
restMethod | If started grpc-gateway, And the request is in http Sent by form , Will be recorded . eg: GET |
restPath | If started grpc-gateway, And the request is in http Sent by form , Will be recorded . eg: /rk/v1/healthy |
grpcService | GRPC The service name .eg: rk.api.v1.RkCommonService |
grpcMethod | GRPC Method name .eg: Healthy |
grpcType | GRPC type .eg: UnaryServer |
resCode | Return code , eg: OK |
边栏推荐
- [51nod] 3047 displacement operation
- What is the price of the elastic public network IP bandwidth
- Permission maintenance topic: domain controller permission maintenance
- [51nod] 3395 n-bit gray code
- How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
- How to design a hybrid system
- [summary of interview questions] zj6 redis
- Understanding Devops from the perspective of decision makers
- 2022-2028 global high tibial osteotomy plate industry research and trend analysis report
- Is your posture correct—— A detailed discussion on horizontal sub database and sub table
猜你喜欢

2022-2028 global marine wet exhaust hose industry research and trend analysis report

2022-2028 Global Industry Survey and trend analysis report on portable pressure monitors for wards

2022-2028 global high tibial osteotomy plate industry research and trend analysis report
![[51nod] 3395 n-bit gray code](/img/b5/2c072a11601de82cb92ade94672ecd.jpg)
[51nod] 3395 n-bit gray code

2022-2028 global third-party data platform industry research and trend analysis report

2022-2028 global cell-based seafood industry research and trend analysis report

2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis

IOS development - multithreading - thread safety (3)

2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report
随机推荐
Production line motor monitoring and maintenance - application case of 5g edge calculation for paperboard line motor maintenance
Velocitytracker use
2022-2028 global aircraft front wheel steering system industry research and trend analysis report
AI intelligent analysis is innovated again. In the future, the technical reserve of the security industry will become the top priority
How to strengthen prison security measures? Technologies you can't imagine
Micro build low code enterprise exchange day · Shenzhen station opens registration
Grpc: based on cloud native environment, distinguish configuration files
What port does the fortress machine use? What is the role of the fortress machine?
What is the fortress machine? What role does the fortress machine play?
What protocol does FTP belong to in Fortress machine and how to use FTP in Fortress machine
Instructions for performance pressure test tool
Gigabyte was attacked by blackmail software, and the FBI banned the hacker organization Revil | global network security hotspot
LeetCode 724. Find the central subscript of the array
Simple and beautiful weather code
RI Geng series: write a simple shell script, but it seems to have technical content
Is the cloud game edge computing server highly required? What problems will occur during the use of cloud game edge computing server?
PHP verify mailbox format
IOS development - multithreading - thread safety (3)
The 2021 Tencent digital ecology conference is coming
[summary of interview questions] zj5