Skip to content

Instantly share code, notes, and snippets.

@takuhiro
Last active June 21, 2020 23:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuhiro/03cd4c19fba5717e6387e73d0feafdd3 to your computer and use it in GitHub Desktop.
Save takuhiro/03cd4c19fba5717e6387e73d0feafdd3 to your computer and use it in GitHub Desktop.
Prometheus Instrumentation Sample
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
errorCount = prometheus.NewCounter(prometheus.CounterOpts{
Name: "greeting_error_count_total",
Help: "Counter of HTTP requests resulting in an error.",
})
)
func init() {
prometheus.MustRegister(errorCount)
}
func main() {
helloHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
switch rand.Intn(4) {
case 0:
log.Println("Hello!")
fmt.Fprint(w, "Hello!")
case 1:
log.Println("Hi!")
fmt.Fprint(w, "Hi!")
case 2:
log.Println("Hey!")
fmt.Fprint(w, "Hey!")
case 3:
log.Println("Error!")
errorCount.Inc()
fmt.Fprint(w, "Error!!")
}
})
http.Handle("/", helloHandler)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
@godwhoa
Copy link

godwhoa commented Jun 21, 2020

Sweet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment