Example #1
0
        public void ValidateClientSideRequestStatisticsToString()
        {
            // Verify that API using the interface get the older v2 string
            CosmosDiagnosticsContext diagnosticsContext = new CosmosDiagnosticsContextCore();

            diagnosticsContext.GetOverallScope().Dispose();

            CosmosClientSideRequestStatistics clientSideRequestStatistics = new CosmosClientSideRequestStatistics(diagnosticsContext);
            string noInfo = clientSideRequestStatistics.ToString();

            Assert.IsNotNull(noInfo);

            StringBuilder stringBuilder = new StringBuilder();

            clientSideRequestStatistics.AppendToBuilder(stringBuilder);
            string noInfoStringBuilder = stringBuilder.ToString();

            Assert.IsNotNull(noInfoStringBuilder);

            Assert.AreEqual(noInfo, noInfoStringBuilder);

            string id = clientSideRequestStatistics.RecordAddressResolutionStart(new Uri("https://testuri"));

            clientSideRequestStatistics.RecordAddressResolutionEnd(id);

            Documents.DocumentServiceRequest documentServiceRequest = new Documents.DocumentServiceRequest(
                operationType: Documents.OperationType.Read,
                resourceIdOrFullName: null,
                resourceType: Documents.ResourceType.Database,
                body: null,
                headers: null,
                isNameBased: false,
                authorizationTokenType: Documents.AuthorizationTokenType.PrimaryMasterKey);

            clientSideRequestStatistics.RecordRequest(documentServiceRequest);
            clientSideRequestStatistics.RecordResponse(
                documentServiceRequest,
                new Documents.StoreResult(
                    storeResponse: new Documents.StoreResponse(),
                    exception: null,
                    partitionKeyRangeId: "PkRange",
                    lsn: 42,
                    quorumAckedLsn: 4242,
                    requestCharge: 9000.42,
                    currentReplicaSetSize: 3,
                    currentWriteQuorum: 4,
                    isValid: true,
                    storePhysicalAddress: null,
                    globalCommittedLSN: 2,
                    numberOfReadRegions: 1,
                    itemLSN: 5,
                    sessionToken: null,
                    usingLocalLSN: true));

            string statistics = clientSideRequestStatistics.ToString();

            Assert.IsTrue(statistics.Contains("\"UserAgent\":\"cosmos-netstandard-sdk"));
            Assert.IsTrue(statistics.Contains("UsingLocalLSN: True, TransportException: null"));
            Assert.IsTrue(statistics.Contains("AddressResolutionStatistics\",\"StartTimeUtc"));
        }
