Skip to content

Commit

Permalink
feat(cmd/example_server): Add simple prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed May 14, 2023
1 parent c83ccf1 commit 1afb4cb
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd/example_server/example_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sikalabs/slu/cmd/root"
"github.com/sikalabs/slu/version"
"github.com/spf13/cobra"
Expand All @@ -19,39 +21,61 @@ var Cmd = &cobra.Command{
Short: "Run example web server",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
var (
requestsTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "example_requests_total",
Help: "Total number of requests received.",
},
)
)

prometheus.MustRegister(requestsTotal)

portStr := strconv.Itoa(FlagPort)
hostname, _ := os.Hostname()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow1s", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(1 * time.Second)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 1s)! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow10s", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(10 * time.Second)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 10s)! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow30s", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(30 * time.Second)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 30s)! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow60s", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(60 * time.Second)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 60s)! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow5m", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(5 * time.Minute)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 5m)! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow10m", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(10 * time.Minute)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 10m)! %s %s \n", hostname, portStr)
})
http.HandleFunc("/slow15m", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
time.Sleep(15 * time.Minute)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 15m)! %s %s \n", hostname, portStr)
})

http.Handle("/metrics", promhttp.Handler())

fmt.Println("[slu " + version.Version + "] Server started on 0.0.0.0:" + portStr + ", see http://127.0.0.1:" + portStr)
http.ListenAndServe(":"+portStr, nil)
},
Expand Down

0 comments on commit 1afb4cb

Please sign in to comment.