Last active
June 21, 2020 23:23
-
-
Save takuhiro/03cd4c19fba5717e6387e73d0feafdd3 to your computer and use it in GitHub Desktop.
Prometheus Instrumentation Sample
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 ( | |
"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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet!