Skip to content

Commit

Permalink
Merge pull request #27 from alexvaluyskiy/feature/sqlclient_counters
Browse files Browse the repository at this point in the history
Use the new SqlClient counters
  • Loading branch information
alexvaluyskiy authored Mar 23, 2021
2 parents 2dc6cee + 98d59df commit f2973fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion samples/WebApp/WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.0-preview1.21075.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Prometheus.Contrib.EventListeners.Counters;

namespace Prometheus.Contrib.EventListeners.Adapters
{
internal class PrometheusSqlClientCounterAdapter : BaseAdapter
{
public const string EventSourceName = "Microsoft.Data.SqlClient.EventSource";

internal readonly MeanCounter ActiveHardConnections = new MeanCounter("active-hard-connections", "sqlclient_active_hard_connections_total", "Actual active connections are made to servers");
internal readonly IncrementCounter HardConnects = new IncrementCounter("hard-connects", "sqlclient_hard_connects_per_second", "Actual connections are made to servers");
internal readonly IncrementCounter HardDisconnects = new IncrementCounter("hard-disconnects", "sqlclient_hard_disconnects_per_second", "Actual disconnections are made to servers");
internal readonly MeanCounter ActiveSoftConnections = new MeanCounter("active-soft-connects", "sqlclient_active_soft_connections_total", "Active connections got from connection pool");
internal readonly IncrementCounter SoftConnects = new IncrementCounter("soft-connects", "sqlclient_soft_connects_per_second", "Connections got from connection pool");
internal readonly IncrementCounter SoftDisconnects = new IncrementCounter("soft-disconnects", "sqlclient_soft_disconnects_per_second", "Connections returned to the connection pool");
internal readonly MeanCounter NumberOfNonPooledConnections = new MeanCounter("number-of-non-pooled-connections", "sqlclient_non_pooled_connections_total", "Number of connections are not using connection pooling");
internal readonly MeanCounter NumberOfPooledConnections = new MeanCounter("number-of-pooled-connections", "sqlclient_pooled_connections_total", "Number of connections are managed by connection pooler");
internal readonly MeanCounter NumberOfActiveConnectionPoolGroups = new MeanCounter("number-of-active-connection-pool-groups", "sqlclient_active_connection_pool_groups_total", "Number of active unique connection strings");
internal readonly MeanCounter NumberOfInactiveConnectionPoolGroups = new MeanCounter("number-of-inactive-connection-pool-groups", "sqlclient_inactive_connection_pool_groups_total", "Number of unique connection strings waiting for pruning");
internal readonly MeanCounter NumberOfActiveConnectionPools = new MeanCounter("number-of-active-connection-pools", "sqlclient_active_connection_pools_total", "Number of active connection pools");
internal readonly MeanCounter NumberOfInactiveConnectionPools = new MeanCounter("number-of-inactive-connection-pools", "sqlclient_inactive_connection_pools_total", "Number of inactive connection pools");
internal readonly MeanCounter NumberOfActiveConnections = new MeanCounter("number-of-active-connections", "sqlclient_active_connections_total", "Number of active connections");
internal readonly MeanCounter NumberOfFreeConnections = new MeanCounter("number-of-free-connections", "sqlclient_free_connections_total", "Number of free-ready connections");
internal readonly MeanCounter NumberOfStasisConnections = new MeanCounter("number-of-stasis-connections", "sqlclient_stasis_connections_total", "Number of connections currently waiting to be ready");
internal readonly IncrementCounter NumberOfReclaimedConnections = new IncrementCounter("number-of-reclaimed-connections", "sqlclient_reclaimed_connections_total", "Number of reclaimed connections from GC");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ internal class CountersEventListener : EventListener
[PrometheusHttpClientCounterAdapter.EventSourceName] = new PrometheusHttpClientCounterAdapter(),
[PrometheusNetSecurityCounterAdapter.EventSourceName] = new PrometheusNetSecurityCounterAdapter(),
[PrometheusNetNameResolutionCounterAdapter.EventSourceName] = new PrometheusNetNameResolutionCounterAdapter(),
[PrometheusNetSocketsCounterAdapter.EventSourceName] = new PrometheusNetSocketsCounterAdapter()
[PrometheusNetSocketsCounterAdapter.EventSourceName] = new PrometheusNetSocketsCounterAdapter(),
[PrometheusSqlClientCounterAdapter.EventSourceName] = new PrometheusSqlClientCounterAdapter()
};

internal CountersEventListener(int refreshPeriodSeconds = 10)
{
this.refreshPeriodSeconds = refreshPeriodSeconds;

EventSourceCreated += OnEventSourceCreated;
}

Expand All @@ -38,22 +39,22 @@ private void OnEventSourceCreated(object sender, EventSourceCreatedEventArgs e)
{
return;
}

var args = new Dictionary<string, string>
{
["EventCounterIntervalSec"] = refreshPeriodSeconds.ToString()
};

EnableEvents(e.EventSource, EventLevel.Verbose, EventKeywords.All, args);
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventName is null || !eventData.EventName.Equals("EventCounters") || eventData.Payload == null)
{
return;
}

if (!counterAdapters.ContainsKey(eventData.EventSource.Name))
{
return;
Expand Down

0 comments on commit f2973fc

Please sign in to comment.