当前位置:网站首页>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 | "" |
边栏推荐
- Network engineers must know the 10 technical points of IPv6. It is recommended to collect them!
- Which cloud game service provider is more reliable when the cloud game server is open source
- Iranian gas station paralyzed by cyber attack, babuk blackmail software source code leaked | global network security hotspot
- Vivo global mall: design and practice of commodity system architecture
- Engineer culture: why doesn't bat call the boss
- Cloudpods golang practice
- Coding -- the leader of R & D tools in the cloud native Era
- Block
- What is a region name? Can a territory name be used for trademark registration?
- How to build a cloud game server what needs to be considered to build a cloud game server
猜你喜欢
随机推荐
Using robot framework to realize multi platform automated testing
Must the company domain name have a trademark registration? What if the registered domain name is rejected?
Evaluation index of machine learning model
Leetcode969: pancake sorting (medium, dynamic programming)
What is the domain name trademark? What are the registration conditions for domain names and trademarks?
How to design and make ppt gradient effect?
Case of data recovery by misoperation under NTFS file system
Tke single node risk avoidance
[Tencent cloud double 12 audio and video communication special session] from 9 yuan for Q4 counter attack artifact, SMS and security (New) package!
Wkwebview audio and video media playback processing
What is a region name? Can a territory name be used for trademark registration?
Uiscrollview add gestures show and hide keyboard
Afnetworking usage and cache processing
Face recognition using cidetector
How about Shenzhen website construction? Is it expensive?
Uipickerview show and hide animation
Buddha's foot before examination: the first bullet of leetcode
Start tcapulusdb process
What is data matrix code
How to build a website? What needs attention?


