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

Add Aspire.MySqlConnector #825

Merged
merged 14 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.RabbitMQ.Client", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.RabbitMQ.Client.Tests", "tests\Aspire.RabbitMQ.Client.Tests\Aspire.RabbitMQ.Client.Tests.csproj", "{165411FE-755E-4869-A756-F87F455860AC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.MySqlConnector", "src\Components\Aspire.MySqlConnector\Aspire.MySqlConnector.csproj", "{CA283D7F-EB95-4353-B196-C409965D2B42}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -406,6 +408,10 @@ Global
{165411FE-755E-4869-A756-F87F455860AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{165411FE-755E-4869-A756-F87F455860AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{165411FE-755E-4869-A756-F87F455860AC}.Release|Any CPU.Build.0 = Release|Any CPU
{CA283D7F-EB95-4353-B196-C409965D2B42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA283D7F-EB95-4353-B196-C409965D2B42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA283D7F-EB95-4353-B196-C409965D2B42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA283D7F-EB95-4353-B196-C409965D2B42}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -477,6 +483,7 @@ Global
{A84C4EE3-2601-4804-BCDC-E9948E164A22} = {A68BA1A5-1604-433D-9778-DC0199831C2A}
{4D8A92AB-4E77-4965-AD8E-8E206DCE66A4} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
{165411FE-755E-4869-A756-F87F455860AC} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
{CA283D7F-EB95-4353-B196-C409965D2B42} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}
Expand Down
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<PackageVersion Include="AspNetCore.HealthChecks.Azure.Storage.Blobs" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.Azure.Storage.Queues" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.AzureServiceBus" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.MySql" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.Rabbitmq" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.Redis" Version="7.0.1" />
Expand Down Expand Up @@ -73,6 +74,7 @@
<PackageVersion Include="JsonSchema.Net" Version="5.2.5" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.0.0-rc.3" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.0.0-rc.3" />
<PackageVersion Include="MySqlConnector.DependencyInjection" Version="2.3.0" />
<PackageVersion Include="Npgsql.DependencyInjection" Version="8.0.0-rc.2" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2" />
<PackageVersion Include="Polly" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCurrent)</TargetFramework>
<IsPackable>true</IsPackable>
<PackageTags>$(ComponentDatabasePackageTags) mysqlconnector mysql sql</PackageTags>
<Description>A MySQL client that integrates with Aspire, including health checks, metrics, logging, and telemetry.</Description>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does MySQL need a ® sign after it, like we do for PostgreSQL?

<PackageIconFullPath>$(SharedDir)SQL_256x.png</PackageIconFullPath>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DamianEdwards - should this have the same icon as the https://www.nuget.org/packages/MySqlConnector/ package?

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be ideal, assuming we have permission to do so 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the MySqlConnector package icon (using free sources) and will give any necessary permission to include it here.

</PropertyGroup>

<ItemGroup>
<Compile Include="..\Common\HealthChecksExtensions.cs" Link="HealthChecksExtensions.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MySql" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="MySqlConnector.DependencyInjection" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data.Common;
using Aspire;
using Aspire.MySqlConnector;
using HealthChecks.MySql;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using MySqlConnector;

namespace Microsoft.Extensions.Hosting;

