Skip to content

Commit 5e3673c

Browse files
authored
batch wsh:run tevents by hour (#3181)
1 parent 1fe0fa2 commit 5e3673c

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

frontend/types/gotypes.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,8 @@ declare global {
15921592
"ai:backendtype"?: string;
15931593
"ai:local"?: boolean;
15941594
"wsh:cmd"?: string;
1595-
"wsh:haderror"?: boolean;
1595+
"wsh:errorcount"?: number;
1596+
"wsh:count"?: number;
15961597
"conn:conntype"?: string;
15971598
"conn:wsherrorcode"?: string;
15981599
"conn:errorcode"?: string;

pkg/telemetry/telemetry.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
const MaxTzNameLen = 50
2929
const ActivityEventName = "app:activity"
30+
const WshRunEventName = "wsh:run"
3031

3132
var cachedTosAgreedTs atomic.Int64
3233

@@ -196,6 +197,44 @@ func updateActivityTEvent(ctx context.Context, tevent *telemetrydata.TEvent) err
196197
})
197198
}
198199

200+
// aggregates wsh:run events per (cmd, haderror) key within the current 1-hour bucket
201+
func updateWshRunTEvent(ctx context.Context, tevent *telemetrydata.TEvent) error {
202+
eventTs := time.Now().Truncate(time.Hour).Add(time.Hour)
203+
incomingCount := tevent.Props.WshCount
204+
if incomingCount <= 0 {
205+
incomingCount = 1
206+
}
207+
return wstore.WithTx(ctx, func(tx *wstore.TxWrap) error {
208+
uuidStr := tx.GetString(
209+
`SELECT uuid FROM db_tevent WHERE ts = ? AND event = ? AND json_extract(props, '$."wsh:cmd"') IS ?`,
210+
eventTs.UnixMilli(), WshRunEventName, tevent.Props.WshCmd,
211+
)
212+
if uuidStr != "" {
213+
var curProps telemetrydata.TEventProps
214+
rawProps := tx.GetString(`SELECT props FROM db_tevent WHERE uuid = ?`, uuidStr)
215+
if rawProps != "" {
216+
if err := json.Unmarshal([]byte(rawProps), &curProps); err != nil {
217+
log.Printf("error unmarshalling wsh:run props: %v\n", err)
218+
}
219+
}
220+
curCount := curProps.WshCount
221+
if curCount <= 0 {
222+
curCount = 1
223+
}
224+
curProps.WshCount = curCount + incomingCount
225+
curProps.WshErrorCount += tevent.Props.WshErrorCount
226+
tx.Exec(`UPDATE db_tevent SET props = ? WHERE uuid = ?`, dbutil.QuickJson(curProps), uuidStr)
227+
} else {
228+
newProps := tevent.Props
229+
newProps.WshCount = incomingCount
230+
tsLocal := utilfn.ConvertToWallClockPT(eventTs).Format(time.RFC3339)
231+
tx.Exec(`INSERT INTO db_tevent (uuid, ts, tslocal, event, props) VALUES (?, ?, ?, ?, ?)`,
232+
uuid.New().String(), eventTs.UnixMilli(), tsLocal, WshRunEventName, dbutil.QuickJson(newProps))
233+
}
234+
return nil
235+
})
236+
}
237+
199238
func TruncateActivityTEventForShutdown(ctx context.Context) error {
200239
nowTs := time.Now()
201240
eventTs := nowTs.Truncate(time.Hour).Add(time.Hour)
@@ -259,6 +298,9 @@ func RecordTEvent(ctx context.Context, tevent *telemetrydata.TEvent) error {
259298
if tevent.Event == ActivityEventName {
260299
return updateActivityTEvent(ctx, tevent)
261300
}
301+
if tevent.Event == WshRunEventName {
302+
return updateWshRunTEvent(ctx, tevent)
303+
}
262304
return insertTEvent(ctx, tevent)
263305
}
264306

pkg/telemetry/telemetrydata/telemetrydata.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ type TEventProps struct {
126126
AiBackendType string `json:"ai:backendtype,omitempty"`
127127
AiLocal bool `json:"ai:local,omitempty"`
128128

129-
WshCmd string `json:"wsh:cmd,omitempty"`
130-
WshHadError bool `json:"wsh:haderror,omitempty"`
129+
WshCmd string `json:"wsh:cmd,omitempty"`
130+
WshErrorCount int `json:"wsh:errorcount,omitempty"`
131+
WshCount int `json:"wsh:count,omitempty"`
131132

132133
ConnType string `json:"conn:conntype,omitempty"`
133134
ConnWshErrorCode string `json:"conn:wsherrorcode,omitempty"`

pkg/wshrpc/wshserver/wshserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,8 @@ func (ws *WshServer) WshActivityCommand(ctx context.Context, data map[string]int
13291329
delete(data, key)
13301330
}
13311331
if strings.HasSuffix(key, "#error") {
1332-
props.WshHadError = true
1332+
props.WshCmd = strings.TrimSuffix(key, "#error")
1333+
props.WshErrorCount = 1
13331334
} else {
13341335
props.WshCmd = key
13351336
}
@@ -1339,7 +1340,7 @@ func (ws *WshServer) WshActivityCommand(ctx context.Context, data map[string]int
13391340
}
13401341
telemetry.GoUpdateActivityWrap(activityUpdate, "wsh-activity")
13411342
telemetry.GoRecordTEventWrap(&telemetrydata.TEvent{
1342-
Event: "wsh:run",
1343+
Event: telemetry.WshRunEventName,
13431344
Props: props,
13441345
})
13451346
return nil

0 commit comments

Comments
 (0)