Skip to content

Commit

Permalink
adds container names to container log filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Dec 9, 2024
1 parent 1af2165 commit e45b8bd
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Framework/KubernetesWorkflow/CrashWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private bool HasContainerBeenRestarted(Kubernetes client)
private void DownloadCrashedContainerLogs(Kubernetes client)
{
using var stream = client.ReadNamespacedPodLog(podName, k8sNamespace, recipeName, previous: true);
var handler = new WriteToFileLogHandler(log, "Crash detected for " + containerName);
var handler = new WriteToFileLogHandler(log, "Crash detected for " + containerName, containerName);
handler.Log(stream);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Framework/KubernetesWorkflow/K8sController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ private void CheckForCrash(RunningContainer container)
var msg = $"Pod crash detected for deployment {deploymentName} (pod:{podName})";
log.Error(msg);

DownloadPodLog(container, new WriteToFileLogHandler(log, msg), tailLines: null, previous: true);
DownloadPodLog(container, new WriteToFileLogHandler(log, msg, deploymentName), tailLines: null, previous: true);

throw new Exception(msg);
}
Expand Down
4 changes: 2 additions & 2 deletions Framework/KubernetesWorkflow/LogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public void Log(Stream log)

public class WriteToFileLogHandler : LogHandler, ILogHandler
{
public WriteToFileLogHandler(ILog sourceLog, string description)
public WriteToFileLogHandler(ILog sourceLog, string description, string addFileName)
{
LogFile = sourceLog.CreateSubfile();
LogFile = sourceLog.CreateSubfile(addFileName);

var msg = $"{description} -->> {LogFile.FullFilename}";
sourceLog.Log(msg);
Expand Down
2 changes: 1 addition & 1 deletion Framework/KubernetesWorkflow/StartupWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public IDownloadedLog DownloadContainerLog(RunningContainer container, int? tail
{
var msg = $"Downloading container log for '{container.Name}'";
log.Log(msg);
var logHandler = new WriteToFileLogHandler(log, msg);
var logHandler = new WriteToFileLogHandler(log, msg, container.Name);

K8s(controller =>
{
Expand Down
10 changes: 7 additions & 3 deletions Framework/Logging/BaseLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface ILog
void Debug(string message = "", int skipFrames = 0);
void Error(string message);
void AddStringReplace(string from, string to);
LogFile CreateSubfile(string ext = "log");
LogFile CreateSubfile(string addName, string ext = "log");
}

public abstract class BaseLog : ILog
Expand Down Expand Up @@ -72,9 +72,13 @@ public virtual void Delete()
File.Delete(LogFile.FullFilename);
}

public LogFile CreateSubfile(string ext = "log")
public LogFile CreateSubfile(string addName, string ext = "log")
{
return new LogFile($"{GetFullName()}_{GetSubfileNumber()}", ext);
addName = addName
.Replace("<", "")
.Replace(">", "");

return new LogFile($"{GetFullName()}_{GetSubfileNumber()}_{addName}", ext);
}

protected string ApplyReplacements(string str)
Expand Down
4 changes: 2 additions & 2 deletions Framework/Logging/LogPrefixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public LogPrefixer(ILog backingLog, string prefix)
public string Prefix { get; set; } = string.Empty;


public LogFile CreateSubfile(string ext = "log")
public LogFile CreateSubfile(string addName, string ext = "log")
{
return backingLog.CreateSubfile(ext);
return backingLog.CreateSubfile(addName, ext);
}

public void Debug(string message = "", int skipFrames = 0)
Expand Down
4 changes: 2 additions & 2 deletions Framework/Logging/LogSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public void AddStringReplace(string from, string to)
OnAll(l => l.AddStringReplace(from, to));
}

public LogFile CreateSubfile(string ext = "log")
public LogFile CreateSubfile(string addName, string ext = "log")
{
return targetLogs.First().CreateSubfile(ext);
return targetLogs.First().CreateSubfile(addName, ext);
}

public void Debug(string message = "", int skipFrames = 0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/CodexContinuousTests/SingleTestRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void DownloadContainerLogs(DateTime testStart)
var namespaceName = container.RunningPod.StartResult.Cluster.Configuration.KubernetesNamespace;
var openingLine =
$"{namespaceName} - {deploymentName} = {node.Container.Name} = {node.GetDebugInfo().Id}";
elasticSearchLogDownloader.Download(fixtureLog.CreateSubfile(), node.Container, effectiveStart,
elasticSearchLogDownloader.Download(fixtureLog.CreateSubfile(node.GetName()), node.Container, effectiveStart,
effectiveEnd, openingLine);
}
}
Expand Down

0 comments on commit e45b8bd

Please sign in to comment.