-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
MetricsBuilderExtensions.Rules.cs
118 lines (107 loc) · 8.2 KB
/
MetricsBuilderExtensions.Rules.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.Extensions.Diagnostics.Metrics
{
/// <summary>
/// Extension methods for <see cref="IMetricsBuilder"/> to add or clear <see cref="IMetricsListener"/> registrations, and to enable or disable metrics.
/// </summary>
public static partial class MetricsBuilderExtensions
{
/// <summary>
/// Enables all <see cref="Instrument"/>'s for the given meter, for all registered <see cref="IMetricsListener"/>'s.
/// </summary>
/// <param name="builder">The <see cref="IMetricsBuilder"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <returns>The original <see cref="IMetricsBuilder"/> for chaining.</returns>
public static IMetricsBuilder EnableMetrics(this IMetricsBuilder builder, string? meterName)
=> builder.ConfigureRule(options => options.EnableMetrics(meterName));
/// <summary>
/// Enables a specified <see cref="Instrument"/> for the given <see cref="Meter"/> and <see cref="IMetricsListener"/>.
/// </summary>
/// <param name="builder">The <see cref="IMetricsBuilder"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <param name="instrumentName">The <see cref="Instrument.Name"/>. A null value matches all instruments.</param>
/// <param name="listenerName">The <see cref="IMetricsListener"/>.Name. A null value matches all listeners.</param>
/// <param name="scopes">Indicates which <see cref="MeterScope"/>'s to consider. Default to all scopes.</param>
/// <returns>The original <see cref="IMetricsBuilder"/> for chaining.</returns>
public static IMetricsBuilder EnableMetrics(this IMetricsBuilder builder, string? meterName, string? instrumentName = null, string? listenerName = null,
MeterScope scopes = MeterScope.Global | MeterScope.Local)
=> builder.ConfigureRule(options => options.EnableMetrics(meterName, instrumentName, listenerName, scopes));
/// <summary>
/// Enables all <see cref="Instrument"/>'s for the given meter, for all registered <see cref="IMetricsListener"/>'s.
/// </summary>
/// <param name="options">The <see cref="MetricsOptions"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <returns>The original <see cref="MetricsOptions"/> for chaining.</returns>
public static MetricsOptions EnableMetrics(this MetricsOptions options, string? meterName)
=> options.EnableMetrics(meterName: meterName, instrumentName: null);
/// <summary>
/// Enables a specified <see cref="Instrument"/> for the given <see cref="Meter"/> and <see cref="IMetricsListener"/>.
/// </summary>
/// <param name="options">The <see cref="MeterOptions"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <param name="instrumentName">The <see cref="Instrument.Name"/>. A null value matches all instruments.</param>
/// <param name="listenerName">The <see cref="IMetricsListener"/>.Name. A null value matches all listeners.</param>
/// <param name="scopes">Indicates which <see cref="MeterScope"/>'s to consider. Default to all scopes.</param>
/// <returns>The original <see cref="MeterOptions"/> for chaining.</returns>
public static MetricsOptions EnableMetrics(this MetricsOptions options, string? meterName, string? instrumentName = null, string? listenerName = null,
MeterScope scopes = MeterScope.Global | MeterScope.Local)
=> options.AddRule(meterName, instrumentName, listenerName, scopes, enable: true);
/// <summary>
/// Disables all <see cref="Instrument"/>'s for the given meter, for all registered <see cref="IMetricsListener"/>'s.
/// </summary>
/// <param name="builder">The <see cref="IMetricsBuilder"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <returns>The original <see cref="IMetricsBuilder"/> for chaining.</returns>
public static IMetricsBuilder DisableMetrics(this IMetricsBuilder builder, string? meterName)
=> builder.ConfigureRule(options => options.DisableMetrics(meterName));
/// <summary>
/// Disables a specified <see cref="Instrument"/> for the given <see cref="Meter"/> and <see cref="IMetricsListener"/>.
/// </summary>
/// <param name="builder">The <see cref="IMetricsBuilder"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <param name="instrumentName">The <see cref="Instrument.Name"/>. A null value matches all instruments.</param>
/// <param name="listenerName">The <see cref="IMetricsListener"/>.Name. A null value matches all listeners.</param>
/// <param name="scopes">Indicates which <see cref="MeterScope"/>'s to consider. Default to all scopes.</param>
/// <returns>The original <see cref="IMetricsBuilder"/> for chaining.</returns>
public static IMetricsBuilder DisableMetrics(this IMetricsBuilder builder, string? meterName, string? instrumentName = null, string? listenerName = null,
MeterScope scopes = MeterScope.Global | MeterScope.Local)
=> builder.ConfigureRule(options => options.DisableMetrics(meterName, instrumentName, listenerName, scopes));
/// <summary>
/// Disables all <see cref="Instrument"/>'s for the given meter, for all registered <see cref="IMetricsListener"/>'s.
/// </summary>
/// <param name="options">The <see cref="MetricsOptions"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <returns>The original <see cref="MetricsOptions"/> for chaining.</returns>
public static MetricsOptions DisableMetrics(this MetricsOptions options, string? meterName)
=> options.DisableMetrics(meterName: meterName, instrumentName: null);
/// <summary>
/// Disables a specified <see cref="Instrument"/> for the given <see cref="Meter"/> and <see cref="IMetricsListener"/>.
/// </summary>
/// <param name="options">The <see cref="MeterOptions"/>.</param>
/// <param name="meterName">The <see cref="Meter.Name"/> or prefix. A null value matches all meters.</param>
/// <param name="instrumentName">The <see cref="Instrument.Name"/>. A null value matches all instruments.</param>
/// <param name="listenerName">The <see cref="IMetricsListener"/>.Name. A null value matches all listeners.</param>
/// <param name="scopes">Indicates which <see cref="MeterScope"/>'s to consider. Default to all scopes.</param>
/// <returns>The original <see cref="MeterOptions"/> for chaining.</returns>
public static MetricsOptions DisableMetrics(this MetricsOptions options, string? meterName, string? instrumentName = null, string? listenerName = null,
MeterScope scopes = MeterScope.Global | MeterScope.Local)
=> options.AddRule(meterName, instrumentName, listenerName, scopes, enable: false);
private static IMetricsBuilder ConfigureRule(this IMetricsBuilder builder, Action<MetricsOptions> configureOptions)
{
ThrowHelper.ThrowIfNull(builder);
builder.Services.Configure(configureOptions);
return builder;
}
private static MetricsOptions AddRule(this MetricsOptions options, string? meterName, string? instrumentName, string? listenerName,
MeterScope scopes, bool enable)
{
ThrowHelper.ThrowIfNull(options);
options.Rules.Add(new InstrumentRule(meterName, instrumentName, listenerName, scopes, enable));
return options;
}
}
}