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

Sentry is not initialized on server-side for SSR Astro project #9463

Closed
3 tasks done
aldy505 opened this issue Nov 7, 2023 · 7 comments
Closed
3 tasks done

Sentry is not initialized on server-side for SSR Astro project #9463

aldy505 opened this issue Nov 7, 2023 · 7 comments
Labels
Package: astro Issues related to the Sentry Astro SDK Type: Bug

Comments

@aldy505
Copy link

aldy505 commented Nov 7, 2023

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which SDK are you using?

@sentry/astro

SDK Version

7.77.0

Framework Version

3.4.2

Link to Sentry event

No response

SDK Setup

See repro below

Steps to Reproduce

See repro on https://github.com/aldy505/sentry-astro-endpoints-reproduction

Try for development build:

Run npm run dev, then curl http://localhost:4321/sentry-endpoints/hello. Check terminal/console, expect to see:

Tracing extension 'startTransaction' is missing. You should 'init' the SDK before calling 'startTransaction'

Then try for production build:

Run npm run build, execute NODE_ENV=production node ./dist/server/entry.mjs, then curl http://localhost:4321/sentry-endpoints/hello. Check terminal/console, expect to see:

Tracing extension 'startTransaction' is missing. You should 'init' the SDK before calling 'startTransaction'

Expected Result

Sentry is initialized and event is sent.

Actual Result

Tracing extension 'startTransaction' is missing. You should 'init' the SDK before calling 'startTransaction'
@github-actions github-actions bot added the Package: astro Issues related to the Sentry Astro SDK label Nov 7, 2023
@Lms24
Copy link
Member

Lms24 commented Nov 7, 2023

Hi @aldy505 thanks for opening this issue ad

I was able to reproduce this issue but only when the curl request is the first request that hits the Astro server. Seems like the SDK is only initialized when the first page load request is sent to the server (for instance, when you open the page in your browser). After this first request, the curl request also creates a transaction. I think that this happens because we inject the SDK code on the server side into the page-ssr bundle:

if (pathToServerInit) {
options.debug && console.log(`[sentry-astro] Using ${pathToServerInit} for server init.`);
injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit));
} else {
options.debug && console.log('[sentry-astro] Using default server init.');
injectScript('page-ssr', buildServerSnippet(options || {}));
}

This probably only is executed one a pageload request comes in.

I think, overall, this is not ideal so we should investigate if we can use another hook to initialize the SDK. However, given that Astro isn't primarily used as a general web service/API, I think initializing the SDK on the first page load request is somewhat acceptable.

Related: I just merged #9445 which adds a Sentry middleware you can register in src/middleware.ts. This will ship in the next release which will probably happen today. The middleware will automatically create a server-side transaction for each incoming request. However, it doesn't change anything in regards to the initialization problem.

@Lms24
Copy link
Member

Lms24 commented Nov 7, 2023

Oh furthermore, the reason why world.ts doesn't log the warning is because in the trace function we check for the SDK initialization (indirectly). However, this only removes the warning and it doesn't change the behaviour w.r.t creating a transaction.

@Zellerich
Copy link

Zellerich commented Nov 8, 2023

We have a similar problem since we upgraded to next 14.
Once starting the local dev enviroment the console is spammed with
"Tracing extension 'startTransaction' is missing. You should 'init' the SDK before calling 'startTransaction'"

There is nothing special in either the sentry.client.config.ts or the sentry.server.config.ts (they are the same)

import * as Sentry from "@sentry/nextjs";

Sentry.init({
  environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT || "unknown",
  dsn: "*****",
});

EDIT: It seems that the latest next canary version seems to fix it (14.0.2-canary.20)

@Lms24
Copy link
Member

Lms24 commented Nov 8, 2023

@Zellerich thanks for letting us know! If this turns out not to be fixed with the next canary version, please open a separate issue as our instrumentation for NextJS differs from the one in Astro. Thank you!

@switz
Copy link

switz commented Nov 13, 2023

I'm also seeing this in next.js on canary/latest and the latest sentry packages.

@Lms24
Copy link
Member

Lms24 commented Nov 15, 2023

Please open a new issue, this issue is related to Astro and has nothing to do with NextJS. Thank you.

@Lms24
Copy link
Member

Lms24 commented Nov 28, 2023

Hi, given that #9444 is complete and released with 7.82.0, as well as the fact that at the moment, we can't do a lot about initializing the server side earlier (i.e. before the first pageload request), I'm going to close this issue. Please let me know if it should be reopened. Thanks!

@Lms24 Lms24 closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: astro Issues related to the Sentry Astro SDK Type: Bug
Projects
Archived in project
Development

No branches or pull requests

4 participants