forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeterProviderBuilderSdk.cs
More file actions
235 lines (181 loc) · 8.44 KB
/
MeterProviderBuilderSdk.cs
File metadata and controls
235 lines (181 loc) · 8.44 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// <copyright file="MeterProviderBuilderSdk.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
#nullable enable
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;
namespace OpenTelemetry.Metrics
{
/// <summary>
/// Stores state used to build a <see cref="MeterProvider"/>.
/// </summary>
internal sealed class MeterProviderBuilderSdk : MeterProviderBuilder, IMeterProviderBuilder
{
public const int MaxMetricsDefault = 1000;
public const int MaxMetricPointsPerMetricDefault = 2000;
private const string DefaultInstrumentationVersion = "1.0.0.0";
private static readonly Regex InstrumentNameRegex = new(
@"^[a-z][a-z0-9-._]{0,62}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private readonly IServiceProvider serviceProvider;
private MeterProviderSdk? meterProvider;
public MeterProviderBuilderSdk(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public List<InstrumentationRegistration> Instrumentation { get; } = new();
public ResourceBuilder? ResourceBuilder { get; private set; }
public MeterProvider? Provider => this.meterProvider;
public List<MetricReader> Readers { get; } = new();
public List<string> MeterSources { get; } = new();
public List<Func<Instrument, MetricStreamConfiguration?>> ViewConfigs { get; } = new();
public int MaxMetricStreams { get; private set; } = MaxMetricsDefault;
public int MaxMetricPointsPerMetricStream { get; private set; } = MaxMetricPointsPerMetricDefault;
/// <summary>
/// Returns whether the given instrument name is valid according to the specification.
/// </summary>
/// <remarks>See specification: <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument"/>.</remarks>
/// <param name="instrumentName">The instrument name.</param>
/// <returns>Boolean indicating if the instrument is valid.</returns>
public static bool IsValidInstrumentName(string instrumentName)
{
if (string.IsNullOrWhiteSpace(instrumentName))
{
return false;
}
return InstrumentNameRegex.IsMatch(instrumentName);
}
/// <summary>
/// Returns whether the given custom view name is valid according to the specification.
/// </summary>
/// <remarks>See specification: <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument"/>.</remarks>
/// <param name="customViewName">The view name.</param>
/// <returns>Boolean indicating if the instrument is valid.</returns>
public static bool IsValidViewName(string customViewName)
{
// Only validate the view name in case it's not null. In case it's null, the view name will be the instrument name as per the spec.
if (customViewName == null)
{
return true;
}
return InstrumentNameRegex.IsMatch(customViewName);
}
public void RegisterProvider(MeterProviderSdk meterProvider)
{
Debug.Assert(meterProvider != null, "meterProvider was null");
if (this.meterProvider != null)
{
throw new NotSupportedException("MeterProvider cannot be accessed while build is executing.");
}
this.meterProvider = meterProvider;
}
public override MeterProviderBuilder AddInstrumentation<TInstrumentation>(
Func<TInstrumentation> instrumentationFactory)
{
Debug.Assert(instrumentationFactory != null, "instrumentationFactory was null");
return this.AddInstrumentation(
typeof(TInstrumentation).Name,
typeof(TInstrumentation).Assembly.GetName().Version?.ToString() ?? DefaultInstrumentationVersion,
instrumentationFactory!());
}
public MeterProviderBuilder AddInstrumentation(
string instrumentationName,
string instrumentationVersion,
object instrumentation)
{
Debug.Assert(!string.IsNullOrWhiteSpace(instrumentationName), "instrumentationName was null or whitespace");
Debug.Assert(!string.IsNullOrWhiteSpace(instrumentationVersion), "instrumentationVersion was null or whitespace");
Debug.Assert(instrumentation != null, "instrumentation was null");
this.Instrumentation.Add(
new InstrumentationRegistration(
instrumentationName,
instrumentationVersion,
instrumentation!));
return this;
}
public MeterProviderBuilder ConfigureResource(Action<ResourceBuilder> configure)
{
Debug.Assert(configure != null, "configure was null");
var resourceBuilder = this.ResourceBuilder ??= ResourceBuilder.CreateDefault();
configure!(resourceBuilder);
return this;
}
public MeterProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder)
{
Debug.Assert(resourceBuilder != null, "resourceBuilder was null");
this.ResourceBuilder = resourceBuilder;
return this;
}
public override MeterProviderBuilder AddMeter(params string[] names)
{
Debug.Assert(names != null, "names was null");
foreach (var name in names!)
{
Guard.ThrowIfNullOrWhitespace(name);
this.MeterSources.Add(name);
}
return this;
}
public MeterProviderBuilder AddReader(MetricReader reader)
{
Debug.Assert(reader != null, "reader was null");
this.Readers.Add(reader!);
return this;
}
public MeterProviderBuilder AddView(Func<Instrument, MetricStreamConfiguration?> viewConfig)
{
Debug.Assert(viewConfig != null, "viewConfig was null");
this.ViewConfigs.Add(viewConfig!);
return this;
}
public MeterProviderBuilder SetMaxMetricStreams(int maxMetricStreams)
{
this.MaxMetricStreams = maxMetricStreams;
return this;
}
public MeterProviderBuilder SetMaxMetricPointsPerMetricStream(int maxMetricPointsPerMetricStream)
{
this.MaxMetricPointsPerMetricStream = maxMetricPointsPerMetricStream;
return this;
}
public MeterProviderBuilder ConfigureBuilder(Action<IServiceProvider, MeterProviderBuilder> configure)
{
Debug.Assert(configure != null, "configure was null");
configure!(this.serviceProvider, this);
return this;
}
public MeterProviderBuilder ConfigureServices(Action<IServiceCollection> configure)
{
throw new NotSupportedException("Services cannot be configured after ServiceProvider has been created.");
}
MeterProviderBuilder IDeferredMeterProviderBuilder.Configure(Action<IServiceProvider, MeterProviderBuilder> configure)
=> this.ConfigureBuilder(configure);
internal readonly struct InstrumentationRegistration
{
public readonly string Name;
public readonly string Version;
public readonly object Instance;
internal InstrumentationRegistration(string name, string version, object instance)
{
this.Name = name;
this.Version = version;
this.Instance = instance;
}
}
}
}