internal static ResponseMessage ToCosmosResponseMessage(this DocumentServiceResponse documentServiceResponse, RequestMessage requestMessage)
        {
            Debug.Assert(requestMessage != null, nameof(requestMessage));

            ResponseMessage responseMessage = new ResponseMessage(documentServiceResponse.StatusCode, requestMessage);

            if (documentServiceResponse.ResponseBody != null)
            {
                responseMessage.Content = documentServiceResponse.ResponseBody;
            }

            if (documentServiceResponse.Headers != null)
            {
                foreach (string key in documentServiceResponse.Headers)
                {
                    responseMessage.Headers.Add(key, documentServiceResponse.Headers[key]);
                }
            }

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics = documentServiceResponse.RequestStats as CosmosClientSideRequestStatistics;
            PointOperationStatistics          pointOperationStatistics          = new PointOperationStatistics(
                activityId: responseMessage.Headers.ActivityId,
                statusCode: documentServiceResponse.StatusCode,
                subStatusCode: documentServiceResponse.SubStatusCode,
                requestCharge: responseMessage.Headers.RequestCharge,
                errorMessage: responseMessage.ErrorMessage,
                method: requestMessage?.Method,
                requestUri: requestMessage?.RequestUri,
                requestSessionToken: requestMessage?.Headers?.Session,
                responseSessionToken: responseMessage.Headers.Session,
                clientSideRequestStatistics: cosmosClientSideRequestStatistics);

            requestMessage.DiagnosticsContext.AddDiagnosticsInternal(pointOperationStatistics);
            return(responseMessage);
        }
        private static void ValidatePointOperationStatistics(PointOperationStatistics stats, DateTime startTimeUtc)
        {
            Assert.IsNotNull(stats.ActivityId);
            Assert.AreNotEqual(Guid.Empty.ToString(), stats.ActivityId);

            Assert.IsNotNull(stats.RequestUri);
            Assert.IsNotNull(stats.RequestCharge);
            if (stats.StatusCode != HttpStatusCode.RequestEntityTooLarge &&
                stats.StatusCode != HttpStatusCode.RequestTimeout)
            {
                Assert.IsTrue(stats.RequestCharge > 0);
            }

            Assert.IsNotNull(stats.Method);
            Assert.IsNotNull(stats.ResponseTimeUtc);
            Assert.IsTrue(startTimeUtc < stats.ResponseTimeUtc);
            Assert.IsTrue(stats.ResponseTimeUtc < DateTime.UtcNow);

            string info = stats.ToString();

            Assert.IsNotNull(info);
            JObject jObject    = JObject.Parse(info.ToString());
            int     statusCode = jObject["StatusCode"].ToObject <int>();

            Assert.IsTrue(statusCode > 0);
            Assert.IsNotNull(jObject["ActivityId"].ToString());
            Assert.IsNotNull(jObject["StatusCode"].ToString());
            Assert.IsNotNull(jObject["RequestCharge"].ToString());
            Assert.IsNotNull(jObject["RequestUri"].ToString());
        }
Esempio n. 3
0
        internal override void AddDiagnosticsInternal(PointOperationStatistics pointOperationStatistics)
        {
            if (pointOperationStatistics == null)
            {
                throw new ArgumentNullException(nameof(pointOperationStatistics));
            }

            this.AddResponseCount((int)pointOperationStatistics.StatusCode);
            this.AddToContextList(pointOperationStatistics);
        }
 public override void Visit(PointOperationStatistics pointOperationStatistics)
 {
     Assert.IsTrue(this.isContextVisited);
     this.isPointOperationStatisticsVisited = true;
     DiagnosticValidator.ValidatePointOperationStatistics(pointOperationStatistics, this.StartTimeUtc.Value);
     if (!pointOperationStatistics.StatusCode.IsSuccess())
     {
         this.containsFailures = true;
     }
 }
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentClientException documentClientException, RequestMessage requestMessage)
        {
            // if StatusCode is null it is a client business logic error and it never hit the backend, so throw
            if (documentClientException.StatusCode == null)
            {
                throw documentClientException;
            }

            // if there is a status code then it came from the backend, return error as http error instead of throwing the exception
            ResponseMessage responseMessage    = new ResponseMessage(documentClientException.StatusCode ?? HttpStatusCode.InternalServerError, requestMessage);
            string          reasonPhraseString = documentClientException.ToString();

            if (!string.IsNullOrEmpty(reasonPhraseString))
            {
                if (documentClientException.Message.IndexOfAny(Extensions.NewLineCharacters) >= 0)
                {
                    StringBuilder sb = new StringBuilder(reasonPhraseString);
                    sb = sb.Replace("\r", string.Empty);
                    sb = sb.Replace("\n", string.Empty);
                    reasonPhraseString = sb.ToString();
                }
            }

            responseMessage.ErrorMessage = reasonPhraseString;
            responseMessage.Error        = documentClientException.Error;

            if (documentClientException.Headers != null)
            {
                foreach (string header in documentClientException.Headers.AllKeys())
                {
                    responseMessage.Headers.Add(header, documentClientException.Headers[header]);
                }
            }

            PointOperationStatistics pointOperationStatistics = new PointOperationStatistics(
                activityId: responseMessage.Headers.ActivityId,
                statusCode: documentClientException.StatusCode.Value,
                subStatusCode: (int)SubStatusCodes.Unknown,
                requestCharge: responseMessage.Headers.RequestCharge,
                errorMessage: responseMessage.ErrorMessage,
                method: requestMessage?.Method,
                requestUri: requestMessage?.RequestUri,
                requestSessionToken: requestMessage?.Headers?.Session,
                responseSessionToken: responseMessage.Headers.Session,
                clientSideRequestStatistics: documentClientException.RequestStatistics as CosmosClientSideRequestStatistics);

            responseMessage.DiagnosticsContext.AddDiagnosticsInternal(pointOperationStatistics);
            if (requestMessage != null)
            {
                requestMessage.Properties.Remove(nameof(DocumentClientException));
                requestMessage.Properties.Add(nameof(DocumentClientException), documentClientException);
            }

            return(responseMessage);
        }
