2023-04-13 12:36:17 +00:00
|
|
|
|
using DistTestCore.Codex;
|
|
|
|
|
using DistTestCore.Metrics;
|
|
|
|
|
using KubernetesWorkflow;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace DistTestCore
|
|
|
|
|
{
|
2023-04-18 11:45:48 +00:00
|
|
|
|
public class PrometheusStarter : BaseStarter
|
2023-04-13 12:36:17 +00:00
|
|
|
|
{
|
|
|
|
|
public PrometheusStarter(TestLifecycle lifecycle, WorkflowCreator workflowCreator)
|
2023-04-18 11:45:48 +00:00
|
|
|
|
: base(lifecycle, workflowCreator)
|
2023-04-13 12:36:17 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 10:21:48 +00:00
|
|
|
|
public RunningContainers CollectMetricsFor(RunningContainers containers)
|
2023-04-13 12:36:17 +00:00
|
|
|
|
{
|
2023-04-18 11:45:48 +00:00
|
|
|
|
LogStart($"Starting metrics server for {containers.Describe()}");
|
2023-04-13 12:36:17 +00:00
|
|
|
|
var startupConfig = new StartupConfig();
|
|
|
|
|
startupConfig.Add(new PrometheusStartupConfig(GeneratePrometheusConfig(containers.Containers)));
|
|
|
|
|
|
|
|
|
|
var workflow = workflowCreator.CreateWorkflow();
|
|
|
|
|
var runningContainers = workflow.Start(1, Location.Unspecified, new PrometheusContainerRecipe(), startupConfig);
|
|
|
|
|
if (runningContainers.Containers.Length != 1) throw new InvalidOperationException("Expected only 1 Prometheus container to be created.");
|
|
|
|
|
|
2023-04-18 11:45:48 +00:00
|
|
|
|
LogEnd("Metrics server started.");
|
2023-04-13 12:36:17 +00:00
|
|
|
|
|
2023-07-11 10:21:48 +00:00
|
|
|
|
return runningContainers;
|
2023-04-13 12:36:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GeneratePrometheusConfig(RunningContainer[] nodes)
|
|
|
|
|
{
|
|
|
|
|
var config = "";
|
|
|
|
|
config += "global:\n";
|
|
|
|
|
config += " scrape_interval: 30s\n";
|
|
|
|
|
config += " scrape_timeout: 10s\n";
|
|
|
|
|
config += "\n";
|
|
|
|
|
config += "scrape_configs:\n";
|
|
|
|
|
config += " - job_name: services\n";
|
|
|
|
|
config += " metrics_path: /metrics\n";
|
|
|
|
|
config += " static_configs:\n";
|
|
|
|
|
config += " - targets:\n";
|
|
|
|
|
|
|
|
|
|
foreach (var node in nodes)
|
|
|
|
|
{
|
2023-06-02 08:04:07 +00:00
|
|
|
|
var ip = node.Pod.PodInfo.Ip;
|
2023-04-13 12:36:17 +00:00
|
|
|
|
var port = node.Recipe.GetPortByTag(CodexContainerRecipe.MetricsPortTag).Number;
|
|
|
|
|
config += $" - '{ip}:{port}'\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bytes = Encoding.ASCII.GetBytes(config);
|
|
|
|
|
return Convert.ToBase64String(bytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|