Beispiel #1
0
        internal override async Task <ContainerProperties> GetCachedContainerPropertiesAsync(
            string containerUri,
            ITrace trace,
            CancellationToken cancellationToken)
        {
            using (ITrace childTrace = trace.StartChild("Get Container Properties", TraceComponent.Transport, Tracing.TraceLevel.Info))
            {
                this.ThrowIfDisposed();
                CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContextCore.Create(requestOptions: null);
                using (diagnosticsContext.GetOverallScope())
                {
                    ClientCollectionCache collectionCache = await this.DocumentClient.GetCollectionCacheAsync();

                    try
                    {
                        using (diagnosticsContext.CreateScope("ContainerCache.ResolveByNameAsync"))
                        {
                            return(await collectionCache.ResolveByNameAsync(
                                       HttpConstants.Versions.CurrentVersion,
                                       containerUri,
                                       forceRefesh : false,
                                       cancellationToken));
                        }
                    }
                    catch (DocumentClientException ex)
                    {
                        throw CosmosExceptionFactory.Create(ex, diagnosticsContext);
                    }
                }
            }
        }
        internal override async Task <ContainerProperties> GetCachedContainerPropertiesAsync(
            string containerUri,
            CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContextCore.Create(requestOptions: null);

            using (diagnosticsContext.GetOverallScope())
            {
                ClientCollectionCache collectionCache = await this.DocumentClient.GetCollectionCacheAsync();

                try
                {
                    using (diagnosticsContext.CreateScope("ContainerCache.ResolveByNameAsync"))
                    {
                        return(await collectionCache.ResolveByNameAsync(
                                   HttpConstants.Versions.CurrentVersion,
                                   containerUri,
                                   cancellationToken));
                    }
                }
                catch (DocumentClientException ex)
                {
                    throw CosmosExceptionFactory.Create(ex, diagnosticsContext);
                }
            }
        }
Beispiel #3
0
 internal override CosmosDiagnosticsContext CreateDiagnosticContext(
     string operationName,
     RequestOptions requestOptions)
 {
     return(CosmosDiagnosticsContextCore.Create(
                operationName,
                requestOptions,
                this.UserAgent));
 }
Beispiel #4
0
 public CosmosClientSideRequestStatistics(CosmosDiagnosticsContext diagnosticsContext = null)
 {
     this.RequestStartTimeUtc = DateTime.UtcNow;
     this.RequestEndTimeUtc   = null;
     this.EndpointToAddressResolutionStatistics = new Dictionary <string, AddressResolutionStatistics>();
     this.ContactedReplicas  = new List <Uri>();
     this.FailedReplicas     = new HashSet <Uri>();
     this.RegionsContacted   = new HashSet <Uri>();
     this.DiagnosticsContext = diagnosticsContext ?? CosmosDiagnosticsContextCore.Create(requestOptions: null);
     this.DiagnosticsContext.AddDiagnosticsInternal(this);
     this.clientSideRequestStatisticsCreateTime = Stopwatch.GetTimestamp();
 }
Beispiel #5
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentClientException documentClientException, RequestMessage requestMessage)
        {
            CosmosDiagnosticsContext diagnosticsContext = requestMessage?.DiagnosticsContext;

            if (diagnosticsContext == null)
            {
                diagnosticsContext = CosmosDiagnosticsContextCore.Create();
            }

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

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

            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);
        }