diff --git a/docs/core/diagnostics/metrics-collection.md b/docs/core/diagnostics/metrics-collection.md index f7f338b66c8aa..027bd00623b3a 100644 --- a/docs/core/diagnostics/metrics-collection.md +++ b/docs/core/diagnostics/metrics-collection.md @@ -2,7 +2,7 @@ title: Collect metrics - .NET description: Tutorial to collect metrics in .NET applications ms.topic: tutorial -ms.date: 10/27/2021 +ms.date: 08/27/2026 --- # Collect metrics @@ -21,6 +21,38 @@ For more information on custom metric instrumentation and options, see [Compare - [.NET 6.0 SDK](https://dotnet.microsoft.com/download/dotnet) or a later +## Configure metrics with IConfiguration + +Starting with .NET 8, Generic Host applications that use the default builder settings automatically call and load the `Metrics` section from . In a non-hosted dependency injection application, call `AddMetrics` and explicitly to bind the same section: + +```csharp +services.AddMetrics(metrics => + metrics.AddConfiguration(configuration.GetSection("Metrics"))); +``` + +Import the `Microsoft.Extensions.DependencyInjection` and `Microsoft.Extensions.Diagnostics.Metrics` namespaces to use these extension methods. + +> [!IMPORTANT] +> The `Metrics` configuration only selects which instruments registered 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. + +Use the following sections to choose which meters the rules apply to: + +| Section | Applies to | +| --- | --- | +| `EnabledMetrics` | Both global and local meters. | +| `EnabledGlobalMetrics` | Meters created directly with . | +| `EnabledLocalMetrics` | Meters created by the service container's . | + +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 value, such as `Metrics:MyListener:EnabledMetrics`. Listener and instrument names require exact matches without regard to case. + +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. + +The following `appsettings.json` example enables the `Contoso.Web` meter by default but disables its noisy `request-duration` instrument: + +:::code language="json" source="snippets/Metrics/appsettings.json"::: + +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. + ## Create an example app 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 API, see [the instrumentation tutorial](metrics-instrumentation.md). diff --git a/docs/core/diagnostics/snippets/Metrics/appsettings.json b/docs/core/diagnostics/snippets/Metrics/appsettings.json new file mode 100644 index 0000000000000..2ff4d0bed4d2c --- /dev/null +++ b/docs/core/diagnostics/snippets/Metrics/appsettings.json @@ -0,0 +1,10 @@ +{ + "Metrics": { + "EnabledMetrics": { + "Contoso.Web": { + "Default": true, + "request-duration": false + } + } + } +}