Skip to content

Commit

Permalink
[AOT] Remove usage of Linq.Expressions and especially lambda compilat…
Browse files Browse the repository at this point in the history
…ion (#4695)
  • Loading branch information
vitek-karas authored Jul 28, 2023
1 parent 8c7173f commit 41daba4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/OpenTelemetry/Trace/ExceptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#nullable enable

using System.Diagnostics;
using System.Runtime.InteropServices;
#if !NET6_0_OR_GREATER && !NETFRAMEWORK
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
#endif

namespace OpenTelemetry.Trace;

Expand All @@ -31,6 +33,11 @@ internal sealed class ExceptionProcessor : BaseProcessor<Activity>

public ExceptionProcessor()
{
#if NET6_0_OR_GREATER || NETFRAMEWORK
this.fnGetExceptionPointers = Marshal.GetExceptionPointers;
#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;
Expand All @@ -43,6 +50,7 @@ public ExceptionProcessor()
{
throw new NotSupportedException($"'{typeof(Marshal).FullName}.GetExceptionPointers' is not supported", ex);
}
#endif
}

/// <inheritdoc />
Expand Down
16 changes: 4 additions & 12 deletions src/Shared/ActivityInstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// </copyright>

using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
#pragma warning restore IDE0005

namespace OpenTelemetry.Instrumentation;
Expand All @@ -28,19 +26,13 @@ internal static class ActivityInstrumentationHelper

private static Action<Activity, ActivitySource> CreateActivitySourceSetter()
{
ParameterExpression instance = Expression.Parameter(typeof(Activity), "instance");
ParameterExpression propertyValue = Expression.Parameter(typeof(ActivitySource), "propertyValue");
PropertyInfo sourcePropertyInfo = typeof(Activity).GetProperty("Source");
var body = Expression.Assign(Expression.Property(instance, sourcePropertyInfo), propertyValue);
return Expression.Lambda<Action<Activity, ActivitySource>>(body, instance, propertyValue).Compile();
return (Action<Activity, ActivitySource>)typeof(Activity).GetProperty("Source")
.SetMethod.CreateDelegate(typeof(Action<Activity, ActivitySource>));
}

private static Action<Activity, ActivityKind> CreateActivityKindSetter()
{
ParameterExpression instance = Expression.Parameter(typeof(Activity), "instance");
ParameterExpression propertyValue = Expression.Parameter(typeof(ActivityKind), "propertyValue");
PropertyInfo kindPropertyInfo = typeof(Activity).GetProperty("Kind");
var body = Expression.Assign(Expression.Property(instance, kindPropertyInfo), propertyValue);
return Expression.Lambda<Action<Activity, ActivityKind>>(body, instance, propertyValue).Compile();
return (Action<Activity, ActivityKind>)typeof(Activity).GetProperty("Kind")
.SetMethod.CreateDelegate(typeof(Action<Activity, ActivityKind>));
}
}

0 comments on commit 41daba4

Please sign in to comment.