Skip to content

Commit

Permalink
Add option for logging download requests to console (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepy authored Jun 11, 2024
1 parent 2b7d37e commit 5368af9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
11 changes: 9 additions & 2 deletions Robust.Cdn/CdnOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Robust.Cdn;
using Robust.Cdn.Services;

namespace Robust.Cdn;

public sealed class CdnOptions
{
Expand Down Expand Up @@ -69,10 +71,15 @@ public sealed class CdnOptions
public float AutoStreamCompressRatio { get; set; } = 0.5f;

/// <summary>
/// Log all download requests to the database.
/// Log all download requests
/// </summary>
public bool LogRequests { get; set; } = false;

/// <summary>
/// Log download requests to database or console
/// </summary>
public RequestLogStorage LogRequestStorage { get; set; } = RequestLogStorage.Database;

/// <summary>
/// Authentication token to initiate version updates via the POST /control/update endpoint.
/// </summary>
Expand Down
29 changes: 27 additions & 2 deletions Robust.Cdn/Services/DownloadRequestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
WriteLogs();
var storage = _options.Value.LogRequestStorage;
if (storage == RequestLogStorage.Database)
WriteLogsDatabase();
else if (storage == RequestLogStorage.Console)
WriteLogsConsole();
else
_logger.LogError("Unsupported LogRequestStorage configured: ${Format}", storage);
}
catch (Exception e)
{
Expand All @@ -52,7 +58,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
// ReSharper disable once FunctionNeverReturns
}

private void WriteLogs()
private void WriteLogsDatabase()
{
using var scope = _scopeFactory.CreateScope();

Expand Down Expand Up @@ -108,6 +114,25 @@ private void WriteLogs()
_logger.LogDebug("Wrote {CountWritten} log entries to disk", countWritten);
}

private void WriteLogsConsole()
{
var countWritten = 0;
while (_channelReader.TryRead(out var entry))
{
var hash = CryptoGenericHashBlake2B.Hash(32, entry.RequestData.Span, ReadOnlySpan<byte>.Empty);
_logger.LogInformation("RequestLog {Time} {Compression} {Protocol} {VersionId} {BytesSent} {DataSize} {Hash}",
entry.Time.ToString("o", System.Globalization.CultureInfo.InvariantCulture),
entry.Compression,
entry.Protocol,
entry.VersionId,
entry.BytesSent,
entry.RequestData.Length,
Convert.ToHexString(hash));
countWritten += 1;
}
_logger.LogDebug("Wrote {CountWritten} log entries to console", countWritten);
}

public sealed record RequestLog(
ReadOnlyMemory<byte> RequestData,
RequestLogCompression Compression,
Expand Down
7 changes: 7 additions & 0 deletions Robust.Cdn/Services/RequestLogStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Robust.Cdn.Services;

public enum RequestLogStorage
{
Database,
Console
}

0 comments on commit 5368af9

Please sign in to comment.