getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.85k stars 1.55k forks source link

RemoteEntry.json from remote component Module Federation error CORS after integrate sentry #12971

Closed alghifarifikri closed 2 weeks ago

alghifarifikri commented 1 month ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/vue

SDK Version

8.18.0

Framework Version

Nuxt 3.11.2

Link to Sentry event

No response

SDK Setup/Reproduction Example

import * as Sentry from '@sentry/vue'
import {
  makeFetchTransport,
  moduleMetadataIntegration,
  makeMultiplexedTransport
} from '@sentry/browser'

export default defineNuxtPlugin((nuxtApp) => {
  const router = useRouter()
  const config = useRuntimeConfig()

  const EXTRA_KEY = 'ROUTE_TO'

  const transport = makeMultiplexedTransport(makeFetchTransport, (args) => {
    const event = args.getEvent()
    if (
      event &&
      event.extra &&
      EXTRA_KEY in event.extra &&
      Array.isArray(event.extra[EXTRA_KEY])
    ) {
      return event.extra[EXTRA_KEY]
    }
    return []
  })

  Sentry.init({
    Vue: nuxtApp.vueApp,
    dsn: config.public.SENTRY_DSN,
    integrations: [
      Sentry.browserTracingIntegration({ router }),
      Sentry.replayIntegration(),
      moduleMetadataIntegration()
    ],
    tracesSampleRate: config.public.NODE_ENV === 'production' ? 0.1 : 1.0,
    tracePropagationTargets: [
      'localhost',
      /^\//
    ],
    replaysSessionSampleRate: 0.1,
    replaysOnErrorSampleRate: 1.0,
    logErrors: false, // Note that this doesn't seem to work with nuxt 3
    debug: false, // Enable debug mode
    environment: config.public.NODE_ENV,
    transport,
    beforeSend: (event) => {
      console.log({ event })
      if (event?.exception?.values?.[0].stacktrace.frames) {
        const frames = event.exception.values[0].stacktrace.frames
        console.log({ frames })
        // Find the last frame with module metadata containing a DSN
        const routeTo = frames
          .filter((frame) => frame.module_metadata && frame.module_metadata.dsn)
          .map((v) => v.module_metadata)
          .slice(-1) // using top frame only - you may want to customize this according to your needs

        if (routeTo.length) {
          event.extra = {
            ...event.extra,
            [EXTRA_KEY]: routeTo
          }
        }
      }
      return event
    },
    allowUrls: [/http?:\/\/(localhost:3003)/]
  })

  nuxtApp.vueApp.mixin({
    beforeCreate() {
      this.$sentry = Sentry
    }
  })
})

Steps to Reproduce

  1. Create plugins sentry.client.js at Nuxt JS (Parent Component)
  2. add config at vite nuxt.config.ts
    vite: {
    plugins: [
      // Put the Sentry vite plugin after all other plugins
      sentryVitePlugin({
        moduleMetadata: ({ release }) => ({
          dsn: import.meta.env.SENTRY_DSN,
          release
        })
      })
    ],
    } 

    Reference : https://docs.sentry.io/platforms/javascript/guides/vue/best-practices/micro-frontends/

  3. run npm run development

Expected Result

Project is Running without error CORS

Actual Result

image

Lms24 commented 1 month ago

Hi @alghifarifikri thanks for writing in!

The Sentry SDK adds headers to outgoing requests matching the entries in your tracePropagationTargets setup. Any chance that your backend blocks the sentry-trace and baggage headers? This is the most likely scenario why people usually run into CORS errors when adding Sentry.

If that is not the reason, I'm a bit confused as to how adding the Sentry SDK would interfere with CORS.

Lms24 commented 1 month ago

To debug this further, please provide a minimal reproduction so that we can look into this on our end. Thank you!

getsantry[bot] commented 4 weeks ago

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