Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count HTTP delegated routing calls by method and return 501 on PUT #55

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion delegated_translator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"io"
"net/http"
Expand All @@ -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 (
Expand All @@ -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()
Expand All @@ -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")
Expand All @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down