Example #2
0
        public override void Visit(CosmosClientSideRequestStatistics clientSideRequestStatistics)
        {
            this.jsonWriter.WriteStartObject();
            this.jsonWriter.WritePropertyName("Id");
            this.jsonWriter.WriteValue("AggregatedClientSideRequestStatistics");

            this.WriteJsonUriArrayWithDuplicatesCounted("ContactedReplicas", clientSideRequestStatistics.ContactedReplicas);

            this.WriteJsonUriArray("RegionsContacted", clientSideRequestStatistics.RegionsContacted);
            this.WriteJsonUriArray("FailedReplicas", clientSideRequestStatistics.FailedReplicas);

            this.jsonWriter.WriteEndObject();
        }
        private static void ThrowTransportExceptionOnItemOperation(
            Uri physicalAddress,
            ResourceOperation resourceOperation,
            DocumentServiceRequest request)
        {
            if (request.ResourceType == ResourceType.Document)
            {
                TransportException transportException = new TransportException(
                    errorCode: TransportErrorCode.ConnectionBroken,
                    innerException: null,
                    activityId: Guid.NewGuid(),
                    requestUri: physicalAddress,
                    sourceDescription: "SourceDescription",
                    userPayload: true,
                    payloadSent: false);

                DocumentClientException documentClientException = new DocumentClientException(
                    message: "Exception",
                    innerException: transportException,
                    statusCode: System.Net.HttpStatusCode.Gone);
                CosmosClientSideRequestStatistics requestStatistics = (CosmosClientSideRequestStatistics)request.RequestContext.ClientRequestStatistics;
                requestStatistics.RecordResponse(request, new StoreResult(
                                                     storeResponse: null,
                                                     exception: documentClientException,
                                                     partitionKeyRangeId: "PkRange",
                                                     lsn: 42,
                                                     quorumAckedLsn: 4242,
                                                     requestCharge: 9000.42,
                                                     currentReplicaSetSize: 3,
                                                     currentWriteQuorum: 4,
                                                     isValid: true,
                                                     storePhysicalAddress: physicalAddress,
                                                     globalCommittedLSN: 2,
                                                     numberOfReadRegions: 1,
                                                     itemLSN: 5,
                                                     sessionToken: null,
                                                     usingLocalLSN: true,
                                                     activityId: Guid.NewGuid().ToString()));

                throw Documents.Rntbd.TransportExceptions.GetServiceUnavailableException(physicalAddress, Guid.NewGuid(),
                                                                                         transportException);
            }
        }
        public void ToString_ReturnItemsToString()
        {
            ItemBatchOperationStatistics itemBatchOperationStatistics = new ItemBatchOperationStatistics();

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics1 = new CosmosClientSideRequestStatistics();

            cosmosClientSideRequestStatistics1.ContactedReplicas.Add(new Uri("https://one.com"));
            PointOperationStatistics pointOperation1 = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: HttpStatusCode.OK,
                subStatusCode: SubStatusCodes.Unknown,
                requestCharge: 0,
                errorMessage: string.Empty,
                method: HttpMethod.Get,
                requestUri: new Uri("http://localhost"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics: cosmosClientSideRequestStatistics1);

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics2 = new CosmosClientSideRequestStatistics();

            cosmosClientSideRequestStatistics2.ContactedReplicas.Add(new Uri("https://two.com"));
            PointOperationStatistics pointOperation2 = new PointOperationStatistics(
                activityId: Guid.NewGuid().ToString(),
                statusCode: HttpStatusCode.OK,
                subStatusCode: SubStatusCodes.Unknown,
                requestCharge: 0,
                errorMessage: string.Empty,
                method: HttpMethod.Get,
                requestUri: new Uri("http://localhost"),
                requestSessionToken: null,
                responseSessionToken: null,
                clientSideRequestStatistics: cosmosClientSideRequestStatistics2);

            itemBatchOperationStatistics.AppendDiagnostics(pointOperation1);
            itemBatchOperationStatistics.AppendDiagnostics(pointOperation2);

            string toString = itemBatchOperationStatistics.ToString();

            Assert.IsTrue(toString.Contains(pointOperation1.ToString()));
            Assert.IsTrue(toString.Contains(pointOperation2.ToString()));
        }
Example #5
0
        public void Complete_AddsCompleteTime()
        {
            ItemBatchOperationStatistics itemBatchOperationStatistics = new ItemBatchOperationStatistics();

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics1 = new CosmosClientSideRequestStatistics();

            cosmosClientSideRequestStatistics1.ContactedReplicas.Add(new Uri("https://one.com"));
            PointOperationStatistics pointOperation1 = new PointOperationStatistics(Guid.NewGuid().ToString(), HttpStatusCode.OK, SubStatusCodes.Unknown, 0, string.Empty, HttpMethod.Get, new Uri("http://localhost"), cosmosClientSideRequestStatistics1);

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics2 = new CosmosClientSideRequestStatistics();

            cosmosClientSideRequestStatistics2.ContactedReplicas.Add(new Uri("https://two.com"));
            PointOperationStatistics pointOperation2 = new PointOperationStatistics(Guid.NewGuid().ToString(), HttpStatusCode.OK, SubStatusCodes.Unknown, 0, string.Empty, HttpMethod.Get, new Uri("http://localhost"), cosmosClientSideRequestStatistics2);

            itemBatchOperationStatistics.AppendDiagnostics(pointOperation1);
            itemBatchOperationStatistics.AppendDiagnostics(pointOperation2);
            itemBatchOperationStatistics.Complete();

            Assert.IsTrue(itemBatchOperationStatistics.ToString().Contains("Completed at"));
        }
