From 8ad9bbaed06aebeac376cfd0f6f265cda7eb89a1 Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 13 Nov 2024 09:37:47 +0100 Subject: [PATCH 1/7] feat: add instrumentation to delegated routing endpoint --- go.mod | 2 +- go.sum | 2 ++ routing/http/server/server.go | 25 ++++++++++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 3054db658..db188a203 100644 --- a/go.mod +++ b/go.mod @@ -57,6 +57,7 @@ require ( github.com/polydawn/refmt v0.89.0 github.com/prometheus/client_golang v1.20.5 github.com/samber/lo v1.47.0 + github.com/slok/go-http-metrics v0.12.0 github.com/spaolacci/murmur3 v1.1.0 github.com/stretchr/testify v1.9.0 github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc @@ -96,7 +97,6 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect - github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/google/gopacket v1.1.19 // indirect github.com/google/pprof v0.0.0-20241017200806-017d972448fc // indirect github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect diff --git a/go.sum b/go.sum index 57bb266c6..f3c5da72b 100644 --- a/go.sum +++ b/go.sum @@ -467,6 +467,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4= github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/slok/go-http-metrics v0.12.0 h1:mAb7hrX4gB4ItU6NkFoKYdBslafg3o60/HbGBRsKaG8= +github.com/slok/go-http-metrics v0.12.0/go.mod h1:Ee/mdT9BYvGrlGzlClkK05pP2hRHmVbRF9dtUVS8LNA= github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= diff --git a/routing/http/server/server.go b/routing/http/server/server.go index a7f9385b6..9d235f229 100644 --- a/routing/http/server/server.go +++ b/routing/http/server/server.go @@ -25,8 +25,12 @@ import ( "github.com/libp2p/go-libp2p/core/routing" "github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multibase" + "github.com/prometheus/client_golang/prometheus" logging "github.com/ipfs/go-log/v2" + metrics "github.com/slok/go-http-metrics/metrics/prometheus" + "github.com/slok/go-http-metrics/middleware" + middlewarestd "github.com/slok/go-http-metrics/middleware/std" ) const ( @@ -133,12 +137,23 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler { opt(server) } + // Create middleware with prometheus recorder + mdlw := middleware.New(middleware.Config{ + Recorder: metrics.NewRecorder(metrics.Config{ + Registry: prometheus.DefaultRegisterer, + Prefix: "delegated_routing", + DurationBuckets: []float64{0.05, 0.1, 0.5, 1, 5, 10, 20, 30, 50, 60}, + }), + }) + r := mux.NewRouter() - r.HandleFunc(findProvidersPath, server.findProviders).Methods(http.MethodGet) - r.HandleFunc(providePath, server.provide).Methods(http.MethodPut) - r.HandleFunc(findPeersPath, server.findPeers).Methods(http.MethodGet) - r.HandleFunc(GetIPNSPath, server.GetIPNS).Methods(http.MethodGet) - r.HandleFunc(GetIPNSPath, server.PutIPNS).Methods(http.MethodPut) + // Wrap each handler with the metrics middleware + r.Handle(findProvidersPath, middlewarestd.Handler(findProvidersPath, mdlw, http.HandlerFunc(server.findProviders))).Methods(http.MethodGet) + r.Handle(providePath, middlewarestd.Handler(providePath, mdlw, http.HandlerFunc(server.provide))).Methods(http.MethodPut) + r.Handle(findPeersPath, middlewarestd.Handler(findPeersPath, mdlw, http.HandlerFunc(server.findPeers))).Methods(http.MethodGet) + r.Handle(GetIPNSPath, middlewarestd.Handler(GetIPNSPath, mdlw, http.HandlerFunc(server.GetIPNS))).Methods(http.MethodGet) + r.Handle(GetIPNSPath, middlewarestd.Handler(GetIPNSPath, mdlw, http.HandlerFunc(server.PutIPNS))).Methods(http.MethodPut) + return r } From 0fbf207b7915766ec4a200674850ee715ec8d60b Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:48:28 +0100 Subject: [PATCH 2/7] feat: allow passing in custom registry --- routing/http/server/server.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/routing/http/server/server.go b/routing/http/server/server.go index 9d235f229..9d5c73d87 100644 --- a/routing/http/server/server.go +++ b/routing/http/server/server.go @@ -126,6 +126,12 @@ func WithStreamingRecordsLimit(limit int) Option { } } +func WithPrometheusRegistry(reg prometheus.Registerer) Option { + return func(s *server) { + s.promRegistry = reg + } +} + func Handler(svc ContentRouter, opts ...Option) http.Handler { server := &server{ svc: svc, @@ -140,7 +146,7 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler { // Create middleware with prometheus recorder mdlw := middleware.New(middleware.Config{ Recorder: metrics.NewRecorder(metrics.Config{ - Registry: prometheus.DefaultRegisterer, + Registry: server.promRegistry, Prefix: "delegated_routing", DurationBuckets: []float64{0.05, 0.1, 0.5, 1, 5, 10, 20, 30, 50, 60}, }), @@ -162,6 +168,7 @@ type server struct { disableNDJSON bool recordsLimit int streamingRecordsLimit int + promRegistry prometheus.Registerer } func (s *server) detectResponseType(r *http.Request) (string, error) { From a9aeafe46d670d72451740b0f5d7231d741a3ede Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:55:05 +0100 Subject: [PATCH 3/7] docs: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c5c621d2..faca07433 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ The following emojis are used to highlight certain changes: ### Added +- `routing/http/server`: added Prometheus instrumentation to http delegated routing endpoints. + ### Changed - No longer using `github.com/jbenet/goprocess` to avoid requiring in dependents. From abb2fc2255b35f9b09b67b39c7dd4226fee7a444 Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:56:48 +0100 Subject: [PATCH 4/7] chore: go mod tidy --- examples/go.sum | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/go.sum b/examples/go.sum index 323084c69..8d7be4a96 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -97,9 +97,8 @@ github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXP github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= -github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= @@ -467,6 +466,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4= github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/slok/go-http-metrics v0.12.0 h1:mAb7hrX4gB4ItU6NkFoKYdBslafg3o60/HbGBRsKaG8= +github.com/slok/go-http-metrics v0.12.0/go.mod h1:Ee/mdT9BYvGrlGzlClkK05pP2hRHmVbRF9dtUVS8LNA= github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= From a5f2aa7d7866310f65283458a1876f06b09fb2d8 Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:23:46 +0100 Subject: [PATCH 5/7] fix: create new registry unless set --- routing/http/server/server.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routing/http/server/server.go b/routing/http/server/server.go index 9d5c73d87..bb1202f3d 100644 --- a/routing/http/server/server.go +++ b/routing/http/server/server.go @@ -143,6 +143,10 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler { opt(server) } + if server.promRegistry == nil { + server.promRegistry = prometheus.NewRegistry() + } + // Create middleware with prometheus recorder mdlw := middleware.New(middleware.Config{ Recorder: metrics.NewRecorder(metrics.Config{ From 2d8efc3ac5e5a7324cfbcfacd6dba48f4eb12642 Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:44:32 +0100 Subject: [PATCH 6/7] chore: adjust histogram buckets --- routing/http/server/server.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/routing/http/server/server.go b/routing/http/server/server.go index bb1202f3d..0650092b1 100644 --- a/routing/http/server/server.go +++ b/routing/http/server/server.go @@ -150,9 +150,10 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler { // Create middleware with prometheus recorder mdlw := middleware.New(middleware.Config{ Recorder: metrics.NewRecorder(metrics.Config{ - Registry: server.promRegistry, - Prefix: "delegated_routing", - DurationBuckets: []float64{0.05, 0.1, 0.5, 1, 5, 10, 20, 30, 50, 60}, + Registry: server.promRegistry, + Prefix: "delegated_routing", + + DurationBuckets: []float64{0.1, 0.5, 1, 2, 5, 8, 10, 20, 30}, }), }) From 786e657f1a6b277d3be3acf6f904ee0e6458f464 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Tue, 19 Nov 2024 00:23:24 +0100 Subject: [PATCH 7/7] refactor: rename histogram --- routing/http/server/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routing/http/server/server.go b/routing/http/server/server.go index 0650092b1..6177da125 100644 --- a/routing/http/server/server.go +++ b/routing/http/server/server.go @@ -151,7 +151,7 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler { mdlw := middleware.New(middleware.Config{ Recorder: metrics.NewRecorder(metrics.Config{ Registry: server.promRegistry, - Prefix: "delegated_routing", + Prefix: "delegated_routing_server", DurationBuckets: []float64{0.1, 0.5, 1, 2, 5, 8, 10, 20, 30}, }),