GithubHelp home page GithubHelp logo

utf18 / go-mux Goto Github PK

View Code? Open in Web Editor NEW
10.0 10.0 5.0 18 KB

golang rest api with a gorilla/mux router and prometheus metrics

License: MIT License

Go 89.97% Shell 10.03%
docker go golang grafana prometheus rest

go-mux's People

Contributors

ben-st avatar utf18 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

go-mux's Issues

add to prometheus

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"))
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.