diff --git a/README.md b/README.md index b1472bc..ac0d51d 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ For configuring how the library connects to Cosmos, the library uses the `Cosmos | `DatabaseThroughput` | The throughput provisioned for the database in measurement of Request Units per second in the Azure Cosmos DB service. | | `SerializerOptions` | The `JsonSerializerOptions` used for the `System.Text.Json.JsonSerializer`. | | `Credential` | The `TokenCredential` used for accessing [cosmos with an Azure AD token](https://docs.microsoft.com/en-us/azure/cosmos-db/managed-identity-based-authentication). Please note that setting this property will ignore any value specified in `AccountKey`. | +| `TelemetryOptions` | Configure the telemetry exposed about the Cosmos usage. |
diff --git a/src/Atc.Cosmos/CosmosOptions.cs b/src/Atc.Cosmos/CosmosOptions.cs index 7625a2b..437b9f3 100644 --- a/src/Atc.Cosmos/CosmosOptions.cs +++ b/src/Atc.Cosmos/CosmosOptions.cs @@ -1,5 +1,6 @@ using System.Text.Json; using Azure.Core; +using Microsoft.Azure.Cosmos; namespace Atc.Cosmos { @@ -67,5 +68,10 @@ public class CosmosOptions /// When is provided the property is not needed. /// public TokenCredential? Credential { get; set; } + + /// + /// Gets or sets the TelemetryOptions/>. + /// + public CosmosClientTelemetryOptions TelemetryOptions { get; set; } = new (); } } \ No newline at end of file diff --git a/src/Atc.Cosmos/Internal/CosmosClientProvider.cs b/src/Atc.Cosmos/Internal/CosmosClientProvider.cs index 09af532..4a6a9d7 100644 --- a/src/Atc.Cosmos/Internal/CosmosClientProvider.cs +++ b/src/Atc.Cosmos/Internal/CosmosClientProvider.cs @@ -7,28 +7,19 @@ namespace Atc.Cosmos.Internal { - public sealed class CosmosClientProvider : IDisposable, ICosmosClientProvider + public sealed class CosmosClientProvider( + IOptions cosmosClientOptions, + IJsonCosmosSerializer serializer) + : IDisposable, ICosmosClientProvider { - private readonly IOptions cosmosClientOptions; - private readonly IJsonCosmosSerializer serializer; - private readonly ConcurrentDictionary cosmosClientCache; - private readonly ConcurrentDictionary cosmosBulkClientCache; - - public CosmosClientProvider( - IOptions cosmosClientOptions, - IJsonCosmosSerializer serializer) - { - this.cosmosClientOptions = cosmosClientOptions; - this.serializer = serializer; - cosmosClientCache = new ConcurrentDictionary(); - cosmosBulkClientCache = new ConcurrentDictionary(); - } + private readonly ConcurrentDictionary cosmosClientCache = new (); + private readonly ConcurrentDictionary cosmosBulkClientCache = new (); public CosmosClient GetClient(CosmosOptions options) - => cosmosClientCache.AddOrUpdate(options, o => CreateClient(o, allowBulk: false), (o, c) => c); + => cosmosClientCache.AddOrUpdate(options, CreateClient, (_, c) => c); public CosmosClient GetBulkClient(CosmosOptions options) - => cosmosBulkClientCache.AddOrUpdate(options, o => CreateClient(o, allowBulk: true), (o, c) => c); + => cosmosBulkClientCache.AddOrUpdate(options, CreateBulkClient, (_, c) => c); public void Dispose() { @@ -43,6 +34,10 @@ public void Dispose() } } + private CosmosClient CreateBulkClient(CosmosOptions cosmosOptions) => CreateClient(cosmosOptions, true); + + private CosmosClient CreateClient(CosmosOptions cosmosOptions) => CreateClient(cosmosOptions, false); + private CosmosClient CreateClient(CosmosOptions cosmosOptions, bool allowBulk) { var connectionString = @@ -51,15 +46,15 @@ private CosmosClient CreateClient(CosmosOptions cosmosOptions, bool allowBulk) var options = CreateCosmosClientOptions(); options.AllowBulkExecution = allowBulk; - options.Serializer = cosmosClientOptions.Value.Serializer - ?? new CosmosSerializerAdapter(serializer); + options.Serializer = cosmosClientOptions.Value.Serializer ?? new CosmosSerializerAdapter(serializer); + options.CosmosClientTelemetryOptions = cosmosClientOptions.Value.CosmosClientTelemetryOptions; return cosmosOptions.Credential is not null - ? new CosmosClient( - cosmosOptions.AccountEndpoint, - cosmosOptions.Credential, - options) - : new CosmosClient(connectionString, options); + ? new CosmosClient( + cosmosOptions.AccountEndpoint, + cosmosOptions.Credential, + options) + : new CosmosClient(connectionString, options); } private CosmosClientOptions CreateCosmosClientOptions() @@ -97,6 +92,7 @@ private CosmosClientOptions CreateCosmosClientOptions() result.EnableTcpConnectionEndpointRediscovery = o.EnableTcpConnectionEndpointRediscovery; result.GatewayModeMaxConnectionLimit = o.GatewayModeMaxConnectionLimit; result.MaxRetryAttemptsOnRateLimitedRequests = o.MaxRetryAttemptsOnRateLimitedRequests; + result.CosmosClientTelemetryOptions = o.CosmosClientTelemetryOptions; } return result;