< Summary

Class:MainScripts.DCL.Helpers.SentryUtils.SentryWebRequestMonitor
Assembly:SentryUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/SentryUtils/SentryWebRequestMonitor.cs
Covered lines:2
Uncovered lines:30
Coverable lines:32
Total lines:79
Line coverage:6.2% (2 of 32)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TrackWebRequest(...)0%2100%
SetSpanStatus(...)0%2101400%
GetData(...)0%20400%
Dispose()0%110100%
Initialize()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/SentryUtils/SentryWebRequestMonitor.cs

#LineLine coverage
 1using DCL;
 2using Sentry;
 3using UnityEngine;
 4using UnityEngine.Networking;
 5
 6namespace 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        {
 015            var webRequest = webRequestOp.webRequest;
 016            var transaction = SentrySdk.StartTransaction(endPointTemplate, webRequest.method);
 17
 018            SentrySdk.ConfigureScope(s =>
 19            {
 020                s.Transaction = transaction;
 21
 022                s.Request = new Request
 23                {
 24                    Method = webRequest.method,
 25                    Url = webRequest.url,
 26                    QueryString = queryString,
 27                    Data = GetData(webRequest, data)
 28                };
 029            });
 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
 035                SetSpanStatus(transaction, webRequest);
 036                if (finishTransactionOnWebRequestFinish)
 037                    transaction.Finish();
 038            }
 39
 040            webRequestOp.completed += WebRequestOpCompleted;
 041            return new DisposableTransaction(transaction);
 42        }
 43
 44        private static void SetSpanStatus(ITransaction transaction, UnityWebRequest webRequest)
 45        {
 046            if (webRequest.WebRequestTimedOut())
 047                transaction.Status = SpanStatus.DeadlineExceeded;
 48            else
 49            {
 050                transaction.Status = webRequest.responseCode switch
 51                                     {
 052                                         200 => SpanStatus.Ok,
 053                                         404 => SpanStatus.NotFound,
 054                                         400 => SpanStatus.InvalidArgument,
 055                                         403 => SpanStatus.PermissionDenied,
 056                                         409 => SpanStatus.Aborted,
 057                                         500 => SpanStatus.InternalError,
 058                                         503 => SpanStatus.Unavailable,
 059                                         401 => SpanStatus.Unauthenticated,
 060                                         429 => SpanStatus.ResourceExhausted,
 061                                         504 => SpanStatus.DeadlineExceeded,
 062                                         _ => SpanStatus.UnknownError
 63                                     };
 64            }
 065        }
 66
 67        private static object GetData(UnityWebRequest webRequest, string data)
 68        {
 069            if (data != null)
 070                return data.Length <= DATA_LIMIT ? data : data.Substring(0, DATA_LIMIT);
 71
 072            return webRequest.uploadHandler?.data;
 73        }
 74
 49375        public void Dispose() { }
 76
 49377        public void Initialize() { }
 78    }
 79}