An open-source, faster Devin alternative
TypeScript
Add user_id, user_email, workos_user_id, workos_org_id to Segment events (#115)
Mohamed Habibe1d3c384ca16
diff --git a/internal/analytics/segment.go b/internal/analytics/segment.goindex 07ceb163..be704618 100644--- a/internal/analytics/segment.go+++ b/internal/analytics/segment.go@@ -25,24 +25,42 @@ func New(writeKey string) *Client {return &Client{c: analytics.New(writeKey)}}+// UsageEvent holds the identity fields shipped alongside a usage metric.+type UsageEvent struct {+ OrgID string+ UserID string+ UserEmail string+ WorkosUserID string+ WorkosOrgID string+ SandboxID string+ GBSeconds float64+}+// TrackGBSeconds enqueues a "Sandbox Memory Usage" event with GB-seconds for-// 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 {+// the given org. User fields are included as properties for downstream+// filtering; the metric of record is gb_seconds bucketed by org.+func (c *Client) TrackGBSeconds(evt UsageEvent) {+ if c == nil || c.c == nil || evt.OrgID == "" || evt.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)+ Set("gb_seconds", evt.GBSeconds).+ Set("sandbox_id", evt.SandboxID).+ Set("org_id", evt.OrgID)+ if evt.UserID != "" {+ props = props.Set("user_id", evt.UserID)+ }+ if evt.UserEmail != "" {+ props = props.Set("user_email", evt.UserEmail)+ }+ if evt.WorkosUserID != "" {+ props = props.Set("workos_user_id", evt.WorkosUserID)+ }+ if evt.WorkosOrgID != "" {+ props = props.Set("workos_org_id", evt.WorkosOrgID)}if err := c.c.Enqueue(analytics.Track{- UserId: orgID,+ UserId: evt.OrgID,Event: "Sandbox Memory Usage",Properties: props,}); err != nil {diff --git a/internal/db/usage.go b/internal/db/usage.goindex 57a73c5a..c30a3aa3 100644--- a/internal/db/usage.go+++ b/internal/db/usage.go@@ -69,26 +69,47 @@ 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+// SandboxOwner holds the org and user details for a sandbox session.+type SandboxOwner struct {+ OrgID string+ UserID string+ UserEmail string+ WorkosUserID string+ WorkosOrgID string+}++// GetSandboxOwner returns the org and user details for a sandbox. User fields// 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) {+func (s *Store) GetSandboxOwner(ctx context.Context, sandboxID string) (SandboxOwner, error) {var orgUUID uuid.UUID- var email *string- err = s.pool.QueryRow(ctx,- `SELECT s.org_id, u.email+ var userUUID *uuid.UUID+ var email, workosUserID, workosOrgID *string+ err := s.pool.QueryRow(ctx,+ `SELECT s.org_id, s.user_id, u.email, u.workos_user_id, o.workos_org_idFROM sandbox_sessions sLEFT JOIN users u ON u.id = s.user_id+ LEFT JOIN orgs o ON o.id = s.org_idWHERE s.sandbox_id = $1ORDER BY s.started_at DESC LIMIT 1`,- sandboxID).Scan(&orgUUID, &email)+ sandboxID).Scan(&orgUUID, &userUUID, &email, &workosUserID, &workosOrgID)if err != nil {- return "", "", err+ return SandboxOwner{}, err+ }+ owner := SandboxOwner{OrgID: orgUUID.String()}+ if userUUID != nil {+ owner.UserID = userUUID.String()}if email != nil {- userEmail = *email+ owner.UserEmail = *email+ }+ if workosUserID != nil {+ owner.WorkosUserID = *workosUserID+ }+ if workosOrgID != nil {+ owner.WorkosOrgID = *workosOrgID}- return orgUUID.String(), userEmail, nil+ return owner, nil}// EndScaleEvent marks the current scale event as ended (sandbox stopped/hibernated).diff --git a/internal/worker/usage_collector.go b/internal/worker/usage_collector.goindex bd358538..6ffdc050 100644--- a/internal/worker/usage_collector.go+++ b/internal/worker/usage_collector.go@@ -90,14 +90,14 @@ func (c *UsageCollector) sample() {continue // sandbox might be shutting down}- orgID, userEmail, err := c.store.GetSandboxOwner(ctx, sb.ID)- if err != nil || orgID == "" {+ owner, err := c.store.GetSandboxOwner(ctx, sb.ID)+ if err != nil || owner.OrgID == "" {continue // no billable owner, skip}samples = append(samples, db.UsageSample{SandboxID: sb.ID,- OrgID: orgID,+ OrgID: owner.OrgID,SampledAt: now,MemoryMB: sb.MemoryMB,CPUUsec: 0, // TODO: parse from cgroup cpu.stat@@ -109,7 +109,15 @@ 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, userEmail, sb.ID, gbSeconds)+ c.segment.TrackGBSeconds(analytics.UsageEvent{+ OrgID: owner.OrgID,+ UserID: owner.UserID,+ UserEmail: owner.UserEmail,+ WorkosUserID: owner.WorkosUserID,+ WorkosOrgID: owner.WorkosOrgID,+ SandboxID: sb.ID,+ GBSeconds: gbSeconds,+ })}if len(samples) == 0 {