Skip to content

Prometheus client

Enable Prometheus client

Install#

go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-fiber

Prometheus options#

options description type default
fiber.prom.enabled Enable prometheus client boolean false
fiber.prom.path Prometheus web path string /metrics
fiber.prom.pusher.enabled Enable prometheus pusher bool false
fiber.prom.pusher.jobName Job name metrics string ""
fiber.prom.pusher.remoteAddress Pushgateway address, http://x.x.x.x or x.x.x.x string ""
fiber.prom.pusher.intervalMs Interval in milliseconds string 1000
fiber.prom.pusher.basicAuth Basic auth of Pushgateway. Scheme:[user:pass] string ""
fiber.prom.pusher.certEntry Name of rkentry.CertEntry string ""

Quick start#

1.Create boot.yaml#

---
fiber:
  - name: greeter
    port: 8080
    enabled: true
    prom:
      enabled: true                                        # Optional, default: false
#      path: ""                                            # Optional, default: "/metrics"
#      pusher:
#        enabled: false                                    # Optional, default: false
#        jobName: "greeter-pusher"                         # Required
#        remoteAddress: "localhost:9091"                   # Required
#        basicAuth: "user:pass"                            # Optional, default: ""
#        intervalMs: 10000                                 # Optional, default: 1000
#        certEntry: my-cert                                # Optional, default: "", reference of cert entry declared above

2.Create main.go#

package main

import (
    "context"
    "github.com/rookie-ninja/rk-boot/v2"
    _ "github.com/rookie-ninja/rk-fiber/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.Validate#

Validate

http://localhost:8080/metrics

prom

Cheers#

4.Add metrics into Prometheus#

prom

Name Description
MetricsSet Register Counter,Gauge,Histogram and Summary through MetricsSet
Prometheus Registerer Prometheus will manage Counter,Gauge,Histogram and Summary through Registrerer 来管理
Prometheus Counter Counter, increase only
Prometheus Gauge Gauge
Prometheus Histogram Histogram
Prometheus Summary Summary
Prometheus Namespace Format: namespace_subSystem_metricsName
Prometheus SubSystem Format: namespace_subSystem_metricsName
package main

import (
  "context"
  "fmt"
  "github.com/gofiber/fiber/v2"
  "github.com/rookie-ninja/rk-boot/v2"
  "github.com/rookie-ninja/rk-entry/v2/middleware/prom"
  "github.com/rookie-ninja/rk-fiber/boot"
  "net/http"
)

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

  // Bootstrap
  boot.Bootstrap(context.TODO())

  // Register handler
  entry := rkfiber.GetFiberEntry("greeter")

  set := rkmidprom.NewMetricsSet("rk", "demo", entry.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", rkmidprom.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)

  boot.WaitForShutdownSig(context.TODO())
}

5.Validate#

Validate

http://localhost:8080/metrics

prom

Cheers#

6.Push to Pushgateway#

---
fiber:
  - name: greeter
    port: 8080
    enabled: true
    prom:
      enabled: true                         # Optional, default: false
      pusher:
        enabled : true                      # Optional, default: false
        jobName: "demo"                     # Required
        remoteAddress: "localhost:9091"     # Required
        intervalMs: 2000                    # Optional, default: 1000
#        certEntry: my-cert                 # Optional, default: "", reference of cert entry declared above

Start pushgateway locally

$ docker run -p 9091:9091 prom/pushgateway

http://localhost:9091

pushgateway

Cheers#