当前位置:网站首页>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

""

原网站

版权声明
本文为[Trespass ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211028080449527J.html