Skip to content

Commit

Permalink
Merge pull request #342 from porters-xyz/feature/poktscan-update-endp…
Browse files Browse the repository at this point in the history
…oint

Poktscan QOS endpoint
  • Loading branch information
scermat authored Aug 15, 2024
2 parents 13d0891 + 75b58b8 commit 22384a5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions gateway/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
LOG_HTTP_RESPONSE = "LOG_HTTP_RESPONSE"
FLY_API_KEY = "FLY_API_KEY"
FLY_GATEWAY_URI = "FLY_GATEWAY_URI"
GATEWAY_API_KEY = "GATEWAY_API_KEY"
GATEWAY_REQUEST_API_KEY = "GATEWAY_REQUEST_API_KEY"
)

// This may evolve to include config outside env, or use .env file for
Expand Down
18 changes: 15 additions & 3 deletions gateway/proxy/kitMetricsKitRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ type Machine struct {
Region string `json:"region"`
}

func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, region string) {
func qosNodesHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, region string) {
// in order to call this endpoint, the user must pass the `GATEWAY_REQUEST_API_KEY` in the header
expectedApiKey := common.GetConfig(common.GATEWAY_REQUEST_API_KEY)
apiKey := r.Header.Get("api-key")

if apiKey == "" || apiKey != expectedApiKey {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}

flyApiKey := common.GetConfig(common.FLY_API_KEY)
// Fetch the machines from Fly.io
machines, err := fetchMachines(flyApiKey)
Expand All @@ -44,7 +53,7 @@ func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, regio
log.Info("Retrieved Machine ID for gatewaykit", "Machine ID", machineID)

// Construct the metrics URL
kitMetricsUrl := fmt.Sprintf("%s/metrics", proxyToUrl)
kitMetricsUrl := fmt.Sprintf("%s/qosnodes", proxyToUrl)

log.Info("Calling metrics endpoint", "kitMetricsUrl", kitMetricsUrl)

Expand All @@ -55,8 +64,11 @@ func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, regio
return
}

// Add the fly-force-instance-id header
gatewayApiKey := common.GetConfig(common.GATEWAY_API_KEY)

//add the headers
req.Header.Set("fly-force-instance-id", machineID)
req.Header.Set("x-api-key", gatewayApiKey)

// Forward the request to the kit's /metrics endpoint
client := &http.Client{}
Expand Down
4 changes: 2 additions & 2 deletions gateway/proxy/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func addMetricsRoute(r *mux.Router) *mux.Router {

// Since the Gateway Kit is on an internal private network, with only the Gateway having access to it, we proxy a gateway-kit/metrics endpoint to expose the data to POKTScan
func addMetricsKitRoute(r *mux.Router, proxyToUrl string) *mux.Router {
subrouter := r.PathPrefix("/gateway-kit/metrics").Subrouter()
subrouter := r.PathPrefix("/gateway-kit/qosnodes").Subrouter()
subrouter.HandleFunc("/{region}", func(w http.ResponseWriter, r *http.Request) {
region := mux.Vars(r)["region"]
kitMetricsHandler(w, r, proxyToUrl, region)
qosNodesHandler(w, r, proxyToUrl, region)
})
return subrouter
}

0 comments on commit 22384a5

Please sign in to comment.