Measurement<T>
Stores one observed metrics value and its associated tags. This type is used by an Observable instrument's Observe() method when reporting current measurements.
using System.Buffers;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Diagnostics.Metrics
{
public readonly struct Measurement<T> where T : struct
{
private readonly KeyValuePair<string, object>[] ;
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1,
2
})]
public ReadOnlySpan<KeyValuePair<string, object>> Tags {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1,
2
})]
get {
return MemoryExtensions.AsSpan<KeyValuePair<string, object>>(_tags);
}
}
public T Value { get; }
public Measurement(T value)
{
_tags = Instrument.EmptyTags;
Value = value;
}
public Measurement(T value, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] IEnumerable<KeyValuePair<string, object>> tags)
{
_tags = ToArray(tags);
Value = value;
}
public Measurement(T value, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] params KeyValuePair<string, object>[] tags)
{
if (tags != null) {
_tags = new KeyValuePair<string, object>[tags.Length];
tags.CopyTo(_tags, 0);
} else
_tags = Instrument.EmptyTags;
Value = value;
}
public Measurement(T value, [System.Runtime.CompilerServices.ParamCollection] [System.Runtime.CompilerServices.ScopedRef] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1,
2
})] ReadOnlySpan<KeyValuePair<string, object>> tags)
{
_tags = tags.ToArray();
Value = value;
}
public Measurement(T value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref TagList tags)
{
if (tags.Count > 0) {
_tags = new KeyValuePair<string, object>[tags.Count];
tags.CopyTo(MemoryExtensions.AsSpan<KeyValuePair<string, object>>(_tags));
} else
_tags = Instrument.EmptyTags;
Value = value;
}
private static KeyValuePair<string, object>[] ToArray(IEnumerable<KeyValuePair<string, object>> tags)
{
if (tags == null)
return Instrument.EmptyTags;
ICollection<KeyValuePair<string, object>> collection = tags as ICollection<KeyValuePair<string, object>>;
KeyValuePair<string, object>[] array;
if (collection != null) {
int count = collection.Count;
if (count == 0)
return Instrument.EmptyTags;
array = new KeyValuePair<string, object>[count];
collection.CopyTo(array, 0);
return array;
}
KeyValuePair<string, object>[] array2 = ArrayPool<KeyValuePair<string, object>>.Shared.Rent(32);
int num = 0;
int length = array2.Length;
foreach (KeyValuePair<string, object> tag in tags) {
if (num == length)
<ToArray>g__Grow|11_0(ref array2, ref length);
array2[num++] = tag;
}
if (num == 0) {
ArrayPool<KeyValuePair<string, object>>.Shared.Return(array2, false);
return Instrument.EmptyTags;
}
array = new KeyValuePair<string, object>[num];
Span<KeyValuePair<string, object>> span = MemoryExtensions.AsSpan<KeyValuePair<string, object>>(array2);
span = span.Slice(0, num);
span.CopyTo(MemoryExtensions.AsSpan<KeyValuePair<string, object>>(array));
ArrayPool<KeyValuePair<string, object>>.Shared.Return(array2, false);
return array;
}
}
}