Code/Types.cs
using System.Text.Json.Serialization;
/// <summary>
/// Base implementation for recorded metrics, add "JsonPropertyName" on properties for labels, and override GetHashCode
/// </summary>
public interface IMetric
{
[JsonInclude, JsonPropertyName( "__name__" )]
public string Name { get; }
public int GetHashCode();
}
public class MetricStreamEntry
{
[JsonInclude, JsonPropertyName( "metric" )]
public object Metric { get; set; }
[JsonInclude, JsonPropertyName( "values" )]
public List<float> values = new();
[JsonInclude, JsonPropertyName( "timestamps" )]
public List<long> timestamps = new();
public override string ToString()
{
IMetric m = (IMetric)Metric;
TypeDescription td = TypeLibrary.GetType( m.GetType() );
var met = $"{td.ClassName} - {td.GetHashCode()}";
return $"MetricEntry for '{met}', values: {values.Count}";
}
/// <summary>
/// record a metric at a specific point in time
/// </summary>
public void Backfill( long unixTime, float value )
{
values.Add( value );
timestamps.Add( unixTime );
}
/// <summary>
/// add a value to a metric now
/// </summary>
public void Add( float value )
{
Backfill( DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), values.LastOrDefault( 0 ) + value );
}
/// <summary>
/// Add 1 to a metric
/// </summary>
public void Increment()
{
Add( 1 );
}
/// <summary>
/// Set metric to value now
/// </summary>
public void Set( float value )
{
Backfill( DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), value );
}
}