2
0
mirror of synced 2025-02-05 13:15:10 +00:00
cs-codex-dist-tests/DistTestCore/PrometheusStarter.cs

132 lines
5.1 KiB
C#
Raw Normal View History

2023-04-13 14:36:17 +02:00
using DistTestCore.Codex;
using DistTestCore.Metrics;
using KubernetesWorkflow;
using Logging;
2023-08-13 08:27:30 +02:00
using Newtonsoft.Json;
2023-08-11 12:38:26 +02:00
using System.Reflection;
2023-04-13 14:36:17 +02:00
using System.Text;
namespace DistTestCore
{
2023-04-18 13:45:48 +02:00
public class PrometheusStarter : BaseStarter
2023-04-13 14:36:17 +02:00
{
public PrometheusStarter(TestLifecycle lifecycle)
: base(lifecycle)
2023-04-13 14:36:17 +02:00
{
}
public RunningContainers CollectMetricsFor(RunningContainers[] containers)
2023-04-13 14:36:17 +02:00
{
2023-04-18 13:45:48 +02:00
LogStart($"Starting metrics server for {containers.Describe()}");
2023-04-13 14:36:17 +02:00
var startupConfig = new StartupConfig();
startupConfig.Add(new PrometheusStartupConfig(GeneratePrometheusConfig(containers.Containers())));
2023-04-13 14:36:17 +02:00
var workflow = lifecycle.WorkflowCreator.CreateWorkflow();
2023-04-13 14:36:17 +02:00
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.");
var pc = runningContainers.Containers.First().ClusterExternalAddress;
var prometheusUrl = pc.Host + ":" + pc.Port;
2023-08-11 09:37:30 +02:00
workflow = lifecycle.WorkflowCreator.CreateWorkflow();
var grafanaContainers = workflow.Start(1, Location.Unspecified, new GrafanaContainerRecipe(), startupConfig);
if (grafanaContainers.Containers.Length != 1) throw new InvalidOperationException("should be 1");
Thread.Sleep(3000);
var c = grafanaContainers.Containers.First().ClusterExternalAddress;
var http = new Http(new NullLog(), new DefaultTimeSet(), c, "api/");
var response = http.HttpPostJson("datasources", new GrafanaDataSource
{
2023-08-11 12:38:26 +02:00
uid = "c89eaad3-9184-429f-ac94-8ba0b1824dbb",
name = "CodexPrometheus",
type = "prometheus",
url = prometheusUrl,
access = "proxy",
basicAuth = false,
jsonData = new GrafanaDataSourceJsonData
{
httpMethod = "POST"
}
});
2023-08-11 12:38:26 +02:00
var response2 = http.HttpPostString("dashboards/db", GetDashboardJson());
2023-08-13 08:27:30 +02:00
var jsonResponse = JsonConvert.DeserializeObject<GrafanaPostDashboardResponse>(response2);
2023-08-13 08:27:30 +02:00
var grafanaUrl = c.Host + ":" + c.Port + jsonResponse.url;
System.Diagnostics.Process.Start("C:\\Users\\Ben\\AppData\\Local\\Programs\\Opera\\opera.exe", grafanaUrl);
2023-04-18 13:45:48 +02:00
LogEnd("Metrics server started.");
2023-04-13 14:36:17 +02:00
return runningContainers;
2023-04-13 14:36:17 +02:00
}
public class GrafanaDataSource
{
2023-08-11 12:38:26 +02:00
public string uid { get; set; } = string.Empty;
public string name { get; set; } = string.Empty;
public string type { get; set; } = string.Empty;
public string url { get; set; } = string.Empty;
public string access { get; set; } = string.Empty;
public bool basicAuth { get; set; }
public GrafanaDataSourceJsonData jsonData { get; set; } = new();
}
public class GrafanaDataSourceJsonData
{
public string httpMethod { get; set; } = string.Empty;
}
2023-08-13 08:27:30 +02:00
public class GrafanaPostDashboardResponse
{
public int id { get; set; }
public string slug { get; set; } = string.Empty;
public string status { get; set; } = string.Empty;
public string uid { get; set; } = string.Empty;
public string url { get; set; } = string.Empty;
public int version { get; set; }
}
2023-08-11 12:38:26 +02:00
private string GetDashboardJson()
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "DistTestCore.Metrics.dashboard.json";
//var names = assembly.GetManifestResourceNames();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
2023-08-13 08:27:30 +02:00
var dashboard = reader.ReadToEnd();
return $"{{\"dashboard\": {dashboard} ,\"message\": \"Default Codex Dashboard\",\"overwrite\": false}}";
2023-08-11 12:38:26 +02:00
}
}
2023-04-13 14:36:17 +02: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)
{
var ip = node.Pod.PodInfo.Ip;
2023-04-13 14:36:17 +02:00
var port = node.Recipe.GetPortByTag(CodexContainerRecipe.MetricsPortTag).Number;
config += $" - '{ip}:{port}'\n";
}
var bytes = Encoding.ASCII.GetBytes(config);
return Convert.ToBase64String(bytes);
}
}
}