Skip to content

Commit d7bf628

Browse files
committed
Add InstrumentAdvice details to instrumentation doc.
1 parent 900fcf6 commit d7bf628

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

docs/core/diagnostics/metrics-instrumentation.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Creating Metrics
33
description: How to add new metrics to a .NET library or application
44
ms.topic: tutorial
5-
ms.date: 08/27/2024
5+
ms.date: 11/19/2024
66
---
77

88
# Creating metrics
@@ -224,6 +224,11 @@ Types of instruments currently available:
224224
<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
225225
might report that the 50th, 90th, and 95th percentiles of these measurements are 5, 9, and 9 respectively.
226226

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+
227232
### Best practices when selecting an instrument type
228233

229234
- 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
542547
> [!NOTE]
543548
> OpenTelemetry refers to tags as 'attributes'. These are two different names for the same functionality.
544549
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+
545610
## Test custom metrics
546611

547612
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

Comments
 (0)