From 2acb669c3120199ab7a1c8d425c99723a3ffbcfc Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 11 Aug 2023 11:06:40 +0200 Subject: [PATCH] automatically adds prometheus as datasource --- DistTestCore/Http.cs | 4 +- .../Metrics/GrafanaContainerRecipe.cs | 4 +- DistTestCore/PrometheusStarter.cs | 91 +++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/DistTestCore/Http.cs b/DistTestCore/Http.cs index 391dbc7..866a640 100644 --- a/DistTestCore/Http.cs +++ b/DistTestCore/Http.cs @@ -1,4 +1,5 @@ -using Logging; +using IdentityModel.Client; +using Logging; using Newtonsoft.Json; using System.Net.Http.Headers; using System.Net.Http.Json; @@ -59,6 +60,7 @@ namespace DistTestCore var url = GetUrl() + route; using var content = JsonContent.Create(body); Log(url, JsonConvert.SerializeObject(body)); + client.SetBasicAuthentication("admin", "admin"); var result = Time.Wait(client.PostAsync(url, content)); var str = Time.Wait(result.Content.ReadAsStringAsync()); Log(url, str); diff --git a/DistTestCore/Metrics/GrafanaContainerRecipe.cs b/DistTestCore/Metrics/GrafanaContainerRecipe.cs index 12af581..b873fa8 100644 --- a/DistTestCore/Metrics/GrafanaContainerRecipe.cs +++ b/DistTestCore/Metrics/GrafanaContainerRecipe.cs @@ -19,8 +19,8 @@ namespace DistTestCore.Metrics // enabled = true //GF____FILE - AddEnvVar("GF_AUTH_ANONYMOUS_ENABLED", "true"); - AddEnvVar("GF_AUTH_DISABLE_LOGIN_FORM", "true"); + //AddEnvVar("GF_AUTH_ANONYMOUS_ENABLED", "true"); + //AddEnvVar("GF_AUTH_DISABLE_LOGIN_FORM", "true"); //[auth] //disable_login_form = true diff --git a/DistTestCore/PrometheusStarter.cs b/DistTestCore/PrometheusStarter.cs index 862907d..4e27952 100644 --- a/DistTestCore/PrometheusStarter.cs +++ b/DistTestCore/PrometheusStarter.cs @@ -1,7 +1,14 @@ using DistTestCore.Codex; using DistTestCore.Metrics; using KubernetesWorkflow; +using Logging; +using System; +using System.Diagnostics; +using System.Net.Http.Headers; using System.Text; +using Utils; +using static System.Net.Mime.MediaTypeNames; +using static System.Net.WebRequestMethods; namespace DistTestCore { @@ -22,15 +29,99 @@ namespace DistTestCore 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; + 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; + + + + //{ + // //setup reusable http client + // HttpClient client = new HttpClient(); + // Uri baseUri = new Uri(c.Host + ":" + c.Port); + // client.BaseAddress = baseUri; + // client.DefaultRequestHeaders.Clear(); + // client.DefaultRequestHeaders.ConnectionClose = true; + + // //Post body content + // var values = new List>(); + // values.Add(new KeyValuePair("grant_type", "client_credentials")); + // var content = new FormUrlEncodedContent(values); + + // var authenticationString = $"admin:admin"; + // var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString)); + + // var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/oauth2/token"); + // requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString); + // requestMessage.Content = content; + + // //make the request + // var responsea = Time.Wait(client.SendAsync(requestMessage)); + // responsea.EnsureSuccessStatusCode(); + // string responseBody = Time.Wait(responsea.Content.ReadAsStringAsync()); + // Console.WriteLine(responseBody); + + //} + + //POST / api / datasources HTTP / 1.1 + //Accept: application / json + //Content - Type: application / json + //Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk + + var http = new Http(new NullLog(), new DefaultTimeSet(), c, "api/"); + var response = http.HttpPostJson("datasources", new GrafanaDataSource + { + name = "CodexPrometheus", + type = "prometheus", + url = prometheusUrl, + access = "proxy", + basicAuth = false, + jsonData = new GrafanaDataSourceJsonData + { + httpMethod = "POST" + } + }); + + + // [{ "id":1,"uid":"c89eaad3-9184-429f-ac94-8ba0b1824dbb","orgId":1, + // "name":"Prometheus","type":"prometheus","typeName":"Prometheus", + // "typeLogoUrl":"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg", + // "access":"proxy","url":"http://kubernetes.docker.internal:31234","user":"","database":"", + // "basicAuth":false,"isDefault":true,"jsonData":{ "httpMethod":"POST"},"readOnly":false}] + + + var grafanaUrl = c.Host + ":" + c.Port; + System.Diagnostics.Process.Start("C:\\Users\\Ben\\AppData\\Local\\Programs\\Opera\\opera.exe", grafanaUrl); + LogEnd("Metrics server started."); + + return runningContainers; } + public class GrafanaDataSource + { + 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; + } + private string GeneratePrometheusConfig(RunningContainer[] nodes) { var config = "";