Skip to content

Commit

Permalink
Add exception.count in Runtime metrics (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang17 committed Jun 23, 2022
1 parent d8d0e93 commit 76f0b06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
([#430](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/430))
* Remove Process related metrics from .NET Runtime metrics
([#446](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/446))
* Add `exception.count` in Runtime metrics
([#431](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/431))

## 0.2.0-alpha.1

Expand Down
9 changes: 9 additions & 0 deletions src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public RuntimeMetrics(RuntimeMetricsOptions options)
{
this.meter.CreateObservableGauge($"{metricPrefix}assembly.count", () => (long)AppDomain.CurrentDomain.GetAssemblies().Length, description: "Number of Assemblies Loaded.");
}

if (options.IsExceptionCountEnabled)
{
var exceptionCounter = this.meter.CreateCounter<long>($"{metricPrefix}exception.count", description: "Number of exceptions thrown.");
AppDomain.CurrentDomain.FirstChanceException += (source, e) =>
{
exceptionCounter.Add(1);
};
}
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class RuntimeMetricsOptions
/// </summary>
public bool? AssembliesEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether exception count metrics should be collected.
/// </summary>
public bool? ExceptionCountEnabled { get; set; }

/// <summary>
/// Gets a value indicating whether all metrics are enabled.
/// </summary>
Expand All @@ -55,7 +60,8 @@ public class RuntimeMetricsOptions
#if NETCOREAPP3_1_OR_GREATER
&& this.ThreadingEnabled == null
#endif
&& this.AssembliesEnabled == null;
&& this.AssembliesEnabled == null
&& this.ExceptionCountEnabled == null;

/// <summary>
/// Gets a value indicating whether garbage collection metrics is enabled.
Expand All @@ -80,5 +86,10 @@ public class RuntimeMetricsOptions
/// Gets a value indicating whether assembly metrics is enabled.
/// </summary>
internal bool IsAssembliesEnabled => this.AssembliesEnabled == true || this.IsAllEnabled;

/// <summary>
/// Gets a value indicating whether exception count metrics is enabled.
/// </summary>
internal bool IsExceptionCountEnabled => this.ExceptionCountEnabled == true || this.IsAllEnabled;
}
}

0 comments on commit 76f0b06

Please sign in to comment.