diff --git a/src/Generators/Microsoft.Gen.Metrics/Parser.cs b/src/Generators/Microsoft.Gen.Metrics/Parser.cs index 20c60fb0529..b9d5697e24e 100644 --- a/src/Generators/Microsoft.Gen.Metrics/Parser.cs +++ b/src/Generators/Microsoft.Gen.Metrics/Parser.cs @@ -215,8 +215,8 @@ private static bool AreTagNamesValid(MetricMethod metricMethod) : symbol.TypeArguments[0]; private static (InstrumentKind instrumentKind, ITypeSymbol? genericType) GetInstrumentType( - INamedTypeSymbol? methodAttributeSymbol, - SymbolHolder symbols) + INamedTypeSymbol? methodAttributeSymbol, + SymbolHolder symbols) { if (methodAttributeSymbol == null) { @@ -492,13 +492,11 @@ private string GetSymbolXmlCommentSummary(ISymbol symbol) Diag(DiagDescriptors.ErrorMethodHasBody, methodSymbol.GetLocation()); keepMethod = false; } -#pragma warning disable S2583 // Conditionally executed code should be reachable else if (!isPartial) { Diag(DiagDescriptors.ErrorNotPartialMethod, methodSymbol.GetLocation()); keepMethod = false; } -#pragma warning restore S2583 // Conditionally executed code should be reachable // ensure Metric name is not empty and starts with a Capital letter. // ensure there are no duplicate ids. @@ -668,14 +666,14 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ /// /// Called recursively to build all required StrongTypeDimensionConfigs. /// - /// The Symbol being extracted. + /// The Symbol being extracted. /// A set of symbols in the current type chain. /// HashSet of all dimensions seen so far. /// Shared symbols. /// List of all property names when walking down the object model. See StrongTypeDimensionConfigs for an example. /// List of all StrongTypeDimensionConfigs seen so far. private List BuildTagConfigs( - ISymbol symbol, + ISymbol member, ISet typesChain, HashSet tagHashSet, Dictionary tagDescriptionDictionary, @@ -688,15 +686,16 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ SpecialType specialType; ITypeSymbol typeSymbol; - if (symbol.IsImplicitlyDeclared) + if (member.IsImplicitlyDeclared || + member.IsStatic) { return tagConfigs; } - switch (symbol.Kind) + switch (member.Kind) { case SymbolKind.Property: - var propertySymbol = symbol as IPropertySymbol; + var propertySymbol = member as IPropertySymbol; kind = propertySymbol!.Type.TypeKind; specialType = propertySymbol.Type.SpecialType; @@ -704,7 +703,7 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ break; case SymbolKind.Field: - var fieldSymbol = symbol as IFieldSymbol; + var fieldSymbol = member as IFieldSymbol; kind = fieldSymbol!.Type.TypeKind; specialType = fieldSymbol.Type.SpecialType; @@ -726,28 +725,28 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ { if (kind == TypeKind.Enum) { - var name = TryGetTagNameFromAttribute(symbol, symbols, out var tagName) + var name = TryGetTagNameFromAttribute(member, symbols, out var tagName) ? tagName - : symbol.Name; + : member.Name; if (!tagHashSet.Add(name)) { - Diag(DiagDescriptors.ErrorDuplicateTagName, symbol.Locations[0], symbol.Name); + Diag(DiagDescriptors.ErrorDuplicateTagName, member.Locations[0], member.Name); } else { tagConfigs.Add(new StrongTypeConfig { - Name = symbol.Name, + Name = member.Name, Path = stringBuilder.ToString(), TagName = name, StrongTypeMetricObjectType = StrongTypeMetricObjectType.Enum }); - var xmlDefinition = GetSymbolXmlCommentSummary(symbol); + var xmlDefinition = GetSymbolXmlCommentSummary(member); if (!string.IsNullOrEmpty(xmlDefinition)) { - tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? symbol.Name : tagName, xmlDefinition); + tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? member.Name : tagName, xmlDefinition); } } @@ -758,28 +757,28 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ { if (specialType == SpecialType.System_String) { - var name = TryGetTagNameFromAttribute(symbol, symbols, out var tagName) + var name = TryGetTagNameFromAttribute(member, symbols, out var tagName) ? tagName - : symbol.Name; + : member.Name; if (!tagHashSet.Add(name)) { - Diag(DiagDescriptors.ErrorDuplicateTagName, symbol.Locations[0], symbol.Name); + Diag(DiagDescriptors.ErrorDuplicateTagName, member.Locations[0], member.Name); } else { tagConfigs.Add(new StrongTypeConfig { - Name = symbol.Name, + Name = member.Name, Path = stringBuilder.ToString(), TagName = name, StrongTypeMetricObjectType = StrongTypeMetricObjectType.String }); - var xmlDefinition = GetSymbolXmlCommentSummary(symbol); + var xmlDefinition = GetSymbolXmlCommentSummary(member); if (!string.IsNullOrEmpty(xmlDefinition)) { - tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? symbol.Name : tagName, xmlDefinition); + tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? member.Name : tagName, xmlDefinition); } } @@ -793,13 +792,13 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ tagConfigs.Add(new StrongTypeConfig { - Name = symbol.Name, + Name = member.Name, Path = stringBuilder.ToString(), StrongTypeMetricObjectType = StrongTypeMetricObjectType.Class }); tagConfigs.AddRange( - WalkObjectModel(symbol, typesChain, namedTypeSymbol, stringBuilder, + WalkObjectModel(member, typesChain, namedTypeSymbol, stringBuilder, tagHashSet, tagDescriptionDictionary, symbols, true)); return tagConfigs; @@ -807,7 +806,7 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ } else { - Diag(DiagDescriptors.ErrorInvalidTagNameType, symbol.Locations[0]); + Diag(DiagDescriptors.ErrorInvalidTagNameType, member.Locations[0]); return tagConfigs; } } @@ -816,20 +815,20 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ { if (typeSymbol is not INamedTypeSymbol namedTypeSymbol) { - Diag(DiagDescriptors.ErrorInvalidTagNameType, symbol.Locations[0]); + Diag(DiagDescriptors.ErrorInvalidTagNameType, member.Locations[0]); } else { // User defined struct. First add into dimensionConfigs, then walk down the rest of the struct. tagConfigs.Add(new StrongTypeConfig { - Name = symbol.Name, + Name = member.Name, Path = stringBuilder.ToString(), StrongTypeMetricObjectType = StrongTypeMetricObjectType.Struct }); tagConfigs.AddRange( - WalkObjectModel(symbol, typesChain, namedTypeSymbol, stringBuilder, + WalkObjectModel(member, typesChain, namedTypeSymbol, stringBuilder, tagHashSet, tagDescriptionDictionary, symbols, false)); } @@ -837,7 +836,7 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ } else { - Diag(DiagDescriptors.ErrorInvalidTagNameType, symbol.Locations[0]); + Diag(DiagDescriptors.ErrorInvalidTagNameType, member.Locations[0]); return tagConfigs; } } @@ -846,9 +845,9 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[ _builders.ReturnStringBuilder(stringBuilder); } } -#pragma warning disable S107 // Methods should not have too many parameters // we can deal with this warning later +#pragma warning disable S107 // Methods should not have too many parameters private List WalkObjectModel( ISymbol parentSymbol, ISet typesChain, diff --git a/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.cs b/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.cs index a4d33fd9720..9b522d2df0a 100644 --- a/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.cs +++ b/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.cs @@ -1,11 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#if false - using System; using System.Collections.Generic; using System.Linq; +using Microsoft.Extensions.Diagnostics.Metrics.Testing; using TestClasses; using Xunit; @@ -16,94 +15,123 @@ public partial class MetricTests [Fact] public void ThrowsOnNullStrongTypeObjectExt() { - StrongTypeHistogramExt recorder = _meter.CreateHistogramExtStrongType(); - var ex = Assert.Throws(() => recorder.Record(4L, null!)); - Assert.NotNull(ex); + using (var collector = new MetricCollector(_meter, "MyHistogramStrongTypeMetricExt")) + { + StrongTypeHistogramExt recorder = _meter.CreateHistogramExtStrongType(); + var ex = Assert.Throws(() => recorder.Record(4L, null!)); + Assert.NotNull(ex); + } - StrongTypeDecimalCounterExt counter = _meter.CreateStrongTypeDecimalCounterExt(); - ex = Assert.Throws(() => counter.Add(4M, null!)); - Assert.NotNull(ex); + using (var collector = new MetricCollector(_meter, "MyCounterStrongTypeMetricExt")) + { + StrongTypeDecimalCounterExt counter = _meter.CreateStrongTypeDecimalCounterExt(); + var ex = Assert.Throws(() => counter.Add(4M, null!)); + Assert.NotNull(ex); + } } [Fact] - public void NonGenericCounterExtInstrumentTests() + public void NonGenericCounterExtNoDimsInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(CounterExt0D)); CounterExt0D counter0D = _meter.CreateCounterExt0D(); counter0D.Add(10L); counter0D.Add(5L); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(10L, x.GetValueOrThrow()), x => Assert.Equal(5L, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(10L, x.Value), x => Assert.Equal(5L, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); - _collector.Clear(); + } + [Fact] + public void NonGenericCounterExtInstrumentTests() + { + using var collector = new MetricCollector(_meter, nameof(CounterExt2D)); CounterExt2D counter2D = _meter.CreateCounterExt2D(); counter2D.Add(11L, "val1", "val2"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(11L, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(11L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2") }, measurement.Tags.Select(x => (x.Key, x.Value))); } [Fact] - public void NonGenericHistogramExtInstrumentTests() + public void NonGenericHistogramExtNoDimsInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(HistogramExt0D)); HistogramExt0D histogram0D = _meter.CreateHistogramExt0D(); histogram0D.Record(12L); histogram0D.Record(6L); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(12L, x.GetValueOrThrow()), x => Assert.Equal(6L, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(12L, x.Value), x => Assert.Equal(6L, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); - _collector.Clear(); + } + + [Fact] + public void NonGenericHistogramExtInstrumentTests() + { + using var collector = new MetricCollector(_meter, nameof(HistogramExt1D)); HistogramExt1D histogram1D = _meter.CreateHistogramExt1D(); histogram1D.Record(17L, "val_1"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(17L, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(17L, measurement.Value); var tag = Assert.Single(measurement.Tags); Assert.Equal(new KeyValuePair("s1", "val_1"), tag); } [Fact] - public void GenericCounterExtInstrumentTests() + public void GenericCounterExtNoDimsInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(GenericIntCounterExt0D)); GenericIntCounterExt0D counter0D = _meter.CreateGenericIntCounterExt0D(); counter0D.Add(10); counter0D.Add(5); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(10, x.GetValueOrThrow()), x => Assert.Equal(5, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(10, x.Value), x => Assert.Equal(5, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); - _collector.Clear(); + } + + [Fact] + public void GenericCounterExtInstrumentTests() + { + using var collector = new MetricCollector(_meter, nameof(GenericIntCounterExt1D)); GenericIntCounterExt1D counter2D = _meter.CreateGenericIntCounterExt1D(); counter2D.Add(11, "val1"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(11, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(11, measurement.Value); var tag = Assert.Single(measurement.Tags); Assert.Equal(new KeyValuePair("s1", "val1"), tag); } [Fact] - public void GenericHistogramExtInstrumentTests() + public void GenericHistogramExtNoDimsInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(GenericIntHistogramExt0D)); GenericIntHistogramExt0D histogram0D = _meter.CreateGenericIntHistogramExt0D(); histogram0D.Record(12); histogram0D.Record(6); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(12, x.GetValueOrThrow()), x => Assert.Equal(6, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(12, x.Value), x => Assert.Equal(6, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); - _collector.Clear(); + } + + [Fact] + public void GenericHistogramExtInstrumentTests() + { + using var collector = new MetricCollector(_meter, nameof(GenericIntHistogramExt2D)); GenericIntHistogramExt2D histogram1D = _meter.CreateGenericIntHistogramExt2D(); histogram1D.Record(17, "val_1", "val_2"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(17, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(17, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val_1"), ("s2", "val_2") }, measurement.Tags.Select(x => (x.Key, x.Value))); } @@ -133,13 +161,12 @@ public void ValidateHistogramExtStrongType() } }; + using var collector = new MetricCollector(_meter, "MyHistogramStrongTypeMetricExt"); StrongTypeHistogramExt recorder = _meter.CreateHistogramExtStrongType(); recorder.Record(1L, histogramDimensionsTest); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(1L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStrongTypeMetricExt", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(1L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -157,13 +184,11 @@ public void ValidateHistogramExtStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); histogramDimensionsTest.ChildDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); recorder.Record(2L, histogramDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(2L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStrongTypeMetricExt", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(2L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -181,13 +206,11 @@ public void ValidateHistogramExtStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); histogramDimensionsTest.GrandChildrenDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); recorder.Record(3L, histogramDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(3L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStrongTypeMetricExt", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(3L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -231,13 +254,12 @@ public void ValidateCounterExtStrongType() Dim1 = "Dim1", }; + using var collector = new MetricCollector(_meter, "MyCounterStrongTypeMetricExt"); StrongTypeDecimalCounterExt counter = _meter.CreateStrongTypeDecimalCounterExt(); counter.Add(1M, counterDimensionsTest); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(1M, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStrongTypeMetricExt", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(1M, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -255,13 +277,11 @@ public void ValidateCounterExtStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); counterDimensionsTest.ChildDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); counter.Add(2M, counterDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(2M, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStrongTypeMetricExt", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(2M, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -279,13 +299,11 @@ public void ValidateCounterExtStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); counterDimensionsTest.GrandChildDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); counter.Add(3M, counterDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(3M, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStrongTypeMetricExt", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(3M, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -303,5 +321,3 @@ public void ValidateCounterExtStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); } } - -#endif diff --git a/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.cs b/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.cs index 2b296e9a423..9dbf84c6d39 100644 --- a/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.cs +++ b/test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.cs @@ -1,14 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#if false - using System; using System.Collections.Generic; using System.Diagnostics.Metrics; using System.Globalization; using System.Linq; -using Microsoft.Extensions.Diagnostics.Metrics; +using Microsoft.Extensions.Diagnostics.Metrics.Testing; using TestClasses; using Xunit; @@ -16,18 +14,15 @@ namespace Microsoft.Gen.Metrics.Test; public partial class MetricTests : IDisposable { - private const string BaseMeterName = "Microsoft.GeneratedCode.Test.Metrics." + nameof(MetricTests) + "."; - - private readonly Meter _meter; - private readonly FakeMetricsCollector _collector; + private const string BaseMeterName = "Microsoft.Gen.Metrics.Test." + nameof(MetricTests) + "."; private readonly string _meterName; + private readonly Meter _meter; private bool _disposedValue; public MetricTests() { _meterName = BaseMeterName + Guid.NewGuid().ToString("d", CultureInfo.InvariantCulture); _meter = new Meter(_meterName); - _collector = new FakeMetricsCollector(new HashSet { _meterName }); } protected virtual void Dispose(bool disposing) @@ -37,7 +32,6 @@ protected virtual void Dispose(bool disposing) if (disposing) { _meter.Dispose(); - _collector.Dispose(); } _disposedValue = true; @@ -53,12 +47,13 @@ public void Dispose() [Fact] public void NonGenericCounter0DInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(Counter0D)); Counter0D counter0D = CounterTestExtensions.CreateCounter0D(_meter); counter0D.Add(10L); counter0D.Add(5L); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(10L, x.GetValueOrThrow()), x => Assert.Equal(5L, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(10L, x.Value), x => Assert.Equal(5L, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); } @@ -67,23 +62,25 @@ public void NonGenericCounter2DInstrumentTests() { const long Value = int.MaxValue + 4L; + using var collector = new MetricCollector(_meter, nameof(Counter2D)); Counter2D counter2D = CounterTestExtensions.CreateCounter2D(_meter); counter2D.Add(Value, "val1", "val2"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(Value, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(Value, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2") }, measurement.Tags.Select(x => (x.Key, x.Value))); } [Fact] public void NonGenericHistogram0DInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(Histogram0D)); Histogram0D histogram0D = HistogramTestExtensions.CreateHistogram0D(_meter); histogram0D.Record(12L); histogram0D.Record(6L); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(12L, x.GetValueOrThrow()), x => Assert.Equal(6L, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(12L, x.Value), x => Assert.Equal(6L, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); } @@ -92,11 +89,12 @@ public void NonGenericHistogram1DInstrumentTests() { const long Value = int.MaxValue + 3L; + using var collector = new MetricCollector(_meter, nameof(Histogram1D)); Histogram1D histogram1D = HistogramTestExtensions.CreateHistogram1D(_meter); histogram1D.Record(Value, "val_1"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(Value, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(Value, measurement.Value); var tag = Assert.Single(measurement.Tags); Assert.Equal(new KeyValuePair("s1", "val_1"), tag); } @@ -104,12 +102,13 @@ public void NonGenericHistogram1DInstrumentTests() [Fact] public void GenericCounter0DInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(GenericIntCounter0D)); GenericIntCounter0D counter0D = CounterTestExtensions.CreateGenericIntCounter0D(_meter); counter0D.Add(10); counter0D.Add(5); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(10, x.GetValueOrThrow()), x => Assert.Equal(5, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(10, x.Value), x => Assert.Equal(5, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); } @@ -118,11 +117,12 @@ public void GenericCounter2DInstrumentTests() { const int Value = int.MaxValue - 1; + using var collector = new MetricCollector(_meter, nameof(GenericIntCounter1D)); GenericIntCounter1D counter2D = CounterTestExtensions.CreateGenericIntCounter1D(_meter); counter2D.Add(Value, "val1"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(Value, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(Value, measurement.Value); var tag = Assert.Single(measurement.Tags); Assert.Equal(new KeyValuePair("s1", "val1"), tag); } @@ -130,12 +130,13 @@ public void GenericCounter2DInstrumentTests() [Fact] public void GenericHistogram0DInstrumentTests() { + using var collector = new MetricCollector(_meter, nameof(GenericIntHistogram0D)); GenericIntHistogram0D histogram0D = HistogramTestExtensions.CreateGenericIntHistogram0D(_meter); histogram0D.Record(12); histogram0D.Record(6); - var measurements = _collector.GetSnapshot(); - Assert.Collection(measurements, x => Assert.Equal(12, x.GetValueOrThrow()), x => Assert.Equal(6, x.GetValueOrThrow())); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Collection(measurements, x => Assert.Equal(12, x.Value), x => Assert.Equal(6, x.Value)); Assert.All(measurements, x => Assert.Empty(x.Tags)); } @@ -144,11 +145,12 @@ public void GenericHistogram2DInstrumentTests() { const int Value = short.MaxValue + 2; + using var collector = new MetricCollector(_meter, nameof(GenericIntHistogram2D)); GenericIntHistogram2D histogram1D = HistogramTestExtensions.CreateGenericIntHistogram2D(_meter); histogram1D.Record(Value, "val_1", "val_2"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(Value, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(Value, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val_1"), ("s2", "val_2") }, measurement.Tags.Select(x => (x.Key, x.Value))); } @@ -157,6 +159,7 @@ public void CreateOnExistingCounter() { const long Value = int.MaxValue + 2L; + using var collector = new MetricCollector(_meter, nameof(Counter4D)); Counter4D counter4D = CounterTestExtensions.CreateCounter4D(_meter); Counter4D newCounter4D = CounterTestExtensions.CreateCounter4D(_meter); Assert.Same(counter4D, newCounter4D); @@ -164,10 +167,9 @@ public void CreateOnExistingCounter() counter4D.Add(Value, "val1", "val2", "val3", "val4"); newCounter4D.Add(Value, "val3", "val4", "val5", "val6"); - Assert.Equal(2, _collector.Count); - var measurements = _collector.GetSnapshot(); - Assert.All(measurements, x => Assert.Equal(Value, x.GetValueOrThrow())); - Assert.Same(measurements[0].Instrument, measurements[1].Instrument); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Equal(2, measurements.Count); + Assert.All(measurements, x => Assert.Equal(Value, x.Value)); var tags = measurements[0].Tags.Select(x => (x.Key, x.Value)); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2"), ("s3", "val3"), ("s4", "val4") }, tags); @@ -181,6 +183,7 @@ public void CreateOnExistingHistogram() { const long Value = int.MaxValue + 1L; + using var collector = new MetricCollector(_meter, nameof(Histogram4D)); Histogram4D histogram4D = HistogramTestExtensions.CreateHistogram4D(_meter); Histogram4D newHistogram4D = HistogramTestExtensions.CreateHistogram4D(_meter); Assert.Same(histogram4D, newHistogram4D); @@ -188,10 +191,9 @@ public void CreateOnExistingHistogram() histogram4D.Record(Value, "val1", "val2", "val3", "val4"); newHistogram4D.Record(Value, "val3", "val4", "val5", "val6"); - Assert.Equal(2, _collector.Count); - var measurements = _collector.GetSnapshot(); - Assert.All(measurements, x => Assert.Equal(Value, x.GetValueOrThrow())); - Assert.Same(measurements[0].Instrument, measurements[1].Instrument); + var measurements = collector.GetMeasurementSnapshot(); + Assert.Equal(2, measurements.Count); + Assert.All(measurements, x => Assert.Equal(Value, x.Value)); var tags = measurements[0].Tags.Select(x => (x.Key, x.Value)); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2"), ("s3", "val3"), ("s4", "val4") }, tags); @@ -241,96 +243,102 @@ public void CreateOnExistingCounter_WithSameMeterName_ShouldReturnDifferentMetri [Fact] public void ValidateCounterWithDifferentDimensions() { + using var collector = new MetricCollector(_meter, nameof(Counter2D)); Counter2D counter2D = CounterTestExtensions.CreateCounter2D(_meter); counter2D.Add(17L, "val1", "val2"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(17L, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(17L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2") }, measurement.Tags.Select(x => (x.Key, x.Value))); - _collector.Clear(); + collector.Clear(); counter2D.Add(5L, "val1", "val2"); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(5L, measurement.GetValueOrThrow()); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(5L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2") }, measurement.Tags.Select(x => (x.Key, x.Value))); - _collector.Clear(); + collector.Clear(); // Different Dimensions counter2D.Add(5L, "val1", "val4"); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(5L, measurement.GetValueOrThrow()); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(5L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val4") }, measurement.Tags.Select(x => (x.Key, x.Value))); } [Fact] public void ValidateHistogramWithDifferentDimensions() { + using var collector = new MetricCollector(_meter, nameof(Histogram2D)); Histogram2D histogram = HistogramTestExtensions.CreateHistogram2D(_meter); histogram.Record(10L, "val1", "val2"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(10L, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(10L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2") }, measurement.Tags.Select(x => (x.Key, x.Value))); - _collector.Clear(); + collector.Clear(); histogram.Record(5L, "val1", "val2"); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(5L, measurement.GetValueOrThrow()); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(5L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val2") }, measurement.Tags.Select(x => (x.Key, x.Value))); - _collector.Clear(); + collector.Clear(); // Different Dimensions histogram.Record(5L, "val1", "val4"); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(5L, measurement.GetValueOrThrow()); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(5L, measurement.Value); Assert.Equal(new (string, object?)[] { ("s1", "val1"), ("s2", "val4") }, measurement.Tags.Select(x => (x.Key, x.Value))); } [Fact] - public void ValidateCounterWithFileScopedNamespace() + public void ValidateCounterWithFileScopedNamespaceNoDims() { - var longCunter = FileScopedExtensions.CreateCounter(_meter); - longCunter.Add(12L); + using var collector = new MetricCollector(_meter, nameof(FileScopedNamespaceCounter)); + FileScopedNamespaceCounter longCounter = FileScopedExtensions.CreateCounter(_meter); + longCounter.Add(12L); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(12L, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(12L, measurement.Value); Assert.Empty(measurement.Tags); - _collector.Clear(); + } + + [Fact] + public void ValidateCounterWithFileScopedNamespace() + { + using var collector = new MetricCollector(_meter, nameof(FileScopedNamespaceGenericDoubleCounter)); var genericDoubleCounter = FileScopedExtensions.CreateGenericDoubleCounter(_meter); genericDoubleCounter.Add(1.05D); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(1.05D, measurement.GetValueOrThrow()); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(1.05D, measurement.Value); Assert.Empty(measurement.Tags); } [Fact] public void ValidateCounterWithVariableParamsDimensions() { + using var collector = new MetricCollector(_meter, "MyCounterMetric"); CounterWithVariableParams counter = CounterTestExtensions.CreateCounterWithVariableParams(_meter); counter.Add(100_500L, Dim1: "val1", Dim_2: "val2", Dim_3: "val3"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(100_500L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterMetric", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(100_500L, measurement.Value); Assert.Equal(new (string, object?)[] { ("Dim1", "val1"), ("Dim_2", "val2"), ("Dim_3", "val3") }, measurement.Tags.Select(x => (x.Key, x.Value))); } [Fact] public void ValidateHistogramWithVariableParamsDimensions() { + using var collector = new MetricCollector(_meter, "MyHistogramMetric"); HistogramWithVariableParams histogram = HistogramTestExtensions.CreateHistogramWithVariableParams(_meter); histogram.Record(100L, Dim1: "val1", Dim_2: "val2", Dim_3: "val3"); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(100L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramMetric", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(100L, measurement.Value); Assert.Equal(new (string, object?)[] { ("Dim1", "val1"), ("Dim_2", "val2"), ("Dim_3", "val3") }, measurement.Tags.Select(x => (x.Key, x.Value))); } @@ -346,13 +354,12 @@ public void ValidateHistogramStructType() Operations2 = HistogramOperations.Operation1 }; + using var collector = new MetricCollector(_meter, "MyHistogramStructTypeMetric"); StructTypeHistogram recorder = HistogramTestExtensions.CreateHistogramStructType(_meter); recorder.Record(10L, histogramStruct); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(10L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStructTypeMetric", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(10L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -391,13 +398,12 @@ public void ValidateHistogramStrongType() } }; + using var collector = new MetricCollector(_meter, "MyHistogramStrongTypeMetric"); StrongTypeHistogram recorder = HistogramTestExtensions.CreateHistogramStrongType(_meter); recorder.Record(1L, histogramDimensionsTest); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(1L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStrongTypeMetric", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(1L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -415,14 +421,12 @@ public void ValidateHistogramStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); histogramDimensionsTest.ChildDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); recorder.Record(2L, histogramDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(2L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStrongTypeMetric", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(2L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -440,14 +444,12 @@ public void ValidateHistogramStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); histogramDimensionsTest.GrandChildrenDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); recorder.Record(3L, histogramDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(3L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyHistogramStrongTypeMetric", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(3L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -468,13 +470,19 @@ public void ValidateHistogramStrongType() [Fact] public void ThrowsOnNullStrongTypeObject() { - StrongTypeHistogram recorder = HistogramTestExtensions.CreateHistogramStrongType(_meter); - var ex = Assert.Throws(() => recorder.Record(4L, null!)); - Assert.NotNull(ex); + using (var collector = new MetricCollector(_meter, "MyHistogramStrongTypeMetric")) + { + StrongTypeHistogram recorder = HistogramTestExtensions.CreateHistogramStrongType(_meter); + var ex = Assert.Throws(() => recorder.Record(4L, null!)); + Assert.NotNull(ex); + } - StrongTypeDecimalCounter counter = CounterTestExtensions.CreateStrongTypeDecimalCounter(_meter); - ex = Assert.Throws(() => counter.Add(4M, null!)); - Assert.NotNull(ex); + using (var collector = new MetricCollector(_meter, "MyCounterStrongTypeMetric")) + { + StrongTypeDecimalCounter counter = CounterTestExtensions.CreateStrongTypeDecimalCounter(_meter); + var ex = Assert.Throws(() => counter.Add(4M, null!)); + Assert.NotNull(ex); + } } [Fact] @@ -489,13 +497,12 @@ public void ValidateCounterStructType() Operations2 = CounterOperations.Operation1 }; + using var collector = new MetricCollector(_meter, "MyCounterStructTypeMetric"); StructTypeCounter recorder = CounterTestExtensions.CreateCounterStructType(_meter); recorder.Add(11L, counterStruct); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(11L, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStructTypeMetric", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(11L, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -534,13 +541,12 @@ public void ValidateCounterStrongType() Dim1 = "Dim1", }; + using var collector = new MetricCollector(_meter, "MyCounterStrongTypeMetric"); StrongTypeDecimalCounter counter = CounterTestExtensions.CreateStrongTypeDecimalCounter(_meter); counter.Add(1M, counterDimensionsTest); - var measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(1M, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStrongTypeMetric", measurement.Instrument.Name); + var measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(1M, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -558,14 +564,12 @@ public void ValidateCounterStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); counterDimensionsTest.ChildDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); counter.Add(2M, counterDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(2M, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStrongTypeMetric", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(2M, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -583,13 +587,11 @@ public void ValidateCounterStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); counterDimensionsTest.GrandChildDimensionsObject = null!; - _collector.Clear(); + collector.Clear(); counter.Add(3M, counterDimensionsTest); - measurement = Assert.Single(_collector.GetSnapshot()); - Assert.Equal(3M, measurement.GetValueOrThrow()); - Assert.NotNull(measurement.Instrument); - Assert.Equal("MyCounterStrongTypeMetric", measurement.Instrument.Name); + measurement = Assert.Single(collector.GetMeasurementSnapshot()); + Assert.Equal(3M, measurement.Value); Assert.Equal( new (string, object?)[] { @@ -607,5 +609,3 @@ public void ValidateCounterStrongType() measurement.Tags.Select(x => (x.Key, x.Value))); } } - -#endif diff --git a/test/Generators/Microsoft.Gen.Metrics/TestClasses/CounterDimensions.cs b/test/Generators/Microsoft.Gen.Metrics/TestClasses/CounterDimensions.cs index 1685b7f669d..6b1f26b6d60 100644 --- a/test/Generators/Microsoft.Gen.Metrics/TestClasses/CounterDimensions.cs +++ b/test/Generators/Microsoft.Gen.Metrics/TestClasses/CounterDimensions.cs @@ -8,6 +8,11 @@ namespace TestClasses #pragma warning disable SA1402 // File may only contain a single type public class CounterDimensions : CounterParentDimensions { + // The generator should ignore these statics: + public const string Const = "Constant Value"; + + public static string Static = "Static Value"; + public string? Dim1; public CounterOperations OperationsEnum { get; set; } diff --git a/test/Generators/Microsoft.Gen.Metrics/TestClasses/HistogramTestExtensions.cs b/test/Generators/Microsoft.Gen.Metrics/TestClasses/HistogramTestExtensions.cs index d132c941bb6..d62bfad65c6 100644 --- a/test/Generators/Microsoft.Gen.Metrics/TestClasses/HistogramTestExtensions.cs +++ b/test/Generators/Microsoft.Gen.Metrics/TestClasses/HistogramTestExtensions.cs @@ -107,7 +107,13 @@ internal static partial class HistogramTestExtensions #pragma warning disable SA1402 // File may only contain a single type public class HistogramDimensionsTest : HistogramParentDimensions { + // The generator should ignore these statics: + public const string Const = "Constant Value"; + + public static string Static = "Static Value"; + public string? Dim1; + public HistogramOperations OperationsEnum { get; set; } [TagName("Enum2")] @@ -156,6 +162,11 @@ public class HistogramGrandChildrenDimensions public struct HistogramStruct { + // The generator should ignore these statics: + public const string Const = "Constant Value"; + + public static string Static = "Static Value"; + public string? Dim1 { get; set; } [TagName("DimInField")] diff --git a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs index e9e26ed8b2a..6532476648c 100644 --- a/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs +++ b/test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.Diagnostics.cs @@ -96,27 +96,6 @@ class Transitive Assert.Contains("Test.Transitive ⇆ Test.Interim", diag.GetMessage()); } - [Fact] - public async Task StrongTypeCounter_CyclicReference_Guid() - { - var d = await RunGenerator(@" - public class TypeA - { - public System.Guid TestA { get; set; } - } - - public static partial class MetricClass - { - [Counter(typeof(TypeA), Name=""CyclicTest"")] - public static partial CyclicTest CreateCyclicTestCounter(Meter meter); - }"); - - Assert.NotNull(d); - var diag = Assert.Single(d); - Assert.Equal(DiagDescriptors.ErrorTagTypeCycleDetected.Id, diag.Id); - Assert.Contains("System.Guid ⇆ System.Guid", diag.GetMessage()); - } - [Fact] public async Task StructTypeGauge() {