Skip to content

Commit

Permalink
Update GetModuleLogs method when tail + since + until options are…
Browse files Browse the repository at this point in the history
… provided. (#4922)

TL;DR - This PR chances the behavior of `GetModuleLogs` method to better facilitate the portal experience. 

Previous, when the `GetModuleLogs` is triggered from the azure portal with `tail + since + until`, all the 3 options are passed to Docker API management to fetch the log. The Docker API does the following: 
1. Fetch the log of the module
2. Apply `tail` to the log
3. Apply the `since` and `until` to the log
This results in an empty log most of the time which is not a desirable behavior. 

This PR changes the `GetModuleLogs` when the `tail + since + until` are specified together by doing the following:
1. Fetch Docker API with `since + until` log option
2. `tail` the returning log using LogProcessor (post-Akka).
This allows a customer to get a tail of log within a given time frame. 

Note: This PR doesn't change the behavior of any other option combinations (i.e. `tail + since`, `tail`, `since`, etc). These combination are still managed by Docker API.

Note2: I set RequestHandle timeout to be 30s instead of 600s by default
  • Loading branch information
yophilav committed May 11, 2021
1 parent 60d411c commit 2b650a8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public async Task<IReadOnlyList<ModuleLogMessage>> GetMessages(string id, Stream
IRunnableGraph<Task<IImmutableList<ModuleLogMessage>>> graph = graphBuilder.GetMaterializingGraph(m => (ModuleLogMessage)m);

IImmutableList<ModuleLogMessage> result = await graph.Run(this.materializer);
return result;
return filter.Tail.Match<IReadOnlyList<ModuleLogMessage>>(
t =>
{
return result.Skip(Math.Max(0, result.Count - t)).ToList().AsReadOnly();
},
() => result);
}

// Gzip encoding or output framing don't apply to this method.
Expand All @@ -86,7 +91,13 @@ IRunnableGraph<Task<IImmutableList<string>>> GetGraph()

IRunnableGraph<Task<IImmutableList<string>>> graph = GetGraph();
IImmutableList<string> result = await graph.Run(this.materializer);
return result;

return filter.Tail.Match<IReadOnlyList<string>>(
t =>
{
return result.Skip(Math.Max(0, result.Count - t)).ToList().AsReadOnly();
},
() => result);
}

public async Task ProcessLogsStream(string id, Stream stream, ModuleLogOptions logOptions, Func<ArraySegment<byte>, Task> callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public LogsProvider(IRuntimeInfoProvider runtimeInfoProvider, ILogsProcessor log
public async Task<byte[]> GetLogs(string id, ModuleLogOptions logOptions, CancellationToken cancellationToken)
{
Preconditions.CheckNotNull(logOptions, nameof(logOptions));

Stream logsStream = await this.runtimeInfoProvider.GetModuleLogs(id, false, logOptions.Filter.Tail, logOptions.Filter.Since, logOptions.Filter.Until, cancellationToken);
Events.ReceivedStream(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ public virtual async Task<Stream> GetModuleLogs(string module, bool follow, Opti
string baseUrl = HttpClientHelper.GetBaseUrl(this.ManagementUri).TrimEnd('/');
var logsUrl = new StringBuilder();
logsUrl.AppendFormat(CultureInfo.InvariantCulture, LogsUrlTemplate, baseUrl, module, this.Version.Name, follow.ToString().ToLowerInvariant());
tail.ForEach(t => logsUrl.AppendFormat($"&{LogsUrlTailParameter}={t}"));
since.ForEach(s => logsUrl.AppendFormat($"&{LogsUrlSinceParameter}={Uri.EscapeUriString(s)}"));
until.ForEach(u => logsUrl.AppendFormat($"&{LogsUrlUntilParameter}={Uri.EscapeUriString(u)}"));

if (!(tail.HasValue && since.HasValue && until.HasValue))
{
tail.ForEach(t => logsUrl.AppendFormat($"&{LogsUrlTailParameter}={t}"));
}

var logsUri = new Uri(logsUrl.ToString());
var httpRequest = new HttpRequestMessage(HttpMethod.Get, logsUri);
Stream stream = await this.Execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public override int Read(byte[] buffer, int offset, int count)
return bytesRead;
}

// Underlying Stream does not support Seek()
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();

public override void SetLength(long value) => throw new NotImplementedException();
Expand Down

0 comments on commit 2b650a8

Please sign in to comment.