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); } } }
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); } } } }
private async Task <PartitionKeyRangeBatchExecutionResult> ExecuteAsync( PartitionKeyRangeServerBatchRequest serverRequest, CancellationToken cancellationToken) { CosmosDiagnosticsContext diagnosticsContext = new CosmosDiagnosticsContextCore(); CosmosDiagnosticScope limiterScope = diagnosticsContext.CreateScope("BatchAsyncContainerExecutor.Limiter"); SemaphoreSlim limiter = this.GetOrAddLimiterForPartitionKeyRange(serverRequest.PartitionKeyRangeId); using (await limiter.UsingWaitAsync(cancellationToken)) { limiterScope.Dispose(); using (Stream serverRequestPayload = serverRequest.TransferBodyStream()) { Debug.Assert(serverRequestPayload != null, "Server request payload expected to be non-null"); ResponseMessage responseMessage = await this.cosmosClientContext.ProcessResourceOperationStreamAsync( this.cosmosContainer.LinkUri, ResourceType.Document, OperationType.Batch, new RequestOptions(), cosmosContainerCore : this.cosmosContainer, partitionKey : null, streamPayload : serverRequestPayload, requestEnricher : requestMessage => BatchAsyncContainerExecutor.AddHeadersToRequestMessage(requestMessage, serverRequest.PartitionKeyRangeId), diagnosticsContext : diagnosticsContext, cancellationToken : cancellationToken).ConfigureAwait(false); using (diagnosticsContext.CreateScope("BatchAsyncContainerExecutor.ToResponse")) { TransactionalBatchResponse serverResponse = await TransactionalBatchResponse.FromResponseMessageAsync(responseMessage, serverRequest, this.cosmosClientContext.SerializerCore).ConfigureAwait(false); return(new PartitionKeyRangeBatchExecutionResult(serverRequest.PartitionKeyRangeId, serverRequest.Operations, serverResponse)); } } } }
internal override CosmosDiagnosticsContext CreateDiagnosticContext( string operationName, RequestOptions requestOptions) { return(CosmosDiagnosticsContextCore.Create( operationName, requestOptions, this.UserAgent)); }
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 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(); }
public void ValidateErrorHandling() { Error error = new Error() { Code = System.Net.HttpStatusCode.BadRequest.ToString(), Message = "Unsupported Query", AdditionalErrorInfo = "Additional error info message", }; CosmosDiagnosticsContext diagnostics = new CosmosDiagnosticsContextCore(); CosmosException cosmosException = CosmosExceptionFactory.CreateBadRequestException( error.ToString(), error: error, diagnosticsContext: diagnostics); ResponseMessage responseMessage = QueryResponse.CreateFailure( statusCode: System.Net.HttpStatusCode.BadRequest, cosmosException: cosmosException, requestMessage: null, diagnostics: diagnostics, responseHeaders: null); Assert.AreEqual(error, responseMessage.CosmosException.Error); Assert.IsTrue(responseMessage.ErrorMessage.Contains(error.Message)); Assert.IsTrue(responseMessage.ErrorMessage.Contains(error.AdditionalErrorInfo)); try { responseMessage.EnsureSuccessStatusCode(); Assert.Fail("Should throw exception"); } catch (CosmosException ce) when(ce.StatusCode == HttpStatusCode.BadRequest) { Assert.IsTrue(ce.Message.Contains(error.Message)); Assert.IsTrue(ce.ToString().Contains(error.Message)); Assert.IsTrue(ce.ToString().Contains(error.AdditionalErrorInfo)); } }