-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Initial addition of CosmosDB support, based on SqlServer * Remove Healthchecks support from CosmosDB EF Component * Cleanup connection string handling in Cosmos EF * Cleanup connection string handling in Cosmos component * Update CosmosDB package to get OTel support * Use the parent name for the connection * Udpate manifest strings * Add CosmosDB components to Progress and Telemetry * Rename CosmosDB components to Aspire.Azure.Data.Cosmos[.EntityFrameworkCore] * Rename options -> settings * Rename Cosmos Components to follow naming guidelines * Update to CosmosDB preview package and pin to get OpenTelemetry support * Update comments and add Keyed DI to Aspire.Microsoft.Azure.Cosmos * Add log categories to Cosmos Component schemas * Add basic support for CosmosClientOptions (no IConfiguration binding yet) * Remove healthchecks support from CosmosDB Component * Add README for Aspire.Microsoft.Azure.Cosmos * Add README for Aspire.Microsoft.EntityFrameworkCore.Cosmos, and rename a couple of things * Update config schema to be nested for Aspire.Microsoft.EntityFramework.Cosmos and Aspire.Microsoft.Azure.Cosmos * Rename AzureDataCosmosSettings -> AzureCosmosDBSettings * Update Aspire_Components_Progress.md * Add PackageTags, Descriptions, and Icons * Add AccountEndpoint to ConfigurationScheama.json * Fix DB context builder config * Add xml doc comments for CosmosDB hosting methods and types * Move Cosmos DB hosting to Aspire.Hosting.Azure * Update manifest type names * Respond to PR feedback Co-authored-by: Kevin Pilch <me@pilchie.com>
- Loading branch information
Showing
25 changed files
with
1,254 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Aspire.Hosting.Azure/AzureCosmosDBCloudApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.ApplicationModel; | ||
using Aspire.Hosting.Azure.Data.Cosmos; | ||
using System.Text.Json; | ||
|
||
namespace Aspire.Hosting; | ||
|
||
/// <summary> | ||
/// Provides extension methods for adding Azure Cosmos DB resources to an <see cref="IDistributedApplicationBuilder"/>. | ||
/// </summary> | ||
public static class AzureCosmosDBCloudApplicationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds an Azure Cosmos DB connection to the application model. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> | ||
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> | ||
/// <param name="connectionString">The connection string.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{AzureCosmosDatabaseResource}"/>.</returns> | ||
public static IResourceBuilder<AzureCosmosDBConnectionResource> AddAzureCosmosDB( | ||
this IDistributedApplicationBuilder builder, | ||
string name, | ||
string? connectionString = null) | ||
{ | ||
var connection = new AzureCosmosDBConnectionResource(name, connectionString); | ||
return builder.AddResource(connection) | ||
.WithAnnotation(new ManifestPublishingCallbackAnnotation(jsonWriter => WriteCosmosDBConnectionToManifest(jsonWriter, connection))); | ||
} | ||
|
||
private static void WriteCosmosDBConnectionToManifest(Utf8JsonWriter jsonWriter, AzureCosmosDBConnectionResource cosmosDbConnection) | ||
{ | ||
jsonWriter.WriteString("type", "azure.cosmosdb.connection.v0"); | ||
jsonWriter.WriteString("connectionString", cosmosDbConnection.GetConnectionString()); | ||
} | ||
|
||
private static void WriteCosmosDBDatabaseToManifest(Utf8JsonWriter jsonWriter, AzureCosmosDatabaseResource cosmosDatabase) | ||
{ | ||
jsonWriter.WriteString("type", "azure.cosmosdb.database.v0"); | ||
jsonWriter.WriteString("parent", cosmosDatabase.Parent.Name); | ||
jsonWriter.WriteString("databaseName", cosmosDatabase.Name); | ||
} | ||
|
||
/// <summary> | ||
/// Adds an Azure Cosmos DB database to a <see cref="IResourceBuilder{AzureCosmosDatabaseResource}"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> | ||
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{AzureCosmosDatabaseResource}"/>.</returns> | ||
public static IResourceBuilder<AzureCosmosDatabaseResource> AddDatabase(this IResourceBuilder<AzureCosmosDBConnectionResource> builder, string name) | ||
{ | ||
var cosmosDatabase = new AzureCosmosDatabaseResource(name, builder.Resource); | ||
return builder | ||
.ApplicationBuilder | ||
.AddResource(cosmosDatabase) | ||
.WithAnnotation(new ManifestPublishingCallbackAnnotation( | ||
(json) => WriteCosmosDBDatabaseToManifest(json, cosmosDatabase))); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Aspire.Hosting.Azure/AzureCosmosDBConnectionResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace Aspire.Hosting.Azure.Data.Cosmos; | ||
|
||
/// <summary> | ||
/// Represents a connection to an Azure Cosmos DB account. | ||
/// </summary> | ||
/// <param name="name">The resource name.</param> | ||
/// <param name="connectionString">The connection string to use to connect.</param> | ||
public class AzureCosmosDBConnectionResource(string name, string? connectionString) | ||
: Resource(name), IResourceWithConnectionString | ||
{ | ||
/// <summary> | ||
/// Gets the connection string to use for this database. | ||
/// </summary> | ||
/// <returns>The connection string to use for this database.</returns> | ||
public string? GetConnectionString() => connectionString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace Aspire.Hosting.Azure.Data.Cosmos; | ||
|
||
/// <summary> | ||
/// Represents an Azure Cosmos DB database. | ||
/// </summary> | ||
/// <param name="name">The database name.</param> | ||
/// <param name="parent">The parent <see cref="AzureCosmosDBConnectionResource"/>.</param> | ||
public class AzureCosmosDatabaseResource(string name, AzureCosmosDBConnectionResource parent) | ||
: Resource(name), IResourceWithParent<AzureCosmosDBConnectionResource>, IResourceWithConnectionString | ||
{ | ||
/// <summary> | ||
/// Gets the parent <see cref="AzureCosmosDBConnectionResource"/>. | ||
/// </summary> | ||
public AzureCosmosDBConnectionResource Parent { get; } = parent; | ||
|
||
/// <summary> | ||
/// Gets the connection string to use for this database. | ||
/// </summary> | ||
/// <returns>The connection string to use for this database.</returns> | ||
public string? GetConnectionString() | ||
{ | ||
return Parent.GetConnectionString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Components/Aspire.Microsoft.Azure.Cosmos/Aspire.Microsoft.Azure.Cosmos.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
<IsPackable>true</IsPackable> | ||
<EnableConfigurationBindingGenerator>false</EnableConfigurationBindingGenerator> | ||
<IsAotCompatible>false</IsAotCompatible> | ||
<PackageTags>$(ComponentAzurePackageTags) cosmos cosmosdb data database db</PackageTags> | ||
<Description>A client for Azure Cosmos DB that integrates with Aspire, including logging and telemetry.</Description> | ||
<PackageIconFullPath>$(SharedDir)AzureCosmosDB_256x.png</PackageIconFullPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" /> | ||
<PackageReference Include="Microsoft.Azure.Cosmos" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
126 changes: 126 additions & 0 deletions
126
src/Components/Aspire.Microsoft.Azure.Cosmos/AspireAzureCosmosDBExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Microsoft.Azure.Cosmos; | ||
using Azure.Identity; | ||
using Microsoft.Azure.Cosmos; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// Azure CosmosDB extension | ||
/// </summary> | ||
public static class AspireAzureCosmosDBExtensions | ||
{ | ||
private const string DefaultConfigSectionName = "Aspire:Microsoft:Azure:Cosmos"; | ||
|
||
/// <summary> | ||
/// Registers <see cref="CosmosClient" /> as a singleton in the services provided by the <paramref name="builder"/>. | ||
/// Configures logging and telemetry for the <see cref="CosmosClient" />. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> | ||
/// <param name="connectionName">The connection name to use to find a connection string.</param> | ||
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="AzureCosmosDBSettings"/>. It's invoked after the settings are read from the configuration.</param> | ||
/// <param name="configureClientOptions">An optional method that can be used for customizing the <see cref="CosmosClientOptions"/>.</param> | ||
/// <remarks>Reads the configuration from "Aspire:Microsoft:Azure:Cosmos" section.</remarks> | ||
/// <exception cref="InvalidOperationException">If required ConnectionString is not provided in configuration section</exception> | ||
public static void AddAzureCosmosDB( | ||
this IHostApplicationBuilder builder, | ||
string connectionName, | ||
Action<AzureCosmosDBSettings>? configureSettings = null, | ||
Action<CosmosClientOptions>? configureClientOptions = null) | ||
{ | ||
AddAzureCosmosDB(builder, DefaultConfigSectionName, configureSettings, configureClientOptions, connectionName, serviceKey: null); | ||
} | ||
|
||
/// <summary> | ||
/// Registers <see cref="CosmosClient" /> as a singleton for given <paramref name="name" /> in the services provided by the <paramref name="builder"/>. | ||
/// Configures logging and telemetry for the <see cref="CosmosClient" />. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> | ||
/// <param name="name">The name of the component, which is used as the <see cref="ServiceDescriptor.ServiceKey"/> of the service and also to retrieve the connection string from the ConnectionStrings configuration section.</param> | ||
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="AzureCosmosDBSettings"/>. It's invoked after the settings are read from the configuration.</param> | ||
/// <param name="configureClientOptions">An optional method that can be used for customizing the <see cref="CosmosClientOptions"/>.</param> | ||
/// <remarks>Reads the configuration from "Aspire:Microsoft:Azure:Cosmos:{name}" section.</remarks> | ||
/// <exception cref="InvalidOperationException">If required ConnectionString is not provided in configuration section</exception> | ||
public static void AddKeyedAzureCosmosDB( | ||
this IHostApplicationBuilder builder, | ||
string name, | ||
Action<AzureCosmosDBSettings>? configureSettings = null, | ||
Action<CosmosClientOptions>? configureClientOptions = null) | ||
{ | ||
AddAzureCosmosDB(builder, $"{DefaultConfigSectionName}:{name}", configureSettings, configureClientOptions, connectionName: name, serviceKey: name); | ||
} | ||
|
||
private static void AddAzureCosmosDB( | ||
this IHostApplicationBuilder builder, | ||
string configurationSectionName, | ||
Action<AzureCosmosDBSettings>? configureSettings, | ||
Action<CosmosClientOptions>? configureClientOptions, | ||
string connectionName, | ||
string? serviceKey) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
|
||
var settings = new AzureCosmosDBSettings(); | ||
builder.Configuration.GetSection(configurationSectionName).Bind(settings); | ||
|
||
if (builder.Configuration.GetConnectionString(connectionName) is string connectionString) | ||
{ | ||
if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri)) | ||
{ | ||
settings.AccountEndpoint = uri; | ||
} | ||
else | ||
{ | ||
settings.ConnectionString = connectionString; | ||
} | ||
} | ||
|
||
configureSettings?.Invoke(settings); | ||
|
||
var clientOptions = new CosmosClientOptions(); | ||
// Needs to be enabled for either logging or tracing to work. | ||
clientOptions.CosmosClientTelemetryOptions.DisableDistributedTracing = false; | ||
if (settings.Tracing) | ||
{ | ||
builder.Services.AddOpenTelemetry().WithTracing(tracerProviderBuilder => | ||
{ | ||
tracerProviderBuilder.AddSource("Azure.Cosmos.Operation"); | ||
}); | ||
} | ||
|
||
configureClientOptions?.Invoke(clientOptions); | ||
|
||
if (serviceKey is null) | ||
{ | ||
builder.Services.AddSingleton(_ => ConfigureDb()); | ||
} | ||
else | ||
{ | ||
builder.Services.AddKeyedSingleton(serviceKey, (sp, key) => ConfigureDb()); | ||
} | ||
|
||
CosmosClient ConfigureDb() | ||
{ | ||
if (!string.IsNullOrEmpty(settings.ConnectionString)) | ||
{ | ||
return new CosmosClient(settings.ConnectionString, clientOptions); | ||
} | ||
else if (settings.AccountEndpoint is not null) | ||
{ | ||
var credential = settings.Credential ?? new DefaultAzureCredential(); | ||
return new CosmosClient(settings.AccountEndpoint.OriginalString, credential, clientOptions); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException( | ||
$"A CosmosClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or either " + | ||
$"{nameof(settings.ConnectionString)} or {nameof(settings.AccountEndpoint)} must be provided " + | ||
$"in the '{configurationSectionName}' configuration section."); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Components/Aspire.Microsoft.Azure.Cosmos/AzureCosmosDBSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Azure.Core; | ||
|
||
namespace Aspire.Microsoft.Azure.Cosmos; | ||
|
||
/// <summary> | ||
/// The settings relevant to accessing Azure Cosmos DB. | ||
/// </summary> | ||
public sealed class AzureCosmosDBSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the connection string of the Azure Cosmos database to connect to. | ||
/// </summary> | ||
public string? ConnectionString { get; set; } | ||
|
||
/// <summary> | ||
/// <para>Gets or sets a boolean value that indicates whether the OpenTelemetry tracing is enabled or not.</para> | ||
/// <para>Enabled by default.</para> | ||
/// </summary> | ||
public bool Tracing { get; set; } = true; | ||
|
||
/// <summary> | ||
/// A <see cref="Uri"/> referencing the Azure Cosmos DB Endpoint. | ||
/// This is likely to be similar to "https://{account_name}.queue.core.windows.net". | ||
/// </summary> | ||
/// <remarks> | ||
/// Must not contain shared access signature. | ||
/// Used along with <see cref="Credential"/> to establish the connection. | ||
/// </remarks> | ||
public Uri? AccountEndpoint { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the credential used to authenticate to the Azure Cosmos DB endpoint. | ||
/// </summary> | ||
public TokenCredential? Credential { get; set; } | ||
} | ||
|
Oops, something went wrong.