Skip to content

chore(replay): Report upload retries in segment telemetry - #5337

Open
sethfowler-datadog wants to merge 1 commit into
mainfrom
seth.fowler/PANA-10393-replay-retry-telemetry
Open

sethfowler-datadog wants to merge 1 commit into
mainfrom
seth.fowler/PANA-10393-replay-retry-telemetry

Conversation

@sethfowler-datadog

Copy link
Copy Markdown
Contributor

Motivation

#5334 makes session replay uploads retry after network errors (responses with status 0), even while navigator.onLine is true. To avoid retrying forever behind a proxy that does not provide CORS headers, it retries only after the endpoint has returned at least one response with a status other than 0. This guard has a cost: the SDK never retries the first segment of a page, which contains the initial full snapshot.

We'll probably have to wait until the next major release to change this, and we need data to choose the right approach:

  • How often are uploads retried, and do the retries succeed?
  • How often does the guard prevent a retry?
  • When the guard does prevent a retry, how often is a proxy in use?

The segment telemetry can't answer these questions today. It doesn't tell whether a segment was retried, whether the endpoint had responded, or whether the SDK uses a proxy. Also, it reports successful requests only for full snapshots, so we can't see whether a retried incremental segment is eventually delivered.

This PR is part 2 of a stack of 2 PRs:

  1. Retry session replay uploads after network errors
  2. Report upload retries in segment telemetry (this PR)

Refs: PANA-10393

Changes

  • Each HttpRequestEvent has a new endpointResponded field: whether the endpoint has returned a status other than 0 so far, including the response that the event reports. The three event types are now named interfaces that share their common fields.
  • The segment telemetry metrics have three new fields:
    • endpointResponded, from the event.
    • retry, if the segment was retried: the number of failed attempts so far, and the status of the last one.
    • useProxy: whether the SDK is configured with a proxy. The configuration telemetry already reports use_proxy, but it is sampled separately, so few metrics could be matched with it.
  • The segment telemetry also reports the successful upload of a retried segment.

The SDK reports a network error that it does not retry as a success. So, with these fields, a full snapshot that is reported as a success with endpointResponded: false is an upload that failed with a network error at the transport level, but which was treated as successful by the guard and not retried.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@sethfowler-datadog
sethfowler-datadog requested review from a team as code owners November 26, 2026 13:48

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Successful retried incremental uploads reach segment telemetry while ordinary incremental successes remain suppressed. Request events carry the endpoint-response state used by the existing retry guard.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit c21b393

Base automatically changed from seth.fowler/PANA-10364-retry-replay-network-errors to main November 26, 2026 15:13
## Motivation

#5334 makes session replay uploads retry after network errors (responses with status 0), even while `navigator.onLine` is `true`. To avoid retrying forever behind a proxy that does not provide CORS headers, it retries only after the endpoint has returned at least one response with a status other than 0. This guard has a cost: the SDK never retries the first segment of a page, which contains the initial full snapshot.

We'll probably have to wait until the next major release to change this, and we need data to choose the right approach:

- How often are uploads retried, and do the retries succeed?
- How often does the guard prevent a retry, and does the SDK use a proxy when it happens?

The segment telemetry can't answer these questions today. It doesn't tell whether a segment was retried, whether the endpoint had responded, or whether the SDK uses a proxy. Also, it reports a successful upload only for full snapshots, so we can't see whether a retried incremental segment is eventually delivered.

This PR is part 2 of a stack of 2 PRs:

