Skip to content

Fix duplicate telemetry recording for API calls - #10

Merged
JamesSmartCell merged 1 commit into
masterfrom
cursor/fix-duplicate-telemetry-1a88
Nov 17, 2026
Merged

JamesSmartCell merged 1 commit into
masterfrom
cursor/fix-duplicate-telemetry-1a88

Conversation

@JamesSmartCell

Copy link
Copy Markdown
Owner

Problem

Production telemetry showed each HTTP API call recording TWO api_call events:

  1. First event: {apiSuccess: false, apiResponseTime: 0}
  2. Second event: {apiSuccess: true, apiResponseTime: 102} ~165ms later

Example from production:

  • id 25: {apiMethod:'e2eTest', apiSuccess:false, apiResponseTime:0} at time t
  • id 26: {apiMethod:'e2eTest', apiSuccess:true, apiResponseTime:102} at t+165ms

This was for a single API call.

Root Cause

The handleApiCall function in routes.ts calls both UDP and TCP services in parallel:

const [responseUDP, responseTCP] = await Promise.all([
  service.getResponse(...),        // UDP service
  tcpService.getResponse(...),     // TCP service  
]);

Each service method internally called telemetry.recordApiCall(), resulting in:

  • UDP service: device not found → records false/0 event
  • TCP service: device responds → records true/102ms event
  • Total: 2 events for 1 HTTP call

Solution

1. Moved Telemetry Recording to HTTP Layer

Removed recordApiCall() from:

  • ASyncService.getResponse() (UDP layer)
  • ASyncTCPService.getResponse() (TCP layer)

Added single recordApiCall() in:

  • routes.ts handleApiCall() function (HTTP layer)

Now records exactly ONE event per HTTP call with the final outcome.

2. Improved Response Selection Logic

Previously: responseTCP ?? responseUDP (always preferred TCP even if it failed)

Now: Smart selection that prefers successful responses:

if (isSuccess(responseTCP)) {
  response = responseTCP;
} else if (isSuccess(responseUDP)) {
  response = responseUDP;
} else {
  response = responseTCP ?? responseUDP;  // Both failed
}

Success defined as NOT: 'No device found', 'Timed out', 'API send error', or empty string.

3. Added Regression Tests

New test file: src/__tests__/telemetry-deduplication.test.ts

Tests verify:

  • ✅ Exactly ONE event when TCP succeeds
  • ✅ Exactly ONE event when UDP succeeds
  • ✅ Exactly ONE failure event when device not found
  • ✅ Exactly ONE failure event on timeout
  • ✅ Exactly ONE failure event on API send error
  • ✅ One event per call for multiple calls
  • ✅ Response time measurement for success and failure

4. Historical Data Deduplication

Added deduplicateApiCallEvents() heuristic to tools/telemetry/analyse.ts:

  • Detects pairs: false/0 followed by true for same device+method within ~5 seconds
  • Removes the false/0 event, keeps only the successful one
  • Automatic cleanup when analyzing old telemetry data
  • Note in code explains this is for historical data only

Testing

✅ All 215 tests passed (including 11 new deduplication tests)
✅ npm run typecheck:tools passed
✅ ASCII-only log output maintained

Impact

Going forward:

  • Each API call records exactly ONE telemetry event
  • Success events include accurate response time
  • Failure events only on real timeout/error/not-connected
  • Telemetry metrics (success rate, response time) now accurate

Historical data:

  • analyse.ts tool automatically de-duplicates old double-recorded events
  • No manual cleanup needed
Open in Web Open in Cursor 

Each HTTP API call was recording TWO telemetry events because both
UDP and TCP service layers called recordApiCall() independently
when Promise.all ran them in parallel.

Root cause:
- handleApiCall in routes.ts called both service.getResponse() and
  tcpService.getResponse() via Promise.all
- Each service method internally called telemetry.recordApiCall()
- Result: false/0 event from the failing path, then true/Nms event
  from the successful path

Fix:
1. Removed all telemetry recording from service layer (ASyncService
   and ASyncTCPService getResponse methods)
2. Added single telemetry recording at HTTP handler level in routes.ts
3. Record exactly ONE event per call: success with response time when
   device replies, failure on real timeout/error/not-connected
4. Improved response selection logic to prefer successful responses
   over error messages

Added regression tests (telemetry-deduplication.test.ts):
- Verify exactly one event per API call
- Test success/failure scenarios
- Test timeout and device-not-found cases
- Test multiple concurrent calls

Added historical data deduplication in analyse.ts:
- Heuristic to de-duplicate false/0 followed by true within ~5s
- Same device+method pairs from double-recording bug
- Automatic cleanup when analyzing old telemetry data

All tests pass, ASCII-only log output maintained.

Co-authored-by: James Brown <JamesSmartCell@users.noreply.github.com>
@JamesSmartCell
JamesSmartCell merged commit d81678d into master Nov 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants