Skip to content

[Docs] Example tunnel fails with compressed replay envelope #13453

Description

@johnathanludwig

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/sveltekit

SDK Version

8.26.0

Framework Version

Sveltekit 2.5.18

Link to Sentry event

No response

Reproduction Example/SDK Setup

Using the example provided from the documentation does not work with replay requests. The docs show using an array buffer. This works well with traces, however for replays the code error on parsing piece. In this case the envelopeBytes is 0 and envelope empty.

https://docs.sentry.io/platforms/javascript/troubleshooting/#dealing-with-ad-blockers

const SENTRY_HOST = "oXXXXXX.ingest.sentry.io";
const SENTRY_PROJECT_IDS = ["123456"];

export const POST: RequestHandler = async ({ request }) => {
  try {
    const envelopeBytes = await request.arrayBuffer();
    const envelope = new TextDecoder().decode(envelopeBytes);
    console.log(envelopeBytes, envelope);
    const piece = envelope.split('\n')[0];
    const header = JSON.parse(piece);
    const dsn = new URL(header['dsn']);
    const project_id = dsn.pathname?.replace('/', '');

    if (dsn.hostname !== SENTRY_HOST) {
      throw new Error(`Invalid sentry hostname: ${dsn.hostname}`);
    }

    if (!project_id || !SENTRY_PROJECT_IDS.has(project_id)) {
      throw new Error(`Invalid sentry project id: ${project_id}`);
    }

    const upstream_sentry_url = `https://${SENTRY_HOST}/api/${project_id}/envelope/`;
    await fetch(upstream_sentry_url, {
      method: 'POST',
      body: envelopeBytes,
    });

    return json({}, { status: 200 });
  } catch (error) {
    console.error('error tunneling to sentry', error);
    return json({ error: 'error tunneling to sentry' }, { status: 500 });
  }
};

Steps to Reproduce

  1. Create an API route, eg /api/errors/+server.ts.
  2. Export a POST api.
  3. Configure sentry client to tunnel through /api/errors.

Expected Result

The replays get sent to sentry successfully.

Actual Result

Traces are tunneled. Replays result in this error:

// console.log(envelopeBytes, envelope);
ArrayBuffer { [Uint8Contents]: <>, byteLength: 0 }

