Skip to content

Commit 95183e6

Browse files
authored
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
1 parent b6268db commit 95183e6

File tree

6 files changed

+123
-4
lines changed

6 files changed

+123
-4
lines changed

examples/AspNetCoreExample/Controllers/ValuesController.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ public async Task<ActionResult<IEnumerable<string>>> Get()
2828

2929
var val = this.r.Next();
3030
CompileMe(() => val);
31-
31+
32+
try
33+
{
34+
var divide = 0;
35+
var result = 1 / divide;
36+
}
37+
catch { }
38+
3239
return new string[] {"value1" + this.r.Next(), "value2"+ this.r.Next()};
3340
}
3441

examples/AspNetCoreExample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void Main(string[] args)
2626
.WithGcStats()
2727
.WithJitStats()
2828
.WithThreadPoolStats()
29+
.WithExceptionStats()
2930
.WithErrorHandler(ex => Console.WriteLine("ERROR: " + ex.ToString()))
3031
//.WithDebuggingMetrics(true);
3132
.StartCollecting();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using NUnit.Framework;
2+
using Prometheus.DotNetRuntime.StatsCollectors;
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace Prometheus.DotNetRuntime.Tests.StatsCollectors.IntegrationTests
8+
{
9+
[TestFixture]
10+
internal class ExceptionStatsCollectorTests : StatsCollectorIntegrationTestBase<ExceptionStatsCollector>
11+
{
12+
protected override ExceptionStatsCollector CreateStatsCollector()
13+
{
14+
return new ExceptionStatsCollector();
15+
}
16+
17+
[Test]
18+
public void Will_measure_when_occurring_an_exception()
19+
{
20+
// arrange
21+
int divider = 0;
22+
string exceptionMessage = string.Empty;
23+
24+
// act
25+
try
26+
{
27+
var result = 1 / divider;
28+
}
29+
catch (Exception ex)
30+
{
31+
exceptionMessage = ex.GetType().FullName;
32+
}
33+
34+
// assert
35+
Assert.That(() => StatsCollector.ExceptionReasons.Labels(exceptionMessage).Value, Is.EqualTo(1).After(100, 1000));
36+
}
37+
38+
[Test]
39+
public void Will_measure_when_not_occurring_an_exception()
40+
{
41+
// arrange
42+
int divider = 1;
43+
string exceptionMessage = string.Empty;
44+
45+
// act
46+
var result = 1 / divider;
47+
48+
// assert
49+
Assert.That(() => StatsCollector.ExceptionReasons.Labels(exceptionMessage).Value, Is.EqualTo(0).After(100, 1000));
50+
}
51+
}
52+
}

src/prometheus-net.DotNetRuntime/DotNetRuntimeStatsBuilder.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static Builder Default()
2828
.WithJitStats()
2929
.WithThreadPoolSchedulingStats()
3030
.WithThreadPoolStats()
31-
.WithGcStats();
31+
.WithGcStats()
32+
.WithExceptionStats();
3233
}
3334

3435
/// <summary>
@@ -149,6 +150,15 @@ public Builder WithGcStats(double[] histogramBuckets = null)
149150
return this;
150151
}
151152

153+
/// <summary>
154+
/// Includes quantitative and qualitative metrics of exceptions thrown
155+
/// </summary>
156+
public Builder WithExceptionStats()
157+
{
158+
StatsCollectors.AddOrReplace(new ExceptionStatsCollector());
159+
return this;
160+
}
161+
152162
public Builder WithCustomCollector(IEventSourceStatsCollector statsCollector)
153163
{
154164
StatsCollectors.AddOrReplace(statsCollector);

src/prometheus-net.DotNetRuntime/DotNetRuntimeStatsCollector.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Immutable;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Runtime;
67
using System.Runtime.InteropServices;
78
using System.Runtime.Versioning;
89
#if PROMV2
@@ -114,15 +115,17 @@ private void SetupConstantMetrics(MetricFactory metrics)
114115
"target_framework",
115116
"runtime_version",
116117
"os_version",
117-
"process_architecture"
118+
"process_architecture",
119+
"gc_mode"
118120
);
119121

120122
buildInfo.Labels(
121123
this.GetType().Assembly.GetName().Version.ToString(),
122124
Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName,
123125
RuntimeInformation.FrameworkDescription,
124126
RuntimeInformation.OSDescription,
125-
RuntimeInformation.ProcessArchitecture.ToString()
127+
RuntimeInformation.ProcessArchitecture.ToString(),
128+
GCSettings.IsServerGC ? "Server" : "Workstation"
126129
)
127130
.Set(1);
128131
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Prometheus.DotNetRuntime.EventSources;
2+
using System;
3+
using System.Diagnostics.Tracing;
4+
#if PROMV2
5+
using Prometheus.Advanced;
6+
#endif
7+
8+
9+
namespace Prometheus.DotNetRuntime.StatsCollectors
10+
{
11+
public class ExceptionStatsCollector : IEventSourceStatsCollector
12+
{
13+
private const int EventIdExceptionThrown = 80;
14+
private const string LabelReason = "exception";
15+
16+
internal Counter ExceptionReasons { get; private set; }
17+
18+
public Guid EventSourceGuid => DotNetRuntimeEventSource.Id;
19+
20+
public EventKeywords Keywords => (EventKeywords)DotNetRuntimeEventSource.Keywords.Exception;
21+
public EventLevel Level => EventLevel.Informational;
22+
23+
public void RegisterMetrics(MetricFactory metrics)
24+
{
25+
ExceptionReasons = metrics.CreateCounter(
26+
"dotnet_exception_reasons_total",
27+
"Reasons that led to an exception",
28+
LabelReason
29+
);
30+
}
31+
32+
public void UpdateMetrics()
33+
{
34+
35+
}
36+
37+
public void ProcessEvent(EventWrittenEventArgs e)
38+
{
39+
if (e.EventId == EventIdExceptionThrown)
40+
{
41+
ExceptionReasons.Labels((string)e.Payload[0]).Inc();
42+
}
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)