go-prom

module
v0.0.0-...-a9b8012 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2019 License: MIT

README

go-prom

Prometheus helper for go web services using httprouter

GoDoc Go Report Card

To get the latest package:

go get -u github.com/FenixAra/go-prom/prom

Usage with httprouter

package main

import (
	"net/http"

	"github.com/FenixAra/go-prom/prom"
	"github.com/julienschmidt/httprouter"
	"github.com/FenixAra/go-util/log"
)

func main() {
	router := httprouter.New()

	// Tracking httprouter handles
	router.GET("/ping", prom.Track(Ping, "Ping"))

	// Tracking external dependencies
	t := time.Now()
	doExternalHttpCall()
	prom.TrackDependency(prom.DependencyHTTP, "Google", status, time.Since(t).Seconds())

	// Tracking external dependencies using closure/wrapper
	req := ReqData{}
	prom.TrackFunc("Postgres", prom.DependencyDB, req,
		func DBConnect(v interface{}) (interface{}, error) {
			return nil, nil 
		})

	http.ListenAndServe(":"+config.PORT, router)
}

func Ping(w http.ResponseWriter, r *http.Request, _ httprouter.Params) int {
	rd.w.Write([]byte("pong"))
	return http.StatusOK
}

Usage with http.Handler and http.HanderFunc

package main

import (
	"fmt"
	"net/http"

	"github.com/FenixAra/go-prom/prom"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

type test struct {
}

func (t *test) ServeHTTP(w http.ResponseWriter, r *http.Request) (string, int) {
	fmt.Fprintf(w, "Welcome to my website!")
	return "Test", http.StatusOK
}

func Test2(w http.ResponseWriter, r *http.Request) (string, int) {
	fmt.Fprintf(w, "Welcome to my website!")
	return "Test2", http.StatusInternalServerError
}

func main() {
	http.Handle("/metrics", promhttp.Handler())
	http.Handle("/test", prom.Prom(&test{}))
	http.Handle("/test2", prom.PromFunc(Test2))

	http.ListenAndServe(":3001", nil)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL