Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosOperationCanceledException: Add CosmosDiagnostic info #1550

Merged
merged 8 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 51 additions & 40 deletions Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,58 +119,69 @@ public virtual async Task<ResponseMessage> SendAsync(
overallScope = diagnosticsContext.GetOverallScope();
}

using (overallScope)
{
HttpMethod method = RequestInvokerHandler.GetHttpMethod(operationType);
RequestMessage request = new RequestMessage(
method,
resourceUri,
diagnosticsContext)
{
OperationType = operationType,
ResourceType = resourceType,
RequestOptions = requestOptions,
Content = streamPayload,
};

if (partitionKey.HasValue)
try
{
using (overallScope)
{
if (cosmosContainerCore == null && object.ReferenceEquals(partitionKey, Cosmos.PartitionKey.None))
HttpMethod method = RequestInvokerHandler.GetHttpMethod(operationType);
RequestMessage request = new RequestMessage(
method,
resourceUri,
diagnosticsContext)
{
throw new ArgumentException($"{nameof(cosmosContainerCore)} can not be null with partition key as PartitionKey.None");
}
else if (partitionKey.Value.IsNone)
OperationType = operationType,
ResourceType = resourceType,
RequestOptions = requestOptions,
Content = streamPayload,
};

if (partitionKey.HasValue)
{
using (diagnosticsContext.CreateScope("GetNonePkValue"))
if (cosmosContainerCore == null && object.ReferenceEquals(partitionKey, Cosmos.PartitionKey.None))
{
try
{
PartitionKeyInternal partitionKeyInternal = await cosmosContainerCore.GetNonePartitionKeyValueAsync(cancellationToken);
request.Headers.PartitionKey = partitionKeyInternal.ToJsonString();
}
catch (DocumentClientException dce)
{
return dce.ToCosmosResponseMessage(request);
}
catch (CosmosException ce)
throw new ArgumentException($"{nameof(cosmosContainerCore)} can not be null with partition key as PartitionKey.None");
}
else if (partitionKey.Value.IsNone)
{
using (diagnosticsContext.CreateScope("GetNonePkValue"))
{
return ce.ToCosmosResponseMessage(request);
try
{
PartitionKeyInternal partitionKeyInternal = await cosmosContainerCore.GetNonePartitionKeyValueAsync(cancellationToken);
request.Headers.PartitionKey = partitionKeyInternal.ToJsonString();
}
catch (DocumentClientException dce)
{
return dce.ToCosmosResponseMessage(request);
}
catch (CosmosException ce)
{
return ce.ToCosmosResponseMessage(request);
}
}
}
else
{
request.Headers.PartitionKey = partitionKey.Value.ToJsonString();
}
}
else

if (operationType == OperationType.Upsert)
{
request.Headers.PartitionKey = partitionKey.Value.ToJsonString();
request.Headers.IsUpsert = bool.TrueString;
}
}

if (operationType == OperationType.Upsert)
{
request.Headers.IsUpsert = bool.TrueString;
requestEnricher?.Invoke(request);
return await this.SendAsync(request, cancellationToken);
}

requestEnricher?.Invoke(request);
return await this.SendAsync(request, cancellationToken);
}
catch (TaskCanceledException te)
j82w marked this conversation as resolved.
Show resolved Hide resolved
{
j82w marked this conversation as resolved.
Show resolved Hide resolved
throw CosmosOperationCanceledException.Create(te, diagnosticsContext);
}
catch (OperationCanceledException oe)
{
throw CosmosOperationCanceledException.Create(oe, diagnosticsContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Threading;

/// <summary>
/// The Cosmos Client exception
j82w marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
internal class CosmosOperationCanceledException : OperationCanceledException
j82w marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly OperationCanceledException originalException;
j82w marked this conversation as resolved.
Show resolved Hide resolved

public static OperationCanceledException Create(
OperationCanceledException originalException,
CosmosDiagnosticsContext diagnosticsContext)
{
if (originalException == null)
{
throw new ArgumentNullException(nameof(originalException));
}

if (diagnosticsContext == null)
{
throw new ArgumentNullException(nameof(diagnosticsContext));
}

if (originalException.CancellationToken != null)
{
return new CosmosOperationCanceledException(
originalException,
originalException.CancellationToken,
diagnosticsContext);
}

return new CosmosOperationCanceledException(
originalException,
diagnosticsContext);
}

private CosmosOperationCanceledException(
OperationCanceledException originalException,
CosmosDiagnosticsContext diagnosticsContext)
{
this.originalException = originalException;
this.DiagnosticsContext = diagnosticsContext;
}

private CosmosOperationCanceledException(
OperationCanceledException originalException,
CancellationToken cancellationToken,
CosmosDiagnosticsContext diagnosticsContext)
: base(cancellationToken)
{
this.originalException = originalException;
this.DiagnosticsContext = diagnosticsContext;
}

public override string Source
{
get => this.originalException.Source;
set => this.originalException.Source = value;
}

public override string Message => this.originalException.Message;

public override string StackTrace => this.originalException.StackTrace;

public override IDictionary Data => this.originalException.Data;

public override string HelpLink
{
get => this.originalException.HelpLink;
set => this.originalException.HelpLink = value;
}

internal CosmosDiagnosticsContext DiagnosticsContext { get; }

public override Exception GetBaseException()
{
return this.originalException.GetBaseException();
}

public override string ToString()
{
return $"{this.originalException.ToString()} {Environment.NewLine}CosmosDiagnostics: {this.DiagnosticsContext.ToString()}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Net;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// The Cosmos Client exception
/// </summary>
internal class CosmosTaskCanceledException : TaskCanceledException
{
private readonly TaskCanceledException originalException;

public static TaskCanceledException Create(
TaskCanceledException originalException,
CosmosDiagnosticsContext diagnosticsContext)
{
if (originalException == null)
{
throw new ArgumentNullException(nameof(originalException));
}

if (diagnosticsContext == null)
{
throw new ArgumentNullException(nameof(diagnosticsContext));
}

if (originalException.Task != null)
{
return new CosmosTaskCanceledException(
originalException,
originalException.Task,
diagnosticsContext);
}

return new CosmosTaskCanceledException(
originalException,
diagnosticsContext);
}

private CosmosTaskCanceledException(
TaskCanceledException originalException,
CosmosDiagnosticsContext diagnosticsContext)
{
this.originalException = originalException;
this.DiagnosticsContext = diagnosticsContext;
}

private CosmosTaskCanceledException(
TaskCanceledException originalException,
Task originalTask,
CosmosDiagnosticsContext diagnosticsContext)
: base(originalTask)
{
this.originalException = originalException;
this.DiagnosticsContext = diagnosticsContext;
}

public override string Source
{
get => this.originalException.Source;
set => this.originalException.Source = value;
}

public override string Message => this.originalException.Message;

public override string StackTrace => this.originalException.StackTrace;

public override IDictionary Data => this.originalException.Data;

public override string HelpLink
{
get => this.originalException.HelpLink;
set => this.originalException.HelpLink = value;
}

internal CosmosDiagnosticsContext DiagnosticsContext { get; }

public override Exception GetBaseException()
{
return this.originalException.GetBaseException();
}

public override string ToString()
{
return $"{this.originalException.ToString()} {Environment.NewLine}CosmosDiagnostics: {this.DiagnosticsContext.ToString()}";
}
}
}
Loading