Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

introduce

Add Prometheus monitoring to the Gin framework microservice with a complete example.

The Gin framework monitors middleware and will be covered in a future article.

We will use RK-boot to start the Gin framework microservice.

Please visit the following address for the full tutorial:

  • rkdocs.netlify.app/cn

The installation

go get github.com/rookie-ninja/rk-boot
Copy the code

Quick start

1. Create the boot. Yaml

The boot.yaml file describes the raw information for Gin framework startup. Rk-boot starts Gin by reading boot.yaml.

---
gin:
  - name: greeter
    port: 8080
    enabled: true
    prom:
      enabled: true      # Enable prometheus client
# path: "metrics" # Default value is "metrics", set path as needed.
Copy the code

2. Create a main. Go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
)

// Application entrance.
func main(a) {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}
Copy the code

3. Start the main. Go

$ go run main.go
Copy the code

4. Verify

Visit: http://localhost:8080/metrics

Add monitoring to the Prometheus client

We need to understand the following concepts from Prometheus.

The name details
MetricsSet RK custom structure that registers the Counter, Gauge, Histogram, and Summary of Prometheus through MetricsSet
Prometheus Registerer Prometheus manages Counter, Gauge, Histogram, and Summary through Registrerer
Prometheus Counter Counter is a cumulative measure that represents a single monotonically increasing Counter whose value can only be incremented or reset to zero
Prometheus Gauge The Gauge value can be added or subtracted at will
Prometheus Histogram Histogram takes samples (typically things like request duration or response size) and calculates them in configurable buckets, as well as providing a total of all observations
Prometheus Summary Similar to Histogram, summary sample observations (usually things like request duration and response size)
Prometheus Namespace Prometheus Monitoring name format: namespace_subSystem_metricsName
Prometheus SubSystem Prometheus Monitoring name format: namespace_subSystem_metricsName

1. Add monitoring items in main.go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-prom"
)

// Application entrance.
func main(a) {
	// 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.GetGinEntry("greeter").PromEntry.Registerer)

	// Register counter, gauge, histogram, summary
	set.RegisterCounter("my_counter"."label")
	set.RegisterGauge("my_gauge"."label")
	set.RegisterHistogram("my_histogram"And []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())
}
Copy the code

2. Start the main. Go

$ go run main.go
Copy the code

3. Verify

Visit: http://localhost:8080/metrics

Pushed to the pushgateway

Next, let’s look at how to automatically push monitoring data to the remote PushGateway.

1. Boot. Yaml Starts 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.
Copy the code

2. Start pushGateway on the local PC

We use Docker to launch pushGateway

$ docker run prom/pushgateway -p 9091:9091
Copy the code

3. Start the main. Go

$ go run main.go
Copy the code

4. Verify

Visit: http://localhost:9091/metrics

Complete options

The name describe type The default value
gin.prom.enabled Start the Prometheus boolean false
gin.prom.path Prometheus Web path string /metrics
gin.prom.pusher.enabled Start the Prometheus pusher bool false
gin.prom.pusher.jobName JobName will be added as a label to the monitoring metric and pushed to the remote PushGateway string “”
gin.prom.pusher.remoteAddress Pushgateway Remote address,http://x.x.x.xOr X.X.X.X string “”
gin.prom.pusher.intervalMs Push interval (ms) string 1000
gin.prom.pusher.basicAuth Basic auth for remote Pushgateway. Format: [user: pass] string “”
gin.prom.pusher.cert.ref For references to rkentry.CertEntry, please refer to the advanced guide on the official website string “”