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

accepted · opened on 2024-11-28T03:02:21Z by erock
Help
checkout latest patchset:
ssh pr.pico.sh print pr-38 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print ps-X | git am -3
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
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
erock changed status on 2024-12-20T18:22:20Z {"status":"accepted"}

Patchsets

ps-81 by erock on 2024-11-28T03:02:21Z
Range Diff ↕ rd-82
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 ↕ rd-83
1: 55cba32 < -: ------- chore(analytics): send content-type to `metric-drain`
-: ------- > 1:
2: 334a6dc < -: ------- chore: check for resp
ps-83 by erock on 2024-11-28T14:08:25Z
Range Diff ↕ rd-84
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
 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
diff --git a/httpmuxer/httpmuxer.go b/httpmuxer/httpmuxer.go
index ef3ece3..1e3560a 100644
--- a/httpmuxer/httpmuxer.go
+++ b/httpmuxer/httpmuxer.go
@@ -32,6 +32,54 @@ import (
 	"github.com/gin-gonic/gin"
 )
 
+func createMetricDrainMdw(state *utils.State) func(*gin.Context) {
+	return func(c *gin.Context) {
+		fmt.Println(c.Keys)
+		originalURI := c.Keys["originalURI"].(string)
+
+		if viper.GetString("admin-console-token") != "" && strings.Contains(originalURI, viper.GetString("admin-console-token")) {
+			originalURI = strings.Replace(originalURI, viper.GetString("admin-console-token"), "[REDACTED]", 1)
+		}
+
+		if viper.GetString("service-console-token") != "" && strings.Contains(originalURI, viper.GetString("service-console-token")) {
+			originalURI = strings.Replace(originalURI, viper.GetString("service-console-token"), "[REDACTED]", 1)
+		}
+
+		if c.Keys["httpHolder"] != nil {
+			currentListener := c.Keys["httpHolder"].(*utils.HTTPHolder)
+
+			if currentListener != nil {
+				currentListener.SSHConnections.Range(func(I string, conn *utils.SSHConnection) bool {
+					userId := conn.UserId()
+
+					visit := &utils.MetricDrainVisit{
+						Namespace: "tuns",
+						UserID:    userId,
+						Host:      c.Request.Host,
+						IpAddress: c.ClientIP(),
+						Status:    c.Writer.Status(),
+						Path:      originalURI,
+						UserAgent: c.Request.UserAgent(),
+						Referer:   c.Request.Referer(),
+					}
+
+					visit.ContentType = c.Writer.Header().Get("content-type")
+
+					select {
+					case state.MetricDrain <- visit:
+					default:
+						slog.Error("unable to send metric drain visit", slog.Any("visit", visit))
+					}
+
+					return true
+				})
+			}
+		}
+
+		c.Next()
+	}
+}
+
 // Start initializes the HTTP service.
 func Start(state *utils.State) {
 	releaseMode := gin.ReleaseMode
@@ -128,23 +176,6 @@ func Start(state *utils.State) {
 
 					slog.Info(logLine, "user", user, "userId", userId)
 
-					visit := &utils.MetricDrainVisit{
-						Namespace: "tuns",
-						UserID:    userId,
-						Host:      param.Request.Host,
-						IpAddress: param.ClientIP,
-						Status:    param.StatusCode,
-						Path:      originalURI,
-						UserAgent: param.Request.UserAgent(),
-						Referer:   param.Request.Referer(),
-					}
-
-					select {
-					case state.MetricDrain <- visit:
-					default:
-						slog.Error("unable to send metric drain visit", slog.Any("visit", visit))
-					}
-
 					return true
 				})
 			}
@@ -379,7 +410,7 @@ func Start(state *utils.State) {
 		}
 
 		gin.WrapH(currentListener.Balancer)(c)
-	})
+	}, createMetricDrainMdw(state))
 
 	var acmeIssuer *certmagic.ACMEIssuer = nil
 
utils/state.go link
+9 -8
 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
diff --git a/utils/state.go b/utils/state.go
index 44ca24f..ec99ef6 100644
--- a/utils/state.go
+++ b/utils/state.go
@@ -218,14 +218,15 @@ type Ports struct {
 
 // MetricDrainVisit represents a visit for the metric drain.
 type MetricDrainVisit struct {
-	Namespace string `json:"namespace"`
-	UserID    string `json:"user_id"`
-	Host      string `json:"host"`
-	Path      string `json:"path"`
-	IpAddress string `json:"ip_address"`
-	UserAgent string `json:"user_agent"`
-	Referer   string `json:"referer"`
-	Status    int    `json:"status"`
+	Namespace   string `json:"namespace"`
+	UserID      string `json:"user_id"`
+	Host        string `json:"host"`
+	Path        string `json:"path"`
+	IpAddress   string `json:"ip_address"`
+	UserAgent   string `json:"user_agent"`
+	Referer     string `json:"referer"`
+	Status      int    `json:"status"`
+	ContentType string `json:"content_type"`
 }
 
 // State handles overall state. It retains mutexed maps for various