From 03d6f68e621d60ee8d6f7c90cd3e3cd1c5fd89da Mon Sep 17 00:00:00 2001 From: Jan Guttek Date: Wed, 18 Oct 2023 09:40:35 +0200 Subject: [PATCH 1/4] Commit a0d6364e: Merged PR 801011: Increased the max supported dimensions via source-gen to 30 dimensions --- .../Microsoft.Gen.Metrics/Parser.cs | 10 +-- .../StrongTypeAttributeParameters.cs | 2 +- .../Unit/ParserTests.Diagnostics.cs | 2 +- .../Unit/ParserTests.StrongTypes.cs | 69 +++++++++++++++++++ 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/Generators/Microsoft.Gen.Metrics/Parser.cs b/src/Generators/Microsoft.Gen.Metrics/Parser.cs index ed7342f999a..d42e91fc50f 100644 --- a/src/Generators/Microsoft.Gen.Metrics/Parser.cs +++ b/src/Generators/Microsoft.Gen.Metrics/Parser.cs @@ -19,7 +19,7 @@ namespace Microsoft.Gen.Metrics; internal sealed class Parser { - private const int MaxTagNames = 20; + private const int MaxTagNames = 30; private static readonly Regex _regex = new("^[A-Z]+[A-za-z0-9]*$", RegexOptions.Compiled); private static readonly Regex _regexTagNames = new("^[A-Za-z]+[A-Za-z0-9_.:-]*$", RegexOptions.Compiled); @@ -450,7 +450,7 @@ private void GetTagDescription( IsExtensionMethod = methodSymbol.IsExtensionMethod, Modifiers = methodSyntax.Modifiers.ToString(), MetricTypeName = methodSymbol.ReturnType.ToDisplayString(), // Roslyn doesn't know this type yet, no need to use a format here - StrongTypeConfigs = strongTypeAttrParams.StrongTypeConfigs, + StrongTypeConfigs = strongTypeAttrParams.DimensionHashSet, StrongTypeObjectName = strongTypeAttrParams.StrongTypeObjectName, IsTagTypeClass = strongTypeAttrParams.IsClass, MetricTypeModifiers = typeDeclaration.Modifiers.ToString(), @@ -640,11 +640,11 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters( var tagConfigs = BuildTagConfigs(member, typesChain, strongTypeAttributeParameters.TagHashSet, strongTypeAttributeParameters.TagDescriptionDictionary, symbols, _builders.GetStringBuilder()); - strongTypeAttributeParameters.StrongTypeConfigs.AddRange(tagConfigs); + strongTypeAttributeParameters.DimensionHashSet.AddRange(tagConfigs); } // Now that all of the current level and below dimensions are extracted, let's get any parent ones - strongTypeAttributeParameters.StrongTypeConfigs.AddRange(GetParentTagConfigs(strongTypeSymbol, + strongTypeAttributeParameters.DimensionHashSet.AddRange(GetParentTagConfigs(strongTypeSymbol, strongTypeAttributeParameters.TagHashSet, strongTypeAttributeParameters.TagDescriptionDictionary, symbols)); } catch (TransitiveTypeCycleException ex) @@ -656,7 +656,7 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters( ex.NamedType.ToDisplayString()); } - if (strongTypeAttributeParameters.StrongTypeConfigs.Count > MaxTagNames) + if (strongTypeAttributeParameters.DimensionHashSet.Count > MaxTagNames) { Diag(DiagDescriptors.ErrorTooManyTagNames, strongTypeSymbol.Locations[0]); } diff --git a/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs b/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs index 139973aa661..f79c7c839c9 100644 --- a/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs +++ b/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs @@ -11,7 +11,7 @@ internal sealed class StrongTypeAttributeParameters public string MetricNameFromAttribute = string.Empty; public HashSet TagHashSet = []; public Dictionary TagDescriptionDictionary = []; - public List StrongTypeConfigs = []; + public List DimensionHashSet = []; public string StrongTypeObjectName = string.Empty; public bool IsClass; } diff --git a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs index ddc0288bdc5..e9e26ed8b2a 100644 --- a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs +++ b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs @@ -387,7 +387,7 @@ public async Task TooManyDimensions() int i = 0; - for (; i < 21; i++) + for (; i < 30; i++) { sb.AppendLine($"public class C{i} : C{i + 1} {{ public string dim{i} {{get;set;}}}}"); } diff --git a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs index 3ad13cee738..6c0defdc098 100644 --- a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs +++ b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Text; using System.Threading.Tasks; using Xunit; @@ -590,4 +591,72 @@ public static partial class MetricClass Assert.Empty(d); } + + [Fact] + public async Task MaxDimensions() + { + StringBuilder sb = new StringBuilder(); + int i = 1; + for (; i < 30; i++) + { + sb.AppendLine($"public class C{i} : C{i + 1} {{ public string dim{i} {{get;set;}}}}"); + } + sb.AppendLine($"public class C{i} {{ public string dim{i} {{get;set;}}}}"); + sb.AppendLine(@" public static partial class MetricClass + { + [Histogram(typeof(C1), Name=""TotalCountTest"")] + public static partial TotalCount CreateTotalCountCounter(Meter meter); + }"); + var d = await RunGenerator(sb.ToString()); + Assert.Empty(d); + } + + [Fact] + public async Task TransitiveDimensions() + { + var d = await RunGenerator(@" + class MyClassA + { + public string Dim1 { get; set; } + public string Dim2 { get; set; } + public string Dim3 { get; set; } + public string Dim4 { get; set; } + public string Dim5 { get; set; } + public string Dim6 { get; set; } + public string Dim7 { get; set; } + public string Dim8 { get; set; } + public string Dim9 { get; set; } + public string Dim10 { get; set; } + public string Dim11 { get; set; } + public string Dim12 { get; set; } + public string Dim13 { get; set; } + public string Dim14 { get; set; } + public string Dim15 { get; set; } + public string Dim16 { get; set; } + public string Dim17 { get; set; } + public string Dim18 { get; set; } + public string Dim19 { get; set; } + public string Dim20 { get; set; } + public MyClassB MyTransitiveProperty { get; set; } + } + class MyClassB + { + public string Dim21 { get; set; } + public string Dim22 { get; set; } + public string Dim23 { get; set; } + public string Dim24 { get; set; } + public string Dim25 { get; set; } + public string Dim26 { get; set; } + public string Dim27 { get; set; } + public string Dim28 { get; set; } + public string Dim29 { get; set; } + public string Dim30 { get; set; } + } + static partial class MetricClass + { + [Histogram(typeof(MyClassA))] + static partial TotalCount CreateTotalCountCounter(Meter meter); + }"); + Assert.Empty(d); + } } From 3502d67494d01703db6755f5930b7dffd874dd75 Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Wed, 18 Oct 2023 11:06:03 +0200 Subject: [PATCH 2/4] Update test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs --- .../Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs index 6c0defdc098..6b345029492 100644 --- a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs +++ b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs @@ -601,6 +601,7 @@ public async Task MaxDimensions() { sb.AppendLine($"public class C{i} : C{i + 1} {{ public string dim{i} {{get;set;}}}}"); } + sb.AppendLine($"public class C{i} {{ public string dim{i} {{get;set;}}}}"); sb.AppendLine(@" public static partial class MetricClass { From 8c03ca4e22c82e5a5a839b277bad82ef32a232ee Mon Sep 17 00:00:00 2001 From: Jan Guttek Date: Wed, 18 Oct 2023 11:39:04 +0200 Subject: [PATCH 3/4] Revert --- src/Generators/Microsoft.Gen.Metrics/Parser.cs | 8 ++++---- .../StrongTypeAttributeParameters.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Generators/Microsoft.Gen.Metrics/Parser.cs b/src/Generators/Microsoft.Gen.Metrics/Parser.cs index d42e91fc50f..5beb1eeb48f 100644 --- a/src/Generators/Microsoft.Gen.Metrics/Parser.cs +++ b/src/Generators/Microsoft.Gen.Metrics/Parser.cs @@ -450,7 +450,7 @@ private void GetTagDescription( IsExtensionMethod = methodSymbol.IsExtensionMethod, Modifiers = methodSyntax.Modifiers.ToString(), MetricTypeName = methodSymbol.ReturnType.ToDisplayString(), // Roslyn doesn't know this type yet, no need to use a format here - StrongTypeConfigs = strongTypeAttrParams.DimensionHashSet, + StrongTypeConfigs = strongTypeAttrParams.StrongTypeConfigs, StrongTypeObjectName = strongTypeAttrParams.StrongTypeObjectName, IsTagTypeClass = strongTypeAttrParams.IsClass, MetricTypeModifiers = typeDeclaration.Modifiers.ToString(), @@ -640,11 +640,11 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters( var tagConfigs = BuildTagConfigs(member, typesChain, strongTypeAttributeParameters.TagHashSet, strongTypeAttributeParameters.TagDescriptionDictionary, symbols, _builders.GetStringBuilder()); - strongTypeAttributeParameters.DimensionHashSet.AddRange(tagConfigs); + strongTypeAttributeParameters.StrongTypeConfigs.AddRange(tagConfigs); } // Now that all of the current level and below dimensions are extracted, let's get any parent ones - strongTypeAttributeParameters.DimensionHashSet.AddRange(GetParentTagConfigs(strongTypeSymbol, + strongTypeAttributeParameters.StrongTypeConfigs.AddRange(GetParentTagConfigs(strongTypeSymbol, strongTypeAttributeParameters.TagHashSet, strongTypeAttributeParameters.TagDescriptionDictionary, symbols)); } catch (TransitiveTypeCycleException ex) @@ -656,7 +656,7 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters( ex.NamedType.ToDisplayString()); } - if (strongTypeAttributeParameters.DimensionHashSet.Count > MaxTagNames) + if (strongTypeAttributeParameters.StrongTypeConfigs.Count > MaxTagNames) { Diag(DiagDescriptors.ErrorTooManyTagNames, strongTypeSymbol.Locations[0]); } diff --git a/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs b/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs index f79c7c839c9..139973aa661 100644 --- a/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs +++ b/src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs @@ -11,7 +11,7 @@ internal sealed class StrongTypeAttributeParameters public string MetricNameFromAttribute = string.Empty; public HashSet TagHashSet = []; public Dictionary TagDescriptionDictionary = []; - public List DimensionHashSet = []; + public List StrongTypeConfigs = []; public string StrongTypeObjectName = string.Empty; public bool IsClass; } From fe61bdddc27c6892a0991e6064ada9c859c811ca Mon Sep 17 00:00:00 2001 From: Jan Guttek Date: Wed, 18 Oct 2023 11:41:18 +0200 Subject: [PATCH 4/4] Check correct property --- src/Generators/Microsoft.Gen.Metrics/Parser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/Microsoft.Gen.Metrics/Parser.cs b/src/Generators/Microsoft.Gen.Metrics/Parser.cs index 5beb1eeb48f..20c60fb0529 100644 --- a/src/Generators/Microsoft.Gen.Metrics/Parser.cs +++ b/src/Generators/Microsoft.Gen.Metrics/Parser.cs @@ -656,7 +656,7 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters( ex.NamedType.ToDisplayString()); } - if (strongTypeAttributeParameters.StrongTypeConfigs.Count > MaxTagNames) + if (strongTypeAttributeParameters.TagHashSet.Count > MaxTagNames) { Diag(DiagDescriptors.ErrorTooManyTagNames, strongTypeSymbol.Locations[0]); }