pico

created pr with 32.1 on 2024-11-12T21:13:35Z · by c8ef7d19
added 32.2 on 2024-11-14T15:42:32Z · by c8ef7d19
1: 7ec3569 = 1: 7ec3569 feat(auth): subscribe to pico's metric-drain pipe
2: 8a197f0 = 2: 8a197f0 chore: update pubsub
3: d4bda15 = 3: d4bda15 refactor: use pipe for analytics
-: ------- > 4: 2b7c358 chore: prep for release
cmds
checkout latest patchset:
ssh pr.pico.sh print 32 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print 32.[rev] | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 32
+13 -3 shared/analytics.go #
......@@ -4,6 +4,7 @@ import (
44 "crypto/hmac"
55 "crypto/sha256"
66 "encoding/hex"
7+ "encoding/json"
78 "errors"
89 "fmt"
910 "log/slog"
......@@ -12,6 +13,7 @@ import (
1213 "net/url"
1314
1415 "github.com/picosh/pico/db"
16+ "github.com/picosh/pubsub"
1517 "github.com/simplesurance/go-ip-anonymizer/ipanonymizer"
1618 "github.com/x-way/crawlerdetect"
1719 )
......@@ -124,10 +126,18 @@ func AnalyticsVisitFromRequest(r *http.Request, dbpool db.DB, userID string, sec
124126 }
125127
126128 func AnalyticsCollect(ch chan *db.AnalyticsVisits, dbpool db.DB, logger *slog.Logger) {
127- for view := range ch {
128- err := dbpool.InsertVisit(view)
129+ info := NewPicoPipeClient()
130+ metricDrain := pubsub.NewRemoteClientWriter(info, logger, 0)
131+ go metricDrain.KeepAlive("pub metric-drain -b=false")
132+
133+ for visit := range ch {
134+ data, err := json.Marshal(visit)
135+ if err != nil {
136+ logger.Error("could not json marshall visit record", "err", err)
137+ }
138+ _, err = metricDrain.Write(data)
129139 if err != nil {
130- logger.Error("could not insert view record", "err", err)
140+ logger.Error("could not write to metric-drain", "err", err)
131141 }
132142 }
133143 }
Back to top