-
Notifications
You must be signed in to change notification settings - Fork 1
/
telemetry.go
302 lines (256 loc) · 7.73 KB
/
telemetry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
"sync"
"time"
"github.com/supabase-community/supabase-go"
"golang.org/x/oauth2"
)
var lg = log.New(os.Stdout, "DEBUG MWARE: ", log.LstdFlags)
// MiddlewareRoundTripper is a struct that implements the http.RoundTripper interface
type MiddlewareRoundTripper struct {
Proxied http.RoundTripper
RoundTripper http.RoundTripper
preReq []func(http.Request)
postReq []func(http.Response, error)
logger *log.Logger
}
// Set client.Transport
// WARN: All middleware must be safe for concurrent use.
// Middleware takes value types to enforce this somewhat.
func (lrt MiddlewareRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
done := make(chan struct{})
var preReqWg sync.WaitGroup
for _, f := range lrt.preReq {
go func(f func(http.Request)) {
preReqWg.Add(1)
defer preReqWg.Done()
if req != nil {
f(*req)
}
}(f)
}
resp, err = lrt.RoundTripper.RoundTrip(req)
var postReqWg sync.WaitGroup
for _, f := range lrt.postReq {
go func(f func(http.Response, error)) {
postReqWg.Add(1)
defer postReqWg.Done()
if resp != nil {
f(*resp, err)
}
}(f)
}
go func() {
defer close(done)
preReqWg.Wait()
postReqWg.Wait()
}()
select {
case <-done:
case <-time.After(5 * time.Second):
lg.Println("Timed out waiting for preReq to finish.")
}
return
}
func OnBeforeRequest(f func(http.Request)) func(*MiddlewareRoundTripper) {
return func(lrt *MiddlewareRoundTripper) {
lrt.preReq = append(lrt.preReq, f)
}
}
func OnAfterRequest(f func(http.Response, error)) func(*MiddlewareRoundTripper) {
return func(lrt *MiddlewareRoundTripper) {
lrt.postReq = append(lrt.postReq, f)
}
}
func displayRequest(lg *log.Logger) func(http.Request) {
return func(req http.Request) {
lg.Printf("REQUEST TO: %s\n", req.URL)
lg.Printf("Method: %s\n", req.Method)
lg.Printf("Headers: %s\n", req.Header)
lg.Printf("Body: %s\n", req.Body)
}
}
func displayRespError(lg *log.Logger) func(http.Response, error) {
return func(resp http.Response, err error) {
if resp.StatusCode != 200 {
lg.Printf("ERROR IN RESPONSE: %s\n", resp.Status)
lg.Printf("Destination: %s\n", resp.Request.URL)
lg.Println(err)
lg.Println("Response: ", resp)
}
}
}
type Middleware func(*MiddlewareRoundTripper)
// Attach middleware to our http client
func NewMiddleware(transport *oauth2.Transport, middlewareFuncs ...Middleware) *MiddlewareRoundTripper {
m := &MiddlewareRoundTripper{
RoundTripper: transport,
logger: log.New(io.Discard, "No logging by default.", log.LstdFlags),
}
for _, f := range middlewareFuncs {
f(m)
}
return m
}
func LiveMiddleware(c *http.Client) *MiddlewareRoundTripper {
return NewMiddleware(
c.Transport.(*oauth2.Transport),
)
}
func WithTelemetry(c *http.Client) *MiddlewareRoundTripper {
if PROD == "TRUE" {
return ProdMiddleware(c)
}
return DebugMiddleware(c)
}
// NOTE: Expects that c is the result of oauth2.NewClient
func ProdMiddleware(c *http.Client) *MiddlewareRoundTripper {
lg = log.New(io.Discard, "PRINT LOGGING DISABLED", log.LstdFlags)
sb := newSupabaseTelemetry(supabaseUrlLive, supabaseKeyLive)
return NewMiddleware(
c.Transport.(*oauth2.Transport),
OnBeforeRequest(sb.trackRequest),
OnAfterRequest(sb.trackError),
)
}
// NOTE: Expects that c is the result of oauth2.NewClient
func DebugMiddleware(c *http.Client) *MiddlewareRoundTripper {
lg.Println("DEBUG MIDDLEWARE ACTIVE")
// sb := newSupabaseTelemetry(supabaseUrlDebug, supabaseKeyDebug)
sb := newSupabaseTelemetry(supabaseUrlDebug, supabaseKeyDebug)
return NewMiddleware(
c.Transport.(*oauth2.Transport),
OnBeforeRequest(displayRequest(lg)),
OnAfterRequest(displayRespError(lg)),
OnBeforeRequest(displayPath(lg)),
OnBeforeRequest(sb.trackRequest),
OnAfterRequest(sb.trackError),
)
}
const ( // These are anon keys, so we good
supabaseUrlLive = "https://wgbxigfylkllztzhjykd.supabase.co"
supabaseKeyLive = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6IndnYnhpZ2Z5bGtsbHp0emhqeWtkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDg4MjY5NDcsImV4cCI6MjAyNDQwMjk0N30.vx_E8RClyIOsfGwAxa6O-J2cvvlT9ajcCf72_kwmI64"
supabaseUrlDebug = "http://localhost:54321"
supabaseKeyDebug = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0"
)
// NOTE: All Insert operations must be done with the `minimal` option
// to avoid the need to view the row. (RLS)
type SupabaseTelemetry struct {
client *supabase.Client
}
func newSupabaseTelemetry(url, key string) *SupabaseTelemetry {
c, err := supabase.NewClient(url, key, nil)
if err != nil {
log.Println("Error setting up telemetry:", err)
}
return &SupabaseTelemetry{
client: c,
}
}
func (t *SupabaseTelemetry) trackUser(req http.Request) {
}
func (t *SupabaseTelemetry) trackError(resp http.Response, err error) {
if err != nil || resp.StatusCode != 200 {
lg.Println("Logging error to supabase.")
_, _, err := t.client.From("errors").Insert(map[string]interface{}{
"url": resp.Request.URL.Path,
"status": resp.Status,
"code": resp.StatusCode,
"error": err,
}, false, "", "minimal", "").Execute()
if err != nil {
log.Println(err)
}
}
}
func (t *SupabaseTelemetry) trackRequest(req http.Request) {
url := req.URL.Path
size, _ := httputil.DumpRequestOut(&req, true)
lg.Println("Logging request to supabase.")
_, _, err := t.client.From("requests").Insert(map[string]interface{}{
"url": url,
"size": len(size),
}, false, "", "minimal", "").Execute()
if err != nil {
lg.Println("Error logging request to supabase.")
lg.Println(err)
} else {
lg.Println("Successfully logged request to supabase.")
}
}
func (t *SupabaseTelemetry) trackResponse(resp http.Response, err error) {
}
func (t *SupabaseTelemetry) trackCoursesAndAssessments(req http.Request) {
path := req.URL.Path
split := strings.Split(path, "/")
for i, s := range split {
lg.Printf("Split[%d]: %s\n", i, s)
}
var courseID string
var assessmentID string
if len(split) > 4 && split[3] == "courses" {
lg.Println("Logging to Supabase: Course")
courseID = split[4]
}
if len(split) > 6 && split[5] == "assessments" {
lg.Println("Logging to Supabase: Assessment")
assessmentID = split[6]
}
if courseID == "" && assessmentID == "" {
return
}
lg.Printf("Logging to Supabase: Course: %s, Assessment: %s\n", courseID, assessmentID)
_, _, err := t.client.From("courses_assessments").Insert(map[string]interface{}{
"course_id": courseID,
"assessment_id": assessmentID,
}, false, "", "minimal", "").Execute()
if err != nil {
log.Println(err)
}
}
func displayPath(lg *log.Logger) func(http.Request) {
return func(req http.Request) {
lg.Printf("PATH: %s\n", req.URL)
split := strings.Split(req.URL.Path, "/")
out := strings.Join(split, " | ")
lg.Println("SPLIT:", out)
}
}
type PocketbaseTelemetry struct {
host string
}
func (pb PocketbaseTelemetry) pbTrackRequest(req http.Request) {
fmt.Println("Logging request to pocketbase.")
url := req.URL.String()
size, _ := httputil.DumpRequestOut(&req, true)
body := []byte(`{
"url": "` + url + `",
"size": ` + fmt.Sprint(len(size)) + `
}`)
endpoint := fmt.Sprintf("%s/api/collections/requests/records", pb.host)
resp, err := http.Post(endpoint, "application/json", bytes.NewBuffer(body))
if err != nil {
lg.Println("Error logging request to pocketbase.")
lg.Println(err)
}
defer resp.Body.Close()
fmt.Println("Successfully logged request to pocketbase.")
fmt.Println(resp)
}
func newPocketbaseClient(host string) PocketbaseTelemetry {
return PocketbaseTelemetry{
host: host,
}
}
var (
pbUrlDebug = "http://localhost:8090"
pbUrlLive = "https://pocketbase.supabase.co"
)