An open-source, faster Devin alternative
TypeScript
Add user_email property to Segment usage events
motatoes0259740e9e3d
diff --git a/internal/analytics/segment.go b/internal/analytics/segment.goindex 525c8c9d..07ceb163 100644--- a/internal/analytics/segment.go+++ b/internal/analytics/segment.go@@ -26,19 +26,25 @@ func New(writeKey string) *Client {}// TrackGBSeconds enqueues a "Sandbox Memory Usage" event with GB-seconds for-// the given org. sandboxID is included as an event property for debugging but-// the metric of record is gb_seconds bucketed by org.-func (c *Client) TrackGBSeconds(orgID, sandboxID string, gbSeconds float64) {+// the given org. sandboxID and userEmail are included as properties for+// downstream filtering; the metric of record is gb_seconds bucketed by org.+// userEmail may be empty (sandboxes created with org-level API keys have no+// associated user).+func (c *Client) TrackGBSeconds(orgID, userEmail, sandboxID string, gbSeconds float64) {if c == nil || c.c == nil || orgID == "" || gbSeconds <= 0 {return}+ props := analytics.NewProperties().+ Set("gb_seconds", gbSeconds).+ Set("sandbox_id", sandboxID).+ Set("org_id", orgID)+ if userEmail != "" {+ props = props.Set("user_email", userEmail)+ }if err := c.c.Enqueue(analytics.Track{- UserId: orgID,- Event: "Sandbox Memory Usage",- Properties: analytics.NewProperties().- Set("gb_seconds", gbSeconds).- Set("sandbox_id", sandboxID).- Set("org_id", orgID),+ UserId: orgID,+ Event: "Sandbox Memory Usage",+ Properties: props,}); err != nil {log.Printf("segment: enqueue failed: %v", err)}diff --git a/internal/db/usage.go b/internal/db/usage.goindex b3925cf3..57a73c5a 100644--- a/internal/db/usage.go+++ b/internal/db/usage.go@@ -69,6 +69,28 @@ func (s *Store) GetSandboxOrgID(ctx context.Context, sandboxID string) (string,return orgID.String(), nil}+// GetSandboxOwner returns the org_id and user_email for a sandbox. user_email+// may be empty if the session has no associated user (e.g. created with an+// org-level API key that isn't tied to a user).+func (s *Store) GetSandboxOwner(ctx context.Context, sandboxID string) (orgID, userEmail string, err error) {+ var orgUUID uuid.UUID+ var email *string+ err = s.pool.QueryRow(ctx,+ `SELECT s.org_id, u.email+ FROM sandbox_sessions s+ LEFT JOIN users u ON u.id = s.user_id+ WHERE s.sandbox_id = $1+ ORDER BY s.started_at DESC LIMIT 1`,+ sandboxID).Scan(&orgUUID, &email)+ if err != nil {+ return "", "", err+ }+ if email != nil {+ userEmail = *email+ }+ return orgUUID.String(), userEmail, nil+}+// EndScaleEvent marks the current scale event as ended (sandbox stopped/hibernated).func (s *Store) EndScaleEvent(ctx context.Context, sandboxID string) error {_, err := s.pool.Exec(ctx,diff --git a/internal/worker/usage_collector.go b/internal/worker/usage_collector.goindex e03351cb..bd358538 100644--- a/internal/worker/usage_collector.go+++ b/internal/worker/usage_collector.go@@ -90,7 +90,7 @@ func (c *UsageCollector) sample() {continue // sandbox might be shutting down}- orgID, err := c.store.GetSandboxOrgID(ctx, sb.ID)+ orgID, userEmail, err := c.store.GetSandboxOwner(ctx, sb.ID)if err != nil || orgID == "" {continue // no billable owner, skip}@@ -109,7 +109,7 @@ func (c *UsageCollector) sample() {// customer reserved), not actual RSS. Reservation is what we bill for —// actual usage is gameable and punishes well-behaved tenants.gbSeconds := (float64(sb.MemoryMB) / 1024.0) * intervalSec- c.segment.TrackGBSeconds(orgID, sb.ID, gbSeconds)+ c.segment.TrackGBSeconds(orgID, userEmail, sb.ID, gbSeconds)}if len(samples) == 0 {