error tunneling to sentry SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at POST (/src/routes/api/errors/+server.ts:17:25)
    at async Module.render_endpoint (/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:49:18)
    at async resolve (/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/respond.js:454:17)
    at async documentPolicy (/src/lib/hooks/document-policy.ts:6:20)
    at async /node_modules/.pnpm/@sentry+sveltekit@8.26.0_@opentelemetry+api@1.9.0_@opentelemetry+core@1.25.1_@opentelemetry+a_gixmxtpq6bgq6uyfpgkkbh5rvy/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:140:21
    at async instrumentHandle (/node_modules/.pnpm/@sentry+sveltekit@8.26.0_@opentelemetry+api@1.9.0_@opentelemetry+core@1.25.1_@opentelemetry+a_gixmxtpq6bgq6uyfpgkkbh5rvy/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:128:27)
    at async Module.respond (/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/respond.js:330:20)
    at async file:///node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 23, 2024
  2. andreiborza commented on Aug 26, 2024

    @andreiborza
    Member

    Hello, thanks for writing in.

    The envelopes should not be empty for replays. You could add a guard to exit out early if envelopeBytes is empty, but I'm curious why you're getting empty envelopes to begin with.

    Please set debug: true in Sentry.init and paste your logs.

  3. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 26, 2024
  4. johnathanludwig commented on Aug 26, 2024

    @johnathanludwig
    Author

    Yes the body of the request is there so I'm not sure why I can't get anything out of it on the server side.

    Image

    Client side debug: https://gist.github.com/johnathanludwig/1bcefb76f2609a2b44eb3820dbdea26e

    Server side:

    Sentry Logger [log]: [Tracing] Starting sampled root span
      op: http.server
      name: GET /(unprotected)/login
      ID: 4e95db40cdd3ecc2
    Sentry Logger [log]: [Tracing] Finishing "http.server" root span "GET /(unprotected)/login" with ID 4e95db40cdd3ecc2
    Sentry Logger [log]: SpanExporter exported 1 spans, 0 unsent spans remaining
    Sentry Logger [log]: [Tracing] Starting sampled root span
      op: http.server
      name: POST /api/errors
      ID: 4d39a562acc53f42
    Sentry Logger [log]: [Tracing] Inheriting parent's sampled decision for HTTP POST: true
    Sentry Logger [log]: [Tracing] Starting sampled span
      op: < unknown op >
      name: HTTP POST
      ID: b8cf5def993c2314
      parent ID: 4d39a562acc53f42
      root ID: 4d39a562acc53f42
      root op: http.server
      root description: POST /api/errors
    Sentry Logger [log]: [Tracing] Finishing "< unknown op >" span "HTTP POST" with ID b8cf5def993c2314
    Sentry Logger [log]: SpanExporter has 1 unsent spans remaining
    Sentry Logger [log]: [Tracing] Finishing "http.server" root span "POST /api/errors" with ID 4d39a562acc53f42
    Sentry Logger [log]: SpanExporter exported 2 spans, 0 unsent spans remaining
    Sentry Logger [log]: Flushing client reports based on interval.
    Sentry Logger [log]: Flushing outcomes...
    Sentry Logger [log]: No outcomes to send
    Sentry Logger [log]: [Tracing] Starting sampled root span
      op: http.server
      name: POST /api/errors
      ID: 3bc95f07de7ced89
    error tunneling to sentry SyntaxError: Unexpected end of JSON input
        at JSON.parse (<anonymous>)
        at POST (/Users/johnathanludwig/git/frontend/apps/web/src/routes/api/errors/+server.ts:16:25)
        at async Module.render_endpoint (/Users/johnathanludwig/git/frontend/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:49:18)
        at async resolve (/Users/johnathanludwig/git/frontend/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/respond.js:454:17)
        at async /Users/johnathanludwig/git/frontend/node_modules/.pnpm/@sentry+sveltekit@8.26.0_@opentelemetry+api@1.9.0_@opentelemetry+core@1.25.1_@opentelemetry+a_gixmxtpq6bgq6uyfpgkkbh5rvy/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:140:21
        at async instrumentHandle (/Users/johnathanludwig/git/frontend/node_modules/.pnpm/@sentry+sveltekit@8.26.0_@opentelemetry+api@1.9.0_@opentelemetry+core@1.25.1_@opentelemetry+a_gixmxtpq6bgq6uyfpgkkbh5rvy/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:128:27)
        at async Module.respond (/Users/johnathanludwig/git/frontend/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/respond.js:330:20)
        at async file:///Users/johnathanludwig/git/frontend/node_modules/.pnpm/@sveltejs+kit@2.5.18_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.3_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22
    Sentry Logger [log]: [Tracing] Finishing "http.server" root span "POST /api/errors" with ID 3bc95f07de7ced89
    Sentry Logger [log]: SpanExporter exported 1 spans, 0 unsent spans remaining
    
  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 26, 2024
  6. andreiborza commented on Aug 26, 2024

    @andreiborza
    Member

    Could you please amend the server side debug log with the logs that occur on app startup?

    Also, does this happen if you disable compression?

    Sentry.replayIntegration({
      useCompression: false,
    });
    
  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 26, 2024
  8. 33 remaining items

  9. Lms24 commented on Jun 6, 2025

    @Lms24
    Member

    @jkittner do you mean the kitql proxy solution (#13453 (comment))? If so, I'm not entirely sure how the library handles proxying, so chances are that somewhere in there the compressed body is consumed and an error occurs?

    I agree that the tunneling documentation is far from great and we need better examples. (Ideally, we can provide a tunnelRoute option just like in the NextJS SDK in more meta framework SDKs and a base proxy building block for lower level SDKs) Unfortunately, we didn't get to this yet, see #14248

  10. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 6, 2025
  11. jkittner commented on Jun 6, 2025

    @jkittner

    Yes, the kitql solution only works w/o compression. For now disabling it solved the problem for me. I subscribed to the other issue as well.

  12. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 6, 2025
  13. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 9, 2025
  14. changed the title [-]Replay tunnel fails due to empty envelope[/-] [+][Docs] Example tunnel fails with compressed replay envelope[/+] on Aug 5, 2025
  15. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 5, 2025
  16. Lms24 commented on Aug 7, 2025

    @Lms24
    Member

    Going to close this issue as the improvements necessary to fix are tracked in other issues.

  17. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions