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

93 lines
3.2 KiB
C#
Raw Permalink Normal View History

2023-09-13 07:12:18 +00:00
using Core;
using KubernetesWorkflow;
using KubernetesWorkflow.Types;
2023-11-07 11:02:17 +00:00
using Logging;
2023-09-13 09:25:08 +00:00
using System.Text;
2023-04-13 12:36:17 +00:00
2023-09-13 07:12:18 +00:00
namespace MetricsPlugin
2023-04-13 12:36:17 +00:00
{
2023-09-13 07:12:18 +00:00
public class PrometheusStarter
2023-04-13 12:36:17 +00:00
{
2023-09-13 14:06:05 +00:00
private readonly PrometheusContainerRecipe recipe = new PrometheusContainerRecipe();
2023-09-13 07:12:18 +00:00
private readonly IPluginTools tools;
public PrometheusStarter(IPluginTools tools)
2023-04-13 12:36:17 +00:00
{
2023-09-13 07:12:18 +00:00
this.tools = tools;
2023-04-13 12:36:17 +00:00
}
public RunningPod CollectMetricsFor(IMetricsScrapeTarget[] targets, TimeSpan scrapeInterval)
2023-04-13 12:36:17 +00:00
{
if (!targets.Any()) throw new ArgumentException(nameof(targets) + " must not be empty.");
2023-09-13 09:25:08 +00:00
Log($"Starting metrics server for {targets.Length} targets...");
var startupConfig = new StartupConfig();
startupConfig.Add(new PrometheusStartupConfig(GeneratePrometheusConfig(targets, scrapeInterval)));
2023-04-13 12:36:17 +00:00
2023-09-13 09:25:08 +00:00
var workflow = tools.CreateWorkflow();
var runningContainers = workflow.Start(1, recipe, startupConfig).WaitForOnline();
2023-09-13 09:25:08 +00:00
if (runningContainers.Containers.Length != 1) throw new InvalidOperationException("Expected only 1 Prometheus container to be created.");
2023-04-13 12:36:17 +00:00
2023-09-13 09:25:08 +00:00
Log("Metrics server started.");
return runningContainers;
2023-04-13 12:36:17 +00:00
}
2024-04-13 14:09:17 +00:00
public MetricsAccess CreateAccessForTarget(RunningPod metricsPod, IMetricsScrapeTarget target)
2023-09-13 09:25:08 +00:00
{
2024-04-13 14:09:17 +00:00
var metricsQuery = new MetricsQuery(tools, metricsPod.Containers.Single());
2023-09-13 09:25:08 +00:00
return new MetricsAccess(metricsQuery, target);
}
2023-09-13 14:06:05 +00:00
public string GetPrometheusId()
{
return recipe.Image;
}
2023-09-13 09:25:08 +00:00
private void Log(string msg)
{
tools.GetLog().Log(msg);
}
private string GeneratePrometheusConfig(IMetricsScrapeTarget[] targets, TimeSpan scrapeInterval)
2023-09-13 09:25:08 +00:00
{
var secs = Convert.ToInt32(scrapeInterval.TotalSeconds);
if (secs < 1) throw new Exception("ScrapeInterval can't be < 1s");
if (secs > 60) throw new Exception("ScrapeInterval can't be > 60s");
2023-09-13 09:25:08 +00:00
var config = "";
config += "global:\n";
config += $" scrape_interval: {secs}s\n";
config += $" scrape_timeout: {secs}s\n";
2023-09-13 09:25:08 +00:00
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 target in targets)
{
2023-11-06 14:27:23 +00:00
config += $" - '{FormatTarget(target)}'\n";
2023-09-13 09:25:08 +00:00
}
var bytes = Encoding.ASCII.GetBytes(config);
return Convert.ToBase64String(bytes);
}
2023-11-06 14:27:23 +00:00
private string FormatTarget(IMetricsScrapeTarget target)
{
2023-11-07 11:02:17 +00:00
return ScrapeTargetHelper.FormatTarget(tools.GetLog(), target);
2023-11-06 14:27:23 +00:00
}
}
public static class ScrapeTargetHelper
{
2023-11-07 11:02:17 +00:00
public static string FormatTarget(ILog log, IMetricsScrapeTarget target)
2023-11-06 14:27:23 +00:00
{
2024-09-23 08:52:12 +00:00
var a = target.Container.GetAddress(target.MetricsPortTag);
2023-11-06 14:27:23 +00:00
var host = a.Host.Replace("http://", "").Replace("https://", "");
return $"{host}:{a.Port}";
}
2023-04-13 12:36:17 +00:00
}
}