Metrics test passed

This commit is contained in:
benbierens 2023-09-13 11:59:21 +02:00
parent 1ca3ddc67e
commit 53bb9968ff
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
14 changed files with 91 additions and 56 deletions

View File

@ -67,15 +67,15 @@ namespace CodexPlugin
{
AddEnvVar("CODEX_BLOCK_MN", config.BlockMaintenanceNumber.ToString()!);
}
//if (config.MetricsMode != Metrics.MetricsMode.None)
//{
// var metricsPort = AddInternalPort(MetricsPortTag);
// AddEnvVar("CODEX_METRICS", "true");
// AddEnvVar("CODEX_METRICS_ADDRESS", "0.0.0.0");
// AddEnvVar("CODEX_METRICS_PORT", metricsPort);
// AddPodAnnotation("prometheus.io/scrape", "true");
// AddPodAnnotation("prometheus.io/port", metricsPort.Number.ToString());
//}
if (config.MetricsEnabled)
{
var metricsPort = AddInternalPort(MetricsPortTag);
AddEnvVar("CODEX_METRICS", "true");
AddEnvVar("CODEX_METRICS_ADDRESS", "0.0.0.0");
AddEnvVar("CODEX_METRICS_PORT", metricsPort);
AddPodAnnotation("prometheus.io/scrape", "true");
AddPodAnnotation("prometheus.io/port", metricsPort.Number.ToString());
}
//if (config.MarketplaceConfig != null)
//{

View File

@ -1,10 +1,11 @@
using Core;
using KubernetesWorkflow;
using MetricsPlugin;
using System.Collections;
namespace CodexPlugin
{
public interface ICodexNodeGroup : IEnumerable<IOnlineCodexNode>
public interface ICodexNodeGroup : IEnumerable<IOnlineCodexNode>, IManyMetricScrapeTargets
{
void BringOffline();
IOnlineCodexNode this[int index] { get; }
@ -41,6 +42,7 @@ namespace CodexPlugin
public RunningContainers[] Containers { get; private set; }
public OnlineCodexNode[] Nodes { get; private set; }
public CodexDebugVersionResponse Version { get; private set; }
public IMetricsScrapeTarget[] ScrapeTargets => Nodes.Select(n => n.MetricsScrapeTarget).ToArray();
public IEnumerator<IOnlineCodexNode> GetEnumerator()
{

View File

@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\KubernetesWorkflow\KubernetesWorkflow.csproj" />
<ProjectReference Include="..\MetricsPlugin\MetricsPlugin.csproj" />
</ItemGroup>
</Project>

View File

@ -12,7 +12,7 @@ namespace CodexPlugin
ICodexSetup WithBlockTTL(TimeSpan duration);
ICodexSetup WithBlockMaintenanceInterval(TimeSpan duration);
ICodexSetup WithBlockMaintenanceNumber(int numberOfBlocks);
//ICodexSetup EnableMetrics();
ICodexSetup EnableMetrics();
//ICodexSetup EnableMarketplace(TestToken initialBalance);
//ICodexSetup EnableMarketplace(TestToken initialBalance, Ether initialEther);
//ICodexSetup EnableMarketplace(TestToken initialBalance, Ether initialEther, bool isValidator);
@ -70,11 +70,11 @@ namespace CodexPlugin
return this;
}
//public ICodexSetup EnableMetrics()
//{
// MetricsMode = Metrics.MetricsMode.Record;
// return this;
//}
public ICodexSetup EnableMetrics()
{
MetricsEnabled = true;
return this;
}
//public ICodexSetup EnableMarketplace(TestToken initialBalance)
//{

View File

@ -14,7 +14,7 @@ namespace CodexPlugin
public Location Location { get; set; }
public CodexLogLevel LogLevel { get; }
public ByteSize? StorageQuota { get; set; }
//public MetricsMode MetricsMode { get; set; }
public bool MetricsEnabled { get; set; }
//public MarketplaceInitialConfig? MarketplaceConfig { get; set; }
public string? BootstrapSpr { get; set; }
public int? BlockTTL { get; set; }

View File

@ -1,6 +1,7 @@
using Core;
using FileUtils;
using Logging;
using MetricsPlugin;
using NUnit.Framework;
using Utils;
@ -15,10 +16,9 @@ namespace CodexPlugin
TrackedFile? DownloadContent(ContentId contentId, string fileLabel = "");
void ConnectToPeer(IOnlineCodexNode node);
IDownloadedLog DownloadLog(int? tailLines = null);
//IMetricsAccess Metrics { get; }
//IMarketplaceAccess Marketplace { get; }
CodexDebugVersionResponse Version { get; }
void BringOffline();
IMetricsScrapeTarget MetricsScrapeTarget { get; }
}
public class OnlineCodexNode : IOnlineCodexNode
@ -27,21 +27,26 @@ namespace CodexPlugin
private const string UploadFailedMessage = "Unable to store block";
private readonly IPluginTools tools;
public OnlineCodexNode(IPluginTools tools, CodexAccess codexAccess, CodexNodeGroup group/*, IMetricsAccess metricsAccess, IMarketplaceAccess marketplaceAccess*/)
public OnlineCodexNode(IPluginTools tools, CodexAccess codexAccess, CodexNodeGroup group)
{
this.tools = tools;
CodexAccess = codexAccess;
Group = group;
//Metrics = metricsAccess;
//Marketplace = marketplaceAccess;
Version = new CodexDebugVersionResponse();
}
public CodexAccess CodexAccess { get; }
public CodexNodeGroup Group { get; }
//public IMetricsAccess Metrics { get; }
//public IMarketplaceAccess Marketplace { get; }
public CodexDebugVersionResponse Version { get; private set; }
public IMetricsScrapeTarget MetricsScrapeTarget
{
get
{
var port = CodexAccess.Container.Recipe.GetPortByTag(CodexContainerRecipe.MetricsPortTag);
if (port == null) throw new Exception("Metrics is not available for this Codex node. Please start it with the option '.EnableMetrics()' to enable it.");
return new MetricsScrapeTarget(CodexAccess.Container, port);
}
}
public string GetName()
{

View File

@ -37,12 +37,12 @@ namespace Core
private readonly IFileManager fileManager;
private ILog log;
public PluginTools(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet)
public PluginTools(ILog log, WorkflowCreator workflowCreator, string fileManagerRootFolder, ITimeSet timeSet)
{
this.log = log;
this.workflowCreator = workflowCreator;
this.timeSet = timeSet;
fileManager = new FileManager(log, fileManagerRootFolder);
workflowCreator = new WorkflowCreator(log, configuration);
}
public void ApplyLogPrefix(string prefix)

View File

@ -11,21 +11,21 @@ namespace Core
internal class ToolsFactory : IToolsFactory
{
private readonly ILog log;
private readonly Configuration configuration;
private readonly WorkflowCreator workflowCreator;
private readonly string fileManagerRootFolder;
private readonly ITimeSet timeSet;
public ToolsFactory(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet)
{
this.log = log;
this.configuration = configuration;
workflowCreator = new WorkflowCreator(log, configuration);
this.fileManagerRootFolder = fileManagerRootFolder;
this.timeSet = timeSet;
}
public PluginTools CreateTools()
{
return new PluginTools(log, configuration, fileManagerRootFolder, timeSet);
return new PluginTools(log, workflowCreator, fileManagerRootFolder, timeSet);
}
}
}

View File

@ -28,9 +28,9 @@
public VolumeMount[] Volumes { get; }
public object[] Additionals { get; }
public Port GetPortByTag(string tag)
public Port? GetPortByTag(string tag)
{
return ExposedPorts.Concat(InternalPorts).Single(p => p.Tag == tag);
return ExposedPorts.Concat(InternalPorts).SingleOrDefault(p => p.Tag == tag);
}
public override string ToString()

View File

@ -16,6 +16,11 @@ namespace MetricsPlugin
return Plugin(ci).CreateAccessForTarget(metricsContainer, scrapeTarget);
}
public static IMetricsAccess[] GetMetricsFor(this CoreInterface ci, params IManyMetricScrapeTargets[] manyScrapeTargets)
{
return ci.GetMetricsFor(manyScrapeTargets.SelectMany(t => t.ScrapeTargets).ToArray());
}
public static IMetricsAccess[] GetMetricsFor(this CoreInterface ci, params IMetricsScrapeTarget[] scrapeTargets)
{
var rc = ci.StartMetricsCollector(scrapeTargets);

View File

@ -4,6 +4,7 @@ namespace MetricsPlugin
{
public interface IMetricsAccess
{
string TargetName { get; }
Metrics? GetAllMetrics();
MetricsSet GetMetric(string metricName);
MetricsSet GetMetric(string metricName, TimeSpan timeout);
@ -18,19 +19,10 @@ namespace MetricsPlugin
{
this.query = query;
this.target = target;
TargetName = target.Name;
}
//public void AssertThat(string metricName, IResolveConstraint constraint, string message = "")
//{
// AssertHelpers.RetryAssert(constraint, () =>
// {
// var metricSet = GetMetricWithTimeout(metricName);
// var metricValue = metricSet.Values[0].Value;
// log.Log($"{node.Name} metric '{metricName}' = {metricValue}");
// return metricValue;
// }, message);
//}
public string TargetName { get; }
public Metrics? GetAllMetrics()
{

View File

@ -4,20 +4,27 @@ namespace MetricsPlugin
{
public interface IMetricsScrapeTarget
{
string Name { get; }
string Ip { get; }
int Port { get; }
}
public interface IManyMetricScrapeTargets
{
IMetricsScrapeTarget[] ScrapeTargets { get; }
}
public class MetricsScrapeTarget : IMetricsScrapeTarget
{
public MetricsScrapeTarget(string ip, int port)
public MetricsScrapeTarget(string ip, int port, string name)
{
Ip = ip;
Port = port;
Name = name;
}
public MetricsScrapeTarget(RunningContainer container, int port)
: this(container.Pod.PodInfo.Ip, port)
: this(container.Pod.PodInfo.Ip, port, container.Name)
{
}
@ -26,6 +33,7 @@ namespace MetricsPlugin
{
}
public string Name { get; }
public string Ip { get; }
public int Port { get; }
}

View File

@ -24,23 +24,23 @@ namespace Tests.BasicTests
[Test]
public void TwoMetricsExample()
{
var rc = Ci.StartMetricsCollector();
var group = Ci.SetupCodexNodes(2, s => s.EnableMetrics());
var group2 = Ci.SetupCodexNodes(2, s => s.EnableMetrics());
//var group = Ci.SetupCodexNodes(2, s => s.EnableMetrics());
//var group2 = Ci.SetupCodexNodes(2, s => s.EnableMetrics());
var primary = group[0];
var secondary = group[1];
var primary2 = group2[0];
var secondary2 = group2[1];
//var primary = group[0];
//var secondary = group[1];
//var primary2 = group2[0];
//var secondary2 = group2[1];
var metrics = Ci.GetMetricsFor(primary.MetricsScrapeTarget, primary2.MetricsScrapeTarget);
//primary.ConnectToPeer(secondary);
//primary2.ConnectToPeer(secondary2);
primary.ConnectToPeer(secondary);
primary2.ConnectToPeer(secondary2);
//Thread.Sleep(TimeSpan.FromMinutes(2));
Thread.Sleep(TimeSpan.FromMinutes(2));
//primary.Metrics.AssertThat("libp2p_peers", Is.EqualTo(1));
//primary2.Metrics.AssertThat("libp2p_peers", Is.EqualTo(1));
metrics[0].AssertThat("libp2p_peers", Is.EqualTo(1));
metrics[1].AssertThat("libp2p_peers", Is.EqualTo(1));
}
[Test]

View File

@ -0,0 +1,22 @@
using DistTestCore.Helpers;
using Logging;
using MetricsPlugin;
using NUnit.Framework.Constraints;
namespace Tests
{
public static class MetricsAccessExtensions
{
public static void AssertThat(this IMetricsAccess access, string metricName, IResolveConstraint constraint, ILog? log = null, string message = "")
{
AssertHelpers.RetryAssert(constraint, () =>
{
var metricSet = access.GetMetric(metricName);
var metricValue = metricSet.Values[0].Value;
if (log != null) log.Log($"{access.TargetName} metric '{metricName}' = {metricValue}");
return metricValue;
}, message);
}
}
}