-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Was added the GC Mode, Total Exceptions and Reason of Exceptions (#37)
* Was added the GC Mode, Total Exceptions and Reason of Exceptions * Removed the redundant metric, created a unit test for exception statistics and metric name adjustment according to prometheus convention. * Decreasing the waiting time * Label changed to error class namespace and unit test improvement
- Loading branch information
1 parent
b6268db
commit 95183e6
Showing
6 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
52 changes: 52 additions & 0 deletions
52
...-net.DotNetRuntime.Tests/StatsCollectors/IntegrationTests/ExceptionStatsCollectorTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using NUnit.Framework; | ||
using Prometheus.DotNetRuntime.StatsCollectors; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Prometheus.DotNetRuntime.Tests.StatsCollectors.IntegrationTests | ||
{ | ||
[TestFixture] | ||
internal class ExceptionStatsCollectorTests : StatsCollectorIntegrationTestBase<ExceptionStatsCollector> | ||
{ | ||
protected override ExceptionStatsCollector CreateStatsCollector() | ||
{ | ||
return new ExceptionStatsCollector(); | ||
} | ||
|
||
[Test] | ||
public void Will_measure_when_occurring_an_exception() | ||
{ | ||
// arrange | ||
int divider = 0; | ||
string exceptionMessage = string.Empty; | ||
|
||
// act | ||
try | ||
{ | ||
var result = 1 / divider; | ||
} | ||
catch (Exception ex) | ||
{ | ||
exceptionMessage = ex.GetType().FullName; | ||
} | ||
|
||
// assert | ||
Assert.That(() => StatsCollector.ExceptionReasons.Labels(exceptionMessage).Value, Is.EqualTo(1).After(100, 1000)); | ||
} | ||
|
||
[Test] | ||
public void Will_measure_when_not_occurring_an_exception() | ||
{ | ||
// arrange | ||
int divider = 1; | ||
string exceptionMessage = string.Empty; | ||
|
||
// act | ||
var result = 1 / divider; | ||
|
||
// assert | ||
Assert.That(() => StatsCollector.ExceptionReasons.Labels(exceptionMessage).Value, Is.EqualTo(0).After(100, 1000)); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
46 changes: 46 additions & 0 deletions
46
src/prometheus-net.DotNetRuntime/StatsCollectors/ExceptionStatsCollector.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Prometheus.DotNetRuntime.EventSources; | ||
using System; | ||
using System.Diagnostics.Tracing; | ||
#if PROMV2 | ||
using Prometheus.Advanced; | ||
#endif | ||
|
||
|
||
namespace Prometheus.DotNetRuntime.StatsCollectors | ||
{ | ||
public class ExceptionStatsCollector : IEventSourceStatsCollector | ||
{ | ||
private const int EventIdExceptionThrown = 80; | ||
private const string LabelReason = "exception"; | ||
|
||
internal Counter ExceptionReasons { get; private set; } | ||
|
||
public Guid EventSourceGuid => DotNetRuntimeEventSource.Id; | ||
|
||
public EventKeywords Keywords => (EventKeywords)DotNetRuntimeEventSource.Keywords.Exception; | ||
public EventLevel Level => EventLevel.Informational; | ||
|
||
public void RegisterMetrics(MetricFactory metrics) | ||
{ | ||
ExceptionReasons = metrics.CreateCounter( | ||
"dotnet_exception_reasons_total", | ||
"Reasons that led to an exception", | ||
LabelReason | ||
); | ||
} | ||
|
||
public void UpdateMetrics() | ||
{ | ||
|
||
} | ||
|
||
public void ProcessEvent(EventWrittenEventArgs e) | ||
{ | ||
if (e.EventId == EventIdExceptionThrown) | ||
{ | ||
ExceptionReasons.Labels((string)e.Payload[0]).Inc(); | ||
} | ||
} | ||
} | ||
|
||
} |