Skip to content

Commit bb9b965

Browse files
archandattaclaude
andcommitted
fix: read full per-browser telemetry stream and keep newest events on limit
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent f2f67b9 commit bb9b965

4 files changed

Lines changed: 380 additions & 403 deletions

File tree

server/cmd/api/api/events.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,22 @@ const defaultReadWindow = 5 * time.Minute
131131
const maxReadLimit = 1000
132132

133133
// ReadTelemetryEvents handles GET /telemetry/events.
134-
// Reads archived telemetry envelopes for the current session from durable S2
135-
// storage, applies category and limit filters, and returns them in ascending
136-
// sequence order. Returns an empty list when S2 storage is not configured.
134+
// Reads archived telemetry envelopes for this browser from durable S2 storage,
135+
// applies category and limit filters, and returns them in ascending sequence
136+
// order. Returns an empty list when S2 storage is not configured.
137137
func (s *ApiService) ReadTelemetryEvents(ctx context.Context, req oapi.ReadTelemetryEventsRequestObject) (oapi.ReadTelemetryEventsResponseObject, error) {
138138
log := logger.FromContext(ctx)
139139

140140
if !s.s2Enabled() {
141141
return readTelemetryEventsOKResponse{}, nil
142142
}
143143

144-
startSeq := s.telemetrySession.SessionStartSeq()
145144
envs, err := events.Read(ctx, s.s2Basin, s.s2AccessToken, s.s2Stream, buildReadOptions(req.Params), log)
146145
if err != nil {
147146
log.Error("failed to read telemetry events from S2", "err", err)
148147
return oapi.ReadTelemetryEvents500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{Message: "failed to read telemetry events"}}, nil
149148
}
150149

151-
envs = dropPriorSessions(envs, startSeq)
152150
envs = filterByCategory(envs, req.Params.Category)
153151
envs = capLimit(envs, req.Params.Limit)
154152

@@ -174,21 +172,6 @@ func buildReadOptions(p oapi.ReadTelemetryEventsParams) events.ReadOptions {
174172
return opts
175173
}
176174

177-
// dropPriorSessions removes envelopes from before the current session's start.
178-
// startSeq is 0 when no session has run, in which case nothing is dropped.
179-
func dropPriorSessions(envs []events.Envelope, startSeq uint64) []events.Envelope {
180-
if startSeq == 0 {
181-
return envs
182-
}
183-
out := make([]events.Envelope, 0, len(envs))
184-
for _, e := range envs {
185-
if e.Seq >= startSeq {
186-
out = append(out, e)
187-
}
188-
}
189-
return out
190-
}
191-
192175
func filterByCategory(envs []events.Envelope, cats *[]oapi.TelemetryEventCategory) []events.Envelope {
193176
if cats == nil || len(*cats) == 0 {
194177
return envs
@@ -206,13 +189,15 @@ func filterByCategory(envs []events.Envelope, cats *[]oapi.TelemetryEventCategor
206189
return out
207190
}
208191

192+
// capLimit returns at most n envelopes, keeping the most recent when the set
193+
// exceeds the limit. Order is preserved (ascending sequence).
209194
func capLimit(envs []events.Envelope, limit *int) []events.Envelope {
210195
n := maxReadLimit
211196
if limit != nil && *limit > 0 && *limit < n {
212197
n = *limit
213198
}
214199
if len(envs) > n {
215-
return envs[:n]
200+
return envs[len(envs)-n:]
216201
}
217202
return envs
218203
}

server/cmd/api/api/events_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,6 @@ func TestReadTelemetryEventsS2Disabled(t *testing.T) {
174174
assert.JSONEq(t, `{"events":[]}`, rec.Body.String())
175175
}
176176

177-
func TestDropPriorSessions(t *testing.T) {
178-
t.Parallel()
179-
envs := []events.Envelope{{Seq: 1}, {Seq: 2}, {Seq: 3}}
180-
181-
got := dropPriorSessions(envs, 2)
182-
require.Len(t, got, 2)
183-
assert.Equal(t, uint64(2), got[0].Seq)
184-
185-
// startSeq 0 means no session ran; keep everything.
186-
assert.Len(t, dropPriorSessions(envs, 0), 3)
187-
}
188-
189177
func TestFilterByCategory(t *testing.T) {
190178
t.Parallel()
191179
mk := func(c oapi.TelemetryEventCategory) events.Envelope {
@@ -201,12 +189,16 @@ func TestFilterByCategory(t *testing.T) {
201189

202190
func TestCapLimit(t *testing.T) {
203191
t.Parallel()
204-
envs := make([]events.Envelope, 5)
192+
envs := []events.Envelope{{Seq: 1}, {Seq: 2}, {Seq: 3}, {Seq: 4}, {Seq: 5}}
205193

206194
assert.Len(t, capLimit(envs, nil), 5, "no limit returns all under the ceiling")
207195

208196
three := 3
209-
assert.Len(t, capLimit(envs, &three), 3)
197+
got := capLimit(envs, &three)
198+
require.Len(t, got, 3)
199+
// The most recent events are kept, in ascending order.
200+
assert.Equal(t, uint64(3), got[0].Seq)
201+
assert.Equal(t, uint64(5), got[2].Seq)
210202
}
211203

212204
func TestBuildReadOptions(t *testing.T) {

0 commit comments

Comments
 (0)