Skip to content

Commit

Permalink
Added diagnostics to exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Willey committed Sep 10, 2019
2 parents 22481e0 + d235751 commit c5a9bd1
Show file tree
Hide file tree
Showing 24 changed files with 897 additions and 793 deletions.
Binary file added 35MSSharedLib1024.snk
Binary file not shown.
12 changes: 4 additions & 8 deletions Microsoft.Azure.Cosmos/src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Runtime.CompilerServices;

#if SignAssembly
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2" + AssemblyKeys.MoqPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Friends" + AssemblyKeys.ProductPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Friends" + AssemblyKeys.TestPublicKey)]
Expand All @@ -20,13 +19,10 @@
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Table.Tests" + AssemblyKeys.TestPublicKey)]

[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Performance.Tests" + AssemblyKeys.ProductPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Performance.Tests" + AssemblyKeys.TestPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Tests" + AssemblyKeys.ProductPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Tests" + AssemblyKeys.TestPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.EmulatorTests" + AssemblyKeys.ProductPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.EmulatorTests" + AssemblyKeys.TestPublicKey)]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.NetFramework.Tests" + AssemblyKeys.ProductPublicKey)]
#else
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Tests")]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.EmulatorTests")]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.NetFramework.Tests")]
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.Performance.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
#endif
[assembly: InternalsVisibleTo("Microsoft.Azure.Cosmos.NetFramework.Tests" + AssemblyKeys.TestPublicKey)]
24 changes: 23 additions & 1 deletion Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public string ApplicationName
/// fallback geo-replicated regions for high availability.
/// When this property is not specified, the SDK uses the write region as the preferred region for all operations.
///
/// <seealso cref="CosmosClientBuilder.WithApplicationRegion(string)"/>
/// <seealso cref="CosmosClientBuilder.WithApplicationRegion(string, bool)"/>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/how-to-multi-master"/>
/// </remarks>
public string ApplicationRegion { get; set; }
Expand Down Expand Up @@ -161,6 +161,15 @@ public ConnectionMode ConnectionMode
get => this.connectionMode;
set
{
if (value == ConnectionMode.Gateway)
{
this.ConnectionProtocol = Protocol.Https;
}
else if (value == ConnectionMode.Direct)
{
this.connectionProtocol = Protocol.Tcp;
}

this.ValidateDirectTCPSettings();
this.connectionMode = value;
}
Expand Down Expand Up @@ -350,6 +359,18 @@ public CosmosSerializer Serializer
#endif
bool AllowBulkExecution { get; set; }

/// <summary>
/// Allow using account region in-case ApplicationRegion is not available. Defaults to 'true'.
/// </summary>
/// <value>
/// Default value is true.
/// </value>
/// <remarks>
/// When the value of this property is true, the SDK will automatically discover all account write and read regions, and use them when the configured application region is not available. When set to false, availability is limited to the configured application region only. </remarks>
/// <seealso cref="CosmosClientOptions.ApplicationRegion"/>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability"/>
internal bool UseAnyAccountRegion { get; set; } = true;

/// <summary>
/// A JSON serializer used by the CosmosClient to serialize or de-serialize cosmos request/responses.
/// The default serializer is always used for all system owned types like DatabaseProperties.
Expand Down Expand Up @@ -506,6 +527,7 @@ internal ConnectionPolicy GetConnectionPolicy()
if (this.ApplicationRegion != null)
{
connectionPolicy.SetCurrentLocation(this.ApplicationRegion);
connectionPolicy.EnableEndpointDiscovery = this.UseAnyAccountRegion;
}

if (this.MaxRetryAttemptsOnRateLimitedRequests != null)
Expand Down
6 changes: 5 additions & 1 deletion Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public CosmosClientBuilder WithApplicationName(string applicationName)
/// Set the preferred geo-replicated region to be used in the Azure Cosmos DB service.
/// </summary>
/// <param name="applicationRegion">Azure region where application is running. <see cref="Regions"/> lists valid Cosmos DB regions.</param>
/// <param name="useAnyAccountRegion">When the value of this property is true, the SDK will automatically discover the current write and read regions to ensure requests are sent to the correct region based on the region specified in the ApplicationRegion property. When setting the value to false, availability is reduced to that of a single region account to the region specified in ApplicationRegion, see https://docs.microsoft.com/azure/cosmos-db/high-availability. </param>
/// <example>
/// The example below creates a new <see cref="CosmosClientBuilder"/> with a of preferred region.
/// <code language="c#">
Expand All @@ -139,9 +140,12 @@ public CosmosClientBuilder WithApplicationName(string applicationName)
/// </example>
/// <returns>The current <see cref="CosmosClientBuilder"/>.</returns>
/// <seealso cref="CosmosClientOptions.ApplicationRegion"/>
public CosmosClientBuilder WithApplicationRegion(string applicationRegion)
public CosmosClientBuilder WithApplicationRegion(
string applicationRegion,
bool useAnyAccountRegion = true)
{
this.clientOptions.ApplicationRegion = applicationRegion;
this.clientOptions.UseAnyAccountRegion = useAnyAccountRegion;
return this;
}

