当前位置:网站首页>Gin framework: add Prometheus monitoring
Gin framework: add Prometheus monitoring
2022-06-24 02:36:00 【Trespass 】
Introduce
Through a complete example , Go to Gin frame Add in microservice Prometheus monitor .
Gin frame Monitoring Middleware , It will be introduced in the following articles .
We will use rk-boot To start up Gin Framework microservices .
Please visit the following address for a complete tutorial :
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-gin
Quick start
1. establish boot.yaml
boot.yaml The document describes Gin Framework started Original information ,rk-boot By reading the boot.yaml To start up Gin.
---
gin:
- name: greeter
port: 8080
enabled: true
prom:
enabled: true # Enable prometheus client
# path: "metrics" # Default value is "metrics", set path as needed.2. establish main.go
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-gin/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. start-up main.go
$ go run main.go
4. verification
visit : http://localhost:8080/metrics
Prometheus Add monitoring to the client
We need to know Prometheus The following concepts in .
name | details |
|---|---|
RK Custom structure , adopt MetricsSet register Prometheus Of Counter,Gauge,Histogram and Summary | |
Prometheus Will pass Registrerer To manage Counter,Gauge,Histogram and Summary | |
Counter It's a cumulative measure , Represents a single monotonically increasing counter , Its value can only be increased or reset to zero | |
Gauge Values can be added or subtracted at will | |
Histogram sampling ( It's usually something like request duration or response size ) And calculate them in configurable buckets , It also provides the sum of all observations | |
And Histogram similar , Abstract sample observation ( It's usually something like request duration and response size ) | |
Prometheus Namespace | Prometheus Monitor name format : namespace_subSystem_metricsName |
Prometheus SubSystem | Prometheus Monitor name format : namespace_subSystem_metricsName |
1. stay main.go Add monitoring items to
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-prom"
"github.com/rookie-ninja/rk-gin/boot"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Create a metrics set into prometheus.Registerer
set := rkprom.NewMetricsSet("rk", "demo", boot.GetEntry("greeter").(*rkgin.GinEntry).PromEntry.Registerer)
// Register counter, gauge, histogram, summary
set.RegisterCounter("my_counter", "label")
set.RegisterGauge("my_gauge", "label")
set.RegisterHistogram("my_histogram", []float64{}, "label")
set.RegisterSummary("my_summary", rkprom.SummaryObjectives, "label")
// Increase counter, gauge, histogram, summary with label value
set.GetCounterWithValues("my_counter", "value").Inc()
set.GetGaugeWithValues("my_gauge", "value").Add(1.0)
set.GetHistogramWithValues("my_histogram", "value").Observe(0.1)
set.GetSummaryWithValues("my_summary", "value").Observe(0.1)
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}2. start-up main.go
$ go run main.go
3. verification
visit : http://localhost:8080/metrics
Pushed to the pushgateway
Next , Let's see , How to automatically push the monitoring data to the remote pushgateway in .
1.boot.yaml Start in pusher
---
gin:
- name: greeter
port: 8080
enabled: true
prom:
enabled: true # Enable prometheus client
pusher:
enabled : true # Enable backend job push metrics to remote pushgateway
jobName: "demo" # Name of current push job
remoteAddress: "localhost:9091" # Remote address of pushgateway
intervalMs: 2000 # Push interval in milliseconds
# basicAuth: "user:pass" # Basic auth of pushgateway
# cert:
# ref: "ref" # Cert reference defined in CertEntry. Please see advanced user guide for details.2. Start locally pushgateway
We use docker start-up pushgateway
$ docker run prom/pushgateway -p 9091:9091
3. start-up main.go
$ go run main.go
4. verification
visit : http://localhost:9091/metrics
Complete options
name | describe | type | The default value is |
|---|---|---|---|
gin.prom.enabled | start-up prometheus | boolean | false |
gin.prom.path | Prometheus Web route | string | /metrics |
gin.prom.pusher.enabled | start-up prometheus pusher | bool | false |
gin.prom.pusher.jobName | JobName It will be added to the monitoring indicators in the form of labels , And push it to the remote pushgateway | string | "" |
gin.prom.pusher.remoteAddress | Pushgateway Remote address , http://x.x.x.x perhaps x.x.x.x | string | "" |
gin.prom.pusher.intervalMs | Push interval ( millisecond ) | string | 1000 |
gin.prom.pusher.basicAuth | long-range Pushgateway Of Basic auth. Format :user:pass | string | "" |
gin.prom.pusher.cert.ref | rkentry.CertEntry References to , Please refer to the advanced guide on the official website | string | "" |
边栏推荐
- How many graphics cards are required for cloud game servers? What should be paid attention to when purchasing servers
- How to batch output ean 13 code to pictures
- Leetcode969: pancake sorting (medium, dynamic programming)
- Start tcapulusdb process
- How to formulate a domain name trademark registration scheme? What if the plan is rejected?
- Must the company domain name have a trademark registration? What if the registered domain name is rejected?
- Simple use of notification
- November 1 global network security hotspot
- DB2 database generates HTML patrol Report
- What is the large bandwidth of IDC machine room?
猜你喜欢
随机推荐
Mutual conversion between qstring and qdatetime
Go language starts again, go modules' past life, present life and basic use
Evaluation index of machine learning model
How to recover the garbled words in the software?
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
Is it illegal to use someone else's trademark to register a domain name? What should we pay attention to when registering a domain name?
How to build your own cloud game server and what are the building steps
What is data matrix code
Flink practice tutorial: getting started 1- zero basic users realize simple Flink tasks
Wkwebview audio and video media playback processing
Gin framework: RPC error code design
What is the industrial Internet? What is the industrial Internet platform?
Afnetworking usage and cache processing
[Tencent cloud load balancing CLB] cross region binding 2.0 (new version) idc-ip best practices!
How to formulate a domain name trademark registration scheme? What if the plan is rejected?
[supply chain • case] Tianneng group: the key to understanding the leading battery manufacturer to achieve the first profit fault
How to understand EDI requirements of trading partners
Leetcode838: push domino (medium)
How does easynvr call the interface to modify a user-defined page?
Start tcapulusdb process


