Skip to content

Commit bbc6f6c

Browse files
author
rosebyte
committed
Add metrics configuration docs
1 parent 8160867 commit bbc6f6c

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

docs/core/diagnostics/metrics-collection.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Collect metrics - .NET
33
description: Tutorial to collect metrics in .NET applications
44
ms.topic: tutorial
5-
ms.date: 10/27/2021
5+
ms.date: 08/27/2026
66
---
77

88
# Collect metrics
@@ -21,6 +21,38 @@ For more information on custom metric instrumentation and options, see [Compare
2121

2222
- [.NET 6.0 SDK](https://dotnet.microsoft.com/download/dotnet) or a later
2323

24+
## Configure metrics with IConfiguration
25+
26+
Starting with .NET 8, Generic Host applications that use the default builder settings automatically call <xref:Microsoft.Extensions.DependencyInjection.MetricsServiceExtensions.AddMetrics*> and load the `Metrics` section from <xref:Microsoft.Extensions.Configuration.IConfiguration>. In a non-hosted dependency injection application, call `AddMetrics` and <xref:Microsoft.Extensions.Diagnostics.Metrics.MetricsBuilderConfigurationExtensions.AddConfiguration(Microsoft.Extensions.Diagnostics.Metrics.IMetricsBuilder,Microsoft.Extensions.Configuration.IConfiguration)> explicitly to bind the same section:
27+
28+
```csharp
29+
services.AddMetrics(metrics =>
30+
metrics.AddConfiguration(configuration.GetSection("Metrics")));
31+
```
32+
33+
Import the `Microsoft.Extensions.DependencyInjection` and `Microsoft.Extensions.Diagnostics.Metrics` namespaces to use these extension methods.
34+
35+
> [!IMPORTANT]
36+
> The `Metrics` configuration only selects which instruments registered <xref:Microsoft.Extensions.Diagnostics.Metrics.IMetricsListener> implementations receive. It doesn't register listeners or activate their subscriptions, create metrics, aggregate measurements, export data, or automatically configure OpenTelemetry, Prometheus, `dotnet-counters`, or any other collector.
37+
38+
Use the following sections to choose which meters the rules apply to:
39+
40+
| Section | Applies to |
41+
| --- | --- |
42+
| `EnabledMetrics` | Both global and local meters. |
43+
| `EnabledGlobalMetrics` | Meters created directly with <xref:System.Diagnostics.Metrics.Meter>. |
44+
| `EnabledLocalMetrics` | Meters created by the service container's <xref:System.Diagnostics.Metrics.IMeterFactory>. |
45+
46+
An `EnabledMetrics`, `EnabledGlobalMetrics`, or `EnabledLocalMetrics` section directly under `Metrics` applies to every registered listener. To target one listener, nest the section under the listener's <xref:Microsoft.Extensions.Diagnostics.Metrics.IMetricsListener.Name> value, such as `Metrics:MyListener:EnabledMetrics`. Listener and instrument names require exact matches without regard to case.
47+
48+
Within any of these sections, a Boolean meter entry enables or disables every instrument whose meter name matches. To set instrument-level rules, use an object with instrument names as keys. The special `Default` entry supplies a fallback. A Boolean `Default` entry at the meter level applies to otherwise unmatched meters. Within a meter object, `Default` applies to otherwise unmatched instruments. A listener doesn't receive an instrument if no rule matches it.
49+
50+
The following `appsettings.json` example enables the `Contoso.Web` meter by default but disables its noisy `request-duration` instrument:
51+
52+
:::code language="json" source="snippets/Metrics/appsettings.json":::
53+
54+
Meter names use case-insensitive prefix matching. A meter name can contain at most one `*` wildcard to match a prefix and suffix. More-specific rules take precedence in this order: listener-specific rules, longer meter-name patterns, instrument-specific rules, and scope-specific rules. If equally specific rules conflict, the last configured rule wins.
55+
2456
## Create an example app
2557

2658
Before metrics can be collected, measurements must be produced. This tutorial creates an app that has basic metric instrumentation. The .NET runtime also has [various metrics built-in](built-in-metrics.md). For more information about creating new metrics using the <xref:System.Diagnostics.Metrics.Meter?displayProperty=nameWithType> API, see [the instrumentation tutorial](metrics-instrumentation.md).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Metrics": {
3+
"EnabledMetrics": {
4+
"Contoso.Web": {
5+
"Default": true,
6+
"request-duration": false
7+
}
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)