Skip to content

Instantly share code, notes, and snippets.

@tlwr
Last active March 2, 2020 14:37
Show Gist options
  • Save tlwr/5e186294b3e11c103d81362e511c3cdc to your computer and use it in GitHub Desktop.
Save tlwr/5e186294b3e11c103d81362e511c3cdc to your computer and use it in GitHub Desktop.
Stupidly simple Go application instrumented with prometheus
package main
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
http.HandleFunc("/horse", handleHorse)
http.HandleFunc("/unicorn", handleUnicorn)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleHorse(w http.ResponseWriter, _ *http.Request) {
log.Println("horse request")
defer log.Println("horse response")
HorseCountMetric.Inc()
w.WriteHeader(200)
w.Write([]byte("HORSE"))
}
func handleUnicorn(w http.ResponseWriter, _ *http.Request) {
log.Println("unicorn request")
defer log.Println("unicorn response")
UnicornCountMetric.Inc()
w.WriteHeader(200)
w.Write([]byte("UNICORN"))
}
var (
HorseCountMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "horse_count",
})
UnicornCountMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "unicorn_count",
})
)
---
global:
scrape_interval: 5s
evaluation_interval: 10s
scrape_configs:
- job_name: example-app
static_configs:
- targets: ['host.docker.internal:8080']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment