| | 1 | | using DCL; |
| | 2 | | using Sentry; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | |
|
| | 6 | | namespace MainScripts.DCL.Helpers.SentryUtils |
| | 7 | | { |
| | 8 | | public class SentryWebRequestMonitor : IWebRequestMonitor |
| | 9 | | { |
| | 10 | | private const int DATA_LIMIT = 150; |
| | 11 | |
|
| | 12 | | public DisposableTransaction TrackWebRequest(UnityWebRequestAsyncOperation webRequestOp, string endPointTemplate |
| | 13 | | string data = null, bool finishTransactionOnWebRequestFinish = false) |
| | 14 | | { |
| 0 | 15 | | var webRequest = webRequestOp.webRequest; |
| 0 | 16 | | var transaction = SentrySdk.StartTransaction(endPointTemplate, webRequest.method); |
| | 17 | |
|
| 0 | 18 | | SentrySdk.ConfigureScope(s => |
| | 19 | | { |
| 0 | 20 | | s.Transaction = transaction; |
| | 21 | |
|
| 0 | 22 | | s.Request = new Request |
| | 23 | | { |
| | 24 | | Method = webRequest.method, |
| | 25 | | Url = webRequest.url, |
| | 26 | | QueryString = queryString, |
| | 27 | | Data = GetData(webRequest, data) |
| | 28 | | }; |
| 0 | 29 | | }); |
| | 30 | |
|
| | 31 | | void WebRequestOpCompleted(AsyncOperation asyncOperation) |
| | 32 | | { |
| | 33 | | // if we don't set the span status manually it will be inferred from the errors happening while the tran |
| | 34 | | // is active: it's not correct |
| 0 | 35 | | SetSpanStatus(transaction, webRequest); |
| 0 | 36 | | if (finishTransactionOnWebRequestFinish) |
| 0 | 37 | | transaction.Finish(); |
| 0 | 38 | | } |
| | 39 | |
|
| 0 | 40 | | webRequestOp.completed += WebRequestOpCompleted; |
| 0 | 41 | | return new DisposableTransaction(transaction); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | private static void SetSpanStatus(ITransaction transaction, UnityWebRequest webRequest) |
| | 45 | | { |
| 0 | 46 | | if (webRequest.WebRequestTimedOut()) |
| 0 | 47 | | transaction.Status = SpanStatus.DeadlineExceeded; |
| | 48 | | else |
| | 49 | | { |
| 0 | 50 | | transaction.Status = webRequest.responseCode switch |
| | 51 | | { |
| 0 | 52 | | 200 => SpanStatus.Ok, |
| 0 | 53 | | 404 => SpanStatus.NotFound, |
| 0 | 54 | | 400 => SpanStatus.InvalidArgument, |
| 0 | 55 | | 403 => SpanStatus.PermissionDenied, |
| 0 | 56 | | 409 => SpanStatus.Aborted, |
| 0 | 57 | | 500 => SpanStatus.InternalError, |
| 0 | 58 | | 503 => SpanStatus.Unavailable, |
| 0 | 59 | | 401 => SpanStatus.Unauthenticated, |
| 0 | 60 | | 429 => SpanStatus.ResourceExhausted, |
| 0 | 61 | | 504 => SpanStatus.DeadlineExceeded, |
| 0 | 62 | | _ => SpanStatus.UnknownError |
| | 63 | | }; |
| | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | private static object GetData(UnityWebRequest webRequest, string data) |
| | 68 | | { |
| 0 | 69 | | if (data != null) |
| 0 | 70 | | return data.Length <= DATA_LIMIT ? data : data.Substring(0, DATA_LIMIT); |
| | 71 | |
|
| 0 | 72 | | return webRequest.uploadHandler?.data; |
| | 73 | | } |
| | 74 | |
|
| 493 | 75 | | public void Dispose() { } |
| | 76 | |
|
| 493 | 77 | | public void Initialize() { } |
| | 78 | | } |
| | 79 | | } |