1. [Retry session replay uploads after network errors](#5334)
2. **Report upload retries in segment telemetry (this PR)**

Refs: PANA-10393

## Changes

- Each `HttpRequestEvent` has a new `endpointResponded` field: whether the endpoint has returned a status other than 0 so far, including the response that the event reports. The three event types are now named interfaces that share their common fields.
- The segment telemetry metrics have three new fields:
  - `endpointResponded`, from the event.
  - `retry`, if the segment was retried: the number of failed attempts so far, and the status of the last one.
  - `useProxy`: whether the SDK is configured with a `proxy`. The configuration telemetry already reports `use_proxy`, but it is sampled separately, so few metrics could be matched with it.
- The segment telemetry also reports the successful upload of a retried segment.

The SDK reports a network error that it does not retry as a success. So, with these fields, a full snapshot that is reported as a success with `endpointResponded: false` is an upload that failed with a network error, and that the guard did not retry.

## Test instructions

This script makes session replay uploads fail with a network error on two pages. On the first page, every upload fails, so the endpoint never responds. On the second page, uploads fail after the first segment has been sent, and then work again. The script then prints the segment telemetry metrics:

```bash
yarn dev-server start
cat > sandbox/test-replay-retry-telemetry.html << 'EOF'
<!doctype html>
<script src="/datadog-rum.js"></script>
<script>
  // Telemetry metrics are sent by only 1% of the SDK instances that send telemetry. Make every
  // sampling draw succeed while the SDK initializes.
  const random = Math.random
  Math.random = () => 0
  DD_RUM.init({
    clientToken: 'xxx',
    applicationId: 'xxx',
    proxy: '/proxy',
    sessionReplaySampleRate: 100,
    telemetrySampleRate: 100,
  })
  Math.random = random
</script>
<button onclick="document.body.append(document.createElement('hr'))">Add</button>
EOF
cat > test-replay-retry-telemetry.mjs << 'EOF'
import { chromium } from 'playwright'

const isReplayUpload = (url) =>
  url.pathname === '/proxy' && url.searchParams.get('ddforward')?.startsWith('/api/v2/replay')
const browser = await chromium.launch()

// Page 1: every replay upload fails with a network error (status 0), so the endpoint never responds
let page = await browser.newPage()
await page.route(isReplayUpload, (route) => route.abort('connectionreset'))
await page.goto('http://localhost:8080/test-replay-retry-telemetry.html')
await page.waitForTimeout(7000) // segment 0 fails, and is not retried
await page.unrouteAll() // otherwise, the telemetry sent as the page exits does not arrive
await page.goto('about:blank') // flush the telemetry
await page.waitForTimeout(2000)
await page.close()

// Page 2: replay uploads fail with a network error after the first segment has been sent
page = await browser.newPage()
let failing = false
await page.route(isReplayUpload, (route) => (failing ? route.abort('connectionreset') : route.continue()))
await page.goto('http://localhost:8080/test-replay-retry-telemetry.html')
await page.waitForTimeout(7000) // segment 0 is sent
failing = true
await page.click('button')
await page.waitForTimeout(8000) // segment 1 fails, and is retried
failing = false
await page.waitForTimeout(10000) // a retry succeeds
await page.unrouteAll() // otherwise, the telemetry sent as the page exits does not arrive
await page.goto('about:blank') // flush the telemetry
await page.waitForTimeout(2000)
await browser.close()
EOF
yarn dev-server intake clear
yarn node test-replay-retry-telemetry.mjs
yarn dev-server intake telemetry-events --format json |
  jq -c 'select(.telemetry.message == "Segment network request metrics") | .telemetry.metrics | {result, isFullSnapshot, endpointResponded, retry, useProxy}'
```

The output (`null` means that the field is absent):

```
{"result":"success","isFullSnapshot":true,"endpointResponded":false,"retry":null,"useProxy":true}
{"result":"success","isFullSnapshot":true,"endpointResponded":true,"retry":null,"useProxy":true}
{"result":"failure","isFullSnapshot":false,"endpointResponded":true,"retry":{"count":1,"lastFailureStatus":0},"useProxy":true}
{"result":"failure","isFullSnapshot":false,"endpointResponded":true,"retry":{"count":2,"lastFailureStatus":0},"useProxy":true}
{"result":"failure","isFullSnapshot":false,"endpointResponded":true,"retry":{"count":3,"lastFailureStatus":0},"useProxy":true}
{"result":"success","isFullSnapshot":false,"endpointResponded":true,"retry":{"count":3,"lastFailureStatus":0},"useProxy":true}
```

- Line 1 is segment 0 of the first page. Its upload failed, and the endpoint had not responded yet, so the SDK did not retry it.
- Line 2 is segment 0 of the second page, which was sent.
- Lines 3 to 5 are the three failed attempts to upload segment 1 of the second page, and line 6 is the attempt that succeeded. Before this change, line 6 was not reported.
- `useProxy` is `true` because the dev server receives the data through the `proxy` option.

Before this change, the metrics had no `endpointResponded`, `retry` or `useProxy` field.

To clean up:

```bash
yarn dev-server stop
rm sandbox/test-replay-retry-telemetry.html test-replay-retry-telemetry.mjs
```

## Checklist

- [x] Tested locally
- [ ] Tested on staging
- [x] Added unit tests for this change.
- [ ] Added e2e/integration tests for this change.
- [ ] Updated documentation and/or relevant AGENTS.md file
@sethfowler-datadog
sethfowler-datadog force-pushed the seth.fowler/PANA-10393-replay-retry-telemetry branch from c21b393 to 6ea8f33 Compare November 26, 2026 15:17
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 194.65 KiB 194.80 KiB +152 B +0.08% ✅
Rum 194.63 KiB 194.78 KiB +152 B +0.08% ✅
Rum Salesforce Profiler 8.10 KiB 8.10 KiB 0 B 0.00% ✅
Rum Profiler 8.10 KiB 8.10 KiB 0 B 0.00% ✅
Rum Salesforce Recorder 32.80 KiB 32.99 KiB +190 B +0.57% ✅
Rum Recorder 32.80 KiB 32.99 KiB +190 B +0.57% ✅
Logs 60.40 KiB 60.55 KiB +152 B +0.25% ✅
Rum Slim 150.02 KiB 150.17 KiB +152 B +0.10% ✅
Worker 24.23 KiB 24.23 KiB 0 B 0.00% ✅
Rum Shopify 226.17 KiB 226.32 KiB +152 B +0.07% ✅
Rum Shopify Profiler 8.10 KiB 8.10 KiB 0 B 0.00% ✅
Rum Shopify Recorder 4.85 KiB 5.04 KiB +190 B +3.82% ✅
Early Collection 309 B 309 B 0 B 0.00% ✅

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

No actionable defect was identified in retry-state reporting or telemetry filtering for guarded network errors, repeated failures, and eventual success after retries.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 6ea8f33

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 83.33%
• Overall Coverage: 78.07% (+0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 6ea8f33 | Docs | View more details | Give us feedback!

This branch has not been deployed

No deployments
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.

1 participant