forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounters.cs
More file actions
223 lines (195 loc) · 9.22 KB
/
Counters.cs
File metadata and controls
223 lines (195 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using Npgsql.Logging;
#if NET461
using System.Diagnostics;
using System.Reflection;
#endif
namespace Npgsql
{
static class Counters
{
/// <summary>
/// The number of connections per second that are being made to a database server.
/// </summary>
internal static readonly Counter HardConnectsPerSecond = new Counter(nameof(HardConnectsPerSecond));
/// <summary>
/// The number of disconnects per second that are being made to a database server.
/// </summary>
internal static readonly Counter HardDisconnectsPerSecond = new Counter(nameof(HardDisconnectsPerSecond));
/// <summary>
/// The total number of connection pools.
/// </summary>
internal static readonly Counter NumberOfActiveConnectionPools = new Counter(nameof(NumberOfActiveConnectionPools));
/// <summary>
/// The number of (pooled) active connections that are currently in use.
/// </summary>
internal static readonly Counter NumberOfActiveConnections = new Counter(nameof(NumberOfActiveConnections));
/// <summary>
/// The number of connections available for use in the connection pools.
/// </summary>
internal static readonly Counter NumberOfFreeConnections = new Counter(nameof(NumberOfFreeConnections));
/// <summary>
/// The number of active connections that are not pooled.
/// </summary>
internal static readonly Counter NumberOfNonPooledConnections = new Counter(nameof(NumberOfNonPooledConnections));
/// <summary>
/// The number of active connections that are being managed by the connection pooling infrastructure.
/// </summary>
internal static readonly Counter NumberOfPooledConnections = new Counter(nameof(NumberOfPooledConnections));
/// <summary>
/// The number of active connections being pulled from the connection pool.
/// </summary>
internal static readonly Counter SoftConnectsPerSecond = new Counter(nameof(SoftConnectsPerSecond));
/// <summary>
/// The number of active connections that are being returned to the connection pool.
/// </summary>
internal static readonly Counter SoftDisconnectsPerSecond = new Counter(nameof(SoftDisconnectsPerSecond));
static bool _initialized;
static readonly object InitLock = new object();
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(Counters));
#pragma warning disable CA1801 // Review unused parameters
internal static void Initialize(bool usePerfCounters)
{
lock (InitLock)
{
if (_initialized)
return;
_initialized = true;
var enabled = false;
var expensiveEnabled = false;
if (usePerfCounters)
{
#if NET461
try
{
enabled = PerformanceCounterCategory.Exists(Counter.DiagnosticsCounterCategory);
if (!enabled)
Log.Warn($"{nameof(NpgsqlConnectionStringBuilder.UsePerfCounters)} was specified but the Performance Counter category wasn't found. You probably need to install the Npgsql MSI.");
var perfCtrSwitch = new TraceSwitch("ConnectionPoolPerformanceCounterDetail",
"level of detail to track with connection pool performance counters");
expensiveEnabled = enabled && perfCtrSwitch.Level == TraceLevel.Verbose;
}
catch (Exception e)
{
Log.Debug("Exception while checking for performance counter category (counters will be disabled)", e);
}
#else
throw new NotSupportedException("The legacy Windows Performance Counters are only supported on .NET Framework. " +
"The new .NET Core performance counter feature (EventSource) doesn't require any connection string parameters.");
#endif
}
try
{
HardConnectsPerSecond.Initialize(enabled);
HardDisconnectsPerSecond.Initialize(enabled);
NumberOfActiveConnectionPools.Initialize(enabled);
NumberOfNonPooledConnections.Initialize(enabled);
NumberOfPooledConnections.Initialize(enabled);
SoftConnectsPerSecond.Initialize(expensiveEnabled);
SoftDisconnectsPerSecond.Initialize(expensiveEnabled);
NumberOfActiveConnections.Initialize(expensiveEnabled);
NumberOfFreeConnections.Initialize(expensiveEnabled);
}
catch (Exception e)
{
Log.Debug("Exception while setting up performance counter (counters will be disabled)", e);
}
}
}
}
#pragma warning restore CA1801 // Review unused parameters
/// <summary>
/// This class is currently a simple wrapper around System.Diagnostics.PerformanceCounter.
/// Since these aren't supported in .NET Standard, all the ifdef'ing happens here.
/// When an alternative performance counter API emerges for netstandard, it can be added here.
/// </summary>
sealed class Counter : IDisposable
{
#if NET461
internal const string DiagnosticsCounterCategory = ".NET Data Provider for PostgreSQL (Npgsql)";
internal PerformanceCounter? DiagnosticsCounter { get; private set; }
#endif
public string Name { get; }
internal Counter(string diagnosticsCounterName)
=> Name = diagnosticsCounterName;
internal void Initialize(bool enabled)
{
#if NET461
if (!enabled)
return;
DiagnosticsCounter = new PerformanceCounter
{
CategoryName = DiagnosticsCounterCategory,
CounterName = Name,
InstanceName = InstanceName,
InstanceLifetime = PerformanceCounterInstanceLifetime.Process,
ReadOnly = false,
RawValue = 0
};
AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
#endif
}
internal void Increment()
{
#if NET461
DiagnosticsCounter?.Increment();
#endif
}
internal void Decrement()
{
#if NET461
DiagnosticsCounter?.Decrement();
#endif
}
public void Dispose()
{
#if NET461
var diagnosticsCounter = DiagnosticsCounter;
DiagnosticsCounter = null;
diagnosticsCounter?.RemoveInstance();
#endif
}
#if NET461
void OnProcessExit(object sender, EventArgs e) => Dispose();
void OnDomainUnload(object sender, EventArgs e) => Dispose();
void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.IsTerminating)
Dispose();
}
const int CounterInstanceNameMaxLength = 127;
static readonly string InstanceName = GetInstanceName();
static string GetInstanceName()
{
string? result = null;
// Code borrowed from the .NET reference sources
var instanceName = Assembly.GetEntryAssembly()?.GetName().Name;
if (string.IsNullOrEmpty(instanceName))
instanceName = AppDomain.CurrentDomain.FriendlyName;
var pid = Process.GetCurrentProcess().Id;
result = string.Format(null, "{0}[{1}]", instanceName, pid);
result = result.Replace('(', '[').Replace(')', ']').Replace('#', '_').Replace('/', '_').Replace('\\', '_');
if (result.Length > CounterInstanceNameMaxLength)
{
// Replacing the middle part with "[...]"
// For example: if path is c:\long_path\very_(Ax200)_long__path\perftest.exe and process ID is 1234 than the resulted instance name will be:
// c:\long_path\very_(AxM)[...](AxN)_long__path\perftest.exe[1234]
// while M and N are adjusted to make each part before and after the [...] = 61 (making the total = 61 + 5 + 61 = 127)
const string insertString = "[...]";
var firstPartLength = (CounterInstanceNameMaxLength - insertString.Length) / 2;
var lastPartLength = CounterInstanceNameMaxLength - firstPartLength - insertString.Length;
result = string.Format(null, "{0}{1}{2}",
result.Substring(0, firstPartLength),
insertString,
result.Substring(result.Length - lastPartLength, lastPartLength));
Debug.Assert(result.Length == CounterInstanceNameMaxLength,
string.Format(null, "wrong calculation of the instance name: expected {0}, actual: {1}", CounterInstanceNameMaxLength, result.Length));
}
return result;
}
#endif
public override string ToString() => Name;
}
}