Efox
Efox

Reputation: 691

How to push metrics to prometheus using client_golang?

I have not yet able to find some good examples in using Gauge, Counter and Histogram in prometheus. Any help on this will do. I tried using the the documentation but I was not able to successfully create a working app.

Upvotes: 9

Views: 20666

Answers (3)

Zstack
Zstack

Reputation: 4743

You could find examples form the prometheus/client_golang. To get you started, you can just get the packages:

$ go get github.com/prometheus/client_golang/prometheus
$ go get github.com/prometheus/client_golang/prometheus/push

The you can just run the following example by setting your correct pushgateway address, which is http://localhost:9091/ in this example:

package main
import (
        "fmt"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/push"
)

func ExamplePusher_Push() {
        completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
                Name: "db_backup_last_completion_timestamp_seconds",
                Help: "The timestamp of the last successful completion of a DB backup.",
        })
        completionTime.SetToCurrentTime()
        if err := push.New("http://localhost:9091/", "db_backup").
                Collector(completionTime).
                Grouping("db", "customers").
                Push(); err != nil {
                fmt.Println("Could not push completion time to Pushgateway:", err)
        }
}
func main() {
        ExamplePusher_Push()
}

Run your script:

$ go run pushExample.go

After running your code, you should see the metrics at your gateway (http://localhost:9091/). The interface looks like the following: enter image description here

Upvotes: 21

Efox
Efox

Reputation: 691

I have found this

`

package main

import (
    "net/http"

    "github.com/prometheus/client_golang/prometheus"
)

var (
cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "cpu_temperature_celsius",
    Help: "Current temperature of the CPU.",
 })
hdFailures = prometheus.NewCounter(prometheus.CounterOpts{
    Name: "hd_errors_total",
    Help: "Number of hard-disk errors.",
})
)

func init() {
    prometheus.MustRegister(cpuTemp)
    prometheus.MustRegister(hdFailures)
}

func main() {
    cpuTemp.Set(65.3)
    hdFailures.Inc()

    http.Handle("/metrics", prometheus.Handler())
    http.ListenAndServe(":8080", nil)
}

`

This might be useful to some.

Upvotes: 5

Vatine
Vatine

Reputation: 21258

Prometheus is a pull-based system, if you want push-based monitoring, you need to use a gateway of some sort. A minimal example (without actually doing anything useful like starting an HTTP listener, or actually doing anything to a metric) follows:

import (
        "github.com/prometheus/client_golang/prometheus"
        "net/http"
)

var responseMetric = prometheus.NewHistogram(
        prometheus.HistogramOpts{
                Name: "request_duration_milliseconds",
                Help: "Request latency distribution",
                Buckets: prometheus.ExponentialBuckets(10.0, 1.13, 40),
        })

func main() {
        prometheus.MustRegister(responseMetric)
        http.Handle("/metrics", prometheus.Handler())
        // Any other setup, then an http.ListenAndServe here
}

You then need to configure Prometheus to scrape the /metrics page your binary provides.

Upvotes: -2

Related Questions