~blu/artemis

1d1a59a7bcc604dbdeb2a7f9069015bfbc469054 — Bryant Conquest 1 year, 4 months ago 9053332
expose expvar with prometheus
3 files changed, 43 insertions(+), 4 deletions(-)

M go.mod
M go.sum
M main.go
M go.mod => go.mod +8 -1
@@ 2,4 2,11 @@ module blu.moe/artemis

go 1.19

require github.com/BurntSushi/toml v1.2.1 // indirect
require (
	github.com/BurntSushi/toml v1.2.1 // indirect
	go4.org/mem v0.0.0-20210711025021-927187094b94 // indirect
	golang.org/x/crypto v0.3.0 // indirect
	golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
	golang.org/x/sys v0.4.0 // indirect
	tailscale.com v1.36.1 // indirect
)

M go.sum => go.sum +10 -0
@@ 1,2 1,12 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
go4.org/mem v0.0.0-20210711025021-927187094b94 h1:OAAkygi2Js191AJP1Ds42MhJRgeofeKGjuoUqNp1QC4=
go4.org/mem v0.0.0-20210711025021-927187094b94/go.mod h1:reUoABIJ9ikfM5sgtSF3Wushcza7+WeD01VB9Lirh3g=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
tailscale.com v1.36.1 h1:kLmV9EIcEEiONVPsCw7NJEuYENAz3IHDIrilyHOJAZM=
tailscale.com v1.36.1/go.mod h1:jtamNns5Y+yuYjOCXvITnP3HemrkHs0gRc/ChTTIPq0=

M main.go => main.go +25 -3
@@ 9,6 9,8 @@ import (
	"strings"
	"sync"

	"tailscale.com/tsweb"

	"blu.moe/artemis/b2"
	"blu.moe/artemis/cache"
	"blu.moe/artemis/config"


@@ 18,6 20,9 @@ var (
	cacheHits   = expvar.NewInt("counter_artemis_cache_hits")
	cacheErrors = expvar.NewInt("counter_artemis_cache_errors")
	cacheLoads  = expvar.NewInt("counter_artemis_cache_loads")

	referers  = expvar.NewMap("guage_artemis_refers")
	assetHits = expvar.NewMap("guage_artemis_asset_hits")
)

type Handler struct {


@@ 26,6 31,12 @@ type Handler struct {
	etags    map[string]bool
}

func MetricsHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		tsweb.VarzHandler(w, r)
	})
}

func Purge(h *Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		err := h.Cache.PurgeCache()


@@ 90,6 101,16 @@ func Logger(next http.Handler) http.Handler {
	return http.HandlerFunc(fn)
}

func Metrics(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		referers.Add(r.Header.Get("Referer"), 1)
		assetHits.Add(r.URL.Path, 1)
		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

func main() {
	cfg, err := config.Load("../artemis.toml")
	if err != nil {


@@ 120,9 141,10 @@ func main() {
	}

	mux := http.NewServeMux()
	mux.Handle("/assets/", Logger(AssetHandler(&h)))
	mux.Handle("/purge", Logger(Purge(&h)))
	mux.Handle("/metrics/json", Logger(expvar.Handler()))
	mux.Handle("/assets/", Metrics(Logger(AssetHandler(&h))))
	mux.Handle("/purge", Metrics(Logger(Purge(&h))))
	mux.Handle("/metrics/json", Metrics(Logger(expvar.Handler())))
	mux.Handle("/metrics/prometheus", Metrics(Logger(MetricsHandler())))

	log.Println("artemis started on 8080")
	log.Fatal(http.ListenAndServe(":8080", mux))