Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include timing info in logged responses #2173

Merged
merged 2 commits into from Jun 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/OmniSharp.Stdio/Host.cs
Expand Up @@ -16,6 +16,7 @@
using OmniSharp.Services;
using OmniSharp.Protocol;
using OmniSharp.Utilities;
using System.Globalization;

namespace OmniSharp.Stdio
{
Expand All @@ -30,6 +31,7 @@ internal class Host : IDisposable
private readonly IOmniSharpEnvironment _environment;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly CachedStringBuilder _cachedStringBuilder;
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;

public Host(
TextReader input, ISharedTextWriter writer, IOmniSharpEnvironment environment,
Expand Down Expand Up @@ -195,6 +197,7 @@ public void Start()

private async Task HandleRequest(string json, ILogger logger)
{
var startTimestamp = Stopwatch.GetTimestamp();
var request = RequestPacket.Parse(json);
if (logger.IsEnabled(LogLevel.Debug))
{
Expand Down Expand Up @@ -243,7 +246,10 @@ private async Task HandleRequest(string json, ILogger logger)
LogRequest(json, logger, LogLevel.Warning);
}

LogResponse(response.ToString(), logger, response.Success);
var currentTimestamp = Stopwatch.GetTimestamp();
var elapsed = new TimeSpan((long)(TimestampToTicks * (currentTimestamp - startTimestamp)));

LogResponse(response.ToString(), logger, response.Success, elapsed);
}

// actually write it
Expand All @@ -266,12 +272,12 @@ void LogRequest(string json, ILogger logger, LogLevel logLevel)
}
}

void LogResponse(string json, ILogger logger, bool isSuccess)
void LogResponse(string json, ILogger logger, bool isSuccess, TimeSpan elapsed)
{
var builder = _cachedStringBuilder.Acquire();
try
{
builder.AppendLine("************ Response ************ ");
builder.AppendLine($"************ Response ({elapsed.TotalMilliseconds.ToString("0.0000", CultureInfo.InvariantCulture)}ms) ************ ");
builder.Append(JToken.Parse(json).ToString(Formatting.Indented));

if (isSuccess)
Expand Down