/// <summary>
/// Extension methods for connecting MySQL database with MySqlConnector client
/// </summary>
public static class AspireMySqlConnectorExtensions
{
private const string DefaultConfigSectionName = "Aspire:MySql";
bgrainger marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Registers <see cref="MySqlDataSource"/> service for connecting MySQL database with MySqlConnector client.
/// Configures health check, logging and telemetry for the MySqlConnector client.
/// </summary>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
/// <param name="connectionName">A name used to retrieve the connection string from the ConnectionStrings configuration section.</param>
/// <param name="configureSettings">An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:MySql" section.</remarks>
/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="MySqlConnectorSettings.ConnectionString"/> is not provided.</exception>
public static void AddMySqlDataSource(this IHostApplicationBuilder builder, string connectionName, Action<MySqlConnectorSettings>? configureSettings = null)
=> AddMySqlDataSource(builder, DefaultConfigSectionName, configureSettings, connectionName, serviceKey: null);

/// <summary>
/// Registers <see cref="MySqlDataSource"/> as a keyed service for given <paramref name="name"/> for connecting MySQL database with MySqlConnector client.
/// Configures health check, logging and telemetry for the MySqlConnector client.
/// </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 options. It's invoked after the settings are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:MySql:{name}" section.</remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="name"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown if mandatory <paramref name="name"/> is empty.</exception>
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="MySqlConnectorSettings.ConnectionString"/> is not provided.</exception>
public static void AddKeyedMySqlDataSource(this IHostApplicationBuilder builder, string name, Action<MySqlConnectorSettings>? configureSettings = null)
{
ArgumentException.ThrowIfNullOrEmpty(name);

AddMySqlDataSource(builder, $"{DefaultConfigSectionName}:{name}", configureSettings, connectionName: name, serviceKey: name);
}

private static void AddMySqlDataSource(IHostApplicationBuilder builder, string configurationSectionName,
Action<MySqlConnectorSettings>? configureSettings, string connectionName, object? serviceKey)
{
ArgumentNullException.ThrowIfNull(builder);

MySqlConnectorSettings settings = new();
builder.Configuration.GetSection(configurationSectionName).Bind(settings);

if (builder.Configuration.GetConnectionString(connectionName) is string connectionString)
{
settings.ConnectionString = connectionString;
}

configureSettings?.Invoke(settings);

builder.RegisterMySqlServices(settings, configurationSectionName, connectionName, serviceKey);

// Same as SqlClient connection pooling is on by default and can be handled with connection string
// https://mysqlconnector.net/connection-options/#Pooling
if (settings.HealthChecks)
{
builder.TryAddHealthCheck(new HealthCheckRegistration(
serviceKey is null ? "MySql" : $"MySql_{connectionName}",
sp => new MySqlHealthCheck(new MySqlHealthCheckOptions()
{
ConnectionString = serviceKey is null
? sp.GetRequiredService<MySqlDataSource>().ConnectionString
: sp.GetRequiredKeyedService<MySqlDataSource>(serviceKey).ConnectionString
Comment on lines +78 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about opening a PR to https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks to make this consistent with Npgsql? We shouldn't be creating another DbDataSource and checking the health of that. Instead, we should be using the same DbDataSource instance as the app is using. That way if that DbDataSource ever gets corrupt, the health check is accurate. From the Components README:

- Consider whether the Health Check should reuse the same client object registered in DI by the component or not. Reusing the same client object has the advantages of getting the same configuration, logging, etc during the health check.

See also Xabaril/AspNetCore.Diagnostics.HealthChecks#1832

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good; I have another reason to open a PR there, too: Xabaril/AspNetCore.Diagnostics.HealthChecks#2031.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}),
failureStatus: default,
tags: default,
timeout: default));
}

if (settings.Tracing)
{
builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
{
tracerProviderBuilder.AddSource("MySqlConnector");
});
}

if (settings.Metrics)
{
builder.Services.AddOpenTelemetry()
.WithMetrics(MySqlConnectorCommon.AddMySqlMetrics);
}
}

private static void RegisterMySqlServices(this IHostApplicationBuilder builder, MySqlConnectorSettings settings, string configurationSectionName, string connectionName, object? serviceKey)
{
if (serviceKey is null)
{
// delay validating the ConnectionString until the DataSource is requested. This ensures an exception doesn't happen until a Logger is established.
builder.Services.AddMySqlDataSource(settings.ConnectionString ?? string.Empty, dataSourceBuilder =>
{
ValidateConnection();
});
}
else
{
// Currently MySqlConnector does not support Keyed DI Registration, so we implement it on our own.
bgrainger marked this conversation as resolved.
Show resolved Hide resolved
// Register a MySqlDataSource factory method, based on https://github.com/mysql-net/MySqlConnector/blob/d895afc013a5849d33a123a7061442e2cbb9ce76/src/MySqlConnector.DependencyInjection/MySqlConnectorServiceCollectionExtensions.cs#L57-L60
builder.Services.AddKeyedSingleton<MySqlDataSource>(serviceKey, (serviceProvider, _) =>
{
ValidateConnection();

var dataSourceBuilder = new MySqlDataSourceBuilder(settings.ConnectionString);
dataSourceBuilder.UseLoggerFactory(serviceProvider.GetService<ILoggerFactory>());
return dataSourceBuilder.Build();
});
// Common Services, based on https://github.com/mysql-net/MySqlConnector/blob/d895afc013a5849d33a123a7061442e2cbb9ce76/src/MySqlConnector.DependencyInjection/MySqlConnectorServiceCollectionExtensions.cs#L64-L70
// They let the users resolve MySqlConnection directly.
builder.Services.AddKeyedSingleton<DbDataSource>(serviceKey, static (serviceProvider, key) => serviceProvider.GetRequiredKeyedService<MySqlDataSource>(key));
builder.Services.AddKeyedTransient<MySqlConnection>(serviceKey, static (serviceProvider, key) => serviceProvider.GetRequiredKeyedService<MySqlDataSource>(key).CreateConnection());
builder.Services.AddKeyedTransient<DbConnection>(serviceKey, static (serviceProvider, key) => serviceProvider.GetRequiredKeyedService<MySqlConnection>(key));
}

void ValidateConnection()
{
if (string.IsNullOrEmpty(settings.ConnectionString))
{
throw new InvalidOperationException($"ConnectionString is missing. It should be provided in 'ConnectionStrings:{connectionName}' or under the 'ConnectionString' key in '{configurationSectionName}' configuration section.");
}
}
}
}
58 changes: 58 additions & 0 deletions src/Components/Aspire.MySqlConnector/ConfigurationSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"definitions": {
"logLevel": {
"properties": {
"MySqlConnector": {
"$ref": "#/definitions/logLevelThreshold"
},
"MySqlConnector.ConnectionPool": {
"$ref": "#/definitions/logLevelThreshold"
},
"MySqlConnector.MySqlBulkCopy": {
"$ref": "#/definitions/logLevelThreshold"
},
"MySqlConnector.MySqlCommand": {
"$ref": "#/definitions/logLevelThreshold"
},
"MySqlConnector.MySqlConnection": {
"$ref": "#/definitions/logLevelThreshold"
},
"MySqlConnector.MySqlDataSource": {
"$ref": "#/definitions/logLevelThreshold"
}
}
}
},
"properties": {
"Aspire": {
"type": "object",
"properties": {
"MySql": {
"type": "object",
"properties": {
"ConnectionString": {
"type": "string",
"description": "Gets or sets the connection string of the MySQL database to connect to."
},
"HealthChecks": {
"type": "boolean",
"description": "Gets or sets a boolean value that indicates whether the database health check is enabled or not.",
"default": true
},
"Tracing": {
"type": "boolean",
"description": "Gets or sets a boolean value that indicates whether the OpenTelemetry tracing is enabled or not.",
"default": true
},
"Metrics": {
"type": "boolean",
"description": "Gets or sets a boolean value that indicates whether the OpenTelemetry metrics are enabled or not.",
"default": true
}
}
}
}
}
},
"type": "object"
}
14 changes: 14 additions & 0 deletions src/Components/Aspire.MySqlConnector/MySqlConnectorCommon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using OpenTelemetry.Metrics;

