Skip to content

Commit

Permalink
Merge branch 'master' into users/dibahlfi/HedgingCancellationTokenExc…
Browse files Browse the repository at this point in the history
…petion
  • Loading branch information
NaluTripician authored Nov 25, 2024
2 parents 3e1e91a + 998dec0 commit 0b175ca
Show file tree
Hide file tree
Showing 47 changed files with 2,078 additions and 403 deletions.
7 changes: 6 additions & 1 deletion Microsoft.Azure.Cosmos/FaultInjection/src/FaultInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Azure.Cosmos.FaultInjection
using System.Collections.Generic;
using Microsoft.Azure.Documents.FaultInjection;

public class FaultInjector
public class FaultInjector : IFaultInjector
{
private readonly ChaosInterceptorFactory chaosInterceptorFactory;

Expand Down Expand Up @@ -48,5 +48,10 @@ internal IChaosInterceptorFactory GetChaosInterceptorFactory()
{
return this.chaosInterceptorFactory;
}

IChaosInterceptorFactory IFaultInjector.GetChaosInterceptorFactory()
{
return this.chaosInterceptorFactory;
}
}
}
2 changes: 2 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,8 @@ private int DecrementNumberOfActiveClients()
// In case dispose is called multiple times. Check if at least 1 active client is there
if (NumberOfActiveClients > 0)
{
CosmosDbOperationMeter.RemoveInstanceCount(this.Endpoint);

return Interlocked.Decrement(ref NumberOfActiveClients);
}

Expand Down
46 changes: 44 additions & 2 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace Microsoft.Azure.Cosmos
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Cosmos.FaultInjection;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
Expand Down Expand Up @@ -72,7 +73,8 @@ public class CosmosClientOptions
private PortReuseMode? portReuseMode;
private IWebProxy webProxy;
private Func<HttpClient> httpClientFactory;
private string applicationName;
private string applicationName;
private IFaultInjector faultInjector;

/// <summary>
/// Creates a new CosmosClientOptions
Expand Down Expand Up @@ -898,6 +900,46 @@ internal Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> GetServerCerti
/// </summary>
public CosmosClientTelemetryOptions CosmosClientTelemetryOptions { get; set; }

/// <summary>
/// Create a client with Fault Injection capabilities using the Cosmos DB Fault Injection Library.
/// </summary>
/// <example>
/// How to create a CosmosClient with Fault Injection capabilities.
/// <code language="c#">
/// <![CDATA[
/// FaultInjectionRule rule = new FaultInjectionRuleBuilder(
/// id: "ruleId",
/// condition: new FaultInjectionConditionBuilder()
/// .WithRegion("East US")
/// .Build(),
/// result: new FaultInjectionResultBuilder.GetResultBuilder(FaultInjectionServerErrorType.ServiceUnavailable)
/// .Build())
/// .Build();
///
/// FaultInjector faultInjector = new FaultInjector(new List<FaultInjectionRule>() { rule });
///
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// FaultInjector = faultInjector
/// };
///
/// CosmosClient client = new CosmosClient("connection string", clientOptions);
/// ]]>
/// </code>
/// </example>
public IFaultInjector FaultInjector
{
get => this.faultInjector;
set
{
this.faultInjector = value;
if (this.faultInjector != null)
{
this.ChaosInterceptorFactory = this.faultInjector.GetChaosInterceptorFactory();
}
}
}

internal IChaosInterceptorFactory ChaosInterceptorFactory { get; set; }

internal void SetSerializerIfNotConfigured(CosmosSerializer serializer)
Expand Down
8 changes: 8 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClientTelemetryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,13 @@ public class CosmosClientTelemetryOptions
/// but has to beware that customer data may be shown when the later option is chosen. It's the user's responsibility to sanitize the queries if necessary.
/// </summary>
public QueryTextMode QueryTextMode { get; set; } = QueryTextMode.None;

/// <summary>
/// Indicates whether client-side metrics collection is enabled or disabled.
/// When set to true, the application will capture and report client metrics such as request counts, latencies, errors, and other key performance indicators.
/// If false, no metrics related to the client will be gathered or reported.
/// <remarks>Metrics data can be published to a monitoring system like Prometheus or Azure Monitor, depending on the configured metrics provider.</remarks>
/// </summary>
public bool IsClientMetricsEnabled { get; set; }
}
}
7 changes: 7 additions & 0 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,13 @@ internal virtual void Initialize(Uri serviceEndpoint,
// Loading VM Information (non blocking call and initialization won't fail if this call fails)
VmMetadataApiHandler.TryInitialize(this.httpClient);

if (this.cosmosClientTelemetryOptions.IsClientMetricsEnabled)
{
CosmosDbOperationMeter.Initialize();

CosmosDbOperationMeter.AddInstanceCount(this.ServiceEndpoint);
}

// Starting ClientTelemetry Job
this.telemetryToServiceHelper = TelemetryToServiceHelper.CreateAndInitializeClientConfigAndTelemetryJob(this.clientId,
this.ConnectionPolicy,
Expand Down
16 changes: 16 additions & 0 deletions Microsoft.Azure.Cosmos/src/FaultInjection/IFaultInjector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.FaultInjection
{
/// <summary>
/// Interface for injecting faults into the Cosmos DB client operations.
/// </summary>
public interface IFaultInjector
{
/// <summary>
/// Gets the chaosInterceptorFactory for client building
/// </summary>
internal IChaosInterceptorFactory GetChaosInterceptorFactory();
}
}
12 changes: 12 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.Azure.Cosmos.Fluent
using global::Azure;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.FaultInjection;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

Expand Down Expand Up @@ -755,6 +756,17 @@ internal CosmosClientBuilder WithFaultInjection(IChaosInterceptorFactory chaosIn
{
this.clientOptions.ChaosInterceptorFactory = chaosInterceptorFactory;
return this;
}

/// <summary>
/// Enables SDK to inject fault. Used for testing applications.
/// </summary>
/// <param name="faultInjector"></param>
/// <returns>>The <see cref="CosmosClientBuilder"/> object</returns>
public CosmosClientBuilder WithFaultInjection(IFaultInjector faultInjector)
{
this.clientOptions.ChaosInterceptorFactory = faultInjector.GetChaosInterceptorFactory();
return this;
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public override async Task<ResponseMessage> SendAsync(
&& response.Content != null
&& response.Content is not CloneableStream)
{
response.Content = await StreamExtension.AsClonableStreamAsync(response.Content, default);
response.Content = await StreamExtension.AsClonableStreamAsync(
mediaStream: response.Content,
allowUnsafeDataAccess: true);
}

return response;
Expand Down
Loading

0 comments on commit 0b175ca

Please sign in to comment.