Counter<T>
Represents an instrument that supports adding non-negative values. For example, you might call counter.Add(1) each time a request is processed to track the total number of requests. Most metric viewers display counters using a rate (requests/sec), by default, but can also display a cumulative total.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Diagnostics.Metrics
{
public sealed class Counter<T> : Instrument<T> where T : struct
{
internal Counter(Meter meter, string name, string unit, string description)
: this(meter, name, unit, description, (IEnumerable<KeyValuePair<string, object>>)null)
{
}
internal Counter(Meter meter, string name, string unit, string description, IEnumerable<KeyValuePair<string, object>> tags)
: base(meter, name, unit, description, tags)
{
Publish();
}
public void Add(T delta)
{
RecordMeasurement(delta);
}
public void Add(T delta, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, object> tag)
{
RecordMeasurement(delta, tag);
}
public void Add(T delta, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, object> tag1, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, object> tag2)
{
RecordMeasurement(delta, tag1, tag2);
}
public void Add(T delta, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, object> tag1, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, object> tag2, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, object> tag3)
{
RecordMeasurement(delta, tag1, tag2, tag3);
}
public void Add(T delta, [System.Runtime.CompilerServices.ParamCollection] [System.Runtime.CompilerServices.ScopedRef] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1,
2
})] ReadOnlySpan<KeyValuePair<string, object>> tags)
{
RecordMeasurement(delta, tags);
}
public void Add(T delta, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
2
})] params KeyValuePair<string, object>[] tags)
{
RecordMeasurement(delta, MemoryExtensions.AsSpan<KeyValuePair<string, object>>(tags));
}
public void Add(T delta, [In] [System.Runtime.CompilerServices.IsReadOnly] ref TagList tagList)
{
RecordMeasurement(delta, ref tagList);
}
}
}