ObservableInstrument<T>
ObservableInstrument{T} is the base class from which all metrics observable instruments will inherit from.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Diagnostics.Metrics
{
public abstract class ObservableInstrument<T> : Instrument where T : struct
{
public override bool IsObservable => true;
[System.Runtime.CompilerServices.NullableContext(1)]
protected ObservableInstrument(Meter meter, string name, [System.Runtime.CompilerServices.Nullable(2)] string unit, [System.Runtime.CompilerServices.Nullable(2)] string description)
: base(meter, name, unit, description)
{
Instrument.ValidateTypeParameter<T>();
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})]
protected abstract IEnumerable<Measurement<T>> Observe();
internal override void Observe(MeterListener listener)
{
object subscriptionState = GetSubscriptionState(listener);
IEnumerable<Measurement<T>> enumerable = Observe();
if (enumerable != null) {
foreach (Measurement<T> item in enumerable) {
listener.NotifyMeasurement<T>((Instrument)this, item.Value, item.Tags, subscriptionState);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static IEnumerable<Measurement<T>> Observe(object callback)
{
Func<T> func = callback as Func<T>;
if (func != null)
return new Measurement<T>[1] {
new Measurement<T>(func())
};
Func<Measurement<T>> func2 = callback as Func<Measurement<T>>;
if (func2 != null)
return new Measurement<T>[1] {
func2()
};
return (callback as Func<IEnumerable<Measurement<T>>>)?.Invoke();
}
}
}