Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replay tunnel fails due to empty envelope #13453

Open
3 tasks done
johnathanludwig opened this issue Aug 23, 2024 · 7 comments · May be fixed by #14137
Open
3 tasks done

Replay tunnel fails due to empty envelope #13453

johnathanludwig opened this issue Aug 23, 2024 · 7 comments · May be fixed by #14137
Labels
Package: browser Issues related to the Sentry Browser SDK Package: sveltekit Issues related to the Sentry SvelteKit SDK

Comments

@johnathanludwig
Copy link

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
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 23, 2024
@github-actions github-actions bot added the Package: svelte Issues related to the Sentry Svelte SDK label Aug 23, 2024
@andreiborza
Copy link
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.

@johnathanludwig
Copy link
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/@[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 (/Users/johnathanludwig/git/frontend/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 /Users/johnathanludwig/git/frontend/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 (/Users/johnathanludwig/git/frontend/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 (/Users/johnathanludwig/git/frontend/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:///Users/johnathanludwig/git/frontend/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
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

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 26, 2024
@andreiborza
Copy link
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,
});

@johnathanludwig
Copy link
Author

Disabling compression does resolve the issue. Thank you for the help with that.

Here are the server startup logs when compression was still enabled:

Sentry Logger [log]: Initializing Sentry: process: 19324, thread: main.
Sentry Logger [log]: Integration installed: InboundFilters
Sentry Logger [log]: Integration installed: FunctionToString
Sentry Logger [log]: Integration installed: LinkedErrors
Sentry Logger [log]: Integration installed: RequestData
Sentry Logger [log]: Integration installed: Console
Sentry Logger [log]: Integration installed: Http
Sentry Logger [log]: Integration installed: NodeFetch
Sentry Logger [log]: Integration installed: OnUncaughtException
Sentry Logger [log]: Integration installed: OnUnhandledRejection
Sentry Logger [log]: Integration installed: ContextLines
Sentry Logger [log]: Integration installed: LocalVariablesAsync
Sentry Logger [log]: Integration installed: Context
Sentry Logger [log]: Integration installed: Modules
Sentry Logger [log]: Integration installed: Express
Sentry Logger [log]: Integration installed: Fastify
Sentry Logger [log]: Integration installed: Graphql
Sentry Logger [log]: Integration installed: Mongo
Sentry Logger [log]: Integration installed: Mongoose
Sentry Logger [log]: Integration installed: Mysql
Sentry Logger [log]: Integration installed: Mysql2
Sentry Logger [log]: Integration installed: Redis
Sentry Logger [log]: Integration installed: Postgres
Sentry Logger [log]: Integration installed: Nest
Sentry Logger [log]: Integration installed: Hapi
Sentry Logger [log]: Integration installed: Koa
Sentry Logger [log]: Integration installed: Connect
Sentry Logger [log]: Integration installed: RewriteFrames
Sentry Logger [log]: Integration installed: ProfilingIntegration
Sentry Logger [log]: Running in CommonJS mode.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for diag v1.9.0.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for trace v1.9.0.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for context v1.9.0.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for propagation v1.9.0.
Sentry Logger [log]: [Tracing] Starting sampled root span
  op: http.server
  name: GET /(unprotected)/login
  ID: 6cd4b34ad8046c2e
Sentry Logger [log]: [Tracing] Finishing "http.server" root span "GET /(unprotected)/login" with ID 6cd4b34ad8046c2e
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: d587828578ab7951
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: 51e6c9757e8effd3
  parent ID: d587828578ab7951
  root ID: d587828578ab7951
  root op: http.server
  root description: POST /api/errors
Sentry Logger [log]: [Tracing] Finishing "< unknown op >" span "HTTP POST" with ID 51e6c9757e8effd3
Sentry Logger [log]: SpanExporter has 1 unsent spans remaining
Sentry Logger [log]: [Tracing] Finishing "http.server" root span "POST /api/errors" with ID d587828578ab7951
Sentry Logger [log]: SpanExporter exported 2 spans, 0 unsent spans remaining
Sentry Logger [log]: [Tracing] Starting sampled root span
  op: http.server
  name: POST /api/errors
  ID: 4592d3baeb001a91
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/@[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 (/Users/johnathanludwig/git/frontend/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 /Users/johnathanludwig/git/frontend/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 (/Users/johnathanludwig/git/frontend/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 (/Users/johnathanludwig/git/frontend/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:///Users/johnathanludwig/git/frontend/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
Sentry Logger [log]: [Tracing] Finishing "http.server" root span "POST /api/errors" with ID 4592d3baeb001a91
Sentry Logger [log]: SpanExporter exported 1 spans, 0 unsent spans remaining

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 26, 2024
@andreiborza
Copy link
Member

Right, so TextDecoder can't deal with compressed zlib data. We'll have to update our docs to show how to deal with that.

For now, I suggest to either turn compression off or adapt your tunnel route to deal with the compressed data. Here's an example from our integration tests.

@johnathanludwig
Copy link
Author

Great, thanks for the help!

@andreiborza
Copy link
Member

Let's keep this open, I added it to our backlog so we can work on it.

@andreiborza andreiborza reopened this Aug 27, 2024
@timfish timfish linked a pull request Oct 30, 2024 that will close this issue
@Lms24 Lms24 added Package: sveltekit Issues related to the Sentry SvelteKit SDK Package: core Issues related to the Sentry Core SDK and removed Package: svelte Issues related to the Sentry Svelte SDK Package: core Issues related to the Sentry Core SDK labels Dec 10, 2024
@Lms24 Lms24 added the Package: browser Issues related to the Sentry Browser SDK label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: browser Issues related to the Sentry Browser SDK Package: sveltekit Issues related to the Sentry SvelteKit SDK
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants