-
Notifications
You must be signed in to change notification settings - Fork 882
Add unit parameter to metric source generator #7099
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a7ee672
updates
mariamgerges 0dfcced
fixes
mariamgerges 0d19bf7
couple more tests
mariamgerges f5f1670
adding @verbatim
mariamgerges 455e035
adding experimental attribute
mariamgerges 81de4ed
testing unit in attribute
mariamgerges 0b716fa
updating tests
mariamgerges 6d566f7
comments
mariamgerges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.Unit.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.Extensions.Diagnostics.Metrics.Testing; | ||
| using TestClasses; | ||
| using Xunit; | ||
| namespace Microsoft.Gen.Metrics.Test; | ||
|
|
||
| public partial class MetricTests | ||
| { | ||
| [Fact] | ||
| public void ValidateCounterWithUnit() | ||
| { | ||
| // Verify that a counter created with a unit works correctly | ||
|
mariamgerges marked this conversation as resolved.
Outdated
|
||
| using var collector = new MetricCollector<long>(_meter, "CounterWithUnit"); | ||
|
|
||
| // You'll need to add this to TestClasses/MetricsWithUnit.cs | ||
| CounterWithUnit counter = MetricsWithUnit.CreateCounterWithUnit(_meter); | ||
| counter.Add(100L); | ||
|
|
||
| var measurement = Assert.Single(collector.GetMeasurementSnapshot()); | ||
| Assert.Equal(100L, measurement.Value); | ||
| Assert.Empty(measurement.Tags); | ||
| Assert.NotNull(collector.Instrument); | ||
| Assert.Equal("seconds", collector.Instrument.Unit); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateHistogramWithUnit() | ||
| { | ||
| // Verify that a histogram created with a unit works correctly | ||
| using var collector = new MetricCollector<long>(_meter, "HistogramWithUnit"); | ||
|
|
||
| // You'll need to add this to TestClasses/HistogramTestExtensions.cs | ||
|
mariamgerges marked this conversation as resolved.
Outdated
|
||
| HistogramWithUnit histogram = MetricsWithUnit.CreateHistogramWithUnit(_meter); | ||
| histogram.Record(50L); | ||
|
|
||
| var measurement = Assert.Single(collector.GetMeasurementSnapshot()); | ||
| Assert.Equal(50L, measurement.Value); | ||
| Assert.Empty(measurement.Tags); | ||
|
|
||
| Assert.NotNull(collector.Instrument); | ||
| Assert.Equal("milliseconds", collector.Instrument.Unit); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateCounterWithUnitAndDimensions() | ||
| { | ||
| const long Value = 12345L; | ||
|
|
||
| using var collector = new MetricCollector<long>(_meter, "CounterWithUnitAndDims"); | ||
|
|
||
| CounterWithUnitAndDims counter = MetricsWithUnit.CreateCounterWithUnitAndDims(_meter); | ||
| counter.Add(Value, "dim1Value", "dim2Value"); | ||
|
|
||
| var measurement = Assert.Single(collector.GetMeasurementSnapshot()); | ||
| Assert.Equal(Value, measurement.Value); | ||
| Assert.Equal(new (string, object?)[] { ("s1", "dim1Value"), ("s2", "dim2Value") }, | ||
| measurement.Tags.Select(x => (x.Key, x.Value))); | ||
|
|
||
| // Verify the instrument has the correct unit | ||
| Assert.NotNull(collector.Instrument); | ||
| Assert.Equal("bytes", collector.Instrument.Unit); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateHistogramWithUnitAndDimensions() | ||
| { | ||
| const int Value = 9876; | ||
|
|
||
| using var collector = new MetricCollector<int>(_meter, "HistogramWithUnitAndDims"); | ||
|
|
||
| HistogramWithUnitAndDims histogram = MetricsWithUnit.CreateHistogramWithUnitAndDims(_meter); | ||
| histogram.Record(Value, "val1"); | ||
|
|
||
| var measurement = Assert.Single(collector.GetMeasurementSnapshot()); | ||
| Assert.Equal(Value, measurement.Value); | ||
| var tag = Assert.Single(measurement.Tags); | ||
| Assert.Equal(new KeyValuePair<string, object?>("s1", "val1"), tag); | ||
|
|
||
| // Verify the instrument has the correct unit | ||
| Assert.NotNull(collector.Instrument); | ||
| Assert.Equal("requests", collector.Instrument.Unit); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateGenericCounterWithUnit() | ||
| { | ||
| using var collector = new MetricCollector<double>(_meter, "GenericDoubleCounterWithUnit"); | ||
|
|
||
| GenericDoubleCounterWithUnit counter = MetricsWithUnit.CreateGenericDoubleCounterWithUnit(_meter); | ||
| counter.Add(3.14); | ||
|
|
||
| var measurement = Assert.Single(collector.GetMeasurementSnapshot()); | ||
| Assert.Equal(3.14, measurement.Value); | ||
| Assert.Empty(measurement.Tags); | ||
|
|
||
| // Verify the instrument has the correct unit | ||
| Assert.NotNull(collector.Instrument); | ||
| Assert.Equal("meters", collector.Instrument.Unit); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateCounterWithEmptyUnit() | ||
| { | ||
| // Test that counters with empty/null units work | ||
| using var collector = new MetricCollector<long>(_meter, nameof(Counter0D)); | ||
| Counter0D counter0D = CounterTestExtensions.CreateCounter0D(_meter); | ||
| counter0D.Add(10L); | ||
|
|
||
| var measurement = Assert.Single(collector.GetMeasurementSnapshot()); | ||
| Assert.Equal(10L, measurement.Value); | ||
|
|
||
| // Verify the instrument has no unit (or default unit) | ||
| Assert.NotNull(collector.Instrument); | ||
| Assert.Null(collector.Instrument.Unit); | ||
| } | ||
| } | ||
|
mariamgerges marked this conversation as resolved.
|
||
43 changes: 43 additions & 0 deletions
43
test/Generators/Microsoft.Gen.Metrics/TestClasses/MetricsWithUnit.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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.CodeAnalysis; | ||
| using System.Diagnostics.Metrics; | ||
| using Microsoft.Extensions.Diagnostics.Metrics; | ||
|
|
||
| namespace TestClasses | ||
| { | ||
| #pragma warning disable SA1402 // File may only contain a single type | ||
| [SuppressMessage("Usage", "CA1801:Review unused parameters", | ||
| Justification = "For testing emitter for classes without namespace")] | ||
| public static partial class MetricsWithUnit | ||
| { | ||
| [Counter(Unit = "seconds")] | ||
| public static partial CounterWithUnit CreateCounterWithUnit(Meter meter); | ||
|
|
||
| [Counter("s1", "s2", Unit = "bytes", Name = "CounterWithUnitAndDims")] | ||
| public static partial CounterWithUnitAndDims CreateCounterWithUnitAndDims(Meter meter); | ||
|
|
||
| [Counter(typeof(Dimensions), Unit = "bytes")] | ||
| public static partial CounterStrongTypeWithUnit CreateCounterStrongTypeWithUnit(Meter meter); | ||
|
|
||
| [Counter<double>(Unit = "meters", Name = "GenericDoubleCounterWithUnit")] | ||
| public static partial GenericDoubleCounterWithUnit CreateGenericDoubleCounterWithUnit(Meter meter); | ||
|
|
||
| [Histogram(Unit = "milliseconds", Name = "HistogramWithUnit")] | ||
| public static partial HistogramWithUnit CreateHistogramWithUnit(Meter meter); | ||
|
|
||
| [Histogram(typeof(Dimensions), Unit = "s")] | ||
| public static partial HistogramStrongTypeWithUnit CreateHistogramStrongTypeWithUnit(Meter meter); | ||
|
|
||
| [Histogram<int>("s1", Unit = "requests", Name = "HistogramWithUnitAndDims")] | ||
| public static partial HistogramWithUnitAndDims CreateHistogramWithUnitAndDims(Meter meter); | ||
| } | ||
|
|
||
| public class Dimensions | ||
| { | ||
| public string? Dim1; | ||
| public string? Dim2; | ||
| } | ||
| #pragma warning disable SA1402 | ||
| } | ||
|
evgenyfedorov2 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.