Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion docs/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ You may continue using obsolete APIs in your application, but we advise explorin
| `METGEN010` | Metric methods must be static |
| `METGEN011` | A strong type object contains duplicate tag names |
| `METGEN012` | A metric class contains an invalid tag name type |
| `METGEN013` | A metric class contains too many tag names |
| `METGEN014` | A metering attribute type argument is invalid |
| `METGEN015` | Metric methods mustn't use any external type as the return type |
| `METGEN016` | Metric methods mustn't use any generic type as the return type |
Expand Down
6 changes: 0 additions & 6 deletions src/Generators/Microsoft.Gen.Metrics/DiagDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
messageFormat: Resources.ErrorInvalidTagTypeMessage,
category: Category);

public static DiagnosticDescriptor ErrorTooManyTagNames { get; } = Make(
id: DiagnosticIds.Metrics.METGEN013,
title: Resources.ErrorTooManyTagNamesTitle,
messageFormat: Resources.ErrorTooManyTagNamesMessage,
category: Category);

public static DiagnosticDescriptor ErrorInvalidAttributeGenericType { get; } = Make(
id: DiagnosticIds.Metrics.METGEN014,
title: Resources.ErrorInvalidAttributeGenericTypeTitle,
Expand Down
7 changes: 0 additions & 7 deletions src/Generators/Microsoft.Gen.Metrics/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace Microsoft.Gen.Metrics;

internal sealed class Parser
{
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);
private static readonly SymbolDisplayFormat _typeSymbolFormat =
Expand Down Expand Up @@ -667,11 +665,6 @@ private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters(
ex.NamedType.ToDisplayString());
}

if (strongTypeAttributeParameters.TagHashSet.Count > MaxTagNames)
{
Diag(DiagDescriptors.ErrorTooManyTagNames, strongTypeSymbol.Locations[0]);
}

strongTypeAttributeParameters.StrongTypeObjectName = constructorArg.Value.ToString();
return strongTypeAttributeParameters;
}
Expand Down
18 changes: 0 additions & 18 deletions src/Generators/Microsoft.Gen.Metrics/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/Generators/Microsoft.Gen.Metrics/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,6 @@
<data name="ErrorNotStaticMethodTitle" xml:space="preserve">
<value>Metric methods must be static</value>
</data>
<data name="ErrorTooManyTagNamesMessage" xml:space="preserve">
<value>The type {0} has too many tag names</value>
</data>
<data name="ErrorTooManyTagNamesTitle" xml:space="preserve">
<value>A metric class contains too many tag names</value>
</data>
<data name="ErrorXmlNotLoadedCorrectlyTitle" xml:space="preserve">
<value>Xml comment was not parsed correctly</value>
</data>
Expand Down
20 changes: 20 additions & 0 deletions test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,24 @@ public void ValidateCounterStrongType()
},
measurement.Tags.Select(x => (x.Key, x.Value)));
}

