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-grpc/v2

Prometheus options#

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

Quick start#

1.Create boot.yaml#

---
grpc:
  - name: greeter
#   gwPort: 8081                                           # Optional, default: gateway port will be the same as grpc port if not provided
    port: 8080
    enabled: true
    enableRkGwOption: 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-grpc/v2/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#

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"
  "github.com/rookie-ninja/rk-boot/v2"
  "github.com/rookie-ninja/rk-entry/v2/middleware/prom"
  "github.com/rookie-ninja/rk-grpc/v2/boot"
  "google.golang.org/grpc"
)

func main() {
  boot := rkboot.NewBoot()

  // register grpc
  entry := rkgrpc.GetGrpcEntry("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)

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

  // Wait for shutdown sig
  boot.WaitForShutdownSig(context.TODO())
}

5.Validate#

Validate

http://localhost:8080/metrics

prom

Cheers#

6.Push to Pushgateway#

---
grpc:
  - name: greeter
    port: 8080
    enabled: true
    enableRkGwOption: 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#