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-zero

Prometheus options#

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

Quick start#

1.Create boot.yaml#

---
zero:
  - 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-zero/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"
  "encoding/json"
  "fmt"
  "github.com/rookie-ninja/rk-boot/v2"
  "github.com/rookie-ninja/rk-entry/v2/middleware/prom"
  "github.com/rookie-ninja/rk-zero/boot"
  "net/http"
)

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

  // Register handler
  zeroEntry := rkzero.GetZeroEntry("greeter")

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

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

  boot.WaitForShutdownSig(context.TODO())
}

5.Validate#

Validate

http://localhost:8080/metrics

prom

Cheers#

6.Push to Pushgateway#

---
zero:
  - 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#