Last active
December 20, 2022 07:25
-
-
Save SlyNet/f9b37a3d9e335770d4153494c9bb5deb to your computer and use it in GitHub Desktop.
Npgsql connections counter publish to Prometheus
This file contains hidden or 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
internal sealed class NpgsqlMetricsCollectionService : EventListener, IHostedService | |
{ | |
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; | |
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | |
protected override void OnEventWritten(EventWrittenEventArgs eventData) | |
{ | |
if (eventData.EventName != "EventCounters" | |
|| eventData.Payload.Count <= 0 | |
|| !(eventData.Payload[0] is IDictionary<string, object> data) | |
) | |
return; | |
WriteCounters(data); | |
} | |
private void WriteCounters(IDictionary<string, object> eventPayload) | |
{ | |
switch (eventPayload["Name"]) | |
{ | |
case "idle-connections": | |
CommonMetrics.NpgsqlConnections.Labels("idle").Set(Convert.ToSingle(eventPayload["Mean"])); | |
break; | |
case "busy-connections": | |
CommonMetrics.NpgsqlConnections.Labels("busy").Set(Convert.ToSingle(eventPayload["Mean"])); | |
break; | |
case "connection-pools": | |
CommonMetrics.NpgsqlConnectionsPoolCount.Set(Convert.ToSingle(eventPayload["Mean"])); | |
break; | |
case "bytes-written-per-second": | |
var written = Convert.ToSingle(eventPayload["Increment"]); | |
if (written > 0) CommonMetrics.NpgsqlDataCounter.Labels("write").Inc(written); | |
break; | |
case "bytes-read-per-second": | |
var read = Convert.ToSingle(eventPayload["Increment"]); | |
if(read > 0 ) CommonMetrics.NpgsqlDataCounter.Labels("read").Inc(read); | |
break; | |
} | |
} | |
protected override void OnEventSourceCreated(EventSource eventSource) | |
{ | |
if (eventSource.Name.Equals("Npgsql", StringComparison.OrdinalIgnoreCase)) | |
{ | |
EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.None, new Dictionary<string, string> | |
{ | |
{"EventCounterIntervalSec", "1"} | |
}); | |
} | |
} | |
} |
This file contains hidden or 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
public static class CommonMetrics | |
{ | |
public static Gauge NpgsqlConnections = new Gauge("npgsql_connections_current", "Number of connections managed by Npgsql (idle/busy)", "type"); | |
public static Gauge NpgsqlConnectionsPoolCount = new Gauge("npgsql_connection_pools_current", "Number of connection pools managed by Npgsql"); | |
public static Counter NpgsqlDataCounter = new Counter("npgsql_data_bytes_total", "Amount of byte read/write by Npgsql", "direction"); | |
public static Counter ExceptionsOccur = new Counter("exceptions_total", "Total number of exceptions happen on site during it's life time"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment