dashboard / tuns / chore(analytics): send content-type to `metric-drain` #38 rss

open · opened on 2024-11-28T03:02:21Z by erock
Help
# add changes to patch request
git format-patch main --stdout | ssh pr.pico.sh pr add 38
# add review to patch request
git format-patch main --stdout | ssh pr.pico.sh pr add --review 38
# remove patchset
ssh pr.pico.sh ps rm ps-x
# checkout all patches
ssh pr.pico.sh pr print 38 | git am -3
# print a diff between the last two patches in a patch request
ssh pr.pico.sh pr diff 38
# accept PR
ssh pr.pico.sh pr accept 38
# close PR
ssh pr.pico.sh pr close 38

Logs

erock created pr with ps-81 on 2024-11-28T03:02:21Z
erock added ps-82 on 2024-11-28T03:36:31Z
erock added ps-83 on 2024-11-28T14:08:25Z
erock added ps-84 on 2024-11-28T14:09:17Z

Patchsets

ps-81 by erock on 2024-11-28T03:02:21Z
Range Diff ↕
1: 55cba32 = 1: 55cba32 chore(analytics): send content-type to `metric-drain`
-: ------- > 2: 334a6dc chore: check for resp
ps-82 by erock on 2024-11-28T03:36:31Z
Range Diff ↕
1: 55cba32 < -: ------- chore(analytics): send content-type to `metric-drain`
2: 334a6dc < -: ------- chore: check for resp
-: ------- > 1:
ps-83 by erock on 2024-11-28T14:08:25Z
Range Diff ↕
1: ! 1: ea8a742 chore(analytics): send content-type to `metric-drain`
ps-84 by erock on 2024-11-28T14:09:17Z

chore(analytics): send content-type to `metric-drain`

httpmuxer/httpmuxer.go link
+49 -18
 1diff --git a/httpmuxer/httpmuxer.go b/httpmuxer/httpmuxer.go
 2index ef3ece3..1e3560a 100644
 3--- a/httpmuxer/httpmuxer.go
 4+++ b/httpmuxer/httpmuxer.go
 5@@ -32,6 +32,54 @@ import (
 6 	"github.com/gin-gonic/gin"
 7 )
 8 
 9+func createMetricDrainMdw(state *utils.State) func(*gin.Context) {
10+	return func(c *gin.Context) {
11+		fmt.Println(c.Keys)
12+		originalURI := c.Keys["originalURI"].(string)
13+
14+		if viper.GetString("admin-console-token") != "" && strings.Contains(originalURI, viper.GetString("admin-console-token")) {
15+			originalURI = strings.Replace(originalURI, viper.GetString("admin-console-token"), "[REDACTED]", 1)
16+		}
17+
18+		if viper.GetString("service-console-token") != "" && strings.Contains(originalURI, viper.GetString("service-console-token")) {
19+			originalURI = strings.Replace(originalURI, viper.GetString("service-console-token"), "[REDACTED]", 1)
20+		}
21+
22+		if c.Keys["httpHolder"] != nil {
23+			currentListener := c.Keys["httpHolder"].(*utils.HTTPHolder)
24+
25+			if currentListener != nil {
26+				currentListener.SSHConnections.Range(func(I string, conn *utils.SSHConnection) bool {
27+					userId := conn.UserId()
28+
29+					visit := &utils.MetricDrainVisit{
30+						Namespace: "tuns",
31+						UserID:    userId,
32+						Host:      c.Request.Host,
33+						IpAddress: c.ClientIP(),
34+						Status:    c.Writer.Status(),
35+						Path:      originalURI,
36+						UserAgent: c.Request.UserAgent(),
37+						Referer:   c.Request.Referer(),
38+					}
39+
40+					visit.ContentType = c.Writer.Header().Get("content-type")
41+
42+					select {
43+					case state.MetricDrain <- visit:
44+					default:
45+						slog.Error("unable to send metric drain visit", slog.Any("visit", visit))
46+					}
47+
48+					return true
49+				})
50+			}
51+		}
52+
53+		c.Next()
54+	}
55+}
56+
57 // Start initializes the HTTP service.
58 func Start(state *utils.State) {
59 	releaseMode := gin.ReleaseMode
60@@ -128,23 +176,6 @@ func Start(state *utils.State) {
61 
62 					slog.Info(logLine, "user", user, "userId", userId)
63 
64-					visit := &utils.MetricDrainVisit{
65-						Namespace: "tuns",
66-						UserID:    userId,
67-						Host:      param.Request.Host,
68-						IpAddress: param.ClientIP,
69-						Status:    param.StatusCode,
70-						Path:      originalURI,
71-						UserAgent: param.Request.UserAgent(),
72-						Referer:   param.Request.Referer(),
73-					}
74-
75-					select {
76-					case state.MetricDrain <- visit:
77-					default:
78-						slog.Error("unable to send metric drain visit", slog.Any("visit", visit))
79-					}
80-
81 					return true
82 				})
83 			}
84@@ -379,7 +410,7 @@ func Start(state *utils.State) {
85 		}
86 
87 		gin.WrapH(currentListener.Balancer)(c)
88-	})
89+	}, createMetricDrainMdw(state))
90 
91 	var acmeIssuer *certmagic.ACMEIssuer = nil
92 
utils/state.go link
+9 -8
 1diff --git a/utils/state.go b/utils/state.go
 2index 44ca24f..ec99ef6 100644
 3--- a/utils/state.go
 4+++ b/utils/state.go
 5@@ -218,14 +218,15 @@ type Ports struct {
 6 
 7 // MetricDrainVisit represents a visit for the metric drain.
 8 type MetricDrainVisit struct {
 9-	Namespace string `json:"namespace"`
10-	UserID    string `json:"user_id"`
11-	Host      string `json:"host"`
12-	Path      string `json:"path"`
13-	IpAddress string `json:"ip_address"`
14-	UserAgent string `json:"user_agent"`
15-	Referer   string `json:"referer"`
16-	Status    int    `json:"status"`
17+	Namespace   string `json:"namespace"`
18+	UserID      string `json:"user_id"`
19+	Host        string `json:"host"`
20+	Path        string `json:"path"`
21+	IpAddress   string `json:"ip_address"`
22+	UserAgent   string `json:"user_agent"`
23+	Referer     string `json:"referer"`
24+	Status      int    `json:"status"`
25+	ContentType string `json:"content_type"`
26 }
27 
28 // State handles overall state. It retains mutexed maps for various