Skip to content

Commit

Permalink
Was added the GC Mode, Total Exceptions and Reason of Exceptions (#37)
Browse files Browse the repository at this point in the history
* 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
tiagotartari authored Aug 18, 2020
1 parent b6268db commit 95183e6
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
9 changes: 8 additions & 1 deletion examples/AspNetCoreExample/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public async Task<ActionResult<IEnumerable<string>>> Get()

var val = this.r.Next();
CompileMe(() => val);


try
{
var divide = 0;
var result = 1 / divide;
}
catch { }

return new string[] {"value1" + this.r.Next(), "value2"+ this.r.Next()};
}

Expand Down
1 change: 1 addition & 0 deletions examples/AspNetCoreExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void Main(string[] args)
.WithGcStats()
.WithJitStats()
.WithThreadPoolStats()
.WithExceptionStats()
.WithErrorHandler(ex => Console.WriteLine("ERROR: " + ex.ToString()))
//.WithDebuggingMetrics(true);
.StartCollecting();
Expand Down
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));
}
}
}
12 changes: 11 additions & 1 deletion src/prometheus-net.DotNetRuntime/DotNetRuntimeStatsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static Builder Default()
.WithJitStats()
.WithThreadPoolSchedulingStats()
.WithThreadPoolStats()
.WithGcStats();
.WithGcStats()
.WithExceptionStats();
}

/// <summary>
Expand Down Expand Up @@ -149,6 +150,15 @@ public Builder WithGcStats(double[] histogramBuckets = null)
return this;
}

/// <summary>
/// Includes quantitative and qualitative metrics of exceptions thrown
/// </summary>
public Builder WithExceptionStats()
{
StatsCollectors.AddOrReplace(new ExceptionStatsCollector());
return this;
}

public Builder WithCustomCollector(IEventSourceStatsCollector statsCollector)
{
StatsCollectors.AddOrReplace(statsCollector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
#if PROMV2
Expand Down Expand Up @@ -114,15 +115,17 @@ private void SetupConstantMetrics(MetricFactory metrics)
"target_framework",
"runtime_version",
"os_version",
"process_architecture"
"process_architecture",
"gc_mode"
);

buildInfo.Labels(
this.GetType().Assembly.GetName().Version.ToString(),
Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName,
RuntimeInformation.FrameworkDescription,
RuntimeInformation.OSDescription,
RuntimeInformation.ProcessArchitecture.ToString()
RuntimeInformation.ProcessArchitecture.ToString(),
GCSettings.IsServerGC ? "Server" : "Workstation"
)
.Set(1);
}
Expand Down
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();
}
}
}

}

0 comments on commit 95183e6

Please sign in to comment.