예제 #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"));
        }
        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 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);
        }