|
2 | 2 | title: Creating Metrics |
3 | 3 | description: How to add new metrics to a .NET library or application |
4 | 4 | ms.topic: tutorial |
5 | | -ms.date: 08/27/2024 |
| 5 | +ms.date: 11/19/2024 |
6 | 6 | --- |
7 | 7 |
|
8 | 8 | # Creating metrics |
@@ -224,6 +224,11 @@ Types of instruments currently available: |
224 | 224 | <xref:System.Diagnostics.Metrics.Histogram%601.Record%2A> to record these measurements during the collection tool's update interval: 1,5,2,3,10,9,7,4,6,8. A collection tool |
225 | 225 | might report that the 50th, 90th, and 95th percentiles of these measurements are 5, 9, and 9 respectively. |
226 | 226 |
|
| 227 | + > [!NOTE] |
| 228 | + > For details about how to set the recommended bucket boundaries when creating |
| 229 | + > a Histogram instrument see: [Using Advice to customize Histogram |
| 230 | + > instruments](#using-advice-to-customize-histogram-instruments). |
| 231 | +
|
227 | 232 | ### Best practices when selecting an instrument type |
228 | 233 |
|
229 | 234 | - For counting things, or any other value that solely increases over time, use Counter or ObservableCounter. Choose between Counter and ObservableCounter depending on which |
@@ -542,6 +547,66 @@ Name Current Value |
542 | 547 | > [!NOTE] |
543 | 548 | > OpenTelemetry refers to tags as 'attributes'. These are two different names for the same functionality. |
544 | 549 |
|
| 550 | +## Using Advice to customize Histogram instruments |
| 551 | + |
| 552 | +Consumers of Histogram instruments can choose different aggregation strategies. |
| 553 | +The default mode when using OpenTelemetry is [Explicit Bucket Histogram |
| 554 | +Aggregation](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#explicit-bucket-histogram-aggregation). |
| 555 | + |
| 556 | +When using explicit bucket aggregation the measurements recorded through a |
| 557 | +Histogram will be grouped into buckets which track the sum and count of values |
| 558 | +to that bucket. |
| 559 | + |
| 560 | +The default bucket configuration is described in the OpenTelemetry |
| 561 | +Specification: `[ 0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, |
| 562 | +7500, 10000 ]`. |
| 563 | + |
| 564 | +The default values may not lead to the best granularity for every Histogram. For |
| 565 | +example, sub-second request durations would all fall into the `0` bucket. |
| 566 | + |
| 567 | +To solve this problem the `9.0.0` version of the |
| 568 | +`System.Diagnostics.DiagnosticSource` package introduced the |
| 569 | +(<xref:System.Diagnostics.Metrics.InstrumentAdvice%2A>) API. |
| 570 | + |
| 571 | +The `InstrumentAdvice` API may be used by instrumentation authors to specify the |
| 572 | +set of recommended default bucket boundaries for a given Histogram. Consumers |
| 573 | +can then choose to use those values when configuring aggregation. |
| 574 | + |
| 575 | +The following code shows an example using the `InstrumentAdvice` API to set |
| 576 | +recommended default buckets. |
| 577 | + |
| 578 | +```csharp |
| 579 | +using System; |
| 580 | +using System.Diagnostics.Metrics; |
| 581 | +using System.Threading; |
| 582 | + |
| 583 | +class Program |
| 584 | +{ |
| 585 | + static Meter s_meter = new Meter("HatCo.Store"); |
| 586 | + static readonly IReadOnlyList<double> s_shortSecondsBucketBoundaries = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]; |
| 587 | + static Histogram<double> s_orderProcessingTime = s_meter.CreateHistogram<double>( |
| 588 | + name: "hatco.store.order_processing_time", |
| 589 | + unit: "s", |
| 590 | + description: "Duration order processing.", |
| 591 | + advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = s_shortSecondsBucketBoundaries }); |
| 592 | + |
| 593 | + static Random s_rand = new Random(); |
| 594 | + |
| 595 | + static void Main(string[] args) |
| 596 | + { |
| 597 | + Console.WriteLine("Press any key to exit"); |
| 598 | + while (!Console.KeyAvailable) |
| 599 | + { |
| 600 | + // Pretend our store has one transaction each 100ms |
| 601 | + Thread.Sleep(100); |
| 602 | + |
| 603 | + // Pretend that we measured how long it took to do the transaction (for example we could time it with Stopwatch) |
| 604 | + s_orderProcessingTime.Record(s_rand.Next(5, 15) / 1000.0); |
| 605 | + } |
| 606 | + } |
| 607 | +} |
| 608 | +``` |
| 609 | + |
545 | 610 | ## Test custom metrics |
546 | 611 |
|
547 | 612 | Its possible to test any custom metrics you add using <xref:Microsoft.Extensions.Diagnostics.Metrics.Testing.MetricCollector%601>. This type makes it easy to record the measurements from specific instruments and assert the values were correct. |
|
0 commit comments