Meter
Meter is the class responsible for creating and tracking the Instruments.
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace System.Diagnostics.Metrics
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
[DebuggerDisplay("Name = {Name}, Instruments = {_instruments.Count}")]
public class Meter : IDisposable
{
private static readonly List<Meter> s_allMeters = new List<Meter>();
private List<Instrument> _instruments = new List<Instrument>();
private Dictionary<string, List<Instrument>> _nonObservableInstrumentsCache = new Dictionary<string, List<Instrument>>();
internal bool Disposed { get; set; }
internal static bool IsSupported { get; } = InitializeIsSupported();
[System.Runtime.CompilerServices.Nullable(1)]
public string Name {
[System.Runtime.CompilerServices.NullableContext(1)]
get;
private set;
}
public string Version { get; set; }
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})]
public IEnumerable<KeyValuePair<string, object>> Tags {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})]
get;
private set;
}
public object Scope { get; set; }
private static bool InitializeIsSupported()
{
if (!AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isEnabled))
return true;
return isEnabled;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public Meter(MeterOptions options)
{
if (options == null)
throw new ArgumentNullException("options");
Initialize(options.Name, options.Version, options.Tags, options.Scope);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public Meter(string name)
: this(name, null, null, null)
{
}
[System.Runtime.CompilerServices.NullableContext(1)]
public Meter(string name, [System.Runtime.CompilerServices.Nullable(2)] string version)
: this(name, version, null, null)
{
}
public Meter([System.Runtime.CompilerServices.Nullable(1)] string name, string version, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags, object scope = null)
{
Initialize(name, version, tags, scope);
}
private void Initialize(string name, string version, IEnumerable<KeyValuePair<string, object>> tags, object scope = null)
{
if (name == null)
throw new ArgumentNullException("name");
Name = name;
Version = version;
if (tags != null) {
List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(tags);
list.Sort((KeyValuePair<string, object> left, KeyValuePair<string, object> right) => string.Compare(left.Key, right.Key, StringComparison.Ordinal));
Tags = list.AsReadOnly();
}
Scope = scope;
if (IsSupported) {
lock (Instrument.SyncObject) {
s_allMeters.Add(this);
}
GC.KeepAlive(MetricsEventSource.Log);
}
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Counter<T> CreateCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit = null, string description = null) where T : struct
{
return CreateCounter<T>(name, unit, description, null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Counter<T> CreateCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return (Counter<T>)GetOrCreateInstrument<T>(typeof(Counter<T>), name, unit, description, tags, () => new Counter<T>(this, name, unit, description, tags));
}
[System.Runtime.CompilerServices.NullableContext(0)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Gauge<T> CreateGauge<T>([System.Runtime.CompilerServices.Nullable(1)] string name) where T : struct
{
return CreateGauge<T>(name, null, null, null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Gauge<T> CreateGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit = null, string description = null, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags = null) where T : struct
{
return (Gauge<T>)GetOrCreateInstrument<T>(typeof(Gauge<T>), name, unit, description, tags, () => new Gauge<T>(this, name, unit, description, tags));
}
[System.Runtime.CompilerServices.NullableContext(0)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Histogram<T> CreateHistogram<T>([System.Runtime.CompilerServices.Nullable(1)] string name) where T : struct
{
return CreateHistogram<T>(name, null, null, null, null);
}
[EditorBrowsable(EditorBrowsableState.Never)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Histogram<T> CreateHistogram<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit, string description) where T : struct
{
return CreateHistogram<T>(name, unit, description, null, null);
}
[EditorBrowsable(EditorBrowsableState.Never)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Histogram<T> CreateHistogram<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return CreateHistogram<T>(name, unit, description, tags, null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public Histogram<T> CreateHistogram<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit = null, string description = null, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags = null, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0
})] InstrumentAdvice<T> advice = null) where T : struct
{
return (Histogram<T>)GetOrCreateInstrument<T>(typeof(Histogram<T>), name, unit, description, tags, () => new Histogram<T>(this, name, unit, description, tags, advice));
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public UpDownCounter<T> CreateUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit = null, string description = null) where T : struct
{
return CreateUpDownCounter<T>(name, unit, description, null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public UpDownCounter<T> CreateUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return (UpDownCounter<T>)GetOrCreateInstrument<T>(typeof(UpDownCounter<T>), name, unit, description, tags, () => new UpDownCounter<T>(this, name, unit, description, tags));
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableUpDownCounter<T> CreateObservableUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Func<T> observeValue, string unit = null, string description = null) where T : struct
{
return new ObservableUpDownCounter<T>(this, name, observeValue, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableUpDownCounter<T> CreateObservableUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Func<T> observeValue, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableUpDownCounter<T>(this, name, observeValue, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableUpDownCounter<T> CreateObservableUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})] Func<Measurement<T>> observeValue, string unit = null, string description = null) where T : struct
{
return new ObservableUpDownCounter<T>(this, name, observeValue, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableUpDownCounter<T> CreateObservableUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})] Func<Measurement<T>> observeValue, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableUpDownCounter<T>(this, name, observeValue, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableUpDownCounter<T> CreateObservableUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
0,
0
})] Func<IEnumerable<Measurement<T>>> observeValues, string unit = null, string description = null) where T : struct
{
return new ObservableUpDownCounter<T>(this, name, observeValues, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableUpDownCounter<T> CreateObservableUpDownCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
0,
0
})] Func<IEnumerable<Measurement<T>>> observeValues, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableUpDownCounter<T>(this, name, observeValues, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableCounter<T> CreateObservableCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Func<T> observeValue, string unit = null, string description = null) where T : struct
{
return new ObservableCounter<T>(this, name, observeValue, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableCounter<T> CreateObservableCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Func<T> observeValue, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableCounter<T>(this, name, observeValue, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableCounter<T> CreateObservableCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})] Func<Measurement<T>> observeValue, string unit = null, string description = null) where T : struct
{
return new ObservableCounter<T>(this, name, observeValue, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableCounter<T> CreateObservableCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})] Func<Measurement<T>> observeValue, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableCounter<T>(this, name, observeValue, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableCounter<T> CreateObservableCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
0,
0
})] Func<IEnumerable<Measurement<T>>> observeValues, string unit = null, string description = null) where T : struct
{
return new ObservableCounter<T>(this, name, observeValues, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableCounter<T> CreateObservableCounter<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
0,
0
})] Func<IEnumerable<Measurement<T>>> observeValues, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableCounter<T>(this, name, observeValues, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableGauge<T> CreateObservableGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Func<T> observeValue, string unit = null, string description = null) where T : struct
{
return new ObservableGauge<T>(this, name, observeValue, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableGauge<T> CreateObservableGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Func<T> observeValue, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableGauge<T>(this, name, observeValue, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableGauge<T> CreateObservableGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})] Func<Measurement<T>> observeValue, string unit = null, string description = null) where T : struct
{
return new ObservableGauge<T>(this, name, observeValue, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableGauge<T> CreateObservableGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})] Func<Measurement<T>> observeValue, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableGauge<T>(this, name, observeValue, unit, description, tags);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableGauge<T> CreateObservableGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
0,
0
})] Func<IEnumerable<Measurement<T>>> observeValues, string unit = null, string description = null) where T : struct
{
return new ObservableGauge<T>(this, name, observeValues, unit, description, (IEnumerable<KeyValuePair<string, object>>)null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public ObservableGauge<T> CreateObservableGauge<[System.Runtime.CompilerServices.Nullable(0)] T>([System.Runtime.CompilerServices.Nullable(1)] string name, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
0,
0
})] Func<IEnumerable<Measurement<T>>> observeValues, string unit, string description, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags) where T : struct
{
return new ObservableGauge<T>(this, name, observeValues, unit, description, tags);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing) {
List<Instrument> list = null;
lock (Instrument.SyncObject) {
if (Disposed)
return;
Disposed = true;
s_allMeters.Remove(this);
list = _instruments;
_instruments = new List<Instrument>();
}
lock (_nonObservableInstrumentsCache) {
_nonObservableInstrumentsCache.Clear();
}
if (list != null) {
foreach (Instrument item in list) {
item.NotifyForUnpublishedInstrument();
}
}
}
}
private static Instrument GetCachedInstrument(List<Instrument> instrumentList, Type instrumentType, string unit, string description, IEnumerable<KeyValuePair<string, object>> tags)
{
foreach (Instrument instrument in instrumentList) {
if (instrument.GetType() == instrumentType && instrument.Unit == unit && instrument.Description == description && DiagnosticsHelper.CompareTags(instrument.Tags as List<KeyValuePair<string, object>>, tags))
return instrument;
}
return null;
}
private Instrument GetOrCreateInstrument<T>(Type instrumentType, string name, string unit, string description, IEnumerable<KeyValuePair<string, object>> tags, Func<Instrument> instrumentCreator)
{
List<Instrument> value;
lock (_nonObservableInstrumentsCache) {
if (!_nonObservableInstrumentsCache.TryGetValue(name, out value)) {
value = new List<Instrument>();
_nonObservableInstrumentsCache.Add(name, value);
}
}
lock (value) {
Instrument cachedInstrument = GetCachedInstrument(value, instrumentType, unit, description, tags);
if (cachedInstrument != null)
return cachedInstrument;
}
Instrument instrument = instrumentCreator();
lock (value) {
Instrument cachedInstrument2 = GetCachedInstrument(value, instrumentType, unit, description, tags);
if (cachedInstrument2 == null) {
value.Add(instrument);
return instrument;
}
return cachedInstrument2;
}
}
internal bool AddInstrument(Instrument instrument)
{
if (!_instruments.Contains(instrument)) {
_instruments.Add(instrument);
return true;
}
return false;
}
internal static List<Instrument> GetPublishedInstruments()
{
List<Instrument> result = null;
if (s_allMeters.Count > 0) {
result = new List<Instrument>();
{
foreach (Meter s_allMeter in s_allMeters) {
foreach (Instrument instrument in s_allMeter._instruments) {
result.Add(instrument);
}
}
return result;
}
}
return result;
}
}
}