Skip to content

Commit 7ca59cb

Browse files
author
Timothy Dodd
committed
Refactor log processing with Deployment/Pod composite key
Enhanced log processing by introducing a composite key (`Deployment/Pod`) to uniquely identify and manage logs for multiple containers per deployment. - Added deployment count retrieval in `InitializeDeploymentTimesAsync` to optimize backfilling. - Updated `ReadNewLinesAsync` and `ProcessEventLogFileAsync` to use the composite key for deployment settings. - Refactored `ConvertEventRecordToLogLine` to improve clarity and align with the composite key approach. - Added a new API endpoint (`GET /api/log/counts`) to expose deployment and pod counts. - Implemented `GetDeploymentCounts` in `LogRepo.cs` to query deployment and pod counts from the database. - Introduced the `DeploymentCount` class to represent deployment and pod count data. - Improved logging for better traceability of deployment/pod combinations. - Cleaned up redundant code and updated comments to reflect the new structure. These changes improve the granularity, accuracy, and maintainability of log processing.
1 parent 410f8f4 commit 7ca59cb

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

src/LogMkAgent/Services/LogWatcher.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,28 @@ private async Task InitializeDeploymentTimesAsync(LogApiClient client)
133133
{
134134
try
135135
{
136+
// First, get deployment counts to know which deployments have already been backfilled
137+
var counts = await client.GetDataAsync<List<DeploymentCount>>("api/log/counts").ConfigureAwait(false);
138+
if (counts == null || !counts.Any())
139+
{
140+
_logger.LogInformation("No existing deployments found, all logs will be processed");
141+
return;
142+
}
143+
144+
_logger.LogInformation("Retrieved deployment counts for {Count} deployment/pod combinations", counts.Count);
145+
146+
// Then, get the latest timestamps for deployments that have logs
136147
var times = await client.GetDataAsync<List<LatestDeploymentEntry>>("api/log/times").ConfigureAwait(false);
137148
if (times != null)
138149
{
139-
_logger.LogInformation("Retrieved latest deployment times for {Count} deployments", times.Count);
140-
foreach (var pod in times)
150+
foreach (var entry in times)
141151
{
142-
var settings = GetDeploymentSettings(pod.Deployment);
143-
settings.LastDeploymentTime = pod.TimeStamp;
144-
_logger.LogDebug("Deployment {Deployment} last wrote at {TimeStamp}", pod.Deployment, pod.TimeStamp);
152+
// Use composite key (Deployment/Pod) to handle multiple containers per deployment
153+
var compositeKey = $"{entry.Deployment}/{entry.Pod}";
154+
var settings = GetDeploymentSettings(compositeKey);
155+
settings.LastDeploymentTime = entry.TimeStamp;
156+
_logger.LogDebug("Container {Deployment}/{Pod} last wrote at {TimeStamp}, will skip older logs",
157+
entry.Deployment, entry.Pod, entry.TimeStamp);
145158
}
146159
}
147160
}
@@ -304,7 +317,9 @@ private async Task ReadNewLinesAsync(PodInfo info, CancellationToken stoppingTok
304317
{
305318
var filePath = info.LogPath;
306319
var podSettings = GetPodSettings(info.PodName);
307-
var deploymentSettings = GetDeploymentSettings(info.DeploymentName);
320+
// Use composite key (Deployment/Pod) for deployment settings
321+
var compositeKey = $"{info.DeploymentName}/{info.PodName}";
322+
var deploymentSettings = GetDeploymentSettings(compositeKey);
308323

309324
if (!File.Exists(filePath))
310325
{
@@ -640,7 +655,10 @@ private async Task ProcessEventLogFileAsync(string filePath, CancellationToken s
640655
// machine hostname
641656

642657
var machine = Environment.MachineName;
643-
var deploymentSettings = GetDeploymentSettings($"{machine}-{logName}");
658+
var podName = $"{machine}-{logName}";
659+
// Use composite key (Deployment/Pod) - for event logs, deployment = machine
660+
var compositeKey = $"{machine}/{podName}";
661+
var deploymentSettings = GetDeploymentSettings(compositeKey);
644662

645663
// Handle file rotation/changes
646664
if (deploymentSettings.FilePath != filePath)
@@ -670,7 +688,7 @@ private async Task ProcessEventLogFileAsync(string filePath, CancellationToken s
670688
if (currentRecordId <= lastRecordId)
671689
continue;
672690

673-
var logLine = ConvertEventRecordToLogLine(eventRecord, logName);
691+
var logLine = ConvertEventRecordToLogLine(eventRecord, podName, machine);
674692
if (logLine != null)
675693
{
676694
// Apply log level filtering
@@ -728,18 +746,15 @@ private async Task ProcessEventLogFileAsync(string filePath, CancellationToken s
728746
}
729747
}
730748

731-
private LogLine? ConvertEventRecordToLogLine(EventRecord eventRecord, string logName)
749+
private LogLine? ConvertEventRecordToLogLine(EventRecord eventRecord, string podName, string machine)
732750
{
733751
try
734752
{
735753

736754

737755
var logLevel = MapEventLevelToLogLevel(eventRecord.Level);
738-
var machine = Environment.MachineName;
739756
// Build the log message
740757
var message = BuildEventLogMessage(eventRecord);
741-
var logname = eventRecord.LogName ?? logName;
742-
var podName = $"{machine}-{logname}";
743758
var logLine = new LogLine
744759
{
745760
DeploymentName = TruncateString(machine, 50),

src/LogMkApi/Controllers/LogController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,14 @@ public async Task<IEnumerable<LatestDeploymentEntry>> GetLatestEntryTimes()
479479
return entries;
480480
}
481481

482+
[AllowAnonymous]
483+
[HttpGet("counts")]
484+
public async Task<IEnumerable<DeploymentCount>> GetDeploymentCounts()
485+
{
486+
var entries = await _logRepo.GetDeploymentCounts();
487+
return entries;
488+
}
489+
482490
[AllowAnonymous]
483491
[HttpGet("settings")]
484492
public IActionResult GetValidationSettings()

src/LogMkApi/Data/LogRepo.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ FROM Log
170170
}
171171
}
172172

173+
public async Task<IEnumerable<DeploymentCount>> GetDeploymentCounts()
174+
{
175+
using (var db = _dbFactory.CreateConnection())
176+
{
177+
var query = @"
178+
SELECT Deployment, Pod, COUNT(*) AS Count
179+
FROM Log
180+
GROUP BY Deployment, Pod
181+
";
182+
return await db.QueryAsync<DeploymentCount>(query);
183+
}
184+
}
185+
173186
public async Task<int> PurgeLogsByDeployment(string deployment, DateTime? startDate)
174187
{
175188
using (var db = _dbFactory.CreateConnection())

src/LogMkCommon/DeploymentCount.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LogMkCommon;
2+
3+
public class DeploymentCount
4+
{
5+
public required string Deployment { get; set; }
6+
public required string Pod { get; set; }
7+
public required long Count { get; set; }
8+
}

0 commit comments

Comments
 (0)