Skip to content

Commit

Permalink
Merge pull request #6 from samsp-msft/metrics-redux
Browse files Browse the repository at this point in the history
Added Histogram support.
  • Loading branch information
samsp-msft committed Aug 31, 2023
2 parents 59e1729 + 9e0f18e commit 49eb637
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 194 deletions.
42 changes: 19 additions & 23 deletions OTLPView/DataModel/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@

namespace OTLPView;

//public class MetricsApplication
//{
// public OtlpApplication OtlpApplication { get; init; }

// public string ApplicationName => OtlpApplication.ApplicationName;
// public IReadOnlyDictionary<string, string> Properties => OtlpApplication.Properties;
// public ConcurrentDictionary<string, MeterResult> Meters { get; } = new();

// public MetricsApplication(OtlpApplication app)
// {
// OtlpApplication = app;
// }

//}

public class MeterResult
{
public string? MeterName { get; init; }
Expand Down Expand Up @@ -122,7 +107,10 @@ public class DimensionScope

private Dictionary<string, string> _dimensions { get; init; }
//public readonly ConcurrentCappedCache<MetricValueBase> _values = new(256);
public readonly ConcurrentBag<MetricValueBase> _values = new();
//public readonly ConcurrentBag<MetricValueBase> _values = new();
public readonly ConcurrentStack<MetricValueBase> _values = new();


// Used to aid in merging values that are the same in a concurrent environment
private MetricValueBase _lastValue;

Expand All @@ -147,7 +135,7 @@ public void AddPointValue(NumberDataPoint d)
{
var start = Helpers.UnixNanoSecondsToDateTime(d.StartTimeUnixNano);
var end = Helpers.UnixNanoSecondsToDateTime(d.TimeUnixNano);
Console.WriteLine($"{start.ToLocalTime().ToLongTimeString()} - {end.ToLocalTime().ToLongTimeString()} - {d.ValueCase} - {d.AsInt} - {d.AsDouble}");
//Console.WriteLine($"{start.ToLocalTime().ToLongTimeString()} - {end.ToLocalTime().ToLongTimeString()} - {d.ValueCase} - {d.AsInt} - {d.AsDouble}");
if (d.ValueCase == NumberDataPoint.ValueOneofCase.AsInt)
{
var value = d.AsInt;
Expand All @@ -166,7 +154,8 @@ public void AddPointValue(NumberDataPoint d)
start = lastLongValue.End;
}
_lastValue = new MetricValue<long>(d.AsInt, start, end);
_values.Add(_lastValue);
_values.Push(_lastValue);
//_values.Add(_lastValue);
}
}
}
Expand All @@ -188,7 +177,8 @@ public void AddPointValue(NumberDataPoint d)
start = lastDoubleValue.End;
}
_lastValue = new MetricValue<double>(d.AsDouble, start, end);
_values.Add(_lastValue);
//_values.Add(_lastValue);
_values.Push(_lastValue);
}
}
}
Expand All @@ -198,7 +188,6 @@ public void AddHistogramValue(HistogramDataPoint h)
{
var start = Helpers.UnixNanoSecondsToDateTime(h.StartTimeUnixNano);
var end = Helpers.UnixNanoSecondsToDateTime(h.TimeUnixNano);

lock (this)
{
var lastHistogramValue = _lastValue as HistogramValue;
Expand All @@ -208,8 +197,13 @@ public void AddHistogramValue(HistogramDataPoint h)
}
else
{
_lastValue = new HistogramValue(h.BucketCounts, h.Sum, h.Count, start, end);
_values.Add(_lastValue);
if (lastHistogramValue is not null)
{
start = lastHistogramValue.End;
}
_lastValue = new HistogramValue(h.BucketCounts, h.Sum, h.Count, start, end, h.ExplicitBounds.ToArray());
//_values.Add(_lastValue);
_values.Push(_lastValue);
}
}
}
Expand Down Expand Up @@ -243,12 +237,14 @@ public class HistogramValue : MetricValueBase
{
public ulong[] Values { get; init; }
public double Sum { get; init; }
public double[]? ExplicitBounds { get; init; }

public HistogramValue(IList<ulong> values, double sum, ulong count, DateTime start, DateTime end) : base(start, end)
public HistogramValue(IList<ulong> values, double sum, ulong count, DateTime start, DateTime end, double[]? explicitBounds) : base(start, end)
{
Values = values?.ToArray();
Sum = sum;
Count = count;
ExplicitBounds = explicitBounds;
}

public override string ToString()
Expand Down
108 changes: 1 addition & 107 deletions OTLPView/Pages/Metrics.razor
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ else
{
@if (Dimension.IsHistogram)
{
<!-- <DimensionedHistogramView Dimension="@Dimension" />-->
<DimensionedHistogramView Counter="@State.SelectedMetric" Dimension="@Dimension" />
}
else
{
Expand All @@ -94,110 +94,4 @@ else
</MudPaper>

</MudStack>




@* <div class="MetricsTree">
<div class="ApplicationsList">
<div class="ApplicationsListTitle">
<h2>Applications</h2>
</div>
<div class="ApplicationsListContents">
@foreach (var app in TelemetryResults.Applications.Values)
{
<div class="@IsAppSelected(app,"ApplicationsListItem" )" @onclick="@(_ => SelectApp(app))">
<h3>@app.UniqueApplicationName</h3>
<table class="ServiceDescription">
<tdata>
<tr>
<td class="Property">Instance Id:</td>
<td class="Value">@app.InstanceId</td>
</tr>
@foreach (var p in app.Properties)
{
<tr>
<td class="Property">@p.Key:</td>
<td class="Value">@p.Value</td>
</tr>
}
</tdata>
</table>
</div>
}
</div>
</div>
<div class="MeterList">
<div class="MeterListTitle">
<h2>Meters</h2>
</div>
@if (State.SelectedApp is not null)
{
<div class="MeterListContents">
@foreach (var m in State.SelectedApp?.Meters?.Values ?? Array.Empty<MeterResult>())
{
<div class="MeterListItem">
<h3>@m.MeterName</h3>
<table class="MeterDescription">
<tdata>
@if (!string.IsNullOrEmpty(m.Version))
{
<tr>
<td class="Property">Version:</td>
<td class="Value">@m.Version</td>
</tr>
}
@foreach (var p in m.Properties)
{
<tr>
<td class="Property">@p.Key:</td>
<td class="Value">@p.Value</td>
</tr>
}
</tdata>
</table>
<div class="MetricListContents">
@foreach (var cnt in m.Counters.Values)
{
<div class="@IsMetricSelected(cnt,"MetricListItem" )" @onclick="@(_ => SelectMetric(cnt))">
<h3 class="CounterName">@cnt.CounterName</h3>
</div>
}
</div>
</div>
}
</div>
}
</div>
</div>
<div class="MetricDetails">
<div class="MetricDetailsTitle">
<h2>Metric Values</h2>
</div>
@if (State.SelectedMetric is not null)
{
<div class="CounterBlock">
<h4 class="CounterName">@State.SelectedMetric.CounterName</h4>
<div class="CounterDetails">
<div>
<span class="Property">Unit: </span>
<span class="Value">@State.SelectedMetric.CounterUnit</span>
<span class="Property">Description:</span>
<span class="Value">@State.SelectedMetric.CounterDescription</span>
</div>
</div>
<div class="CounterContents">
@foreach (var Dimension in State.SelectedMetric.Dimensions.Values)
{
<DimensionedCounterView Dimension="@Dimension" />
}
</div>
</div>
}
</div> *@
}
2 changes: 2 additions & 0 deletions OTLPView/Shared/DimensionedCounterView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
}
else
{
<MudStack Row="true">
@foreach (var kv in Dimension.Dimensions)
{
<MudText>
<span class="DimensionKey">@kv.Key: </span>
<span class="DimensionValue">@kv.Value</span>
</MudText>
}
</MudStack>
}
</MudCardContent>
</MudCardHeader>
Expand Down
55 changes: 34 additions & 21 deletions OTLPView/Shared/DimensionedCounterView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ namespace OTLPView.Shared;

public sealed partial class DimensionedCounterView
{
// Define the size of the graph based on the number of points and the duration of each point
private const int GRAPH_POINT_COUNT = 18; // 3 minutes
private const int GRAPH_POINT_SIZE = 10; // 10s


private DimensionScope _dimension;
private string[] chartLabels;
private List<ChartSeries> chartValues;
Expand All @@ -18,7 +23,7 @@ public required DimensionScope Dimension
new ChartSeries()
{
Name = Counter?.CounterName ?? "unknown",
Data = CalcChartValues(_dimension)
Data = CalcChartValues(_dimension, GRAPH_POINT_COUNT, GRAPH_POINT_SIZE)
}
};
}
Expand All @@ -29,29 +34,34 @@ public required DimensionScope Dimension