[Fact]
public void ValidateStrongTypeWithMoreThanThirtyTags()
{
var tags = new ManyTagsDimensions();
for (int i = 1; i <= 33; i++)
{
typeof(ManyTagsDimensions).GetProperty($"D{i}")!.SetValue(tags, $"v{i}");
}

using var collector = new MetricCollector<long>(_meter, nameof(ManyTagsCounter));
ManyTagsCounter counter = ManyTagsTestExtensions.CreateManyTagsCounter(_meter);
counter.Add(1L, tags);

var measurement = Assert.Single(collector.GetMeasurementSnapshot());
Assert.Equal(1L, measurement.Value);
Assert.Equal(
Enumerable.Range(1, 33).Select(i => ($"D{i}", (object?)$"v{i}")),
measurement.Tags.Select(x => (x.Key, x.Value)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.Metrics;
using Microsoft.Extensions.Diagnostics.Metrics;

namespace TestClasses
{
#pragma warning disable SA1402 // File may only contain a single type
internal static partial class ManyTagsTestExtensions
{
[Counter(typeof(ManyTagsDimensions))]
public static partial ManyTagsCounter CreateManyTagsCounter(Meter meter);
}

public class ManyTagsDimensions
{
public string D1 { get; set; } = string.Empty;
public string D2 { get; set; } = string.Empty;
public string D3 { get; set; } = string.Empty;
public string D4 { get; set; } = string.Empty;
public string D5 { get; set; } = string.Empty;
public string D6 { get; set; } = string.Empty;
public string D7 { get; set; } = string.Empty;
public string D8 { get; set; } = string.Empty;
public string D9 { get; set; } = string.Empty;
public string D10 { get; set; } = string.Empty;
public string D11 { get; set; } = string.Empty;
public string D12 { get; set; } = string.Empty;
public string D13 { get; set; } = string.Empty;
public string D14 { get; set; } = string.Empty;
public string D15 { get; set; } = string.Empty;
public string D16 { get; set; } = string.Empty;
public string D17 { get; set; } = string.Empty;
public string D18 { get; set; } = string.Empty;
public string D19 { get; set; } = string.Empty;
public string D20 { get; set; } = string.Empty;
public string D21 { get; set; } = string.Empty;
public string D22 { get; set; } = string.Empty;
public string D23 { get; set; } = string.Empty;
public string D24 { get; set; } = string.Empty;
public string D25 { get; set; } = string.Empty;
public string D26 { get; set; } = string.Empty;
public string D27 { get; set; } = string.Empty;
public string D28 { get; set; } = string.Empty;
public string D29 { get; set; } = string.Empty;
public string D30 { get; set; } = string.Empty;
public string D31 { get; set; } = string.Empty;
public string D32 { get; set; } = string.Empty;
public string D33 { get; set; } = string.Empty;
}
#pragma warning restore SA1402
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,32 +357,6 @@ public static partial class MetricClass
Assert.Equal(DiagDescriptors.ErrorInvalidTagNameType.Id, d[0].Id);
}

[Fact]
public async Task TooManyDimensions()
{
StringBuilder sb = new StringBuilder();

int i = 0;

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(C0), Name=""TotalCountTest"")]
public static partial TotalCount CreateTotalCountCounter(Meter meter);
}");

var d = await RunGenerator(sb.ToString());

_ = Assert.Single(d);
Assert.Equal(DiagDescriptors.ErrorTooManyTagNames.Id, d[0].Id);
}

[Theory]
[InlineData("ulong")]
[InlineData("uint")]
Expand Down Expand Up @@ -540,29 +514,27 @@ partial class C
}

[Fact]
public async Task Gauge_StrongType_TooManyTags()
public async Task Gauge_StrongType_MoreThanThirtyTags()
{
var sb = new StringBuilder();

// Create 31 nested classes (max is 30)
for (int i = 0; i < 31; i++)
for (int i = 0; i < 33; i++)
{
sb.AppendLine($"public class C{i} : C{i + 1} {{ public string Tag{i} {{ get; set; }} }}");
}

sb.AppendLine("public class C31 { public string Tag31 { get; set; } }");
sb.AppendLine("public class C33 { public string Tag33 { get; set; } }");

sb.AppendLine(@"
partial class C
{
[Gauge(typeof(C0), Name=""TooManyTags"")]
static partial TooManyTags CreateGauge(Meter meter);
[Gauge(typeof(C0), Name=""ManyTags"")]
static partial ManyTags CreateGauge(Meter meter);
}");

var d = await RunGenerator(sb.ToString());

var diag = Assert.Single(d);
Assert.Equal(DiagDescriptors.ErrorTooManyTagNames.Id, diag.Id);
Assert.Empty(d);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,20 @@ public static partial class MetricClass
}

[Fact]
public async Task MaxDimensions()
public async Task MoreThanThirtyDimensions()
{
StringBuilder sb = new StringBuilder();
int i = 1;
for (; i < 30; i++)
sb.AppendLine("public class ManyTags");
sb.AppendLine("{");
for (int i = 1; i <= 33; i++)
{
sb.AppendLine($"public class C{i} : C{i + 1} {{ public string dim{i} {{get;set;}}}}");
sb.AppendLine($" public string D{i} {{ get; set; }} = string.Empty;");
}

sb.AppendLine($"public class C{i} {{ public string dim{i} {{get;set;}}}}");
sb.AppendLine("}");
sb.AppendLine(@" public static partial class MetricClass
{
[Histogram(typeof(C1), Name=""TotalCountTest"")]
[Histogram(typeof(ManyTags), Name=""TotalCountTest"")]
public static partial TotalCount CreateTotalCountCounter(Meter meter);
}");
var d = await RunGenerator(sb.ToString());
Expand Down
Loading