Skip to content

Commit

Permalink
fix: WebGL BackgroundWorker implementation & sample packages
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Mar 23, 2022
1 parent ef4eab4 commit 472cb00
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
3 changes: 2 additions & 1 deletion samples/unity-of-bugs/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"com.unity.modules.androidjni": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.screencapture": "1.0.0",
"com.unity.modules.ui": "1.0.0"
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions samples/unity-of-bugs/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
"depth": 0,
"source": "builtin",
"dependencies": {}
},
"com.unity.modules.unitywebrequest": {
"version": "1.0.0",
"depth": 0,
"source": "builtin",
"dependencies": {}
}
}
}
41 changes: 14 additions & 27 deletions src/Sentry.Unity/WebGL/SentryWebGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Sentry.Extensibility;
using Sentry.Infrastructure;
using Sentry.Internal;
using Sentry.Internal.Http;
using Sentry.Protocol.Envelopes;
using UnityEngine;
using UnityEngine.Networking;
Expand All @@ -28,6 +29,9 @@ public static void Configure(SentryUnityOptions options)

// Caching transport relies on a background thread
options.CacheDirectoryPath = null;
// Note: we need to use a custom background worker which actually doesn't work in the background
// because Unity doesn't support async (multithreading) yet. This may change in the future so let's watch
// https://docs.unity3d.com/2019.4/Documentation/ScriptReference/PlayerSettings.WebGL-threadsSupport.html
options.BackgroundWorker = new WebBackgroundWorker(options, SentryMonoBehaviour.Instance);

// Still cant' find out what's using Threads so:
Expand All @@ -53,52 +57,35 @@ public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behav
{
_options = options;
_behaviour = behaviour;
// var composer = new SdkComposer(options);
// HTTP transport is not compatible. Need to use Unity's one.
// _transport = composer.CreateTransport();
}

public bool EnqueueEnvelope(Envelope envelope)
{
// _transport.SendEnvelopeAsync(envelope, CancellationToken.None)
// .ContinueWith(r => _options.DiagnosticLogger?.LogInfo("Result of envelope capture was: {0}", r.Status));
_ = _behaviour.StartCoroutine(SendEnvelope(envelope));
return true;
}

private IEnumerator SendEnvelope(Envelope envelope)
{
var dsn = Dsn.Parse(_options.Dsn!);
var authHeader =
$"Sentry sentry_version={Sentry.Constants.ProtocolVersion}," +
$"sentry_client={UnitySdkInfo.Name}/{UnitySdkInfo.Version}," +
$"sentry_key={dsn.PublicKey}," +
(dsn.SecretKey is { } secretKey ? $"sentry_secret={secretKey}," : null) +
$"sentry_timestamp={_clock.GetUtcNow().ToUnixTimeSeconds()}";

var www = new UnityWebRequest(dsn.GetEnvelopeEndpointUri());
www.method = "POST";
www.SetRequestHeader("X-Sentry-Auth", authHeader);
var builder = new HttpRequestBuilder(_options);
var www = new UnityWebRequest();
www.url = builder.GetEnvelopeEndpointUri().ToString();
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader(builder.AuthHeaderName, builder.AuthHeader(_clock.GetUtcNow()));
// TODO is it OK to call .Wait() here in webGL?
var stream = new MemoryStream();
envelope.SerializeAsync(stream, _options.DiagnosticLogger).Wait(TimeSpan.FromSeconds(2));
stream.Flush();
www.uploadHandler = new UploadHandlerRaw(stream.ToArray());
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();

while (!www.isDone)
{
yield return null;
}
if (
www.isNetworkError || www.isHttpError
|| www.responseCode != 200)
if (www.isNetworkError || www.isHttpError || www.responseCode != 200)
{
_options.DiagnosticLogger?.LogWarning("error sending request to sentry: {0}", www.error);
}
{
_options.DiagnosticLogger?.LogDebug("Sentry sent back: {0}", www.downloadHandler.text);
_options.DiagnosticLogger?.LogWarning("error sending request to Sentry: {0}", www.error);
}

_options.DiagnosticLogger?.LogDebug("Sentry sent back: {0}", www.downloadHandler.text);
}

public Task FlushAsync(TimeSpan timeout) => Task.CompletedTask;
Expand Down

0 comments on commit 472cb00

Please sign in to comment.