Skip to content

rookie-ninja/rk-prom

Repository files navigation

rk-prom

build codecov Go Report Card License

A simple prometheus initializer. What rk-prom trying to do is described as bellow:

  • Start prometheus client by calling StartProm()
  • Start prometheus client by providing yaml config
  • Start a daemon thread which will periodically push local prometheus metrics to PushGateway
  • Simple wrapper of Counter, Gauge, Summary, Histogram like POJO with GetXXX(), RegisterXXX(), UnRegisterXXX()
  • Go & Process collector variables which is originally implemented by prometheus client package.

Table of Contents generated with DocToc

Installation

go get -u github.com/rookie-ninja/rk-prom

Development Status: Active

In Prod version.

Quick start

Start with Bootstrap() with code

package main

import (
	"github.com/rookie-ninja/rk-prom"
	"github.com/rookie-ninja/rk-query"
	"time"
)

func main() {
	// create prom entry
	entry := rkprom.RegisterPromEntry()

	// start server
	entry.Bootstrap(context.TODO())

	// stop server
	entry.Interrupt(context.TODO())
}

Start with Bootstrap() with config file

---
prom:
  enabled: true
#  port: 1608
#  path: metrics
#  pusher:
#    enabled: false
#    intervalMS: 1
#    jobName: "rk-job"
#    remoteAddress: "localhost:9091"
#    basicAuth: "user:pass"
package main

import (
	"github.com/rookie-ninja/rk-prom"
	"github.com/rookie-ninja/rk-query"
	"time"
)

func main() {
	rkentry.RegisterInternalEntriesFromConfig("example/boot.yaml")

	maps := rkprom.RegisterPromEntriesWithConfig("example/boot.yaml")

	entry := maps[rkprom.PromEntryNameDefault]
	entry.Bootstrap(context.TODO())

	rkentry.GlobalAppCtx.WaitForShutdownSig()
    
	// stop server
	entry.Interrupt(context.TODO())
}
Name Description Option Default Value
prom.enabled Enable prometheus bool false
prom.port Prometheus port integer 1608
prom.path Prometheus path string metrics
prom.pusher.enabled Enable push gateway pusher bool false
prom.pusher.intervalMS Push interval to remote push gateway integer 0
prom.pusher.jobName Pusher job name string empty string
prom.pusher.remoteAddress Pusher url string empty string
prom.pusher.basicAuth basic auth as user:password string empty string

Example

  • Working with Counter (namespace and subsystem)
metricsSet := rkprom.NewMetricsSet("my_namespace", "my_service")

metricsSet.RegisterCounter("counter", "key_1")

metricsSet.GetCounterWithValues("counter", "value_1").Inc()
metricsSet.GetCounterWithLabels("counter", prometheus.Labels{"key_1":"value_1"}).Inc()
  • Working with Gauge (namespace and subsystem)
metricsSet := rkprom.NewMetricsSet("my_namespace", "my_service")
metricsSet.RegisterGauge("gauge", "key_1")

metricsSet.GetGaugeWithValues("gauge", "value_1").Inc()
metricsSet.GetGaugeWithLabels("gauge", prometheus.Labels{"key_1":"value_1"}).Inc()
  • Working with Summary (custom namespace and subsystem)
metricsSet := rkprom.NewMetricsSet("my_namespace", "my_service")
metricsSet.RegisterSummary("summary", rk_prom.SummaryObjectives, "key_1")

metricsSet.GetSummaryWithValues("summary", "value_1").Observe(1.0)
metricsSet.GetSummaryWithLabels("summary", prometheus.Labels{"key_1":"value_1"}).Observe(1.0)
  • Working with Histogram (custom namespace and subsystem)
metricsSet := rkprom.NewMetricsSet("new_namespace", "new_service")
metricsSet.RegisterHistogram("histogram", []float64{}, "key_1")

metricsSet.GetHistogramWithValues("histogram", "value_1").Observe(1.0)
metricsSet.GetHistogramWithLabels("histogram", prometheus.Labels{"key_1":"value_1"}).Observe(1.0)
  • Working with PushGateway publisher
pusher, _ := NewPushGatewayPusher(
	WithIntervalMSPusher(2 * time.Second),
	WithRemoteAddressPusher("localhost:8888"),
	WithJobNamePusher("test_job"))

pusher.Start()
defer pusher.Shutdown()

time.Sleep(2 * time.Second)

Contributing

We encourage and support an active, healthy community of contributors — including you! Details are in the contribution guide and the code of conduct. The pulse-line maintainers keep an eye on issues and pull requests. So don't hesitate to hold us to a high standard.