Skip to content

Replay tunnel fails due to empty envelope #13453

Open
@johnathanludwig

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/@[email protected]_@[email protected][email protected][email protected]_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:49:18)
    at async resolve (/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_@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/@[email protected]_@[email protected]_@[email protected]_@opentelemetry+a_gixmxtpq6bgq6uyfpgkkbh5rvy/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:140:21
    at async instrumentHandle (/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@opentelemetry+a_gixmxtpq6bgq6uyfpgkkbh5rvy/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:128:27)
    at async Module.respond (/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/runtime/server/respond.js:330:20)
    at async file:///node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_@types+node@_l5lnugt2i4tpym4dqtslohvg24/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions