Forked from sysdig-blog/prometheus-metrics-golang.go
Created
September 17, 2019 05:15
-
-
Save elmer/8fa1a96939d70b91828dd61548546dda to your computer and use it in GitHub Desktop.
Prometheus metrics code instrumentation in Golang
This file contains 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