Esempio n. 6
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentClientException documentClientException, RequestMessage requestMessage)
        {
            CosmosDiagnosticsContext diagnosticsContext = requestMessage?.DiagnosticsContext;

            if (requestMessage != null)
            {
                diagnosticsContext = requestMessage.DiagnosticsContext;

                if (diagnosticsContext == null)
                {
                    throw new ArgumentNullException("Request message should contain a DiagnosticsContext");
                }
            }
            else
            {
                diagnosticsContext = new CosmosDiagnosticsContextCore();
            }

            CosmosException cosmosException = CosmosExceptionFactory.Create(
                documentClientException,
                diagnosticsContext);

            PointOperationStatistics pointOperationStatistics = new PointOperationStatistics(
                activityId: cosmosException.Headers.ActivityId,
                statusCode: cosmosException.StatusCode,
                subStatusCode: (int)SubStatusCodes.Unknown,
                responseTimeUtc: DateTime.UtcNow,
                requestCharge: cosmosException.Headers.RequestCharge,
                errorMessage: documentClientException.ToString(),
                method: requestMessage?.Method,
                requestUri: requestMessage?.RequestUriString,
                requestSessionToken: requestMessage?.Headers?.Session,
                responseSessionToken: cosmosException.Headers.Session);

            diagnosticsContext.AddDiagnosticsInternal(pointOperationStatistics);

            // if StatusCode is null it is a client business logic error and it never hit the backend, so throw
            if (documentClientException.StatusCode == null)
            {
                throw cosmosException;
            }

            // if there is a status code then it came from the backend, return error as http error instead of throwing the exception
            ResponseMessage responseMessage = cosmosException.ToCosmosResponseMessage(requestMessage);

            if (requestMessage != null)
            {
                requestMessage.Properties.Remove(nameof(DocumentClientException));
                requestMessage.Properties.Add(nameof(DocumentClientException), documentClientException);
            }

            return(responseMessage);
        }
        public void ToStringTest()
        {
            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics = new CosmosClientSideRequestStatistics();

            //Setting null supplementalResponseStatisticsList
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList = null;
            PointOperationStatistics pointOperationStatistics = new PointOperationStatistics(cosmosClientSideRequestStatistics);

            pointOperationStatistics.ToString();
            Assert.IsNull(pointOperationStatistics.supplementalResponseStatisticsList);

            //Adding 5 objects supplementalResponseStatisticsList
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList = new List <StoreResponseStatistics>
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            };

            pointOperationStatistics = new PointOperationStatistics(cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(5, pointOperationStatistics.supplementalResponseStatisticsList.Count);

            //Adding 5 more objects supplementalResponseStatisticsList, making total 10
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList.AddRange(new List <StoreResponseStatistics>()
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            });

            pointOperationStatistics = new PointOperationStatistics(cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(10, pointOperationStatistics.supplementalResponseStatisticsList.Count);

            //Adding 2 more objects supplementalResponseStatisticsList, making total 12
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList.AddRange(new List <StoreResponseStatistics>()
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            });

            pointOperationStatistics = new PointOperationStatistics(cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(10, pointOperationStatistics.supplementalResponseStatisticsList.Count);
        }
