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

Implement CosmosDBOptions to allow configuration of the CosmosDB service client via CosmosClientOptions #2483

Merged
merged 9 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
27 changes: 23 additions & 4 deletions extensions/Worker.Extensions.CosmosDB/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@
- My change description (#PR/#issue)
-->

### Microsoft.Azure.Functions.Worker.Extensions.CosmosDB 4.8.1
### Microsoft.Azure.Functions.Worker.Extensions.CosmosDB 5.0.0

- Updating `Microsoft.Azure.WebJobs.Extensions.CosmosDB` reference to 4.6.1
- Updating `Microsoft.Extensions.Azure` reference to 1.7.3
- Updating `Microsoft.Azure.Cosmos` reference to 3.39.1
- Expose `CosmosClientOptions` to allow configuration of the CosmosDB client (#2483)

#### Breaking Change

- **Default `ConnectionMode` Change:** The default `ConnectionMode` for `CosmosClientOptions` has been changed from `Gateway` to `Direct`.
This change is due to `Direct` being the default value for `ConnectionMode` in the Cosmos SDK; now that we are exposing those options,
it is not possible for us to tell whether the user has explicitly set the connection mode to `Direct` or not, so we must default to the
SDK's default value

##### How to Maintain Previous Behavior

To continue using `Gateway` mode, configure the `CosmosClientOptions` in your `Program` class as shown below:

```csharp
.ConfigureFunctionsWorkerDefaults((builder) =>
{
builder.Services.Configure<CosmosClientOptions>((options) =>
liliankasem marked this conversation as resolved.
Show resolved Hide resolved
{
options.ConnectionMode = ConnectionMode.Gateway;
});
})
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal class CosmosDBBindingOptions

public CosmosSerializer? Serializer { get; set; }

public CosmosClientOptions? CosmosClientOptions { get; set; }

internal string BuildCacheKey(string connection, string region) => $"{connection}|{region}";

internal ConcurrentDictionary<string, CosmosClient> ClientCache { get; } = new ConcurrentDictionary<string, CosmosClient>();
Expand All @@ -34,29 +36,26 @@ internal virtual CosmosClient GetClient(string preferredLocations = "")

string cacheKey = BuildCacheKey(ConnectionName!, preferredLocations);

CosmosClientOptions cosmosClientOptions = new ()
{
ConnectionMode = ConnectionMode.Gateway
};

if (!string.IsNullOrEmpty(preferredLocations))
// Do not override if preferred locations is configured via CosmosClientOptions
if (!string.IsNullOrEmpty(preferredLocations) && CosmosClientOptions.ApplicationPreferredRegions is null)
{
cosmosClientOptions.ApplicationPreferredRegions = Utilities.ParsePreferredLocations(preferredLocations);
CosmosClientOptions.ApplicationPreferredRegions = Utilities.ParsePreferredLocations(preferredLocations);
}

if (Serializer is not null)
// Do not override if the serializer is configured via CosmosClientOptions
if (Serializer is not null && CosmosClientOptions.Serializer is null)
{
cosmosClientOptions.Serializer = Serializer;
CosmosClientOptions.Serializer = Serializer;
}

return ClientCache.GetOrAdd(cacheKey, (c) => CreateService(cosmosClientOptions));
return ClientCache.GetOrAdd(cacheKey, (c) => CreateService());
}

private CosmosClient CreateService(CosmosClientOptions cosmosClientOptions)
private CosmosClient CreateService()
{
return string.IsNullOrEmpty(ConnectionString)
? new CosmosClient(AccountEndpoint, Credential, cosmosClientOptions) // AAD auth
: new CosmosClient(ConnectionString, cosmosClientOptions); // Connection string based auth
? new CosmosClient(AccountEndpoint, Credential, CosmosClientOptions) // AAD auth
: new CosmosClient(ConnectionString, CosmosClientOptions); // Connection string based auth
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Functions.Worker.Extensions;
using Microsoft.Azure.Functions.Worker.Extensions.CosmosDB;
using Microsoft.Extensions.Azure;
Expand All @@ -15,12 +16,14 @@ internal class CosmosDBBindingOptionsSetup : IConfigureNamedOptions<CosmosDBBind
private readonly IConfiguration _configuration;
private readonly AzureComponentFactory _componentFactory;
private readonly IOptionsMonitor<WorkerOptions> _workerOptions;
private readonly IOptionsMonitor<CosmosClientOptions> _cosmosClientOptions;

public CosmosDBBindingOptionsSetup(IConfiguration configuration, AzureComponentFactory componentFactory, IOptionsMonitor<WorkerOptions> workerOptions)
public CosmosDBBindingOptionsSetup(IConfiguration configuration, AzureComponentFactory componentFactory, IOptionsMonitor<WorkerOptions> workerOptions, IOptionsMonitor<CosmosClientOptions> cosmosClientOptions)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_componentFactory = componentFactory ?? throw new ArgumentNullException(nameof(componentFactory));
_workerOptions = workerOptions ?? throw new ArgumentNullException(nameof(workerOptions));
_cosmosClientOptions = cosmosClientOptions ?? throw new ArgumentNullException(nameof(cosmosClientOptions));
}

public void Configure(CosmosDBBindingOptions options)
Expand Down Expand Up @@ -57,6 +60,7 @@ public void Configure(string connectionName, CosmosDBBindingOptions options)
}

options.Serializer = new WorkerCosmosSerializer(_workerOptions.CurrentValue.Serializer);
options.CosmosClientOptions = _cosmosClientOptions.CurrentValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Description>Azure Cosmos DB extensions for .NET isolated functions</Description>

<!--Version information-->
<VersionPrefix>4.8.1</VersionPrefix>
<VersionPrefix>5.0.0</VersionPrefix>
liliankasem marked this conversation as resolved.
Show resolved Hide resolved

<!--Temporarily opting out of documentation. Pending documentation-->
<GenerateDocumentationFile>false</GenerateDocumentationFile>
Expand Down
Loading