Skip to content

Commit 58cd1fd

Browse files
author
Timothy Mothra
authored
[AzureMonitor] fix AOT warnings (part 5) (Azure#49733)
* set DynamicallyAccessedMembers attribute on generic methods * Introduce new method GetPropertyType * add extra unit test
1 parent 1d4f4c3 commit 58cd1fd

File tree

7 files changed

+61
-11
lines changed

7 files changed

+61
-11
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/CollectionConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering
77
using System.Collections.Generic;
88
using System.Globalization;
99
using System.Linq;
10+
using System.Diagnostics.CodeAnalysis;
1011
using Azure.Monitor.OpenTelemetry.LiveMetrics.Models;
1112

1213
using ExceptionDocument = Models.Exception;
@@ -118,7 +119,7 @@ private void UpdateAllErrorsWithKeyValue(CollectionConfigurationError[] errors,
118119

119120
public string ETag => info.ETag;
120121

121-
private static void AddMetric<TTelemetry>(
122+
private static void AddMetric<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry>(
122123
DerivedMetricInfo metricInfo,
123124
List<DerivedMetric<TTelemetry>> metrics,
124125
out CollectionConfigurationError[] errors)

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/DerivedMetric.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering
55
{
66
using System;
77
using System.Collections.Generic;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Globalization;
910
using System.Linq.Expressions;
1011
using System.Reflection;
@@ -16,7 +17,7 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering
1617
/// which defines which field to use as a value, and an aggregation which dictates the algorithm of arriving at
1718
/// a single reportable value within a second.
1819
/// </summary>
19-
internal class DerivedMetric<TTelemetry> where TTelemetry : DocumentIngress
20+
internal class DerivedMetric<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry> where TTelemetry : DocumentIngress
2021
{
2122
private const string ProjectionCount = "Count()";
2223

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/DocumentStream.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering
66
using System;
77
using System.Collections.Generic;
88
using System.Globalization;
9+
using System.Diagnostics.CodeAnalysis;
910
using Azure.Monitor.OpenTelemetry.LiveMetrics.Models;
1011
using ExceptionDocument = Models.Exception;
1112

@@ -88,7 +89,7 @@ public bool CheckFilters(Trace document, out CollectionConfigurationError[] erro
8889
return CheckFilters(traceFilterGroups, document, out errors);
8990
}
9091

91-
private static bool CheckFilters<TTelemetry>(
92+
private static bool CheckFilters<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry>(
9293
List<FilterConjunctionGroup<TTelemetry>> filterGroups,
9394
TTelemetry document,
9495
out CollectionConfigurationError[] errors)
@@ -121,7 +122,10 @@ private static bool CheckFilters<TTelemetry>(
121122
return leastOneConjunctionGroupPassed;
122123
}
123124

124-
private static bool CheckFiltersGeneric<TTelemetry>(TTelemetry document, FilterConjunctionGroup<TTelemetry> filterGroup, List<CollectionConfigurationError> errorList)
125+
private static bool CheckFiltersGeneric<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry>(
126+
TTelemetry document,
127+
FilterConjunctionGroup<TTelemetry> filterGroup,
128+
List<CollectionConfigurationError> errorList)
125129
where TTelemetry : DocumentIngress
126130
{
127131
bool filterPassed = false;

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/Filter.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering
1919
/// The filter's configuration (condition) is specified in a <see cref="FilterInfo"/> DTO.
2020
/// </summary>
2121
/// <typeparam name="TTelemetry">Type of telemetry documents.</typeparam>
22-
internal class Filter<TTelemetry> where TTelemetry : DocumentIngress
22+
internal class Filter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry> where TTelemetry : DocumentIngress
2323
{
2424
private const string FieldNameCustomDimensionsPrefix = "CustomDimensions.";
2525

@@ -370,10 +370,12 @@ private static Type GetPropertyTypeFromFieldName(string fieldName)
370370
{
371371
try
372372
{
373-
Type propertyType = fieldName.Split(FieldNameTrainSeparator)
374-
.Aggregate(
375-
typeof(TTelemetry),
376-
(type, propertyName) => type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public).PropertyType);
373+
Type propertyType = typeof(TTelemetry);
374+
375+
foreach (string propertyName in fieldName.Split(FieldNameTrainSeparator))
376+
{
377+
propertyType = GetPropertyType(propertyType, propertyName);
378+
}
377379

378380
if (fieldName == "Duration")
379381
{
@@ -401,6 +403,16 @@ private static Type GetPropertyTypeFromFieldName(string fieldName)
401403
}
402404
}
403405

406+
private static Type GetPropertyType(
407+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type,
408+
string propertyName)
409+
{
410+
PropertyInfo propertyInfo = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public)
411+
?? throw new ArgumentOutOfRangeException(nameof(propertyName), $"Property '{propertyName}' not found on type '{type.FullName}'");
412+
413+
return propertyInfo.PropertyType;
414+
}
415+
404416
[SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "Argument exceptions are valid.")]
405417
private static void ValidateInput(FilterInfo filterInfo)
406418
{

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/FilterConjunctionGroup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering
55
{
66
using System;
77
using System.Collections.Generic;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Globalization;
910
using Azure.Monitor.OpenTelemetry.LiveMetrics.Models;
1011

1112
/// <summary>
1213
/// Defines an AND group of filters.
1314
/// </summary>
14-
internal class FilterConjunctionGroup<TTelemetry> where TTelemetry : DocumentIngress
15+
internal class FilterConjunctionGroup<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry> where TTelemetry : DocumentIngress
1516
{
1617
private readonly FilterConjunctionGroupInfo info;
1718

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/LiveMetricsClientManager.MetricsCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.Filtering;
1212
using Azure.Monitor.OpenTelemetry.LiveMetrics.Internals.DataCollection;
1313
using System.Linq;
14+
using System.Diagnostics.CodeAnalysis;
1415

1516
namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Internals
1617
{
@@ -219,7 +220,7 @@ private Dictionary<string, AccumulatedValues> CreateMetricAccumulators(Collectio
219220
return metricAccumulators;
220221
}
221222

222-
private void ApplyFilters<TTelemetry>(
223+
private void ApplyFilters<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTelemetry>(
223224
Dictionary<string, AccumulatedValues> metricAccumulators,
224225
IEnumerable<DerivedMetric<TTelemetry>> metrics,
225226
TTelemetry telemetry,

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/tests/Azure.Monitor.OpenTelemetry.LiveMetrics.Tests/Filtering/FilterTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,5 +1747,35 @@ public void FilterSupportsRequestTelemetry()
17471747
}
17481748

17491749
#endregion
1750+
1751+
[Fact]
1752+
public void GetFieldType_CorrectlyDiscoversNestedType()
1753+
{
1754+
// ARRANGE
1755+
string fieldName = "Parent.Child.GrandChild";
1756+
1757+
// ACT
1758+
Filter<DocumentMockWithNestedProperties>.FieldNameType fieldNameType;
1759+
Type result = Filter<DocumentMockWithNestedProperties>.GetFieldType(fieldName, out fieldNameType);
1760+
1761+
// ASSERT
1762+
Assert.Equal(typeof(string), result);
1763+
Assert.Equal(Filter<DocumentMockWithNestedProperties>.FieldNameType.FieldName, fieldNameType);
1764+
}
1765+
1766+
internal class DocumentMockWithNestedProperties : DocumentIngress
1767+
{
1768+
public ParentType Parent { get; set; } = new ParentType();
1769+
1770+
public class ParentType
1771+
{
1772+
public ChildType Child { get; set; } = new ChildType();
1773+
1774+
public class ChildType
1775+
{
1776+
public string GrandChild { get; set; } = "TestValue";
1777+
}
1778+
}
1779+
}
17501780
}
17511781
}

0 commit comments

Comments
 (0)