From 44cab9f5e298d190c96a05b4119622232914ed82 Mon Sep 17 00:00:00 2001 From: "Masih H. Derkani" Date: Wed, 14 Dec 2022 16:31:57 +0000 Subject: [PATCH] Count HTTP delegated routing calls by method and return 501 on `PUT` Count the number of calls made to the HTTP delegated routing tagged by method. Explicitly return `501 Not Implemented` on `PUT` to `/routing/v1/providers`. This is to identify that putting provider records is not currently implemented by `cid.contact`. Relates to: - https://github.com/ipfs/specs/pull/337#discussion_r1047399812 Fixes #54 --- delegated_translator.go | 15 ++++++++++++++- metrics/server.go | 13 ++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/delegated_translator.go b/delegated_translator.go index df1b7f3..da2edef 100644 --- a/delegated_translator.go +++ b/delegated_translator.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "io" "net/http" @@ -11,9 +12,12 @@ import ( "github.com/filecoin-project/index-provider/metadata" "github.com/filecoin-project/storetheindex/api/v0/finder/model" "github.com/filecoin-shipyard/indexstar/httpserver" + "github.com/filecoin-shipyard/indexstar/metrics" "github.com/libp2p/go-libp2p/core/peer" "github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multicodec" + "go.opencensus.io/stats" + "go.opencensus.io/tag" ) const ( @@ -33,6 +37,10 @@ type delegatedTranslator struct { } func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request) { + _ = stats.RecordWithOptions(context.Background(), + stats.WithTags(tag.Insert(metrics.Method, r.Method)), + stats.WithMeasurements(metrics.HttpDelegatedRoutingMethod.M(1))) + // read out / close the request body. _, err := io.ReadAll(r.Body) _ = r.Body.Close() @@ -42,6 +50,11 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request) { return } + if r.Method == http.MethodPut { + http.Error(w, "", http.StatusNotImplemented) + return + } + h := w.Header() h.Add("Access-Control-Allow-Origin", "*") h.Add("Access-Control-Allow-Methods", "GET, PUT, OPTIONS") @@ -64,7 +77,7 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request) { http.Error(w, "", http.StatusInternalServerError) return } - rcode, resp := dt.be(r.Context(), "GET", findMethodDelegated, uri, []byte{}) + rcode, resp := dt.be(r.Context(), http.MethodGet, findMethodDelegated, uri, []byte{}) if rcode != http.StatusOK { http.Error(w, "", rcode) diff --git a/metrics/server.go b/metrics/server.go index ebe55a5..0b183a0 100644 --- a/metrics/server.go +++ b/metrics/server.go @@ -26,9 +26,10 @@ var ( // Measures var ( - FindLatency = stats.Float64("indexstar/find/latency", "Time to respond to a find request", stats.UnitMilliseconds) - FindBackends = stats.Float64("indexstar/find/backends", "Backends reachd in a find request", stats.UnitDimensionless) - FindLoad = stats.Int64("indexstar/find/load", "Amount of calls to find", stats.UnitDimensionless) + FindLatency = stats.Float64("indexstar/find/latency", "Time to respond to a find request", stats.UnitMilliseconds) + FindBackends = stats.Float64("indexstar/find/backends", "Backends reached in a find request", stats.UnitDimensionless) + FindLoad = stats.Int64("indexstar/find/load", "Amount of calls to find", stats.UnitDimensionless) + HttpDelegatedRoutingMethod = stats.Int64("indexstar/http_delegated_routing/load", "Amount of HTTP delegated routing calls by tagged method", stats.UnitDimensionless) ) // Views @@ -47,6 +48,11 @@ var ( Aggregation: view.Count(), TagKeys: []tag.Key{Method}, } + httpDelegRoutingMethodView = &view.View{ + Measure: HttpDelegatedRoutingMethod, + Aggregation: view.Count(), + TagKeys: []tag.Key{Method}, + } ) // Start creates an HTTP router for serving metric info @@ -56,6 +62,7 @@ func Start(views []*view.View) http.Handler { findLatencyView, findBackendView, findLoadView, + httpDelegRoutingMethodView, ) if err != nil { log.Errorf("cannot register metrics default views: %s", err)