aboutsummaryrefslogtreecommitdiff
path: root/cmd/xedn
diff options
context:
space:
mode:
authorXe <me@christine.website>2022-09-02 19:20:19 +0000
committerXe <me@christine.website>2022-09-02 19:20:19 +0000
commiteceb8af2f9b78a00911fd3c744ac9b09b7ec614e (patch)
treeaecf8062822eec0b8e4ff16ad70489bf117de016 /cmd/xedn
parentd01e35dd9416a5fcda1bf809fd9389dd1ff2f7e2 (diff)
downloadx-eceb8af2f9b78a00911fd3c744ac9b09b7ec614e.tar.xz
x-eceb8af2f9b78a00911fd3c744ac9b09b7ec614e.zip
custom cache metrics
Signed-off-by: Xe <me@christine.website>
Diffstat (limited to 'cmd/xedn')
-rw-r--r--cmd/xedn/fly.toml4
-rw-r--r--cmd/xedn/main.go24
2 files changed, 28 insertions, 0 deletions
diff --git a/cmd/xedn/fly.toml b/cmd/xedn/fly.toml
index a1ad8c0..a721ba5 100644
--- a/cmd/xedn/fly.toml
+++ b/cmd/xedn/fly.toml
@@ -41,3 +41,7 @@ processes = []
interval = "15s"
restart_limit = 0
timeout = "2s"
+
+[metrics]
+port = 8080
+path = "/.within/metrics" \ No newline at end of file
diff --git a/cmd/xedn/main.go b/cmd/xedn/main.go
index b5f861a..2feae65 100644
--- a/cmd/xedn/main.go
+++ b/cmd/xedn/main.go
@@ -5,12 +5,14 @@ import (
"bytes"
"context"
"encoding/gob"
+ "expvar"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
+ "time"
"github.com/golang/groupcache"
"github.com/sebest/xff"
@@ -71,10 +73,31 @@ var Group = groupcache.NewGroup("b2-bucket", cacheSize, groupcache.GetterFunc(
},
))
+var (
+ cacheGets = expvar.NewInt("cache_gets")
+ cacheHits = expvar.NewInt("cache_hits")
+ cacheErrors = expvar.NewInt("cache_errors")
+ cacheLoads = expvar.NewInt("cache_loads")
+)
+
+func refreshMetrics () {
+ t := time.NewTicker(10 * time.Second)
+ defer t.Stop()
+
+ for range t.C {
+ cacheGets.Set(Group.Stats.Gets.Get())
+ cacheHits.Set(Group.Stats.CacheHits.Get())
+ cacheErrors.Set(int64(Group.Stats.LocalLoadErrs))
+ cacheLoads.Set(int64(Group.Stats.LocalLoads))
+ }
+}
+
func main() {
internal.HandleStartup()
ctx := opname.With(context.Background(), "startup")
+ go refreshMetrics()
+
go func () {
srv := &tsnet.Server{
Hostname: "xedn-" + os.Getenv("FLY_REGION"),
@@ -100,6 +123,7 @@ func main() {
}
mux := http.NewServeMux()
+ mux.HandleFunc("/.within/metrics", tsweb.VarzHandler)
mux.HandleFunc("/file/christine-static/", func(w http.ResponseWriter, r *http.Request) {
var b []byte
err := Group.Get(nil, r.URL.Path, groupcache.AllocatingByteSliceSink(&b))