当前位置:网站首页>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 | "" |
边栏推荐
- Build a reliable, scalable and maintainable application system
- What is the backbone of marketing website construction? What does it do?
- An attempt to use Navicat tool to copy and export MySQL database data
- What about registered domain names? How long does it take to register a domain name?
- How to use the cloud game server is the cloud server stable
- The difference between classless routing and classless routing
- What is a port? The most complete and strongest port number in history, collection!
- How to build a website? What needs attention?
- How does [lightweight application server] build a cross-border e-commerce management environment?
- Gin framework: add API logging Middleware
猜你喜欢
随机推荐
A detailed explanation of the laser slam framework logo-loam
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?
Iranian gas station paralyzed by cyber attack, babuk blackmail software source code leaked | global network security hotspot
Tencent peace of mind ecological alliance was officially established as a linkage partner. Open technology helps "digital agriculture"
An attempt to use Navicat tool to copy and export MySQL database data
How to quickly handle third-party login and easy to expand?
[Tencent cloud double 12.12] from 56 yuan! New users of Tencent cloud buy for the first time, which is more cost-effective!
Tornado code for file download
Cloudpods golang practice
How to batch output ean 13 code to pictures
DB2 database generates HTML patrol Report
Offline store + online mall, why do you want to be an online mall
What is the domain name trademark? What are the registration conditions for domain names and trademarks?
The easydss on demand file upload interface calls postman to report an error. Failed to upload the file?
How to use the cloud game server is the cloud server stable
How to build an enterprise website? Is it difficult?
Afnetworking server client
What is the large bandwidth of IDC machine room?
Code 128 barcode details
Designing complex messaging systems using bridging patterns


