2023-10-02 12:42:36 +00:00
|
|
|
|
using Core;
|
2023-11-12 09:07:23 +00:00
|
|
|
|
using KubernetesWorkflow.Types;
|
2023-10-02 12:42:36 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace ContinuousTests
|
|
|
|
|
{
|
|
|
|
|
public class ElasticSearchLogDownloader
|
|
|
|
|
{
|
|
|
|
|
private readonly IPluginTools tools;
|
|
|
|
|
private readonly ILog log;
|
|
|
|
|
|
|
|
|
|
public ElasticSearchLogDownloader(IPluginTools tools, ILog log)
|
|
|
|
|
{
|
|
|
|
|
this.tools = tools;
|
|
|
|
|
this.log = log;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 06:31:48 +00:00
|
|
|
|
public void Download(LogFile targetFile, RunningContainer container, DateTime startUtc, DateTime endUtc, string openingLine)
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-10-08 06:31:48 +00:00
|
|
|
|
DownloadLog(targetFile, container, startUtc, endUtc, openingLine);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Failed to download log: " + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 06:31:48 +00:00
|
|
|
|
private void DownloadLog(LogFile targetFile, RunningContainer container, DateTime startUtc, DateTime endUtc, string openingLine)
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
|
|
|
|
log.Log($"Downloading log (from ElasticSearch) for container '{container.Name}' within time range: " +
|
|
|
|
|
$"{startUtc.ToString("o")} - {endUtc.ToString("o")}");
|
2023-10-08 06:31:48 +00:00
|
|
|
|
log.Log(openingLine);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
|
2024-04-01 06:29:55 +00:00
|
|
|
|
var endpoint = CreateElasticSearchEndpoint();
|
2023-10-02 12:42:36 +00:00
|
|
|
|
var queryTemplate = CreateQueryTemplate(container, startUtc, endUtc);
|
|
|
|
|
|
|
|
|
|
targetFile.Write($"Downloading '{container.Name}' to '{targetFile.FullFilename}'.");
|
2024-04-01 06:29:55 +00:00
|
|
|
|
var reconstructor = new LogReconstructor(targetFile, endpoint, queryTemplate);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
reconstructor.DownloadFullLog();
|
|
|
|
|
|
|
|
|
|
log.Log("Log download finished.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CreateQueryTemplate(RunningContainer container, DateTime startUtc, DateTime endUtc)
|
|
|
|
|
{
|
|
|
|
|
var start = startUtc.ToString("o");
|
|
|
|
|
var end = endUtc.ToString("o");
|
|
|
|
|
|
2023-11-07 13:33:45 +00:00
|
|
|
|
var containerName = container.RunningContainers.StartResult.Deployment.Name;
|
|
|
|
|
var namespaceName = container.RunningContainers.StartResult.Cluster.Configuration.KubernetesNamespace;
|
|
|
|
|
|
|
|
|
|
//container_name : codex3-5 - deploymentName as stored in pod
|
|
|
|
|
// pod_namespace : codex - continuous - nolimits - tests - 1
|
|
|
|
|
|
|
|
|
|
//var source = "{ \"sort\": [ { \"@timestamp\": { \"order\": \"asc\" } } ], \"fields\": [ { \"field\": \"@timestamp\", \"format\": \"strict_date_optional_time\" }, { \"field\": \"pod_name\" }, { \"field\": \"message\" } ], \"size\": <SIZE>, <SEARCHAFTER> \"_source\": false, \"query\": { \"bool\": { \"must\": [], \"filter\": [ { \"range\": { \"@timestamp\": { \"format\": \"strict_date_optional_time\", \"gte\": \"<STARTTIME>\", \"lte\": \"<ENDTIME>\" } } }, { \"match_phrase\": { \"pod_name\": \"<PODNAME>\" } } ] } } }";
|
|
|
|
|
var source = "{ \"sort\": [ { \"@timestamp\": { \"order\": \"asc\" } } ], \"fields\": [ { \"field\": \"@timestamp\", \"format\": \"strict_date_optional_time\" }, { \"field\": \"message\" } ], \"size\": <SIZE>, <SEARCHAFTER> \"_source\": false, \"query\": { \"bool\": { \"must\": [], \"filter\": [ { \"range\": { \"@timestamp\": { \"format\": \"strict_date_optional_time\", \"gte\": \"<STARTTIME>\", \"lte\": \"<ENDTIME>\" } } }, { \"match_phrase\": { \"container_name\": \"<CONTAINERNAME>\" } }, { \"match_phrase\": { \"pod_namespace\": \"<NAMESPACENAME>\" } } ] } } }";
|
2023-10-03 14:11:14 +00:00
|
|
|
|
return source
|
2023-10-02 12:42:36 +00:00
|
|
|
|
.Replace("<STARTTIME>", start)
|
|
|
|
|
.Replace("<ENDTIME>", end)
|
2023-11-07 13:33:45 +00:00
|
|
|
|
.Replace("<CONTAINERNAME>", containerName)
|
|
|
|
|
.Replace("<NAMESPACENAME>", namespaceName);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 06:29:55 +00:00
|
|
|
|
private IEndpoint CreateElasticSearchEndpoint()
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
2023-10-03 12:32:31 +00:00
|
|
|
|
var serviceName = "elasticsearch";
|
|
|
|
|
var k8sNamespace = "monitoring";
|
|
|
|
|
var address = new Address($"http://{serviceName}.{k8sNamespace}.svc.cluster.local", 9200);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
var baseUrl = "";
|
2023-10-03 13:11:45 +00:00
|
|
|
|
|
2024-04-01 06:29:55 +00:00
|
|
|
|
var http = tools.CreateHttp(client =>
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
|
|
|
|
client.DefaultRequestHeaders.Add("kbn-xsrf", "reporting");
|
|
|
|
|
});
|
2024-04-01 06:29:55 +00:00
|
|
|
|
|
|
|
|
|
return http.CreateEndpoint(address, baseUrl);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class LogReconstructor
|
|
|
|
|
{
|
|
|
|
|
private readonly List<LogQueueEntry> queue = new List<LogQueueEntry>();
|
|
|
|
|
private readonly LogFile targetFile;
|
2024-04-01 06:29:55 +00:00
|
|
|
|
private readonly IEndpoint endpoint;
|
2023-10-02 12:42:36 +00:00
|
|
|
|
private readonly string queryTemplate;
|
|
|
|
|
private const int sizeOfPage = 2000;
|
|
|
|
|
private string searchAfter = "";
|
|
|
|
|
private int lastHits = 1;
|
2023-10-03 14:11:14 +00:00
|
|
|
|
private ulong? lastLogLine;
|
2023-10-02 12:42:36 +00:00
|
|
|
|
|
2024-04-01 06:29:55 +00:00
|
|
|
|
public LogReconstructor(LogFile targetFile, IEndpoint endpoint, string queryTemplate)
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
|
|
|
|
this.targetFile = targetFile;
|
2024-04-01 06:29:55 +00:00
|
|
|
|
this.endpoint = endpoint;
|
2023-10-02 12:42:36 +00:00
|
|
|
|
this.queryTemplate = queryTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DownloadFullLog()
|
|
|
|
|
{
|
|
|
|
|
while (lastHits > 0)
|
|
|
|
|
{
|
|
|
|
|
QueryElasticSearch();
|
|
|
|
|
ProcessQueue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void QueryElasticSearch()
|
|
|
|
|
{
|
|
|
|
|
var query = queryTemplate
|
|
|
|
|
.Replace("<SIZE>", sizeOfPage.ToString())
|
|
|
|
|
.Replace("<SEARCHAFTER>", searchAfter);
|
|
|
|
|
|
2024-04-01 06:29:55 +00:00
|
|
|
|
var response = endpoint.HttpPostString<SearchResponse>("_search", query);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
|
|
|
|
|
lastHits = response.hits.hits.Length;
|
|
|
|
|
if (lastHits > 0)
|
|
|
|
|
{
|
|
|
|
|
UpdateSearchAfter(response);
|
|
|
|
|
foreach (var hit in response.hits.hits)
|
|
|
|
|
{
|
|
|
|
|
AddHitToQueue(hit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddHitToQueue(SearchHitEntry hit)
|
|
|
|
|
{
|
|
|
|
|
var message = hit.fields.message.Single();
|
2023-10-03 11:34:39 +00:00
|
|
|
|
var number = ParseCountNumber(message);
|
|
|
|
|
if (number != null)
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
2023-10-03 11:34:39 +00:00
|
|
|
|
queue.Add(new LogQueueEntry(message, number.Value));
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 11:34:39 +00:00
|
|
|
|
private ulong? ParseCountNumber(string message)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(message)) return null;
|
|
|
|
|
var tokens = message.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (!tokens.Any()) return null;
|
|
|
|
|
var countToken = tokens.SingleOrDefault(t => t.StartsWith("count="));
|
|
|
|
|
if (countToken == null) return null;
|
|
|
|
|
var number = countToken.Substring(6);
|
|
|
|
|
if (ulong.TryParse(number, out ulong value))
|
|
|
|
|
{
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 12:42:36 +00:00
|
|
|
|
private void UpdateSearchAfter(SearchResponse response)
|
|
|
|
|
{
|
|
|
|
|
var uniqueSearchNumbers = response.hits.hits.Select(h => h.sort.Single()).Distinct().ToList();
|
|
|
|
|
uniqueSearchNumbers.Reverse();
|
|
|
|
|
|
|
|
|
|
var searchNumber = GetSearchNumber(uniqueSearchNumbers);
|
|
|
|
|
searchAfter = $"\"search_after\": [{searchNumber}],";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long GetSearchNumber(List<long> uniqueSearchNumbers)
|
|
|
|
|
{
|
|
|
|
|
if (uniqueSearchNumbers.Count == 1) return uniqueSearchNumbers.First();
|
|
|
|
|
return uniqueSearchNumbers.Skip(1).First();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessQueue()
|
|
|
|
|
{
|
2023-10-03 14:11:14 +00:00
|
|
|
|
if (lastLogLine == null)
|
|
|
|
|
{
|
|
|
|
|
lastLogLine = queue.Min(q => q.Number) - 1;
|
|
|
|
|
}
|
2023-10-03 13:36:44 +00:00
|
|
|
|
|
2023-10-02 12:42:36 +00:00
|
|
|
|
while (queue.Any())
|
|
|
|
|
{
|
2023-10-03 14:11:14 +00:00
|
|
|
|
ulong wantedNumber = lastLogLine.Value + 1;
|
2023-10-03 13:36:44 +00:00
|
|
|
|
|
2023-10-02 12:42:36 +00:00
|
|
|
|
DeleteOldEntries(wantedNumber);
|
|
|
|
|
|
|
|
|
|
var currentEntry = queue.FirstOrDefault(e => e.Number == wantedNumber);
|
|
|
|
|
|
|
|
|
|
if (currentEntry != null)
|
|
|
|
|
{
|
|
|
|
|
WriteEntryToFile(currentEntry);
|
|
|
|
|
queue.Remove(currentEntry);
|
|
|
|
|
lastLogLine = currentEntry.Number;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The line number we want is not in the queue.
|
|
|
|
|
// It will be returned by the elastic search query, some time in the future.
|
|
|
|
|
// Stop processing the queue for now.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteEntryToFile(LogQueueEntry currentEntry)
|
|
|
|
|
{
|
|
|
|
|
targetFile.WriteRaw(currentEntry.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 11:34:39 +00:00
|
|
|
|
private void DeleteOldEntries(ulong wantedNumber)
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
|
|
|
|
queue.RemoveAll(e => e.Number < wantedNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class LogQueueEntry
|
|
|
|
|
{
|
2023-10-03 11:34:39 +00:00
|
|
|
|
public LogQueueEntry(string message, ulong number)
|
2023-10-02 12:42:36 +00:00
|
|
|
|
{
|
|
|
|
|
Message = message;
|
|
|
|
|
Number = number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Message { get; }
|
2023-10-03 11:34:39 +00:00
|
|
|
|
public ulong Number { get; }
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SearchResponse
|
|
|
|
|
{
|
2023-10-03 12:32:31 +00:00
|
|
|
|
public SearchHits hits { get; set; } = new SearchHits();
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SearchHits
|
|
|
|
|
{
|
2023-10-03 12:32:31 +00:00
|
|
|
|
public SearchHitEntry[] hits { get; set; } = Array.Empty<SearchHitEntry>();
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SearchHitEntry
|
|
|
|
|
{
|
2023-10-03 12:32:31 +00:00
|
|
|
|
public SearchHitFields fields { get; set; } = new SearchHitFields();
|
|
|
|
|
public long[] sort { get; set; } = Array.Empty<long>();
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SearchHitFields
|
|
|
|
|
{
|
2023-10-03 12:32:31 +00:00
|
|
|
|
public string[] @timestamp { get; set; } = Array.Empty<string>();
|
|
|
|
|
public string[] message { get; set; } = Array.Empty<string>();
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|