utf18 / go-mux Goto Github PK
View Code? Open in Web Editor NEWgolang rest api with a gorilla/mux router and prometheus metrics
License: MIT License
golang rest api with a gorilla/mux router and prometheus metrics
License: MIT License
hi,
i currently play with your repo. i would like a counter in your example and expose it to prometheus/grafana. also how can you define an alarm (hipchat msg or email) in prometheus when the counter reaches 100 for example?
i changed your code as follows.
i send a request to http://localhost:8080/hello and watch http://localhost:8080/metrics i see the counter incrementing... but cannot see the counter in the prometheus ui on localhost:9090. any idea?
package main
import (
"fmt"
"math/rand"
"net/http"
"os"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
var (
// Counter vector to which we can attach labels. That creates many key-value
// label combinations. So in our case we count requests by status code separetly.
counter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "hello_requests_total",
Help: "Total number of /hello requests.",
},
[]string{"status"},
)
)
func init() {
prometheus.MustRegister(counter)
}
func main() {
var log = logrus.New()
// set the log output
log.Out = os.Stdout
// set the log level
log.Level = logrus.DebugLevel
// Routes consist of a path and a handler function.
r := mux.NewRouter()
// sample log.Info
log.Info("http server is ready")
// sample log.Debug
log.Debug("i am only visible in debug mode\n")
// exposes /hello endpoint with the helloHandler handler
r.HandleFunc("/hello", helloHandler)
// exposes /metrics endpoint with standard golang metrics used by prometheus
r.Handle("/metrics", promhttp.Handler())
// wrap a logger arount the mux router
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
// wrap a prometheus instrument handler around the logger
prometheusRouter := prometheus.InstrumentHandlerWithOpts(prometheus.SummaryOpts{}, loggedRouter)
// Bind to a port and pass our loggedRouter in
log.Fatal(http.ListenAndServe(":8080", prometheusRouter))
}
func doSomeWork() int {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
statusCodes := [...]int{
http.StatusOK,
http.StatusBadRequest,
http.StatusUnauthorized,
http.StatusInternalServerError,
}
return statusCodes[rand.Intn(len(statusCodes))]
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
var status int
defer func(begun time.Time) {
// hello_requests_total{status="200"} 2385
counter.With(prometheus.Labels{
"status": fmt.Sprint(status),
}).Inc()
}(time.Now())
status = doSomeWork()
w.WriteHeader(status)
w.Write([]byte("Hello, World!\n"))
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.