protected override void OnInitialized()
{
chartLabels = CalcLabels();
chartLabels = CalcLabels(GRAPH_POINT_COUNT, GRAPH_POINT_SIZE);
}

private string[] CalcLabels()
private string[] CalcLabels(int pointCount, int pointSize)
{
var labels = new string[18];
for (var i = 0; i < 18; i++)
var duration = pointSize * pointCount;
var labels = new string[pointCount];
for (var i = 0; i < pointCount; i++)
{
labels[i] = (i < 17) ? $"{(-170 + (10 * i))}s" : "now";
labels[i] = (i < pointCount - 1) ? $"{(pointSize * (i + 1)) - duration}s" : "Now";
}
return labels;
}

private double[] CalcChartValues(DimensionScope dimension)
// Graph is not based on x,y coordinates, but rather a series of data points with a value
// Each point in the graph is the max value of all the values in that point's time range
private double[] CalcChartValues(DimensionScope dimension, int pointCount, int pointSize)
{
var values = new double[18];
var duration = pointSize * pointCount;
var values = new double[pointCount];
var now = DateTime.UtcNow;
foreach (var point in dimension.Values)
{
var start = CalcOffset(point.Start);
var end = CalcOffset(point.End);
var start = CalcOffset(now-point.Start, pointCount, pointSize);
var end = CalcOffset(now-point.End, pointCount, pointSize);
if (start is not null && end is not null)
{
for (var i = start.GetValueOrDefault(0); i <= end.GetValueOrDefault(17); i++)
for (var i = start.GetValueOrDefault(0); i <= end.GetValueOrDefault(pointCount-1); i++)
{
values[i] = point switch
{
Expand All @@ -66,22 +76,25 @@ private double[] CalcChartValues(DimensionScope dimension)
return values;
}

private double[] CalcFakeValues()
// Calculate the offset of each metric value in the graph based on the current time
private int? CalcOffset(TimeSpan difference, int pointCount, int pointSize)
{

var offset = (pointCount - 1) - (int)Math.Floor(difference.TotalSeconds / pointSize);
return (offset >= 0 && offset < pointCount) ? offset : null;
}

private double[] CalcFakeValues(int pointCount)
{
var values = new double[18];
var values = new double[pointCount];
var rnd = Random.Shared;
for (var i = 0; i < 18; i++)
for (var i = 0; i < pointCount; i++)
{
values[i] = rnd.NextDouble() * 100;
}
return values;
}

private int? CalcOffset(DateTime time)
{
var now = DateTime.UtcNow;
var diff = now - time;
var offset = 17 - (int)Math.Floor(diff.TotalSeconds / 10);
return (offset >= 0 && offset < 18) ? offset : null;
}


}
Loading

0 comments on commit 49eb637

Please sign in to comment.