-
Notifications
You must be signed in to change notification settings - Fork 526
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
Changes from 1 commit
abb3d0b
0c3f020
1deac05
b6a5e0e
3255804
5b16736
ca9ef22
9f85ccf
68077a2
b1c30a9
a64653f
4ad05e2
28c84fa
ec14198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
<PackageIconFullPath>$(SharedDir)SQL_256x.png</PackageIconFullPath> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be ideal, assuming we have permission to do so 😀 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: aspire/src/Components/README.md Line 68 in ab931f0
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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."); | ||||
} | ||||
} | ||||
} | ||||
} |
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" | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
} | ||
} |
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; | ||
} |
There was a problem hiding this comment.
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?