cs-codex-dist-tests/ProjectPlugins/MetricsPlugin/MetricsAccess.cs

65 lines
1.8 KiB
C#
Raw Normal View History

2023-09-14 13:30:09 +00:00
using Core;
using KubernetesWorkflow.Types;
2023-09-14 13:30:09 +00:00
using Utils;
2023-09-13 09:25:08 +00:00
namespace MetricsPlugin
{
2023-09-14 13:30:09 +00:00
public interface IMetricsAccess : IHasContainer
2023-09-13 09:25:08 +00:00
{
2023-09-13 09:59:21 +00:00
string TargetName { get; }
2023-09-13 09:25:08 +00:00
Metrics? GetAllMetrics();
MetricsSet GetMetric(string metricName);
MetricsSet GetMetric(string metricName, TimeSpan timeout);
}
2023-09-13 09:25:08 +00:00
public class MetricsAccess : IMetricsAccess
{
private readonly MetricsQuery query;
private readonly IMetricsScrapeTarget target;
2023-09-13 09:25:08 +00:00
public MetricsAccess(MetricsQuery query, IMetricsScrapeTarget target)
{
this.query = query;
this.target = target;
2023-11-06 14:27:23 +00:00
TargetName = target.Container.Name;
2023-09-13 09:25:08 +00:00
}
2023-09-13 09:59:21 +00:00
public string TargetName { get; }
2023-09-14 13:30:09 +00:00
public RunningContainer Container => query.RunningContainer;
2023-09-13 09:25:08 +00:00
public Metrics? GetAllMetrics()
{
return query.GetAllMetricsForNode(target);
}
2023-09-13 09:25:08 +00:00
public MetricsSet GetMetric(string metricName)
{
return GetMetric(metricName, TimeSpan.FromSeconds(10));
}
2023-09-13 09:25:08 +00:00
public MetricsSet GetMetric(string metricName, TimeSpan timeout)
{
var start = DateTime.UtcNow;
2023-09-13 09:25:08 +00:00
while (true)
{
var mostRecent = GetMostRecent(metricName);
if (mostRecent != null) return mostRecent;
if (DateTime.UtcNow - start > timeout)
{
throw new TimeoutException();
}
2023-09-13 09:25:08 +00:00
Time.Sleep(TimeSpan.FromSeconds(2));
}
}
2023-09-13 09:25:08 +00:00
private MetricsSet? GetMostRecent(string metricName)
{
var result = query.GetMostRecent(metricName, target);
if (result == null) return null;
return result.Sets.LastOrDefault();
}
}
}