Created
March 19, 2019 13:01
-
-
Save sysdig-blog/3640f39a7bb1172f986d0e2080c64a75 to your computer and use it in GitHub Desktop.
Prometheus metrics code instrumentation in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"github.com/prometheus/client_golang/prometheus" | |
"github.com/prometheus/client_golang/prometheus/promhttp" | |
"log" | |
"time" | |
"math/rand" | |
) | |
var ( | |
counter = prometheus.NewCounter( | |
prometheus.CounterOpts{ | |
Namespace: "golang", | |
Name: "my_counter", | |
Help: "This is my counter", | |
}) | |
gauge = prometheus.NewGauge( | |
prometheus.GaugeOpts{ | |
Namespace: "golang", | |
Name: "my_gauge", | |
Help: "This is my gauge", | |
}) | |
histogram = prometheus.NewHistogram( | |
prometheus.HistogramOpts{ | |
Namespace: "golang", | |
Name: "my_histogram", | |
Help: "This is my histogram", | |
}) | |
summary = prometheus.NewSummary( | |
prometheus.SummaryOpts{ | |
Namespace: "golang", | |
Name: "my_summary", | |
Help: "This is my summary", | |
}) | |
) | |
func main() { | |
rand.Seed(time.Now().Unix()) | |
http.Handle("/metrics", promhttp.Handler()) | |
prometheus.MustRegister(counter) | |
prometheus.MustRegister(gauge) | |
prometheus.MustRegister(histogram) | |
prometheus.MustRegister(summary) | |
go func() { | |
for { | |
counter.Add(rand.Float64() * 5) | |
gauge.Add(rand.Float64()*15 - 5) | |
histogram.Observe(rand.Float64() * 10) | |
summary.Observe(rand.Float64() * 10) | |
time.Sleep(time.Second) | |
} | |
}() | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment