forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricsReporter.cs
More file actions
245 lines (194 loc) · 8.65 KB
/
MetricsReporter.cs
File metadata and controls
245 lines (194 loc) · 8.65 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
namespace Npgsql;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.InteropServices;
using System.Threading;
// .NET docs on metric instrumentation: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-instrumentation
// OpenTelemetry semantic conventions for database metric: https://opentelemetry.io/docs/specs/otel/metrics/semantic_conventions/database-metrics
sealed class MetricsReporter : IDisposable
{
const string Version = "0.1.0";
static readonly Meter Meter;
static readonly UpDownCounter<int> CommandsExecuting;
static readonly Counter<int> CommandsFailed;
static readonly Histogram<double> CommandDuration;
static readonly Counter<long> BytesWritten;
static readonly Counter<long> BytesRead;
static readonly UpDownCounter<int> PendingConnectionRequests;
static readonly Counter<int> ConnectionTimeouts;
static readonly Histogram<double> ConnectionCreateTime;
static readonly ObservableGauge<double> PreparedRatio;
readonly NpgsqlDataSource _dataSource;
readonly KeyValuePair<string, object?> _poolNameTag;
static readonly List<MetricsReporter> Reporters = [];
CommandCounters _commandCounters;
[StructLayout(LayoutKind.Explicit)]
struct CommandCounters
{
[FieldOffset(0)] internal int CommandsStarted;
[FieldOffset(4)] internal int PreparedCommandsStarted;
[FieldOffset(0)] internal long All;
}
static MetricsReporter()
{
Meter = new("Npgsql", Version);
CommandsExecuting = Meter.CreateUpDownCounter<int>(
"db.client.commands.executing",
unit: "{command}",
description: "The number of currently executing database commands.");
CommandsFailed = Meter.CreateCounter<int>(
"db.client.commands.failed",
unit: "{command}",
description: "The number of database commands which have failed.");
CommandDuration = Meter.CreateHistogram<double>(
"db.client.commands.duration",
unit: "s",
description: "The duration of database commands, in seconds.");
BytesWritten = Meter.CreateCounter<long>(
"db.client.commands.bytes_written",
unit: "By",
description: "The number of bytes written.");
BytesRead = Meter.CreateCounter<long>(
"db.client.commands.bytes_read",
unit: "By",
description: "The number of bytes read.");
PendingConnectionRequests = Meter.CreateUpDownCounter<int>(
"db.client.connections.pending_requests",
unit: "{request}",
description: "The number of pending requests for an open connection, cumulative for the entire pool.");
ConnectionTimeouts = Meter.CreateCounter<int>(
"db.client.connections.timeouts",
unit: "{timeout}",
description: "The number of connection timeouts that have occurred trying to obtain a connection from the pool.");
ConnectionCreateTime = Meter.CreateHistogram<double>(
"db.client.connections.create_time",
unit: "s",
description: "The time it took to create a new connection.");
// Observable metrics; these are for values we already track internally (and efficiently) inside the connection pool implementation.
Meter.CreateObservableUpDownCounter(
"db.client.connections.usage",
GetConnectionUsage,
unit: "{connection}",
description: "The number of connections that are currently in state described by the state attribute.");
// It's a bit ridiculous to manage "max connections" as an observable counter, given that it never changes for a given pool.
// However, we can't simply report it once at startup, since clients who connect later wouldn't have it. And since reporting it
// repeatedly isn't possible because we need to provide incremental figures, we just manage it as an observable counter.
Meter.CreateObservableUpDownCounter(
"db.client.connections.max",
GetMaxConnections,
unit: "{connection}",
description: "The maximum number of open connections allowed.");
PreparedRatio = Meter.CreateObservableGauge(
"db.client.commands.prepared_ratio",
GetPreparedCommandsRatio,
description: "The ratio of prepared command executions.");
}
public MetricsReporter(NpgsqlDataSource dataSource)
{
_dataSource = dataSource;
_poolNameTag = new KeyValuePair<string, object?>("pool.name", dataSource.Name);
lock (Reporters)
{
Reporters.Add(this);
Reporters.Sort((x,y) => string.Compare(x._dataSource.Name, y._dataSource.Name, StringComparison.Ordinal));
}
}
internal long ReportCommandStart()
{
CommandsExecuting.Add(1, _poolNameTag);
if (PreparedRatio.Enabled)
Interlocked.Increment(ref _commandCounters.CommandsStarted);
return CommandDuration.Enabled ? Stopwatch.GetTimestamp() : 0;
}
internal void ReportCommandStop(long startTimestamp)
{
CommandsExecuting.Add(-1, _poolNameTag);
if (CommandDuration.Enabled && startTimestamp > 0)
{
CommandDuration.Record(Stopwatch.GetElapsedTime(startTimestamp).TotalSeconds, _poolNameTag);
}
}
internal void CommandStartPrepared()
{
if (PreparedRatio.Enabled)
Interlocked.Increment(ref _commandCounters.PreparedCommandsStarted);
}
internal void ReportCommandFailed() => CommandsFailed.Add(1, _poolNameTag);
internal void ReportBytesWritten(long bytesWritten) => BytesWritten.Add(bytesWritten, _poolNameTag);
internal void ReportBytesRead(long bytesRead) => BytesRead.Add(bytesRead, _poolNameTag);
internal void ReportConnectionPoolTimeout()
=> ConnectionTimeouts.Add(1, _poolNameTag);
internal void ReportPendingConnectionRequestStart()
=> PendingConnectionRequests.Add(1, _poolNameTag);
internal void ReportPendingConnectionRequestStop()
=> PendingConnectionRequests.Add(-1, _poolNameTag);
internal void ReportConnectionCreateTime(TimeSpan duration)
=> ConnectionCreateTime.Record(duration.TotalSeconds, _poolNameTag);
static IEnumerable<Measurement<int>> GetConnectionUsage()
{
lock (Reporters)
{
var measurements = new List<Measurement<int>>();
for (var i = 0; i < Reporters.Count; i++)
{
var reporter = Reporters[i];
if (reporter._dataSource is PoolingDataSource poolingDataSource)
{
var stats = poolingDataSource.Statistics;
measurements.Add(new Measurement<int>(
stats.Idle,
reporter._poolNameTag,
new KeyValuePair<string, object?>("state", "idle")));
measurements.Add(new Measurement<int>(
stats.Busy,
reporter._poolNameTag,
new KeyValuePair<string, object?>("state", "used")));
}
}
return measurements;
}
}
static IEnumerable<Measurement<int>> GetMaxConnections()
{
lock (Reporters)
{
var measurements = new List<Measurement<int>>();
foreach (var reporter in Reporters)
{
if (reporter._dataSource is PoolingDataSource poolingDataSource)
{
measurements.Add(new Measurement<int>(poolingDataSource.MaxConnections, reporter._poolNameTag));
}
}
return measurements;
}
}
static IEnumerable<Measurement<double>> GetPreparedCommandsRatio()
{
lock (Reporters)
{
var measurements = new List<Measurement<double>>(Reporters.Count);
for (var i = 0; i < Reporters.Count; i++)
{
var reporter = Reporters[i];
var counters = new CommandCounters
{
All = Interlocked.Exchange(ref reporter._commandCounters.All, default)
};
var value = (double)counters.PreparedCommandsStarted / counters.CommandsStarted * 100;
if (double.IsFinite(value))
measurements.Add(new Measurement<double>(value, reporter._poolNameTag));
}
return measurements;
}
}
public void Dispose()
{
lock (Reporters)
{
Reporters.Remove(this);
}
}
}