Esempio n. 8
0
        internal override void AddDiagnosticsInternal(PointOperationStatistics pointOperationStatistics)
        {
            if (pointOperationStatistics == null)
            {
                throw new ArgumentNullException(nameof(pointOperationStatistics));
            }

            this.TotalRequestCount++;
            int statusCode = (int)pointOperationStatistics.StatusCode;

            if (statusCode < 200 || statusCode > 299)
            {
                this.FailedRequestCount++;
            }

            this.ContextList.Add(pointOperationStatistics);
        }
 internal abstract void AddDiagnosticsInternal(PointOperationStatistics pointOperationStatistics);
 internal override void AddDiagnosticsInternal(PointOperationStatistics pointOperationStatistics)
 {
 }
 public override void Visit(PointOperationStatistics pointOperationStatistics)
 {
 }
 public override void Visit(PointOperationStatistics pointOperationStatistics)
 {
     throw new ArgumentException($"Point Operation should not have {nameof(pointOperationStatistics)}");
 }
 public override void Visit(PointOperationStatistics pointOperationStatistics)
 {
     Assert.IsFalse(this.isPointOperationStatisticsVisited, $"Should only be a single {nameof(PointOperationStatistics)}");
     this.isPointOperationStatisticsVisited = true;
 }
        public void ToStringTest()
        {
            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics = new CosmosClientSideRequestStatistics();
            //Setting null supplementalResponseStatisticsList
            PointOperationStatistics pointOperationStatistics = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics: cosmosClientSideRequestStatistics);

            Assert.IsNotNull(pointOperationStatistics.ToString());

            //Adding 5 objects supplementalResponseStatisticsList
            for (int i = 0; i < 5; i++)
            {
                cosmosClientSideRequestStatistics.SupplementalResponseStatisticsList.Add(new StoreResponseStatistics());
            }

            pointOperationStatistics = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics: cosmosClientSideRequestStatistics);

            pointOperationStatistics.ToString();
            Assert.AreEqual(5, pointOperationStatistics.ClientSideRequestStatistics.SupplementalResponseStatisticsList.Count);

            //Adding 5 more objects supplementalResponseStatisticsList, making total 10
            cosmosClientSideRequestStatistics.SupplementalResponseStatisticsList.AddRange(new List <StoreResponseStatistics>()
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            });

            pointOperationStatistics = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics:  cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(10, pointOperationStatistics.ClientSideRequestStatistics.SupplementalResponseStatisticsList.Count);

            //Adding 2 more objects supplementalResponseStatisticsList, making total 12
            cosmosClientSideRequestStatistics.SupplementalResponseStatisticsList.AddRange(new List <StoreResponseStatistics>()
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            });

            pointOperationStatistics = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics:  cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(12, pointOperationStatistics.ClientSideRequestStatistics.SupplementalResponseStatisticsList.Count);
        }
Esempio n. 15
0
        public void ToStringTest()
        {
            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics = new CosmosClientSideRequestStatistics();

            //Setting null supplementalResponseStatisticsList
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList = null;
            PointOperationStatistics pointOperationStatistics = new PointOperationStatistics(
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                clientSideRequestStatistics: cosmosClientSideRequestStatistics);

            pointOperationStatistics.ToString();
            Assert.IsNull(pointOperationStatistics.supplementalResponseStatisticsList);

            //Adding 5 objects supplementalResponseStatisticsList
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList = new List <StoreResponseStatistics>
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            };

            pointOperationStatistics = new PointOperationStatistics(
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                clientSideRequestStatistics: cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(5, pointOperationStatistics.supplementalResponseStatisticsList.Count);

            //Adding 5 more objects supplementalResponseStatisticsList, making total 10
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList.AddRange(new List <StoreResponseStatistics>()
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            });

            pointOperationStatistics = new PointOperationStatistics(
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                clientSideRequestStatistics:  cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(10, pointOperationStatistics.supplementalResponseStatisticsList.Count);

            //Adding 2 more objects supplementalResponseStatisticsList, making total 12
            cosmosClientSideRequestStatistics.supplementalResponseStatisticsList.AddRange(new List <StoreResponseStatistics>()
            {
                new StoreResponseStatistics(),
                new StoreResponseStatistics()
            });

            pointOperationStatistics = new PointOperationStatistics(
                statusCode: System.Net.HttpStatusCode.OK,
                subStatusCode: Documents.SubStatusCodes.Unknown,
                requestCharge: 42,
                errorMessage: null,
                method: HttpMethod.Get,
                requestUri: new System.Uri("https://localhost:8081"),
                clientSideRequestStatistics:  cosmosClientSideRequestStatistics);
            pointOperationStatistics.ToString();
            Assert.AreEqual(10, pointOperationStatistics.supplementalResponseStatisticsList.Count);
        }