/ ProjectPlugins / MetricsPlugin / MetricsAccess.cs
MetricsAccess.cs
 1  using Core;
 2  using KubernetesWorkflow.Types;
 3  using Utils;
 4  
 5  namespace MetricsPlugin
 6  {
 7      public interface IMetricsAccess : IHasContainer
 8      {
 9          string TargetName { get; }
10          Metrics GetAllMetrics();
11          MetricsSet GetMetric(string metricName);
12          MetricsSet GetMetric(string metricName, TimeSpan timeout);
13      }
14  
15      public class MetricsAccess : IMetricsAccess
16      {
17          private readonly MetricsQuery query;
18          private readonly Address target;
19  
20          public MetricsAccess(MetricsQuery query, Address target)
21          {
22              this.query = query;
23              this.target = target;
24              TargetName = $"'{target.Host}'";
25          }
26  
27          public string TargetName { get; }
28          public RunningContainer Container => query.RunningContainer;
29  
30          public Metrics GetAllMetrics()
31          {
32              return query.GetAllMetricsForNode(target);
33          }
34  
35          public MetricsSet GetMetric(string metricName)
36          {
37              return GetMetric(metricName, TimeSpan.FromSeconds(10));
38          }
39  
40          public MetricsSet GetMetric(string metricName, TimeSpan timeout)
41          {
42              var start = DateTime.UtcNow;
43  
44              while (true)
45              {
46                  var mostRecent = GetMostRecent(metricName);
47                  if (mostRecent != null) return mostRecent;
48                  if (DateTime.UtcNow - start > timeout)
49                  {
50                      throw new TimeoutException();
51                  }
52  
53                  Time.Sleep(TimeSpan.FromSeconds(2));
54              }
55          }
56  
57          private MetricsSet GetMostRecent(string metricName)
58          {
59              var result = query.GetMostRecent(metricName, target);
60              return result.Sets.Last();
61          }
62      }
63  }