7
7
using Sentry . Extensibility ;
8
8
using Sentry . Infrastructure ;
9
9
using Sentry . Internal ;
10
+ using Sentry . Internal . Http ;
10
11
using Sentry . Protocol . Envelopes ;
11
12
using UnityEngine ;
12
13
using UnityEngine . Networking ;
@@ -28,6 +29,9 @@ public static void Configure(SentryUnityOptions options)
28
29
29
30
// Caching transport relies on a background thread
30
31
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
31
35
options . BackgroundWorker = new WebBackgroundWorker ( options , SentryMonoBehaviour . Instance ) ;
32
36
33
37
// Still cant' find out what's using Threads so:
@@ -53,52 +57,35 @@ public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behav
53
57
{
54
58
_options = options ;
55
59
_behaviour = behaviour ;
56
- // var composer = new SdkComposer(options);
57
- // HTTP transport is not compatible. Need to use Unity's one.
58
- // _transport = composer.CreateTransport();
59
60
}
60
61
61
62
public bool EnqueueEnvelope ( Envelope envelope )
62
63
{
63
- // _transport.SendEnvelopeAsync(envelope, CancellationToken.None)
64
- // .ContinueWith(r => _options.DiagnosticLogger?.LogInfo("Result of envelope capture was: {0}", r.Status));
65
64
_ = _behaviour . StartCoroutine ( SendEnvelope ( envelope ) ) ;
66
65
return true ;
67
66
}
68
67
69
68
private IEnumerator SendEnvelope ( Envelope envelope )
70
69
{
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?
82
76
var stream = new MemoryStream ( ) ;
83
77
envelope . SerializeAsync ( stream , _options . DiagnosticLogger ) . Wait ( TimeSpan . FromSeconds ( 2 ) ) ;
84
78
stream . Flush ( ) ;
85
79
www . uploadHandler = new UploadHandlerRaw ( stream . ToArray ( ) ) ;
86
80
www . downloadHandler = new DownloadHandlerBuffer ( ) ;
87
81
yield return www . SendWebRequest ( ) ;
88
82
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 )
96
84
{
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 ) ;
101
86
}
87
+
88
+ _options . DiagnosticLogger ? . LogDebug ( "Sentry sent back: {0}" , www . downloadHandler . text ) ;
102
89
}
103
90
104
91
public Task FlushAsync ( TimeSpan timeout ) => Task . CompletedTask ;
0 commit comments