Example #6
0
        public void ToString_ReturnItemsToString()
        {
            ItemBatchOperationStatistics itemBatchOperationStatistics = new ItemBatchOperationStatistics();

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics1 = new CosmosClientSideRequestStatistics();

            cosmosClientSideRequestStatistics1.ContactedReplicas.Add(new Uri("https://one.com"));
            PointOperationStatistics pointOperation1 = new PointOperationStatistics(HttpStatusCode.OK, SubStatusCodes.Unknown, 0, string.Empty, HttpMethod.Get, new Uri("http://localhost"), cosmosClientSideRequestStatistics1);

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics2 = new CosmosClientSideRequestStatistics();

            cosmosClientSideRequestStatistics2.ContactedReplicas.Add(new Uri("https://two.com"));
            PointOperationStatistics pointOperation2 = new PointOperationStatistics(HttpStatusCode.OK, SubStatusCodes.Unknown, 0, string.Empty, HttpMethod.Get, new Uri("http://localhost"), cosmosClientSideRequestStatistics2);

            itemBatchOperationStatistics.AppendDiagnostics(pointOperation1);
            itemBatchOperationStatistics.AppendDiagnostics(pointOperation2);

            string toString = itemBatchOperationStatistics.ToString();

            Assert.IsTrue(toString.Contains(pointOperation1.ToString()));
            Assert.IsTrue(toString.Contains(pointOperation2.ToString()));
        }
 public PointOperationStatistics(
     string activityId,
     HttpStatusCode statusCode,
     SubStatusCodes subStatusCode,
     double requestCharge,
     string errorMessage,
     HttpMethod method,
     Uri requestUri,
     string requestSessionToken,
     string responseSessionToken,
     CosmosClientSideRequestStatistics clientSideRequestStatistics)
 {
     this.ActivityId                  = activityId;
     this.StatusCode                  = statusCode;
     this.SubStatusCode               = subStatusCode;
     this.RequestCharge               = requestCharge;
     this.ErrorMessage                = errorMessage;
     this.Method                      = method;
     this.RequestUri                  = requestUri;
     this.RequestSessionToken         = requestSessionToken;
     this.ResponseSessionToken        = responseSessionToken;
     this.ClientSideRequestStatistics = clientSideRequestStatistics;
 }
 public abstract TResult Visit(CosmosClientSideRequestStatistics clientSideRequestStatistics);
 public abstract void Visit(CosmosClientSideRequestStatistics clientSideRequestStatistics);
        public async Task MockQueryOutput()
        {
            CosmosClientSideRequestStatistics clientSideRequestStatistics = new CosmosClientSideRequestStatistics();
            string id = clientSideRequestStatistics.RecordAddressResolutionStart(new Uri("https://testuri"));

            clientSideRequestStatistics.RecordAddressResolutionEnd(id);

            Documents.DocumentServiceRequest documentServiceRequest = new Documents.DocumentServiceRequest(
                operationType: Documents.OperationType.Read,
                resourceIdOrFullName: null,
                resourceType: Documents.ResourceType.Database,
                body: null,
                headers: null,
                isNameBased: false,
                authorizationTokenType: Documents.AuthorizationTokenType.PrimaryMasterKey);

            clientSideRequestStatistics.RecordRequest(documentServiceRequest);
            clientSideRequestStatistics.RecordResponse(
                documentServiceRequest,
                new Documents.StoreResult(
                    storeResponse: new Documents.StoreResponse(),
                    exception: null,
                    partitionKeyRangeId: "PkRange",
                    lsn: 42,
                    quorumAckedLsn: 4242,
                    requestCharge: 9000.42,
                    currentReplicaSetSize: 3,
                    currentWriteQuorum: 4,
                    isValid: true,
                    storePhysicalAddress: null,
                    globalCommittedLSN: 2,
                    numberOfReadRegions: 1,
                    itemLSN: 5,
                    sessionToken: null,
                    usingLocalLSN: true,
                    activityId: Guid.NewGuid().ToString()));

            Trace queryTrace;

            using (queryTrace = Trace.GetRootTrace(
                       name: "Cross Partition Query",
                       component: TraceComponent.Query,
                       level: TraceLevel.Info))
            {
                using (ITrace getQueryPlanTrace = queryTrace.StartChild("GetQueryPlan"))
                {
                    using (ITrace gatewayTrace = getQueryPlanTrace.StartChild(
                               "Gateway Call",
                               component: TraceComponent.Transport,
                               level: TraceLevel.Info))
                    {
                        Thread.Sleep(1);
                        gatewayTrace.AddDatum("ClientSideRequestStats", new CosmosDiagnosticsTraceDatum(clientSideRequestStatistics));
                    }
                }

                using (ITrace getPkRanges = queryTrace.StartChild("GetPkRanges"))
                {
                    using (ITrace addressResolution = getPkRanges.StartChild(
                               "AddressResolution",
                               component: TraceComponent.Transport,
                               level: TraceLevel.Info))
                    {
                        await Task.Delay(1);

                        addressResolution.AddDatum("AddressResolutionStatistics", new CosmosDiagnosticsTraceDatum(
                                                       new AddressResolutionStatistics(
                                                           DateTime.MinValue,
                                                           DateTime.MinValue,
                                                           "https://testuri")));
                    }
                }

                using (ITrace queryPkRange1 = queryTrace.StartChild("Query PkRange 1"))
                {
                    using (ITrace continuation1 = queryPkRange1.StartChild("Continuation 1"))
                    {
                        using (ITrace gatewayTrace = continuation1.StartChild(
                                   "Execute Query Direct",
                                   component: TraceComponent.Transport,
                                   level: TraceLevel.Info))
                        {
                            await Task.Delay(1);

                            gatewayTrace.AddDatum("ClientSideRequestStats", new CosmosDiagnosticsTraceDatum(clientSideRequestStatistics));
                        }

                        continuation1.AddDatum("QueryMetrics", new QueryMetricsTraceDatum(MockQueryMetrics));
                        continuation1.AddDatum("RequestCharge", 42);
                    }
                }

                using (ITrace queryPkRange2 = queryTrace.StartChild("Query PkRange 2"))
                {
                    using (ITrace continuation1 = queryPkRange2.StartChild("Continuation 1"))
                    {
                        using (ITrace gatewayTrace = continuation1.StartChild(
                                   "Execute Query Direct",
                                   component: TraceComponent.Transport,
                                   level: TraceLevel.Info))
                        {
                            await Task.Delay(1);

                            gatewayTrace.AddDatum("ClientSideRequestStats", new CosmosDiagnosticsTraceDatum(clientSideRequestStatistics));
                        }

                        continuation1.AddDatum("QueryMetrics", new QueryMetricsTraceDatum(MockQueryMetrics));
                        continuation1.AddDatum("RequestCharge", 42);
                    }

                    using (ITrace continuation2 = queryPkRange2.StartChild("Continuation 2"))
                    {
                        using (ITrace gatewayTrace = continuation2.StartChild(
                                   "Execute Query Direct",
                                   component: TraceComponent.Transport,
                                   level: TraceLevel.Info))
                        {
                            await Task.Delay(1);

                            gatewayTrace.AddDatum("ClientSideRequestStats", new CosmosDiagnosticsTraceDatum(clientSideRequestStatistics));
                        }

                        continuation2.AddDatum("QueryMetrics", new QueryMetricsTraceDatum(MockQueryMetrics));
                        continuation2.AddDatum("RequestCharge", 42);
                    }
                }
            }

            string textTraceString = TraceWriter.TraceToText(queryTrace, asciiType: TraceWriter.AsciiType.DoubleLine);
            string jsonTraceString = TraceWriter.TraceToJson(queryTrace);
        }