From e0569ac31e8f2642a25a127ea0dbbb32907dc584 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 22 Oct 2023 15:14:16 +0200 Subject: [PATCH] packages/hyprspace: enable optional prometheus metrics endpoint --- packages/networking/hyprspace/cli/up.go | 13 +++++++++++++ packages/networking/hyprspace/go.mod | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/networking/hyprspace/cli/up.go b/packages/networking/hyprspace/cli/up.go index be7a91f..7363b83 100644 --- a/packages/networking/hyprspace/cli/up.go +++ b/packages/networking/hyprspace/cli/up.go @@ -7,6 +7,7 @@ import ( "fmt" "io/fs" "net" + "net/http" "os" "os/signal" "path/filepath" @@ -26,6 +27,7 @@ import ( "github.com/libp2p/go-libp2p/core/peer" "github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multibase" + "github.com/prometheus/client_golang/prometheus/promhttp" ) var ( @@ -140,6 +142,17 @@ func UpRun(r *cmd.Root, c *cmd.Sub) { // RPC server go hsrpc.RpcServer(ctx, multiaddr.StringCast(fmt.Sprintf("/unix/run/hyprspace-rpc.%s.sock", cfg.Interface.Name)), host, *cfg) + // metrics endpoint + metricsPort, ok := os.LookupEnv("HYPRSPACE_METRICS_PORT") + if ok { + metricsTuple := fmt.Sprintf("127.0.0.1:%s", metricsPort) + http.Handle("/metrics", promhttp.Handler()) + go func() { + http.ListenAndServe(metricsTuple, nil) + }() + fmt.Printf("[+] Listening for metrics scrape requests on http://%s/metrics\n", metricsTuple) + } + // Write lock to filesystem to indicate an existing running daemon. err = os.WriteFile(lockPath, []byte(fmt.Sprint(os.Getpid())), os.ModePerm) checkErr(err) diff --git a/packages/networking/hyprspace/go.mod b/packages/networking/hyprspace/go.mod index c309015..9c79d18 100644 --- a/packages/networking/hyprspace/go.mod +++ b/packages/networking/hyprspace/go.mod @@ -8,6 +8,7 @@ require ( github.com/libp2p/go-libp2p-kad-dht v0.25.0 github.com/multiformats/go-multiaddr v0.11.0 github.com/multiformats/go-multibase v0.2.0 + github.com/prometheus/client_golang v1.16.0 github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 github.com/vishvananda/netlink v1.1.0 gopkg.in/yaml.v2 v2.4.0 @@ -87,7 +88,6 @@ require ( github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/polydawn/refmt v0.89.0 // indirect - github.com/prometheus/client_golang v1.16.0 // indirect github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect