-
Notifications
You must be signed in to change notification settings - Fork 849
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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
dariusclay
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
solving ticket #7079