Skip to content

Instantly share code, notes, and snippets.

@hagen1778
Created November 29, 2020 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagen1778/81e09261fbe0dc3d99fffd6354ed7787 to your computer and use it in GitHub Desktop.
Save hagen1778/81e09261fbe0dc3d99fffd6354ed7787 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/VictoriaMetrics/metrics"
)
func main() {
http.Handle("/", handle(home))
http.HandleFunc("/metrics", handle(metricsPage))
http.HandleFunc("/articles", handle(articles))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handle(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
metrics.GetOrCreateCounter(fmt.Sprintf(`requests_total{path=%q}`, r.URL.Path)).Inc()
h.ServeHTTP(w, r)
}
}
func metricsPage(w http.ResponseWriter, r *http.Request) {
metrics.WritePrometheus(w, true)
}
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome!")
}
func articles(w http.ResponseWriter, r *http.Request) {
metrics.GetOrCreateCounter(`requests_total{path="/articles"}`).Inc()
resp, err := http.DefaultClient.Get("https://www.google.com/search?q=observability+articles")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, _ := ioutil.ReadAll(resp.Body)
fmt.Fprint(w, string(data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment