getsentry / sentry-dart-plugin

A Dart Build Plugin that uploads debug symbols for Android, iOS/macOS and source maps for Web to Sentry via sentry-cli
MIT License
66 stars 29 forks source link

feat: Add urlPrefix to sentry configuration #222

Closed JoydS closed 1 month ago

JoydS commented 5 months ago

:scroll: Description

Add UrlPrefix for Source Map

:bulb: Motivation and Context

Required for subpath web app

:green_heart: How did you test it?

Closes https://github.com/getsentry/sentry-dart-plugin/issues/204

:pencil: Checklist

:crystal_ball: Next steps

buenaflor commented 5 months ago

Hey! thx for the contrib, really appreciate that :)

JoydS commented 5 months ago

@buenaflor I don't understand why the CI is not working here ?

buenaflor commented 5 months ago

Changelog entry was missing a link to the PR, fixed it now

buenaflor commented 5 months ago

I'd like to wait for https://github.com/getsentry/sentry-dart-plugin/pull/198 first before getting to this PR

buenaflor commented 5 months ago

@JoydS we merged the other pr and can continue here, there should be some changes to the code that you also need to adjust

JoydS commented 5 months ago

@JoydS we merged the other pr and can continue here, there should be some changes to the code that you also need to adjust

Can you give me more details ?

buenaflor commented 5 months ago

@JoydS I think resolving the merge conflics is all you need to do here

JoydS commented 5 months ago

@buenaflor to be honnest for now I can't make it work.

Do you have an idea where could be the issue ?

Capture d’écran 2024-04-16 à 18 17 28

I checked my source maps that seems good

Capture d’écran 2024-04-16 à 18 21 43

But I still don't have the trace.

buenaflor commented 5 months ago

Does it not work even when not using the url prefix?

JoydS commented 5 months ago

When I do not use url prefix, my sources maps are wrong. But when I use it, sentry find my sources maps but can't unminify (I have no error but I don't have really more informations).

buenaflor commented 4 months ago

Can you follow this and see if it works?

https://github.com/getsentry/sentry-dart/issues/897#issuecomment-2059250182

This text was written for the plugin version 1.7.1

JoydS commented 4 months ago

Hello @buenaflor

I just did, and it's not working.

Here is my config :

sentry:
  dsn: "https://XXX@ZZZ.ingest.sentry.io/YYY"
  upload_source_maps: true
  project: flutter
  org: yumi-q2
  auth_token: XXXXXX
  ignore_missing: true
  upload_sources: true
  upload_debug_symbols: true
  url_prefix: "~/app/"

Here is my error :

Capture d’écran 2024-04-24 à 15 45 43

I do have this message, is it a requirement to have my github repository set-up on sentry ?

Capture d’écran 2024-04-24 à 15 46 28
buenaflor commented 4 months ago

I've received some information that uploading debug symbols might not work with sentry-cli versions below 2.31.0 due to some issues.

our newest dart plugin 2.0.0 does have 2.31.0 which should have this fixed, maybe that would work?

JoydS commented 4 months ago

Im already in 2.0.0.

Here is my logs :

[❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚] 0% reading config values..        
#21 2.402 retrieving config from pubspec.yaml
#21 2.444 
#21 2.444 Downloading Sentry CLI 2.31.0 from https://downloads.sentry-cdn.com/sentry-cli/2.31.0/sentry-cli-Linux-x86_64 to .dart_tool/pub/bin/sentry_dart_plugin/sentry-cli
#21 3.038 
#21 3.038 Sentry CLI binary checksum verification passed successfully (hash: baeb5b4ca0a5e500d667087f0b7fbb2865d3b8f01896cfba5144433dbe59bebd).
#21 3.038 
#21 3.038 Sentry CLI downloaded successfully.
#21 3.054 
☑ reading config values                                                             
#21 3.055 
[❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚] 9% validating config values..                             
☑ validating config values                                                             
#21 3.063 
[❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚] 18% uploading debug symbols..                                
#21 3.754 > Found 1 debug information file
#21 3.754 
#21 3.754 > Resolved source code for 0 debug information files
#21 3.894 
#21 3.894 > Prepared debug information file for upload
#21 4.023 
#21 4.023 > Nothing to upload, all files are on the server
#21 4.133 
☑ uploading debug symbols                                                             
#21 4.413 
#21 4.414 Created release yumi@1.0.61+56
#21 4.417 
[❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚❚] 27% uploading source maps..                               
#21 4.543 > Found 10 files
#21 4.547 
#21 4.547 > Analyzing 10 sources
#21 4.568 
#21 4.569 > Rewriting sources
#21 4.950 
#21 4.950 > Adding source map references
#21 7.597 
#21 7.597 > Bundled 10 files for upload
#21 7.597 > Bundle ID: e1ba8f62-bada-58ce-bd51-2929e17b1578
#21 8.[746](https://github.com/xxx/xxx/actions/runs/8803629375/job/24162261507#step:4:751) 
#21 8.746 > Uploaded files to Sentry
#21 10.01 
#21 10.01 > File processing complete
#21 10.01 > Organization: yumi-q2
#21 10.01 > Project: flutter
#21 10.01 > Release: yumi@1.0.61+56
#21 10.01 > Dist: 56
#21 10.01 > Upload type: artifact bundle
#21 10.01 
#21 10.01 Source Map Upload Report
#21 10.01   Scripts
#21 10.01     ~/app/assets/packages/wakelock_plus/assets/no_sleep.js (sourcemap at ../../../../main.dart.js.map)
#21 10.01     ~/app/canvaskit/canvaskit.js (sourcemap at ../main.dart.js.map)
#21 10.01     ~/app/canvaskit/chromium/canvaskit.js (sourcemap at ../../main.dart.js.map)
#21 10.01     ~/app/canvaskit/skwasm.js (sourcemap at ../main.dart.js.map)
#21 10.01     ~/app/canvaskit/skwasm.worker.js (sourcemap at ../main.dart.js.map)
#21 10.01     ~/app/firebase-messaging-sw.js (sourcemap at main.dart.js.map)
#21 10.01     ~/app/flutter.js (sourcemap at flutter.js.map)
#21 10.01 
#21 10.01 ~/app/flutter_service_worker.js (sourcemap at main.dart.js.map)
#21 10.01     ~/app/main.dart.js (sourcemap at main.dart.js.map)
#21 10.01   Source Maps
#21 10.01     ~/app/main.dart.js.map
#21 10.17 
#21 10.17 > Found 89 files
#21 10.17 
#21 10.17 > Analyzing 89 sources
#21 10.17 
#21 10.17 > Rewriting sources
#21 10.17 
#21 10.17 > Adding source map references
#21 10.66 
#21 10.66 > Bundled 89 files for upload
#21 10.66 > Bundle ID: e8c3b912-bb78-5ce9-8130-670d3817aec4
#21 10.78 
#21 10.78 > Nothing to upload, all files are on the server
#21 11.97 
#21 11.97 > File processing complete
#21 11.97 > Organization: yumi-q2
#21 11.97 > Project: flutter
#21 11.97 > Release: yumi@1.0.61+56
#21 11.97 > Dist: 56
#21 11.97 > Upload type: artifact bundle
#21 11.97 
#21 11.97 Source Map Upload Report
#21 11.97   Scripts
#21 11.97     ~/lib/event_bus.dart (no sourcemap ref)
#21 11.97       - warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ~/lib/event_bus.dart)
#21 11.97     ~/lib/firebase_options.dart (no sourcemap ref)
#21 11.97       - warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ~/lib/firebase_options.dart)
#21 11.97     ~/lib/main.dart (no sourcemap ref)
#21 11.97       - warning: could not determine a source map reference (Could not auto-detect referenced sourcemap for ~/lib/main.dart)
....
....
....
#21 11.98 
☑ uploading source maps                                                             
#21 11.98 
#21 11.98 Commit integration is disabled.
#21 12.49 
#21 12.49 Finalized release yumi@1.0.61+56
#21 DONE 12.6s

Maybe the issue is from "warning: could not determine a source map reference" ?

buenaflor commented 4 months ago

@denrase can you look into this? :pray:

JoydS commented 4 months ago

Hello, Im still stuck on this subject, and it's really embarrassing... Thanks

