Skip to content

Commit

Permalink
Throw NotSupportedException when using SetErrorStatusOnException in…
Browse files Browse the repository at this point in the history
… Mono Runtime and Native AOT environment. (#5374)
  • Loading branch information
Yun-Ting authored Mar 2, 2024
1 parent 97404a2 commit 28ead76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

## Unreleased

* Throw NotSupportedException when using `SetErrorStatusOnException` method for
Tracing in Mono Runtime and Native AOT environment because the dependent
`Marshal.GetExceptionPointers()` API is not supported on these platforms.
([#5374](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5374))

* Fixed an issue where `LogRecord.Attributes` (or `LogRecord.StateValues` alias)
could become out of sync with `LogRecord.State` if either is set directly via
the public setters. This was done to further mitigate issues introduced in
1.5.0 causing attributes added using custom processor(s) to be missing after
upgrading. For details see:
[#5169](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5169)
([#5169](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5169))

* Fixed an issue where `SimpleExemplarReservoir` was not resetting internal
state for cumulative temporality.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public static class TracerProviderBuilderExtensions
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/>.</param>
/// <param name="enabled">Enabled or not. Default value is <c>true</c>.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
/// <remarks>
/// This method is not supported in native AOT or Mono Runtime as of .NET 8.
/// </remarks>
#if NET7_0_OR_GREATER
[RequiresDynamicCode("The code for detecting exception and setting error status might not be available.")]
#endif
public static TracerProviderBuilder SetErrorStatusOnException(this TracerProviderBuilder tracerProviderBuilder, bool enabled = true)
{
tracerProviderBuilder.ConfigureBuilder((sp, builder) =>
Expand Down
18 changes: 6 additions & 12 deletions src/OpenTelemetry/Trace/ExceptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@ public ExceptionProcessor()
#else
// When running on netstandard or similar the Marshal class is not a part of the netstandard API
// but it would still most likely be available in the underlying framework, so use reflection to retrieve it.
try
{
var flags = BindingFlags.Static | BindingFlags.Public;
var method = typeof(Marshal).GetMethod("GetExceptionPointers", flags, null, Type.EmptyTypes, null)
?? throw new InvalidOperationException("Marshal.GetExceptionPointers method could not be resolved reflectively.");
var lambda = Expression.Lambda<Func<IntPtr>>(Expression.Call(method));
this.fnGetExceptionPointers = lambda.Compile();
}
catch (Exception ex)
{
throw new NotSupportedException($"'{typeof(Marshal).FullName}.GetExceptionPointers' is not supported", ex);
}
var flags = BindingFlags.Static | BindingFlags.Public;
var method = typeof(Marshal).GetMethod("GetExceptionPointers", flags, null, Type.EmptyTypes, null)
?? throw new InvalidOperationException("Marshal.GetExceptionPointers method could not be resolved reflectively.");
var lambda = Expression.Lambda<Func<IntPtr>>(Expression.Call(method));
this.fnGetExceptionPointers = lambda.Compile();
#endif
this.fnGetExceptionPointers(); // attempt to access pointers to test for platform support
}

/// <inheritdoc />
Expand Down

0 comments on commit 28ead76

Please sign in to comment.