Skip to content

Commit 7cb6626

Browse files
authored
Merge pull request #341 from porters-xyz/develop
Added regional metrics endpoint for POKTSCAN
2 parents 3076a88 + 13d0891 commit 7cb6626

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

gateway/common/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
INSTRUMENT_ENABLED = "ENABLE_INSTRUMENT"
2525
LOG_LEVEL = "LOG_LEVEL"
2626
LOG_HTTP_RESPONSE = "LOG_HTTP_RESPONSE"
27+
FLY_API_KEY = "FLY_API_KEY"
28+
FLY_GATEWAY_URI = "FLY_GATEWAY_URI"
2729
)
2830

2931
// This may evolve to include config outside env, or use .env file for

gateway/proxy/kitMetricsKitRouter.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,66 @@
11
package proxy
22

33
import (
4+
"compress/gzip"
5+
"encoding/json"
46
"fmt"
57
"io"
8+
log "log/slog"
69
"net/http"
10+
"strings"
11+
12+
"porters/common"
713
)
814

9-
func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl string) {
15+
type Machine struct {
16+
ID string `json:"id"`
17+
InstanceID string `json:"instance_id"`
18+
Region string `json:"region"`
19+
}
20+
21+
func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, region string) {
22+
flyApiKey := common.GetConfig(common.FLY_API_KEY)
23+
// Fetch the machines from Fly.io
24+
machines, err := fetchMachines(flyApiKey)
25+
if err != nil {
26+
http.Error(w, "Unable to retrieve machines from Fly.io", http.StatusInternalServerError)
27+
return
28+
}
29+
30+
// Find the machine for the given region
31+
machineID := ""
32+
for _, machine := range machines {
33+
if strings.EqualFold(machine.Region, region) {
34+
machineID = machine.ID
35+
break
36+
}
37+
}
38+
39+
if machineID == "" {
40+
http.Error(w, "Machine not found for the given region", http.StatusNotFound)
41+
return
42+
}
43+
44+
log.Info("Retrieved Machine ID for gatewaykit", "Machine ID", machineID)
45+
46+
// Construct the metrics URL
1047
kitMetricsUrl := fmt.Sprintf("%s/metrics", proxyToUrl)
1148

49+
log.Info("Calling metrics endpoint", "kitMetricsUrl", kitMetricsUrl)
50+
51+
// Create a new HTTP request
52+
req, err := http.NewRequest("GET", kitMetricsUrl, nil)
53+
if err != nil {
54+
http.Error(w, "Unable to create request", http.StatusInternalServerError)
55+
return
56+
}
57+
58+
// Add the fly-force-instance-id header
59+
req.Header.Set("fly-force-instance-id", machineID)
60+
1261
// Forward the request to the kit's /metrics endpoint
13-
resp, err := http.Get(kitMetricsUrl)
62+
client := &http.Client{}
63+
resp, err := client.Do(req)
1464
if err != nil {
1565
http.Error(w, "Unable to retrieve kit metrics", http.StatusInternalServerError)
1666
return
@@ -21,3 +71,44 @@ func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl string
2171
w.WriteHeader(resp.StatusCode)
2272
io.Copy(w, resp.Body)
2373
}
74+
75+
func fetchMachines(authToken string) ([]Machine, error) {
76+
flyGatewayMachines := common.GetConfig(common.FLY_GATEWAY_URI)
77+
// Create a new request to fetch the machines
78+
req, err := http.NewRequest("GET", flyGatewayMachines, nil)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
// Set the Authorization header with the Bearer token
84+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken))
85+
86+
// Perform the request
87+
client := &http.Client{}
88+
resp, err := client.Do(req)
89+
if err != nil {
90+
return nil, err
91+
}
92+
defer resp.Body.Close()
93+
94+
// Check if the response is gzipped and handle accordingly
95+
var reader io.ReadCloser
96+
if resp.Header.Get("Content-Encoding") == "gzip" {
97+
reader, err = gzip.NewReader(resp.Body)
98+
if err != nil {
99+
return nil, err
100+
}
101+
defer reader.Close()
102+
} else {
103+
reader = resp.Body
104+
}
105+
106+
// Decode the JSON response into the machines slice
107+
var machines []Machine
108+
err = json.NewDecoder(reader).Decode(&machines)
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
return machines, nil
114+
}

gateway/proxy/mux.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
)
1212

1313
const (
14-
APP_PATH string = "appId"
15-
PRODUCT_NAME = "product"
16-
HEALTH = "health"
14+
APP_PATH string = "appId"
15+
PRODUCT_NAME = "product"
16+
HEALTH = "health"
1717
)
1818

1919
func PluckAppId(req *http.Request) string {
@@ -48,8 +48,9 @@ func addMetricsRoute(r *mux.Router) *mux.Router {
4848
// 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
4949
func addMetricsKitRoute(r *mux.Router, proxyToUrl string) *mux.Router {
5050
subrouter := r.PathPrefix("/gateway-kit/metrics").Subrouter()
51-
subrouter.HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
52-
kitMetricsHandler(w, r, proxyToUrl)
51+
subrouter.HandleFunc("/{region}", func(w http.ResponseWriter, r *http.Request) {
52+
region := mux.Vars(r)["region"]
53+
kitMetricsHandler(w, r, proxyToUrl, region)
5354
})
5455
return subrouter
5556
}

0 commit comments

Comments
 (0)