Skip to content

Commit 3f7e1fb

Browse files
authored
Adding unit to metric source gen documentation (#50361)
* Adding unit to metric source gen * copilot suggestion
1 parent dec6f24 commit 3f7e1fb

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

docs/core/diagnostics/metrics-generator.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ The following code demonstrates how to use the generator with primitive types:
4949

5050
:::code language="csharp" source="snippets/MetricsGen/Metrics.cs" id="metrics" :::
5151

52+
## Specifying units
53+
54+
You can optionally specify a unit of measurement for your metrics using the `Unit` parameter. This helps provide context about what the metric measures (for example, "seconds", "bytes", and "requests"). The unit is passed to the underlying <xref:System.Diagnostics.Metrics.Meter> when creating the instrument.
55+
56+
The following code demonstrates how to use the generator with primitive types with units specified:
57+
58+
```csharp
59+
internal static partial class Metric
60+
{
61+
[Histogram<long>("requestName", "duration", Name = "MyCustomMetricName", Unit = "ms")]
62+
public static partial Latency CreateLatency(Meter meter);
63+
64+
[Counter<int>(
65+
MetricConstants.EnvironmentName,
66+
MetricConstants.Region,
67+
MetricConstants.RequestName,
68+
MetricConstants.RequestStatus,
69+
Unit = "requests")]
70+
public static partial TotalCount CreateTotalCount(Meter meter);
71+
72+
[Counter<int>(Unit = "failures")]
73+
public static partial TotalFailures CreateTotalFailures(this Meter meter);
74+
}
75+
```
76+
5277
The previous declaration automatically returns the following:
5378

5479
- `Latency` class with a `Record` method

docs/core/diagnostics/metrics-strongly-typed.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ The following code shows how to create and use these metrics in a class:
7878

7979
In the preceding `MyClass.DoWork` method, a `MetricTags` object is populated with values for each tag. This single `tags` object is then passed to all three instruments when recording data. The `Latency` metric (a histogram) records the elapsed time, and both counters (`TotalCount` and `TotalFailures`) record occurrence counts. Because all metrics share the same tag object type, the tags (`Dim1DimensionName`, `Operation`, `Dim2`, `Dim3`, `DimensionNameOfParentOperation`) are present on every measurement.
8080

81+
## Specifying units
82+
83+
You can optionally specify a unit of measurement for your metrics using the `Unit` parameter. This helps provide context about what the metric measures (for example, "seconds", "bytes", and "requests"). The unit is passed to the underlying <xref:System.Diagnostics.Metrics.Meter> when creating the instrument.
84+
85+
The following code demonstrates how to use the generator with primitive types with units specified:
86+
87+
```csharp
88+
public static partial class Metric
89+
{
90+
[Histogram<long>(typeof(MetricTags), Unit = "ms")]
91+
public static partial Latency CreateLatency(Meter meter);
92+
93+
[Counter<long>(typeof(MetricTags), Unit = "requests")]
94+
public static partial TotalCount CreateTotalCount(Meter meter);
95+
96+
[Counter<int>(typeof(MetricTags), Unit = "failures")]
97+
public static partial TotalFailures CreateTotalFailures(Meter meter);
98+
}
99+
```
100+
81101
## Performance considerations
82102

83103
Using strongly-typed tags via source generation adds no overhead compared to using metrics directly. If you need to further minimize allocations for very high-frequency metrics, consider defining your tag object as a `struct` (value type) instead of a `class`. Using a `struct` for the tag object can avoid heap allocations when recording metrics, since the tags would be passed by value.

0 commit comments

Comments
 (0)