Skip to content

Commit 28ead76

Browse files
authored
Throw NotSupportedException when using SetErrorStatusOnException in Mono Runtime and Native AOT environment. (#5374)
1 parent 97404a2 commit 28ead76

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/OpenTelemetry/CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
## Unreleased
44

5+
* Throw NotSupportedException when using `SetErrorStatusOnException` method for
6+
Tracing in Mono Runtime and Native AOT environment because the dependent
7+
`Marshal.GetExceptionPointers()` API is not supported on these platforms.
8+
([#5374](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5374))
9+
510
* Fixed an issue where `LogRecord.Attributes` (or `LogRecord.StateValues` alias)
611
could become out of sync with `LogRecord.State` if either is set directly via
712
the public setters. This was done to further mitigate issues introduced in
813
1.5.0 causing attributes added using custom processor(s) to be missing after
914
upgrading. For details see:
10-
[#5169](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5169)
15+
([#5169](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5169))
1116

1217
* Fixed an issue where `SimpleExemplarReservoir` was not resetting internal
1318
state for cumulative temporality.

src/OpenTelemetry/Trace/Builder/TracerProviderBuilderExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public static class TracerProviderBuilderExtensions
2424
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/>.</param>
2525
/// <param name="enabled">Enabled or not. Default value is <c>true</c>.</param>
2626
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
27+
/// <remarks>
28+
/// This method is not supported in native AOT or Mono Runtime as of .NET 8.
29+
/// </remarks>
30+
#if NET7_0_OR_GREATER
31+
[RequiresDynamicCode("The code for detecting exception and setting error status might not be available.")]
32+
#endif
2733
public static TracerProviderBuilder SetErrorStatusOnException(this TracerProviderBuilder tracerProviderBuilder, bool enabled = true)
2834
{
2935
tracerProviderBuilder.ConfigureBuilder((sp, builder) =>

src/OpenTelemetry/Trace/ExceptionProcessor.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,13 @@ public ExceptionProcessor()
2323
#else
2424
// When running on netstandard or similar the Marshal class is not a part of the netstandard API
2525
// but it would still most likely be available in the underlying framework, so use reflection to retrieve it.
26-
try
27-
{
28-
var flags = BindingFlags.Static | BindingFlags.Public;
29-
var method = typeof(Marshal).GetMethod("GetExceptionPointers", flags, null, Type.EmptyTypes, null)
30-
?? throw new InvalidOperationException("Marshal.GetExceptionPointers method could not be resolved reflectively.");
31-
var lambda = Expression.Lambda<Func<IntPtr>>(Expression.Call(method));
32-
this.fnGetExceptionPointers = lambda.Compile();
33-
}
34-
catch (Exception ex)
35-
{
36-
throw new NotSupportedException($"'{typeof(Marshal).FullName}.GetExceptionPointers' is not supported", ex);
37-
}
26+
var flags = BindingFlags.Static | BindingFlags.Public;
27+
var method = typeof(Marshal).GetMethod("GetExceptionPointers", flags, null, Type.EmptyTypes, null)
28+
?? throw new InvalidOperationException("Marshal.GetExceptionPointers method could not be resolved reflectively.");
29+
var lambda = Expression.Lambda<Func<IntPtr>>(Expression.Call(method));
30+
this.fnGetExceptionPointers = lambda.Compile();
3831
#endif
32+
this.fnGetExceptionPointers(); // attempt to access pointers to test for platform support
3933
}
4034

4135
/// <inheritdoc />

0 commit comments

Comments
 (0)