Add unit parameter to metric source generator#7099
Merged
mariamgerges merged 8 commits intomainfrom Dec 5, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds support for a Unit parameter to the metric source generator, allowing developers to specify units of measurement (like "seconds", "bytes", "meters") when defining metrics. The unit is stored in the generated metric instruments and can be retrieved for observability purposes.
Key changes:
- Added
Unitproperty to metric attribute classes (Counter,Histogram,Gauge, andTagNameattributes) - Updated parser to extract unit values from both named arguments and strong type configurations
- Modified emitter to include unit parameter when creating instruments
- Added comprehensive test coverage including parser tests and integration tests
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/CounterAttribute.cs | Added Unit property to allow specifying measurement units for counters |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/CounterAttributeT.cs | Added Unit property for generic counter attributes |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/HistogramAttribute.cs | Added Unit property for histogram attributes |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/HistogramAttributeT.cs | Added Unit property for generic histogram attributes |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/GaugeAttribute.cs | Added Unit property for gauge attributes |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/TagNameAttribute.cs | Incorrectly added Unit property to tag name attribute (used for dimension naming, not metrics) |
| src/Generators/Microsoft.Gen.Metrics/Model/MetricMethod.cs | Added MetricUnit field to store unit in the metric model |
| src/Generators/Microsoft.Gen.Metrics/StrongTypeAttributeParameters.cs | Added MetricUnitFromAttribute field to capture unit from strong type configurations |
| src/Generators/Microsoft.Gen.Metrics/Parser.cs | Updated parsing logic to extract unit from both named arguments and strong type attributes |
| src/Generators/Microsoft.Gen.Metrics/MetricFactoryEmitter.cs | Modified code generation to include unit parameter when creating instruments |
| test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.cs | Added parser test to verify unit parameter is accepted without errors |
| test/Generators/Microsoft.Gen.Metrics/Unit/ParserTests.StrongTypes.cs | Added parser test for strong types with unit parameter |
| test/Generators/Microsoft.Gen.Metrics/TestClasses/MetricsWithUnit.cs | Added test metric definitions covering various unit scenarios |
| test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.Unit.cs | Added integration tests validating unit functionality for counters and histograms |
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/TagNameAttribute.cs
Outdated
Show resolved
Hide resolved
test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.Unit.cs
Outdated
Show resolved
Hide resolved
evgenyfedorov2
approved these changes
Dec 2, 2025
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Metrics/CounterAttribute.cs
Outdated
Show resolved
Hide resolved
test/Generators/Microsoft.Gen.Metrics/Generated/MetricTests.Ext.Unit.cs
Outdated
Show resolved
Hide 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
solving ticket #7079