Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased the max supported dimensions via source gen to 30 dimensions #4580

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Generators/Microsoft.Gen.Metrics/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand All @@ -656,7 +656,7 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters(
ex.NamedType.ToDisplayString());
}

if (strongTypeAttributeParameters.StrongTypeConfigs.Count > MaxTagNames)
if (strongTypeAttributeParameters.DimensionHashSet.Count > MaxTagNames)
tekian marked this conversation as resolved.
Show resolved Hide resolved
{
Diag(DiagDescriptors.ErrorTooManyTagNames, strongTypeSymbol.Locations[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class StrongTypeAttributeParameters
public string MetricNameFromAttribute = string.Empty;
public HashSet<string> TagHashSet = [];
public Dictionary<string, string> TagDescriptionDictionary = [];
public List<StrongTypeConfig> StrongTypeConfigs = [];
public List<StrongTypeConfig> DimensionHashSet = [];
tekian marked this conversation as resolved.
Show resolved Hide resolved
public string StrongTypeObjectName = string.Empty;
public bool IsClass;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;}}}}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -590,4 +591,73 @@ 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;}}}}");
}
xakep139 marked this conversation as resolved.
Show resolved Hide resolved

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; }
tekian marked this conversation as resolved.
Show resolved Hide resolved
}
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);
}
}