Skip to content

Commit fbe0a3d

Browse files
authored
Apply suggestions from code review
1 parent a898474 commit fbe0a3d

File tree

7 files changed

+34
-30
lines changed

7 files changed

+34
-30
lines changed

docs/core/diagnostics/metrics-generator.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ The following example shows a class that declares two metrics. The methods are m
4343
The code generator runs at build time and provides an implementation of these methods, along with accompanying
4444
types.
4545

46-
:::code language="csharp" source="snippets/MetricsGen/MetricConstants.cs" id="snippet_metricConstants":::
46+
:::code language="csharp" source="snippets/MetricsGen/MetricConstants.cs" id="constants":::
4747

48-
:::code language="csharp" source="snippets/MetricsGen/Metrics.cs" id="snippet_Metrics" :::
48+
The following code demonstrates how to use the generator with primitive types:
49+
50+
:::code language="csharp" source="snippets/MetricsGen/Metrics.cs" id="metrics" :::
4951

5052
The previous declaration automatically returns the following:
5153

@@ -74,7 +76,7 @@ internal class Latency
7476

7577
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 instances created, you can call `Add` and `Record` to register metric values, as shown in the following example:
7678

77-
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs" id ="snippet_metricCreation":::
79+
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs" id ="creation":::
7880

7981
## Metric methods requirements
8082

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,25 @@ By default, the source generator derives metric tag names from the field and pro
4242

4343
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:
4444

45-
:::code language="csharp" source="snippets/MetricsGen/MyMetrics.cs" id= "snippet_SimpleMetricTag":::
45+
:::code language="csharp" source="snippets/MetricsGen/MyMetrics.cs" id= "tag":::
4646

47-
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.
47+
In the preceding code, `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 uses the tags defined in `RequestTags` when recording metrics. The source generator produces a strongly-typed instrument class (named `RequestCount`) with an `Add` method that accepts integer value and `RequestTags` object.
4848

4949
To use the generated metric, create a <xref:System.Diagnostics.Metrics.Meter> and record measurements as shown below:
5050

51-
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs" id ="snippet_SimpleMetricTagUsage":::
51+
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs" id ="tag":::
5252

5353
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.
5454

5555
## Example 2: Metric with nested tag objects
5656

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:
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 effectively share a common set of tags. In the next example, you define a set of tag classes and use them for three different metrics:
5858

5959
:::code language="csharp" source="snippets/MetricsGen/MetricTags.cs" :::
60-
:::code language="csharp" source="snippets/MetricsGen/Metrics.cs" id="snippet_MetricTags" :::
60+
61+
The preceding code defines the metric inheritance and object shapes. The following code demonstrates how to use these shapes with the generator, as shown in the `Metric` class:
62+
63+
:::code language="csharp" source="snippets/MetricsGen/Metrics.cs" id="tags" :::
6164

6265
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:
6366

@@ -71,7 +74,7 @@ All three metric definitions `CreateLatency`, `CreateTotalCount`, and `CreateTot
7174

7275
The following code shows how to create and use these metrics in a class:
7376

74-
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs" id ="snippet_strongMetricCreation":::
77+
:::code language="csharp" source="snippets/MetricsGen/MyClass.cs" id ="creation":::
7578

7679
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.
7780

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace MetricsGen;
22

3-
//<snippet_metricConstants>
3+
// <constants>
44
internal class MetricConstants
55
{
66
public const string EnvironmentName = "env";
77
public const string Region = "region";
88
public const string RequestName = "requestName";
99
public const string RequestStatus = "requestStatus";
1010
}
11-
// </snippet_metricConstants>
11+
// </constants>
1212

docs/core/diagnostics/snippets/MetricsGen/MetricTags.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Diagnostics.Metrics;
22

33
namespace MetricsGen;
4+
45
public class MetricTags : MetricParentTags
56
{
67
[TagName("Dim1DimensionName")]

docs/core/diagnostics/snippets/MetricsGen/Metrics.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define FIRST // FIRST SECOND
22

33
#if FIRST
4-
//<snippet_Metrics>
4+
// <metrics>
55
using System.Diagnostics.Metrics;
66
using Microsoft.Extensions.Diagnostics.Metrics;
77

@@ -20,11 +20,11 @@ internal static partial class Metric
2020
[Counter<int>]
2121
public static partial TotalFailures CreateTotalFailures(this Meter meter);
2222
}
23-
//</snippet_Metrics>
23+
// </metrics>
2424
#elif SECOND
2525

2626
using MetricsGen;
27-
//<snippet_MetricTags>
27+
// <tags>
2828
using System.Diagnostics.Metrics;
2929
using Microsoft.Extensions.Diagnostics.Metrics;
3030

@@ -39,5 +39,5 @@ public static partial class Metric
3939
[Counter<int>(typeof(MetricTags))]
4040
public static partial TotalFailures CreateTotalFailures(Meter meter);
4141
}
42-
//</snippet_MetricTags>
42+
// </tags>
4343
#endif

docs/core/diagnostics/snippets/MetricsGen/MyClass.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MetricsGen;
66

7-
// <snippet_metricCreation>
7+
// <creation>
88
internal class MyClass
99
{
1010
private string envName = "envValue";
@@ -50,16 +50,15 @@ public void ReportSampleFailuresCount()
5050
_totalFailuresMetric.Add(1);
5151
}
5252
}
53-
//</snippet_metricCreation>
53+
// </creation>
5454

5555
#elif SECOND
5656

5757
using System.Diagnostics;
5858
using System.Diagnostics.Metrics;
5959
using MetricsGen;
6060

61-
// <snippet_strongMetricCreation>
62-
61+
// <creation>
6362
internal class MyClass
6463
{
6564
private readonly Latency _latencyMetric;
@@ -76,11 +75,10 @@ public MyClass(Meter meter)
7675

7776
public void DoWork()
7877
{
79-
var stopwatch = new Stopwatch();
80-
stopwatch.Start();
78+
var startingTimestamp = Stopwatch.GetTimestamp();
8179
bool requestSuccessful = true;
82-
// ... perform some operation ...
83-
stopwatch.Stop();
80+
// Perform some operation to measure
81+
var elapsedTime = Stopwatch.GetElapsedTime(startingTimestamp);
8482

8583
// Create a tag object with values for all tags
8684
var tags = new MetricTags
@@ -99,22 +97,22 @@ public void DoWork()
9997
};
10098

10199
// Record the metric values with the associated tags
102-
_latencyMetric.Record(stopwatch.ElapsedMilliseconds, tags);
100+
_latencyMetric.Record(elapsedTime.ElapsedMilliseconds, tags);
103101
_totalCountMetric.Add(1, tags);
104102
if (!requestSuccessful)
105103
{
106104
_totalFailuresMetric.Add(1, tags);
107105
}
108106
}
109107
}
110-
// </snippet_strongMetricCreation>
108+
// </creation>
111109
#elif THIRD
112110

113111
using MetricsGen;
114112
using System.Diagnostics.Metrics;
115113

116-
//<snippet_SimpleMetricTagUsage>
117-
Meter meter = new Meter("MyCompany.MyApp", "1.0");
114+
// <tag>
115+
Meter meter = new("MyCompany.MyApp", "1.0");
118116
RequestCount requestCountMetric = MyMetrics.CreateRequestCount(meter);
119117

120118
// Create a tag object with the relevant tag value
@@ -123,5 +121,5 @@ public void DoWork()
123121
// Record a metric value with the associated tag
124122
requestCountMetric.Add(1, tags);
125123

126-
//</snippet_SimpleMetricTagUsage>
124+
// </tag>
127125
#endif

docs/core/diagnostics/snippets/MetricsGen/MyMetrics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Microsoft.Extensions.Diagnostics.Metrics;
33

44
namespace MetricsGen;
5-
//<snippet_SimpleMetricTag>
5+
// <tag>
66
public struct RequestTags
77
{
88
public string Region { get; set; }
@@ -13,4 +13,4 @@ public static partial class MyMetrics
1313
[Counter<int>(typeof(RequestTags))]
1414
public static partial RequestCount CreateRequestCount(Meter meter);
1515
}
16-
//</snippet_SimpleMetricTag>
16+
// </tag>

0 commit comments

Comments
 (0)