Expand Down
21 changes: 13 additions & 8 deletions Microsoft.Azure.Cosmos/src/Microsoft.Azure.Cosmos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
<Description>This client library enables client applications to connect to Azure Cosmos via the SQL API. Azure Cosmos is a globally distributed, multi-model database service. For more information, refer to http://azure.microsoft.com/services/cosmos-db/. </Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<ClientVersion>3.1.1</ClientVersion>
<DirectVersion>3.2.1</DirectVersion>
<HybridRowVersion>1.0.0-preview</HybridRowVersion>
<DirectPackageName Condition=" '$(SignAssembly)' == 'true' ">Microsoft.Azure.Cosmos.Direct</DirectPackageName>
<DirectPackageName Condition=" '$(SignAssembly)' != 'true' ">Microsoft.Azure.Cosmos.Direct.MyGet</DirectPackageName>
<CurrentDate>$([System.DateTime]::Now.ToString(yyyyMMdd))</CurrentDate>
<VersionSuffix Condition=" '$(IsNightly)' == 'true' ">nightly-$(CurrentDate)</VersionSuffix>
<VersionSuffix Condition=" '$(IsPreview)' == 'true' ">preview</VersionSuffix>
Expand Down Expand Up @@ -42,7 +37,9 @@
<RootNamespace>Microsoft.Azure.Cosmos</RootNamespace>
<NoWarn>NU5125</NoWarn>
</PropertyGroup>


<Import Project="..\version.props"/>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
Expand Down Expand Up @@ -77,7 +74,7 @@
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="16.0.102" PrivateAssets="All" />

<PackageReference Include="$(DirectPackageName)" Version="[$(DirectVersion)]" PrivateAssets="All" />
<PackageReference Include="Microsoft.Azure.Cosmos.Direct" Version="[$(DirectVersion)]" PrivateAssets="All" />
<PackageReference Include="Microsoft.Azure.Cosmos.Serialization.HybridRow" Version="[$(HybridRowVersion)]" PrivateAssets="All" />

<!--Direct Dependencies-->
Expand All @@ -97,15 +94,23 @@
<None Include="$(OutputPath)\Microsoft.Azure.Cosmos.ServiceInterop.dll" Pack="true" IsAssembly="true" PackagePath="runtimes\win-x64\native" />
<None Include="$(NugetPackageRoot)\Microsoft.Azure.Cosmos.Serialization.HybridRow\$(HybridRowVersion)\lib\netstandard2.0\Microsoft.Azure.Cosmos.Core.dll" Pack="true" IsAssembly="true" PackagePath="lib\netstandard2.0" />
<None Include="$(NugetPackageRoot)\Microsoft.Azure.Cosmos.Serialization.HybridRow\$(HybridRowVersion)\lib\netstandard2.0\Microsoft.Azure.Cosmos.Serialization.HybridRow.dll" Pack="true" IsAssembly="true" PackagePath="lib\netstandard2.0" />
<None Include="$(NugetPackageRoot)\$(DirectPackageName)\$(DirectVersion)\lib\netstandard2.0\Microsoft.Azure.Cosmos.Direct.dll" Pack="true" IsAssembly="true" PackagePath="lib\netstandard2.0" />
<None Include="$(NugetPackageRoot)\Microsoft.Azure.Cosmos.Direct\$(DirectVersion)\lib\netstandard2.0\Microsoft.Azure.Cosmos.Direct.dll" Pack="true" IsAssembly="true" PackagePath="lib\netstandard2.0" />
<None Include="$(MSBuildThisFileDirectory)\Microsoft.Azure.Cosmos.targets" Pack="true" PackagePath="build\netstandard2.0">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<PropertyGroup>
<SignAssembly>true</SignAssembly>
<DelaySign>true</DelaySign>
<AssemblyOriginatorKeyFile>..\..\35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<PropertyGroup>
<DefineConstants Condition=" '$(IsNightly)' == 'true' or '$(IsPreview)' == 'true' ">$(DefineConstants);PREVIEW</DefineConstants>
<DefineConstants Condition=" '$(SignAssembly)' == 'true' ">$(DefineConstants);SignAssembly</DefineConstants>
<DefineConstants Condition=" '$(DelaySign)' == 'true' ">DelaySignKeys</DefineConstants>
<DefineConstants>$(DefineConstants);DOCDBCLIENT;COSMOSCLIENT;NETSTANDARD20</DefineConstants>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public override Task<ContainerResponse> DeleteContainerAsync(
public async override Task<int?> ReadThroughputAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
ThroughputResponse response = await this.ReadThroughputAsync(null, cancellationToken);
ThroughputResponse response = await this.ReadThroughputIfExistsAsync(null, cancellationToken);
return response.Resource?.Throughput;
}

