You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The previous declaration automatically returns the following:
74
50
@@ -97,47 +73,7 @@ internal class Latency
97
73
98
74
The dimensions specified in the attributes have been turned into arguments to the `Add` and `Record` methods. You then use the generated methods to create instances of these types. With the instancescreated, youcancall `Add` and `Record` toregistermetricvalues, asshowninthefollowingexample:
99
75
100
-
```csharp
101
-
internalclassMyClass
102
-
{
103
-
privatereadonlyLatency_latencyMetric;
104
-
privatereadonlyTotalCount_totalCountMetric;
105
-
privatereadonlyTotalFailures_totalFailuresMetric;
106
-
107
-
publicMyClass(Metermeter)
108
-
{
109
-
// Create metric instances using the source-generated factory methods
110
-
_latencyMetric=Metric.CreateLatency(meter);
111
-
_totalCountMetric=Metric.CreateTotalCount(meter);
112
-
// This syntax is available since `CreateTotalFailures` is defined as an extension method
113
-
_totalFailuresMetric=meter.CreateTotalFailures();
114
-
}
115
-
116
-
publicvoidReportSampleRequestCount()
117
-
{
118
-
// method logic ...
119
-
120
-
// Invoke Add on the counter and pass the dimension values.
Copy file name to clipboardExpand all lines: docs/core/diagnostics/metrics-strongly-typed.md
+5-121Lines changed: 5 additions & 121 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,90 +42,22 @@ By default, the source generator derives metric tag names from the field and pro
42
42
43
43
The following example demonstrates a simple counter metric with one tag. In this scenario, we want to count the number of processed requests and categorize them by a `Region` tag:
In the code above, `RequestTags` is a strongly-typed tag struct with a single property `Region`. The `CreateRequestCount` method is marked with <xref:Microsoft.Extensions.Diagnostics.Metrics.CounterAttribute`1> where `T` is an `int`, indicating it generates a **Counter** instrument that tracks `int` values. The attribute references `typeof(RequestTags)`, meaning the counter will use the tags defined in `RequestTags` when recording metrics. The source generator will produce a strongly-typed instrument class (named `RequestCount`) with an `Add` method that accepts integer value and `RequestTags` object.
59
48
60
49
To use the generated metric, create a <xref:System.Diagnostics.Metrics.Meter> and record measurements as shown below:
// Create a tag object with the relevant tag value
67
-
vartags=newRequestTags { Region="NorthAmerica" };
68
-
69
-
// Record a metric value with the associated tag
70
-
requestCountMetric.Add(1, tags);
71
-
```
51
+
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs id ="snippet_SimpleMetricTagUsage":::
72
52
73
53
In this usage example, calling `MyMetrics.CreateRequestCount(meter)` creates a counter instrument (via the `Meter`) and returns a `RequestCount` metric object. When you call `requestCountMetric.Add(1, tags)`, the metric system records a count of 1 associated with the tag `Region="NorthAmerica"`. You can reuse the `RequestTags` object or create new ones to record counts for different regions, and the tag name `Region` will consistently be applied to every measurement.
74
54
75
55
## Example 2: Metric with nested tag objects
76
56
77
57
For more complex scenarios, you can define tag classes that include multiple tags, nested objects, or even inherited properties. This allows a group of related metrics to share a common set of tags easily. In the next example, we define a set of tag classes and use them for three different metrics:
78
58
79
-
```csharp
80
-
usingSystem.Diagnostics.Metrics;
81
-
usingMicrosoft.Extensions.Diagnostics.Metrics;
82
-
83
-
publicclassMetricTags : MetricParentTags
84
-
{
85
-
[TagName("Dim1DimensionName")]
86
-
publicstring? Dim1; // custom tag name via attribute
87
-
88
-
publicOperationsOperation { get; set; } // tag name defaults to "Operation"
In this example, `MetricTags` is a tag class that inherits from `MetricParentTags` and also contains a nested tag object (`MetricChildTags`) and a nested struct (`MetricTagsStruct`). The tag properties demonstrate both default and customized tag names:
131
63
@@ -139,55 +71,7 @@ All three metric definitions `CreateLatency`, `CreateTotalCount`, and `CreateTot
139
71
140
72
The following code shows how to create and use these metrics in a class:
141
73
142
-
```csharp
143
-
internalclassMyClass
144
-
{
145
-
privatereadonlyLatency_latencyMetric;
146
-
privatereadonlyTotalCount_totalCountMetric;
147
-
privatereadonlyTotalFailures_totalFailuresMetric;
148
-
149
-
publicMyClass(Metermeter)
150
-
{
151
-
// Create metric instances using the source-generated factory methods
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs id ="snippet_strongMetricCreation":::
191
75
192
76
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.
0 commit comments