77using Sentry . Extensibility ;
88using Sentry . Infrastructure ;
99using Sentry . Internal ;
10+ using Sentry . Internal . Http ;
1011using Sentry . Protocol . Envelopes ;
1112using UnityEngine ;
1213using UnityEngine . Networking ;
@@ -28,6 +29,9 @@ public static void Configure(SentryUnityOptions options)
2829
2930 // Caching transport relies on a background thread
3031 options . CacheDirectoryPath = null ;
32+ // Note: we need to use a custom background worker which actually doesn't work in the background
33+ // because Unity doesn't support async (multithreading) yet. This may change in the future so let's watch
34+ // https://docs.unity3d.com/2019.4/Documentation/ScriptReference/PlayerSettings.WebGL-threadsSupport.html
3135 options . BackgroundWorker = new WebBackgroundWorker ( options , SentryMonoBehaviour . Instance ) ;
3236
3337 // Still cant' find out what's using Threads so:
@@ -53,52 +57,35 @@ public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behav
5357 {
5458 _options = options ;
5559 _behaviour = behaviour ;
56- // var composer = new SdkComposer(options);
57- // HTTP transport is not compatible. Need to use Unity's one.
58- // _transport = composer.CreateTransport();
5960 }
6061
6162 public bool EnqueueEnvelope ( Envelope envelope )
6263 {
63- // _transport.SendEnvelopeAsync(envelope, CancellationToken.None)
64- // .ContinueWith(r => _options.DiagnosticLogger?.LogInfo("Result of envelope capture was: {0}", r.Status));
6564 _ = _behaviour . StartCoroutine ( SendEnvelope ( envelope ) ) ;
6665 return true ;
6766 }
6867
6968 private IEnumerator SendEnvelope ( Envelope envelope )
7069 {
71- var dsn = Dsn . Parse ( _options . Dsn ! ) ;
72- var authHeader =
73- $ "Sentry sentry_version={ Sentry . Constants . ProtocolVersion } ," +
74- $ "sentry_client={ UnitySdkInfo . Name } /{ UnitySdkInfo . Version } ," +
75- $ "sentry_key={ dsn . PublicKey } ," +
76- ( dsn . SecretKey is { } secretKey ? $ "sentry_secret={ secretKey } ," : null ) +
77- $ "sentry_timestamp={ _clock . GetUtcNow ( ) . ToUnixTimeSeconds ( ) } ";
78-
79- var www = new UnityWebRequest ( dsn . GetEnvelopeEndpointUri ( ) ) ;
80- www . method = "POST" ;
81- www . SetRequestHeader ( "X-Sentry-Auth" , authHeader ) ;
70+ var builder = new HttpRequestBuilder ( _options ) ;
71+ var www = new UnityWebRequest ( ) ;
72+ www . url = builder . GetEnvelopeEndpointUri ( ) . ToString ( ) ;
73+ www . method = UnityWebRequest . kHttpVerbPOST ;
74+ www . SetRequestHeader ( builder . AuthHeaderName , builder . AuthHeader ( _clock . GetUtcNow ( ) ) ) ;
75+ // TODO is it OK to call .Wait() here in webGL?
8276 var stream = new MemoryStream ( ) ;
8377 envelope . SerializeAsync ( stream , _options . DiagnosticLogger ) . Wait ( TimeSpan . FromSeconds ( 2 ) ) ;
8478 stream . Flush ( ) ;
8579 www . uploadHandler = new UploadHandlerRaw ( stream . ToArray ( ) ) ;
8680 www . downloadHandler = new DownloadHandlerBuffer ( ) ;
8781 yield return www . SendWebRequest ( ) ;
8882
89- while ( ! www . isDone )
90- {
91- yield return null ;
92- }
93- if (
94- www . isNetworkError || www . isHttpError
95- || www . responseCode != 200 )
83+ if ( www . isNetworkError || www . isHttpError || www . responseCode != 200 )
9684 {
97- _options . DiagnosticLogger ? . LogWarning ( "error sending request to sentry: {0}" , www . error ) ;
98- }
99- {
100- _options . DiagnosticLogger ? . LogDebug ( "Sentry sent back: {0}" , www . downloadHandler . text ) ;
85+ _options . DiagnosticLogger ? . LogWarning ( "error sending request to Sentry: {0}" , www . error ) ;
10186 }
87+
88+ _options . DiagnosticLogger ? . LogDebug ( "Sentry sent back: {0}" , www . downloadHandler . text ) ;
10289 }
10390
10491 public Task FlushAsync ( TimeSpan timeout ) => Task . CompletedTask ;
0 commit comments