Skip to content

Commit db9b141

Browse files
committed
When a before-collection metrics update callback throws an exception, it is now logged and ignored instead of breaking all metrics collection.
1 parent 4f9f19c commit db9b141

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

History

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Added some debug metrics to indicate the number of metric families, instances and timeseries being exported by prometheus-net.
1111
- Added typical benchmark results to readme for easy reference, and to show relative speed of the simple versus complex instruments.
1212
- Removed MetricConfiguration.StaticLabels because it had negative performance impact on many code paths that did not use it. The same functionality can be manually implemented in user code. Static labels remain available on registry and factory level.
13+
- When a before-collection metrics update callback throws an exception, it is now logged and ignored instead of breaking all metrics collection.
1314
* 6.0.0
1415
- Dropped library support for unsupported .NET runtime versions (.NET 2.1).
1516
- If Razor Pages is enabled, the "page" label is now automatically added to the default HTTP server metrics, unless there is a user-defined label already present with this name.

Prometheus/CollectorRegistry.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Concurrent;
2+
using System.Diagnostics;
23

34
namespace Prometheus
45
{
@@ -242,9 +243,28 @@ internal async Task CollectAndSerializeAsync(IMetricsSerializer serializer, Canc
242243
}
243244

244245
foreach (var callback in _beforeCollectCallbacks)
245-
callback();
246+
{
247+
try
248+
{
249+
callback();
250+
}
251+
catch (Exception ex)
252+
{
253+
Trace.WriteLine($"Metrics before-collect callback failed: {ex}");
254+
}
255+
}
246256

247-
await Task.WhenAll(_beforeCollectAsyncCallbacks.Select(callback => callback(cancel)));
257+
await Task.WhenAll(_beforeCollectAsyncCallbacks.Select(callback =>
258+
{
259+
try
260+
{
261+
return callback(cancel);
262+
}
263+
catch (Exception ex)
264+
{
265+
Trace.WriteLine($"Metrics before-collect callback failed: {ex}");
266+
}
267+
}));
248268

249269
UpdateRegistryMetrics();
250270

0 commit comments

Comments
 (0)