1
1
package proxy
2
2
3
3
import (
4
+ "compress/gzip"
5
+ "encoding/json"
4
6
"fmt"
5
7
"io"
8
+ log "log/slog"
6
9
"net/http"
10
+ "strings"
11
+
12
+ "porters/common"
7
13
)
8
14
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
10
47
kitMetricsUrl := fmt .Sprintf ("%s/metrics" , proxyToUrl )
11
48
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
+
12
61
// 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 )
14
64
if err != nil {
15
65
http .Error (w , "Unable to retrieve kit metrics" , http .StatusInternalServerError )
16
66
return
@@ -21,3 +71,44 @@ func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl string
21
71
w .WriteHeader (resp .StatusCode )
22
72
io .Copy (w , resp .Body )
23
73
}
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
+ }
0 commit comments