Expand Down
9 changes: 8 additions & 1 deletion Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal CosmosException(
this.RequestCharge = this.Headers.RequestCharge;
this.RetryAfter = this.Headers.RetryAfter;
this.SubStatusCode = (int)this.Headers.SubStatusCode;
this.Diagnostics = cosmosResponseMessage.Diagnostics;
if (this.Headers.ContentLengthAsLong > 0)
{
using (StreamReader responseReader = new StreamReader(cosmosResponseMessage.Content))
Expand Down Expand Up @@ -122,6 +123,11 @@ public CosmosException(
/// </summary>
public virtual Headers Headers { get; }

/// <summary>
/// Gets the diagnostics for the request
/// </summary>
public virtual CosmosDiagnostics Diagnostics { get; }

/// <summary>
/// Gets the internal error object
/// </summary>
Expand Down Expand Up @@ -150,7 +156,8 @@ public virtual bool TryGetHeader(string headerName, out string value)
/// <returns>A string representation of the exception.</returns>
public override string ToString()
{
return $"{nameof(CosmosException)};StatusCode={this.StatusCode};SubStatusCode={this.SubStatusCode};ActivityId={this.ActivityId ?? string.Empty};RequestCharge={this.RequestCharge};Message={this.Message};";
string diagnostics = this.Diagnostics != null ? this.Diagnostics.ToString() : string.Empty;
return $"{nameof(CosmosException)};StatusCode={this.StatusCode};SubStatusCode={this.SubStatusCode};ActivityId={this.ActivityId ?? string.Empty};RequestCharge={this.RequestCharge};Message={this.Message};Diagnostics{diagnostics}";
}

internal ResponseMessage ToCosmosResponseMessage(RequestMessage request)
Expand Down
30 changes: 27 additions & 3 deletions Microsoft.Azure.Cosmos/src/Resource/CosmosResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,44 @@ internal CosmosResponseFactory(
this.cosmosSerializer = userJsonSerializer;
}

internal FeedResponse<T> CreateQueryFeedResponseWithPropertySerializer<T>(
ResponseMessage cosmosResponseMessage)
{
return this.CreateQueryFeedResponseHelper<T>(
cosmosResponseMessage,
true);
}

internal FeedResponse<T> CreateQueryFeedResponse<T>(
ResponseMessage cosmosResponseMessage)
{
return this.CreateQueryFeedResponseHelper<T>(
cosmosResponseMessage,
false);
}

private FeedResponse<T> CreateQueryFeedResponseHelper<T>(
ResponseMessage cosmosResponseMessage,
bool usePropertySerializer)
{
//Throw the exception
cosmosResponseMessage.EnsureSuccessStatusCode();

// The property serializer should be used for internal
// query operations like throughput since user serializer can break the logic
CosmosSerializer serializer = usePropertySerializer ? this.propertiesSerializer : this.cosmosSerializer;

QueryResponse queryResponse = cosmosResponseMessage as QueryResponse;
if (queryResponse != null)
{
return QueryResponse<T>.CreateResponse<T>(
cosmosQueryResponse: queryResponse,
jsonSerializer: this.cosmosSerializer);
jsonSerializer: serializer);
}

return ReadFeedResponse<T>.CreateResponse<T>(
cosmosResponseMessage,
this.cosmosSerializer);
serializer);
}

internal Task<ItemResponse<T>> CreateItemResponseAsync<T>(
Expand Down Expand Up @@ -115,7 +136,10 @@ internal Task<DatabaseResponse> CreateDatabaseResponseAsync(
{
return this.ProcessMessageAsync(cosmosResponseMessageTask, (cosmosResponseMessage) =>
{
DatabaseProperties databaseProperties = this.ToObjectInternal<DatabaseProperties>(cosmosResponseMessage, this.propertiesSerializer);
DatabaseProperties databaseProperties = this.ToObjectInternal<DatabaseProperties>(
cosmosResponseMessage,
this.propertiesSerializer);
return new DatabaseResponse(
cosmosResponseMessage.StatusCode,
cosmosResponseMessage.Headers,
Expand Down
Loading

0 comments on commit c5a9bd1

Please sign in to comment.