denrase commented 4 months ago

@JoydS It seems the issue with sourcemaps is also there with sentry-cli 2.32.0. Could you check with version 2.27.0 to see if this resolves your issues?

buenaflor commented 4 months ago

Let us know if you have any updates here

JoydS commented 4 months ago

Hello @denrase @buenaflor It still doesn't work with version 2.27.0 :/

JoydS commented 3 months ago

Hello guys, It's been a while Im working on this subject, and I still have minified alerts. I really need to fix this, I can't stay like that. Thanks

denrase commented 3 months ago

@JoydS Looking into this.

denrase commented 3 months ago

I have used the plugin from your PR in the sentry flutter example app.

In the override I configured the path to your fork, which I checked out locally

  sentry_dart_plugin:
    path: ../../../forks/joyds/sentry-dart-plugin

and added the prefix to the config.

  url_prefix: ~/app/

I build the web version with:

flutter build web --source-maps

and ran the plugin

flutter pub run sentry_dart_plugin

Changed to the correct directory

cd build/web

and started a local web server

python3 -m http.server

Then I triggered a couple of issues and could see them on sentry.io

Issue
Bildschirmfoto 2024-05-21 um 12 17 02

https://sentry-sdks.sentry.io/issues/5384272416/?project=5428562&query=is%3Aunresolved+issue.priority%3A%5Bhigh%2C+medium%5D&referrer=issue-stream&statsPeriod=1h&stream_index=0

Also, the prefix seems to be there in the uploaded source maps

Source Maps
Bildschirmfoto 2024-05-21 um 12 15 57

I'm not sure if there is anything, like uploads of previous releases/files, that would interfere and give us false positives here. Could you provide a minimal setup in a public GitHub repo so we can try to reproduce this issue?

JoydS commented 3 months ago

Hello @denrase

Im going to try to give you the more information that I can.

dependency_overrides:
  sentry_dart_plugin:
    git:
      url: https://github.com/JoydS/sentry-dart-plugin.git
  url_prefix: "~/app/"

Dans ma config Dockerfile :

RUN flutter clean
RUN flutter pub get
RUN flutter build web \
    --dart-define=STAGE=${BUILD_ENV} \
    --base-href=/app/ \
    --verbose \
    --source-maps \
    --web-renderer canvaskit

# Upload source maps to sentry only on main branch
# see https://docs.sentry.io/platforms/flutter/upload-debug/#uploading-source-maps-for-flutter-web
RUN flutter packages pub run sentry_dart_plugin

# use nginx to deploy
FROM nginx:alpine

ENV PORT=8080
EXPOSE $PORT

# copy the info of the builded web app to nginx
COPY --from=build-env /app/client/app/build/web /usr/share/nginx/html/app

# copy nginx config file
COPY docker/client/webapp/nginx.conf /etc/nginx/conf.d/default.conf.template

# replace the port in the nginx config file
RUN envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf

CMD ["nginx", "-g", "daemon off;"]

Here is my uploaded source maps :

Capture d’écran 2024-05-21 à 15 42 58 Capture d’écran 2024-05-21 à 15 43 12 Capture d’écran 2024-05-21 à 15 43 25

My app is here : https://preprod.yumi.club/app/

Could you give me more informations about how it suppose to work ? Because I have the sources maps uploaded (and the path seems good), but here is one error :

Capture d’écran 2024-05-21 à 15 47 40

Im sorry but I can't create a project with the same config from scratch, I have no time for that and the config that we have is kinda complex (behind load balancer and so on).

Thanks for your help

JoydS commented 3 months ago

What I don't understand is that this URL still wrong :

Capture d’écran 2024-05-23 à 17 32 36

Is this could be the issue ?

denrase commented 3 months ago

@buenaflor @JoydS I could reproduce the issue now. Before I was still executing in the url root without prefix. I created a release with flutter build web --base-href=/foo/ --source-maps now and when starting the local web server I moved the build files into the foo folder.

Issue

Bildschirmfoto 2024-05-31 um 11 14 56

Source Maps

Bildschirmfoto 2024-05-31 um 11 14 31

The prefix is missing in the url which is shown in the ST. The symbol maps were uploaded and showing as expected.

denrase commented 3 months ago

@JoydS We have created an issue, let's see if we get some feedback.

buenaflor commented 3 months ago

@JoydS Okay sorry for the delay. The missing piece is that in the flutter sdk we have to modify the absolute path of the stackframe and add the url prefix. see my comment here

basically the absolute path of each stack frame doesn't have the url prefix which messes up symbolication with the source maps

if you have time please try it out and if everything is fine then we're good to go here

denrase commented 2 months ago

Added to docs: https://github.com/getsentry/sentry-docs/pull/10470

JoydS commented 2 months ago

Hello @buenaflor & @denrase What Im suppose to do here, Im sorry but it's not really clear for me :) Thx

buenaflor commented 2 months ago

@JoydS you need to apply the beforeSend code snippet I wrote into your SentryFlutter.init options (and change the baseUrl to your url) and then try out the url prefix from this PR.

@denrase could you also try out if it works with this

buenaflor commented 2 months ago

also let's add this to the sample when this is done @denrase

denrase commented 2 months ago

@buenaflor

Build again with the approach from above, with the addition of rewriting the stack frame like in documentation. I now have a dart stacktrace, source context is pointing to the corerct address, it's also present in the json, but the frame without source context is pointing to a different url.

Source Map Frame
Bildschirmfoto 2024-07-16 um 15 33 15 Bildschirmfoto 2024-07-16 um 15 33 26

https://sentry-sdks.sentry.io/issues/5611698860/?project=5428562&query=is%3Aunresolved+issue.priority%3A%5Bhigh%2C+medium%5D&referrer=issue-stream&statsPeriod=1h&stream_index=0

denrase commented 2 months ago

@JoydS Here's the code that's missing. You just have to use your URL and correctly insert the prefix.

options.beforeSend = (event, hint) async {
  final exceptions = event.exceptions?.map((exception) {
    final stackTrace = exception.stackTrace;
    if (stackTrace != null) {
      final frames = stackTrace.frames.map((frame) {
        const baseUrl = 'https://example.com/';
        final modifiedAbsPath = frame.absPath?.replaceFirst(
          baseUrl,
          '${baseUrl}my_prefix/',
        );
        return frame.copyWith(absPath: modifiedAbsPath);
      }).toList();
      return exception.copyWith(
        stackTrace: SentryStackTrace(frames: frames),
      );
    }
    return exception;
  }).toList();
  return event.copyWith(exceptions: exceptions ?? []);
};
JoydS commented 1 month ago

Hello @denrase

I added the script above, but I still have the issue :

Capture d’écran 2024-07-23 à 13 18 43
denrase commented 1 month ago

@JoydS You could try to debug the script and check if the prefix is set correctly. Maybe we have missed something. Sorry this is taking so long, but it's really difficult to pinpoint where the issue is.

buenaflor commented 1 month ago

@denrase let's test it again and then wrap it up in a forked branch

denrase commented 1 month ago

For running on localhost i needed to slightly update the code snippet, but again, this did work for me:

options.beforeSend = (event, hint) async {
  final exceptions = event.exceptions?.map((exception) {
    final stackTrace = exception.stackTrace;
    if (stackTrace != null) {
      final frames = stackTrace.frames.map((frame) {
        final modifiedAbsPath = frame.absPath?.replaceFirstMapped(
            RegExp(r'(http://[^/]+)(/.*)'),
            (match) => '${match.group(1)}/foo${match.group(2)}',
        );
        return frame.copyWith(absPath: modifiedAbsPath ?? frame.absPath);
      }).toList();
      return exception.copyWith(
        stackTrace: SentryStackTrace(frames: frames),
      );
    }
    return exception;
  }).toList();
  return event.copyWith(exceptions: exceptions ?? []);
};
Bildschirmfoto 2024-08-06 um 14 15 42

Just need to make sure that the source maps also have the correct prefix. Either by setting url_prefix or by moving them in the correct folder structure before running the plugin.

Bildschirmfoto 2024-08-06 um 14 10 17
buenaflor commented 1 month ago

Closing this in favour of this PR https://github.com/getsentry/sentry-dart-plugin/pull/253