Skip to content

Commit

Permalink
feat(cmd/example_server): Add path to example_requests_total metric
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Feb 16, 2024
1 parent a0e7357 commit 814bead
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions cmd/example_server/example_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ var Cmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
var (
requestsTotal = prometheus.NewCounter(
requestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "example_requests_total",
Help: "Total number of requests received.",
},
Namespace: "example",
Name: "requests_total",
Help: "Total number of requests per endpoint",
}, []string{"path"},
)
)

Expand All @@ -36,48 +37,48 @@ var Cmd = &cobra.Command{
portStr := strconv.Itoa(FlagPort)
hostname, _ := os.Hostname()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).Inc()
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server! %s %s \n", hostname, portStr)
fmt.Printf("RemoteAddr=%s\n", r.RemoteAddr)
})
http.HandleFunc("/slow1s", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).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()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).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()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).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()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).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()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).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()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).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()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).Inc()
time.Sleep(15 * time.Minute)
fmt.Fprintf(w, "[slu "+version.Version+"] Example HTTP Server (after 15m)! %s %s \n", hostname, portStr)
})

http.HandleFunc("/dela", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).Inc()
fmt.Fprint(w, `<!doctype html><html lang="en"><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dela</title><style>
Expand All @@ -91,7 +92,7 @@ body, html { margin: 0; padding: 0; height: 100%; width: 100%; }
})

http.HandleFunc("/dela.jpg", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
requestsTotal.With(prometheus.Labels{"path": r.URL.Path}).Inc()
w.Header().Set("Content-Type", "image/jpeg")
w.WriteHeader(http.StatusOK)
w.Write(dela.DELA1_JPG)
Expand Down

0 comments on commit 814bead

Please sign in to comment.