automatically adds prometheus as datasource

This commit is contained in:
benbierens 2023-08-11 11:06:40 +02:00
parent 79908b8a54
commit 2acb669c31
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
3 changed files with 96 additions and 3 deletions

View File

@ -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);

View File

@ -19,8 +19,8 @@ namespace DistTestCore.Metrics
// enabled = true
//GF_<SectionName>_<KeyName>__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

View File

@ -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<KeyValuePair<string, string>>();
// values.Add(new KeyValuePair<string, string>("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 = "";