Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksii-valuiskyi committed Feb 23, 2021
1 parent 77209f8 commit 2db2b9e
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions src/prometheus-net.Contrib/Diagnostics/SqlClientListenerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,36 @@ private static class PrometheusCounters
new HistogramConfiguration { Buckets = Histogram.ExponentialBuckets(0.001, 2, 16) });
public static readonly Counter SqlCommandsErrors = Metrics.CreateCounter(
"sqlclient_commands_errors_total",
"Total DB requests errors",
new CounterConfiguration { LabelNames = new[] { "type" } });
"Total DB command errors",
new CounterConfiguration { LabelNames = new[] { "code" } });

public static readonly Counter DbConnectionsOpenTotal = Metrics.CreateCounter("sqlclient_connection_opened_total", "Total opened DB connections");
public static readonly Counter DbConnectionsCloseTotal = Metrics.CreateCounter("sqlclient_connection_closed_total", "Total closed DB connections");
public static readonly Counter DbConnectionsErrors = Metrics.CreateCounter("sqlclient_connection_errors_total", "Total DB connections errors");
public static readonly Counter DbConnectionsOpenTotal = Metrics.CreateCounter("sqlclient_connections_opened_total", "Total opened DB connections");
public static readonly Counter DbConnectionsCloseTotal = Metrics.CreateCounter("sqlclient_connections_closed_total", "Total closed DB connections");
public static readonly Counter DbConnectionsErrors = Metrics.CreateCounter(
"sqlclient_connections_errors_total",
"Total DB connections errors",
new CounterConfiguration { LabelNames = new[] { "code" } });

public static readonly Counter DbTransactionsCommitedCount = Metrics.CreateCounter("sqlclient_transaction_committed_total", "Total committed transactions.");
public static readonly Counter DbTransactionsRollbackCount = Metrics.CreateCounter("sqlclient_transaction_rollback_total", "Total rollback transactions.");
public static readonly Counter DbTransactionsCommitedCount = Metrics.CreateCounter("sqlclient_transactions_committed_total", "Total committed transactions.");
public static readonly Counter DbTransactionsRollbackCount = Metrics.CreateCounter("sqlclient_transactions_rollback_total", "Total rollback transactions.");
public static readonly Counter DbTransactionsErrors = Metrics.CreateCounter(
"sqlclient_transactions_errors_total",
"Total DB transaction errors",
new CounterConfiguration { LabelNames = new[] { "code" } });
}

private ConcurrentDictionary<string, Dictionary<string, long>> Statistics = new ConcurrentDictionary<string, Dictionary<string, long>>();
private readonly PropertyFetcher<object> commandException = new PropertyFetcher<object>("Exception");
private readonly PropertyFetcher<int> commandExceptionNumber = new PropertyFetcher<int>("Number");

private readonly PropertyFetcher<object> connectionException = new PropertyFetcher<object>("Exception");
private readonly PropertyFetcher<int> connectionExceptionNumber = new PropertyFetcher<int>("Number");

private readonly PropertyFetcher<object> transactionException = new PropertyFetcher<object>("Exception");
private readonly PropertyFetcher<int> transactionExceptionNumber = new PropertyFetcher<int>("Number");

private readonly PropertyFetcher<object> rollbackException = new PropertyFetcher<object>("Exception");
private readonly PropertyFetcher<int> rollbackExceptionNumber = new PropertyFetcher<int>("Number");

private readonly AsyncLocal<long> commandTimestampContext = new AsyncLocal<long>();

public SqlClientListenerHandler(string sourceName, SqlMetricsOptions options) : base(sourceName)
Expand Down Expand Up @@ -71,7 +89,13 @@ public void OnWriteCommand(string name, object payload)
break;
case "Microsoft.Data.SqlClient.WriteCommandError":
{
PrometheusCounters.SqlCommandsErrors.WithLabels("command").Inc();
if (commandException.TryFetch(payload, out var sqlException))
{
if (commandExceptionNumber.TryFetch(sqlException, out var errorCode))
{
PrometheusCounters.SqlCommandsErrors.WithLabels(errorCode.ToString()).Inc();
}
}
}
break;
}
Expand All @@ -92,7 +116,13 @@ public void OnWriteConnectionOpen(string name, object payload)
break;
case "Microsoft.Data.SqlClient.WriteConnectionOpenError":
{
PrometheusCounters.DbConnectionsErrors.Inc();
if (connectionException.TryFetch(payload, out var sqlException))
{
if (connectionExceptionNumber.TryFetch(sqlException, out var errorCode))
{
PrometheusCounters.DbConnectionsErrors.WithLabels(errorCode.ToString()).Inc();
}
}
}
break;
}
Expand Down Expand Up @@ -134,7 +164,13 @@ public void OnWriteTransactionCommit(string name, object payload)
break;
case "Microsoft.Data.SqlClient.WriteTransactionCommitError":
{
PrometheusCounters.SqlCommandsErrors.WithLabels("transaction").Inc();
if (transactionException.TryFetch(payload, out var sqlException))
{
if (transactionExceptionNumber.TryFetch(sqlException, out var errorCode))
{
PrometheusCounters.DbTransactionsErrors.WithLabels(errorCode.ToString()).Inc();
}
}
}
break;
}
Expand All @@ -155,7 +191,13 @@ public void OnWriteTransactionRollback(string name, object payload)
break;
case "Microsoft.Data.SqlClient.WriteTransactionRollbackError":
{
PrometheusCounters.SqlCommandsErrors.WithLabels("transaction").Inc();
if (rollbackException.TryFetch(payload, out var sqlException))
{
if (rollbackExceptionNumber.TryFetch(sqlException, out var errorCode))
{
PrometheusCounters.DbTransactionsErrors.WithLabels(errorCode.ToString()).Inc();
}
}
}
break;
}
Expand Down

0 comments on commit 2db2b9e

Please sign in to comment.