MySqlConnector
MySqlConnector copied to clipboard
Extract metrics from a connection pool
Is there a way to extract some metrics from the pool like idle connections, connections in use, available connections... ?
There is not currently any such method.
Do you know of another ADO.NET provider that exposes similar metrics? (Just curious if there's an existing example of exposing such an API.)
We can gather it from the MySql Oracle driver through the reflection, however, I'm wondering if we can do it in this driver in a more elegant way
Interesting question indeed! Would a pull-request be accepted which introduces some metrics abstraction on the lines of the logging abstraction? I'm thinking about aIMySqlConnectorMetricsProvider
kind of thing.
I am looking for guidance (from the ADO.NET or ASP.NET Core team) about high-performance instrumentation with EventSource or DiagnosticSource (#471). One can also imagine exposing Prometheus-style metrics or integrating with OpenTracing, and I'd like to do it in a way that's standardised with other ADO.NET providers, not just implemented in an ad-hoc way.
It would also be extremely important to implement in a zero-overhead way when not activated, e.g., to not impact TFB performance benchmarks.
I see, thanks! I've stumbled over #471 after I wrote this comment and figured this would be the way to go anyway.
https://github.com/mysql-net/MySqlConnector/issues/1137 provides a link to SQL Server Provider Statistics, which is an in-memory (?) Dictionary enabled via a connection-string setting. This could provide a template for a low-overhead approach, but also seems like it might be an historical method that would now be implemented instead with OpenTelemetry.
We can now use System.Diagnostics.Metrics for integration-agnostic metrics: https://docs.microsoft.com/en-us/dotnet/core/diagnostics/metrics-instrumentation
Hi, @bgrainger. Do you have any update for this topic?
Based on Metric APIs comparison, System.Diagnostics.Metrics
(.NET 6.0 and later) is the best approach to use for tracking metrics.
This namespace doesn't appear to be used by Npsql or Microsoft.Data.SqlClient, so there's no "prior art" to adapt for MySQL.
@bgrainger https://www.npgsql.org/doc/diagnostics/metrics.html It is possible to create metrics view as maked in pgsql?
Using System.Diagnostics.Metrics
in MySqlConnector would make it possible for dotnet-counters monitor MySqlConnector -p <PID>
to produce similar output to that table, yes.
@bgrainger If I understand correctly, this is currently not implemented. I have searched the repository and have not found any evidence of the System.Diagnostics.Metrics namespace being used. Right?
Correct; this issue is still open because the change has not been implemented.
I opened a PR with a proposal for a specification for using System.Diagnostics.Metrics
to implement the OpenTelemetry Semantic Conventions for Database Metrics: https://github.com/mysql-net/MySqlConnector/pull/1328.
This will provide a way to "extract some metrics from the pool like idle connections, connections in use, available connections" using the latest supported .NET APIs.