internal static class MySqlConnectorCommon
{
public static void AddMySqlMetrics(MeterProviderBuilder meterProviderBuilder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need this given we only have 1 component. Is there an EntityFramework "MySqlConnector" library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EF Core library built on top of MySqlConnector is https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to add support for that too, but will probably do it as a separate PR.

{
meterProviderBuilder
.AddMeter("MySqlConnector")
.AddView("db.client.connections.create_time", new HistogramConfiguration());
bgrainger marked this conversation as resolved.
Show resolved Hide resolved
}
}
33 changes: 33 additions & 0 deletions src/Components/Aspire.MySqlConnector/MySqlConnectorSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Aspire.MySqlConnector;

/// <summary>
/// Provides the client configuration settings for connecting to a MySQL database using MySqlConnector.
/// </summary>
public sealed class MySqlConnectorSettings
{
/// <summary>
/// The connection string of the MySQL database to connect to.
/// </summary>
public string? ConnectionString { get; set; }

/// <summary>
/// <para>Gets or sets a boolean value that indicates whether the database health check is enabled or not.</para>
/// <para>Enabled by default.</para>
/// </summary>
public bool HealthChecks { get; set; } = true;

/// <summary>
/// <para>Gets or sets a boolean value that indicates whether the Open Telemetry tracing is enabled or not.</para>
/// <para>Enabled by default.</para>
/// </summary>
public bool Tracing { get; set; } = true;

/// <summary>
/// <para>Gets or sets a boolean value that indicates whether the Open Telemetry metrics are enabled or not.</para>
/// <para>Enabled by default.</para>
/// </summary>
public bool Metrics { get; set; } = true;
}
Loading